From 19aaf7eb62c78804822d2ef7dc0584c89614cb2d Mon Sep 17 00:00:00 2001 From: JeremiahUy Date: Thu, 26 Oct 2023 12:15:55 +0200 Subject: [PATCH] UPDATE: search code for behandling when searching with behandling nummer --- .../integration/behandling/BkatClient.java | 6 +- .../src/components/search/MainSearch.tsx | 161 +++++++++--------- 2 files changed, 80 insertions(+), 87 deletions(-) diff --git a/apps/backend/src/main/java/no/nav/data/integration/behandling/BkatClient.java b/apps/backend/src/main/java/no/nav/data/integration/behandling/BkatClient.java index 76b92c562..a18122033 100644 --- a/apps/backend/src/main/java/no/nav/data/integration/behandling/BkatClient.java +++ b/apps/backend/src/main/java/no/nav/data/integration/behandling/BkatClient.java @@ -79,11 +79,7 @@ public Map getProcessesById(Collection ids) { } public List findProcesses(String search) { - if (search.toLowerCase().matches("b[0-9]+(.*)")) { - return processSearchCache.get(search.substring(1)); - } else { - return processSearchCache.get(search); - } + return processSearchCache.get(search); } public List getProcessesForTeam(String teamId) { diff --git a/apps/frontend/src/components/search/MainSearch.tsx b/apps/frontend/src/components/search/MainSearch.tsx index b7793127f..8a0465b1e 100644 --- a/apps/frontend/src/components/search/MainSearch.tsx +++ b/apps/frontend/src/components/search/MainSearch.tsx @@ -204,81 +204,82 @@ const useMainSearch = (searchParam?: string) => { setSearchResult(getCodelist(search, ListName.UNDERAVDELING, 'Underavdeling')) } else { if (search && search.replace(/ /g, '').length > 2) { - ;(async () => { - let results: SearchItem[] = [] - let searches: Promise[] = [] - const compareFn = (a: SearchItem, b: SearchItem) => prefixBiasedSort(search, a.sortKey, b.sortKey) - const add = (items: SearchItem[]) => { - results = [...results, ...items] - results = results - .filter((item, index, self) => index === self.findIndex((searchItem) => searchItem.id === item.id)) - .sort((a, b) => { - const same = a.type === b.type - const typeOrder = order(a.type) - order(b.type) - return same || typeOrder !== 0 ? typeOrder : compareFn(a, b) - }) - setSearchResult(results) - } - setLoading(true) - - if (type === 'all') { - add(searchCodelist(search, ListName.UNDERAVDELING, 'Underavdeling', searchResultColor.underavdelingBackground)) - } - - if (type === 'all' || type === ObjectType.Krav) { - searches.push((async () => add((await searchKrav(search)).filter((k) => k.status !== KravStatus.UTGAATT).map(kravMap)))()) - - let kravNumber = search - if (kravNumber[0].toLowerCase() === 'k') { - kravNumber = kravNumber.substring(1) + console.log(search) + ; (async () => { + let results: SearchItem[] = [] + let searches: Promise[] = [] + const compareFn = (a: SearchItem, b: SearchItem) => prefixBiasedSort(search, a.sortKey, b.sortKey) + const add = (items: SearchItem[]) => { + results = [...results, ...items] + results = results + .filter((item, index, self) => index === self.findIndex((searchItem) => searchItem.id === item.id)) + .sort((a, b) => { + const same = a.type === b.type + const typeOrder = order(a.type) - order(b.type) + return same || typeOrder !== 0 ? typeOrder : compareFn(a, b) + }) + setSearchResult(results) } + setLoading(true) - if (Number.parseFloat(kravNumber) && Number.parseFloat(kravNumber) % 1 === 0) { - searches.push( - (async () => - add( - (await searchKravByNumber(Number.parseFloat(kravNumber).toString())) - .filter((k) => k.status !== KravStatus.UTGAATT) - .sort((a, b) => { - if (a.kravNummer === b.kravNummer) { - return b.kravVersjon - a.kravVersjon - } else { - return b.kravNummer - a.kravNummer - } - }) - .map(kravMap), - ))(), - ) + if (type === 'all') { + add(searchCodelist(search, ListName.UNDERAVDELING, 'Underavdeling', searchResultColor.underavdelingBackground)) } - if (Number.parseFloat(kravNumber) && Number.parseFloat(kravNumber) % 1 !== 0) { - const kravNummerMedVersjon = kravNumber.split('.') - const searchResult = [await getKravByKravNumberAndVersion(kravNummerMedVersjon[0], kravNummerMedVersjon[1])].filter((k) => k && k.status !== KravStatus.UTGAATT) - if (typeof searchResult[0] !== 'undefined') { - const mappedResult = [ - { - id: searchResult[0].id, - sortKey: searchResult[0].navn, - label: , - type: ObjectType.Krav, - }, - ] + if (type === 'all' || type === ObjectType.Krav) { + searches.push((async () => add((await searchKrav(search)).filter((k) => k.status !== KravStatus.UTGAATT).map(kravMap)))()) + + let kravNumber = search + if (kravNumber[0].toLowerCase() === 'k') { + kravNumber = kravNumber.substring(1) + } - searches.push((async () => add(mappedResult))()) + if (Number.parseFloat(kravNumber) && Number.parseFloat(kravNumber) % 1 === 0) { + searches.push( + (async () => + add( + (await searchKravByNumber(Number.parseFloat(kravNumber).toString())) + .filter((k) => k.status !== KravStatus.UTGAATT) + .sort((a, b) => { + if (a.kravNummer === b.kravNummer) { + return b.kravVersjon - a.kravVersjon + } else { + return b.kravNummer - a.kravNummer + } + }) + .map(kravMap), + ))(), + ) + } + + if (Number.parseFloat(kravNumber) && Number.parseFloat(kravNumber) % 1 !== 0) { + const kravNummerMedVersjon = kravNumber.split('.') + const searchResult = [await getKravByKravNumberAndVersion(kravNummerMedVersjon[0], kravNummerMedVersjon[1])].filter((k) => k && k.status !== KravStatus.UTGAATT) + if (typeof searchResult[0] !== 'undefined') { + const mappedResult = [ + { + id: searchResult[0].id, + sortKey: searchResult[0].navn, + label: , + type: ObjectType.Krav, + }, + ] + + searches.push((async () => add(mappedResult))()) + } } } - } - - if (type === 'all' || type === ObjectType.Behandling) { - searches.push((async () => add((await searchBehandling(search)).map(behandlingMap)))()) - } - if (type === 'all' || type === ObjectType.EtterlevelseDokumentasjon) { - searches.push((async () => add((await searchEtterlevelsedokumentasjon(search)).map(EtterlevelseDokumentasjonMap)))()) - } - - await Promise.all(searches) - setLoading(false) - })() + + if (type === 'all' || type === ObjectType.Behandling) { + searches.push((async () => add((await searchBehandling(search)).map(behandlingMap)))()) + } + if (type === 'all' || type === ObjectType.EtterlevelseDokumentasjon) { + searches.push((async () => add((await searchEtterlevelsedokumentasjon(search)).map(EtterlevelseDokumentasjonMap)))()) + } + + await Promise.all(searches) + setLoading(false) + })() } } }, [search, type]) @@ -444,24 +445,20 @@ const MainSearch = () => { value={value} onOpen={() => setFilterClicked(true)} onInputChange={(event) => { - if (event.currentTarget.value.toLowerCase().match(/b[0-9]+/)) { - setSearch('0' + event.currentTarget.value.substring(1)) - } else { - setSearch(event.currentTarget.value) - } + setSearch(event.currentTarget.value) setValue([{ id: event.currentTarget.value, label: event.currentTarget.value }]) }} onChange={(params) => { const item = params.value[0] as SearchItem - ;(async () => { - if (item && item.type !== '__ungrouped') { - setValue([item]) - navigate(urlForObject(item.type, item.id)) - window.location.reload() - } else if (item && item.type === '__ungrouped') { - setFilterClicked(true) - } - })() + ; (async () => { + if (item && item.type !== '__ungrouped') { + setValue([item]) + navigate(urlForObject(item.type, item.id)) + window.location.reload() + } else if (item && item.type === '__ungrouped') { + setFilterClicked(true) + } + })() }} filterOptions={(options) => options} setValue={setValue}