Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forhaapentligvis raskere henting av identer #3372

Merged
merged 2 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/dolly-frontend/src/main/js/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export const multiFetcherDokarkiv = (miljoUrlListe) =>
? fetcher(obj.url, { miljo: obj.miljo }).then((result) => ({
miljo: obj.miljo,
data: result,
}))
}))
: { miljo: obj.miljo, data: null },
),
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import React, { useContext, useEffect, useState } from 'react'
import React, { useContext } from 'react'
import { FormikSelect } from '@/components/ui/form/inputs/select/Select'
import Loading from '@/components/ui/loading/Loading'
import { isEmpty } from '@/components/fagsystem/pdlf/form/partials/utils'
import * as _ from 'lodash-es'
import { BestillingsveilederContext } from '@/components/bestillingsveileder/BestillingsveilederContext'
import { identFraTestnorge } from '@/components/bestillingsveileder/stegVelger/steg/steg1/Steg1Person'
import { Option, SelectOptionsOppslag } from '@/service/SelectOptionsOppslag'
import { useBoolean } from 'react-use'
import { Option } from '@/service/SelectOptionsOppslag'
import { FormikProps } from 'formik'
import { ForeldreBarnRelasjon, NyIdent, Sivilstand } from '@/components/fagsystem/pdlf/PdlTypes'
import { ForeldreBarnRelasjon, NyIdent } from '@/components/fagsystem/pdlf/PdlTypes'
import { Alert } from '@navikt/ds-react'
import { useParams } from 'react-router-dom'
import { useGruppeIdenter } from '@/utils/hooks/useGruppe'
import { usePdlOptions } from '@/utils/hooks/useSelectOptions'

interface PdlEksisterendePersonValues {
nyPersonPath?: string
Expand All @@ -37,10 +37,8 @@ export const PdlEksisterendePerson = ({
const antall = opts?.antall || 1
const { gruppeId } = useParams()

const isTestnorgeIdent = identFraTestnorge(opts)

const [identOptions, setIdentOptions] = useState<Array<Option>>([])
const [loadingIdentOptions, setLoadingIdentOptions] = useBoolean(true)
const { identer, loading: gruppeLoading, error: gruppeError } = useGruppeIdenter(gruppeId)
const { data: pdlOptions, loading: pdlLoading, error: pdlError } = usePdlOptions(identer)

const harSivilstand = eksisterendePersonPath?.includes('sivilstand')
const harNyIdent = eksisterendePersonPath?.includes('nyident')
Expand Down Expand Up @@ -116,54 +114,44 @@ export const PdlEksisterendePerson = ({

const getFilteredOptionList = () => {
const eksisterendeIdent = opts?.personFoerLeggTil?.pdlforvalter?.person?.ident
let tmpOptions = []
// @ts-ignore
SelectOptionsOppslag.hentGruppeIdentOptions(gruppeId).then((response: [Option]) => {
tmpOptions = response?.filter((person) => {
return person.value !== eksisterendeIdent && filterOptions(person)
})
if (
eksisterendeNyPerson &&
!tmpOptions.find((person) => person.value === eksisterendeNyPerson.value)
) {
tmpOptions.push(eksisterendeNyPerson)
}
setIdentOptions(tmpOptions)
setLoadingIdentOptions(false)
})
}

useEffect(() => {
if (!isTestnorgeIdent && gruppeId) {
getFilteredOptionList()
}
}, [])

useEffect(() => {
if (formikBag) {
getFilteredOptionList()
const tmpOptions = pdlOptions?.filter(
(person) => person.value !== eksisterendeIdent && filterOptions(person),
)
if (
eksisterendeNyPerson &&
!tmpOptions.find((person) => person.value === eksisterendeNyPerson.value)
) {
tmpOptions.push(eksisterendeNyPerson)
}
}, [formikBag])
return tmpOptions
}

const hasNyPersonValues = nyIdentValg
? !isEmpty(nyIdentValg, ['syntetisk'])
: nyPersonPath && !isEmpty(_.get(formikBag?.values, nyPersonPath), ['syntetisk'])

const bestillingFlerePersoner = parseInt(antall) > 1 && (harSivilstand || harNyIdent)

const filteredOptions = getFilteredOptionList()

return (
<div className={'flexbox--full-width'}>
{loadingIdentOptions && <Loading label="Henter valg for eksisterende ident..." />}
{identOptions?.length > 0 ? (
{(pdlLoading || gruppeLoading) && <Loading label="Henter valg for eksisterende ident..." />}
{filteredOptions?.length > 0 ? (
<FormikSelect
name={eksisterendePersonPath}
label={label}
options={identOptions}
options={filteredOptions}
size={'xlarge'}
isDisabled={hasNyPersonValues || bestillingFlerePersoner || disabled}
/>
) : pdlError || gruppeError ? (
<Alert variant="error" size="small" style={{ marginBottom: '15px' }}>
{pdlError?.message || gruppeError?.message || 'Feil ved henting av personer'}
</Alert>
) : (
!loadingIdentOptions && (
!pdlLoading &&
!gruppeLoading && (
<Alert variant="info" size="small" style={{ marginBottom: '15px' }}>
Det finnes ingen eksisterende gyldige personer i denne gruppen.
</Alert>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { useAsync } from 'react-use'
import { BrregstubApi, DollyApi, KrrApi, PdlforvalterApi } from '@/service/Api'
import { BrregstubApi, DollyApi, KrrApi } from '@/service/Api'
import Api from '@/api'
import { Person, PersonData } from '@/components/fagsystem/pdlf/PdlTypes'
import { getAlder } from '@/ducks/fagsystem'

const uri = `/dolly-backend/api/v1`

Expand All @@ -19,68 +17,7 @@ export type Option = {
relasjoner?: Array<string>
}

type Data = {
label: string
id: string
navn: string
value: {
data: any
}
}

type PersonListe = {
value: string
label: string
alder: number
sivilstand: string
vergemaal: boolean
doedsfall: boolean
foreldre: Array<string>
foreldreansvar: Array<string>
}

export const SelectOptionsOppslag = {
hentGruppeIdentOptions: async (gruppeId: string) => {
const gruppe = await DollyApi.getGruppeById(gruppeId).then((response: any) => {
return response.data?.identer?.map((person: PersonData) => {
if (person.master === 'PDL' || person.master === 'PDLF') {
return person.ident
}
})
})
if (gruppe?.length < 1) {
return null
}
const options = await PdlforvalterApi.getPersoner(gruppe).then((response: any) => {
if (gruppe?.length < 1) {
return null
}
const personListe: Array<PersonListe> = []
response?.data?.forEach((id: Person) => {
const navn =
id.person.navn?.length > 0
? `- ${id.person.navn?.[0]?.fornavn} ${id.person.navn?.[0]?.etternavn}`
: ''
personListe.push({
value: id.person.ident,
label: `${id.person.ident} ${navn}`,
alder: getAlder(id.person.foedsel?.[0]?.foedselsdato),
sivilstand: id.person.sivilstand?.[0]?.type,
vergemaal: id.person.vergemaal?.length > 0,
doedsfall: id.person.doedsfall?.length > 0,
foreldre: id.relasjoner
?.filter((relasjon) => relasjon.relasjonType === 'FAMILIERELASJON_FORELDER')
?.map((relasjon) => relasjon.relatertPerson?.ident),
foreldreansvar: id.relasjoner
?.filter((relasjon) => relasjon.relasjonType === 'FORELDREANSVAR_BARN')
?.map((relasjon) => relasjon.relatertPerson?.ident),
})
})
return personListe
})
return options || Promise.resolve()
},

hentKrrLeverandoerer: () => {
return useAsync(async () => KrrApi.getSdpLeverandoerListe(), [KrrApi.getSdpLeverandoerListe])
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,13 @@ import Request from '@/service/services/Request'

const getPdlUrl = () => '/testnav-pdl-forvalter/api/v1'

const sliceIdentListe = (identListe: string[]) => {
const maxAntall = 150
if (identListe.length <= maxAntall) return [identListe]
const identer = []
for (let i = 0; i < identListe.length; i += maxAntall) {
identer.push(identListe.slice(i, i + maxAntall))
}
return identer
}

export default {
getPersoner(identListe: string[]) {
if (!identListe) {
return
}
const identerDelt = sliceIdentListe(identListe)
const promises = identerDelt.map((identer) => {
const endpoint = `${getPdlUrl()}/personer?identer=${identer}`
return Request.get(endpoint)
})
return Promise.all(promises).then((responses) => {
const data = responses.map((response: any) => response?.data).flat()
return { data }
})
const endpoint = `${getPdlUrl()}/personer?identer=${identListe}`
return Request.get(endpoint)
},
soekPersoner(fragment: string) {
if (!fragment || fragment.length > 11) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import useSWR from 'swr'
import { fetcher, multiFetcherAll } from '@/api'
import { getAlder } from '@/ducks/fagsystem'

const sliceGruppe = (gruppe, maxAntall, url) => {
let urlListe = []
Expand All @@ -12,20 +13,23 @@ const sliceGruppe = (gruppe, maxAntall, url) => {

const multiplePdlforvalterUrl = (gruppe) => {
if (!gruppe) return null
const maxAntall = 40
const maxAntall = 150
const url = '/testnav-pdl-forvalter/api/v1/personer?'
return sliceGruppe(gruppe, maxAntall, url)
}

const multiplePdlPersonUrl = (gruppe) => {
if (!gruppe) return null
const maxAntall = 40
const maxAntall = 150
const url = '/person-service/api/v2/personer/identer?'
return sliceGruppe(gruppe, maxAntall, url)
}

export const usePdlOptions = (gruppe) => {
const { data, error } = useSWR<any, Error>([multiplePdlforvalterUrl(gruppe)], multiFetcherAll)
const { data, isLoading, error } = useSWR<any, Error>(
multiplePdlforvalterUrl(gruppe),
multiFetcherAll,
)

const personData = []
data?.flat().forEach((id) => {
Expand All @@ -37,18 +41,28 @@ export const usePdlOptions = (gruppe) => {
value: id?.person?.ident,
label: `${id?.person?.ident} - ${fornavn} ${mellomnavn} ${etternavn}`,
relasjoner: id?.relasjoner?.map((r) => r?.relatertPerson?.ident),
alder: getAlder(id.person.foedsel?.[0]?.foedselsdato),
sivilstand: id.person.sivilstand?.[0]?.type,
vergemaal: id.person.vergemaal?.length > 0,
doedsfall: id.person.doedsfall?.length > 0,
foreldre: id.relasjoner
?.filter((relasjon) => relasjon.relasjonType === 'FAMILIERELASJON_FORELDER')
?.map((relasjon) => relasjon.relatertPerson?.ident),
foreldreansvar: id.relasjoner
?.filter((relasjon) => relasjon.relasjonType === 'FORELDREANSVAR_BARN')
?.map((relasjon) => relasjon.relatertPerson?.ident),
})
})

return {
data: personData,
loading: false,
loading: isLoading,
error: gruppe ? error : undefined,
}
}

export const useTestnorgeOptions = (gruppe) => {
const { data, error } = useSWR<any, Error>([multiplePdlPersonUrl(gruppe)], multiFetcherAll)
const { data, error } = useSWR<any, Error>(multiplePdlPersonUrl(gruppe), multiFetcherAll)

const getRelatertePersoner = (person) => {
if (!person) {
Expand Down