Skip to content

Commit

Permalink
UPDATE: search code for behandling when searching with behandling nummer
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremiahUy committed Oct 26, 2023
1 parent ba9229a commit 19aaf7e
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,7 @@ public Map<String, BkatProcess> getProcessesById(Collection<String> ids) {
}

public List<BkatProcess> 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<BkatProcess> getProcessesForTeam(String teamId) {
Expand Down
161 changes: 79 additions & 82 deletions apps/frontend/src/components/search/MainSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>[] = []
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<any>[] = []
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: <SearchLabel name={kravName(searchResult[0])} type={'Krav'} backgroundColor={searchResultColor.kravBackground} />,
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: <SearchLabel name={kravName(searchResult[0])} type={'Krav'} backgroundColor={searchResultColor.kravBackground} />,
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])
Expand Down Expand Up @@ -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}
Expand Down

0 comments on commit 19aaf7e

Please sign in to comment.