-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #756 from Klantinteractie-Servicesysteem/DIM@6313_…
…KvK_Zoeken_API_upgraden DIM@6313 - KvK Zoeken API upgraden
- Loading branch information
Showing
11 changed files
with
232 additions
and
215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,203 +0,0 @@ | ||
import { | ||
fetchLoggedIn, | ||
parseJson, | ||
ServiceResult, | ||
throwIfNotOk, | ||
type Paginated, | ||
enforceOneOrZero, | ||
defaultPagination, | ||
FriendlyError, | ||
} from "@/services"; | ||
|
||
import type { | ||
SearchCategories, | ||
BedrijfQuery, | ||
BedrijfQueryDictionary, | ||
Bedrijf, | ||
} from "./types"; | ||
|
||
export const bedrijfQuery = <K extends SearchCategories>( | ||
query: BedrijfQuery<K> | ||
) => query; | ||
|
||
type KvkPagination = { | ||
pagina: number; | ||
aantal: number; | ||
totaal: number; | ||
resultaten: any[]; | ||
}; | ||
|
||
const parseKvkPagination = async ({ | ||
pagina, | ||
aantal, | ||
totaal, | ||
resultaten, | ||
}: KvkPagination): Promise<Paginated<Bedrijf>> => ({ | ||
page: await Promise.all(resultaten.map(mapHandelsRegister)), | ||
pageNumber: pagina, | ||
totalRecords: totaal, | ||
pageSize: aantal, | ||
totalPages: totaal === 0 ? 0 : Math.ceil(totaal / aantal), | ||
}); | ||
|
||
const handelsRegisterBaseUrl = "/api/kvk"; | ||
|
||
const bedrijfQueryDictionary: BedrijfQueryDictionary = { | ||
postcodeHuisnummer: ({ postcode, huisnummer }) => [ | ||
["postcode", postcode.numbers + postcode.digits], | ||
["huisnummer", huisnummer], | ||
], | ||
kvkNummer: (search) => [["kvkNummer", search]], | ||
handelsnaam: (search) => [["handelsnaam", search]], | ||
}; | ||
|
||
const getSearchBedrijvenUrl = <K extends SearchCategories>({ | ||
query, | ||
page, | ||
}: SearchBedrijfArguments<K>) => { | ||
if (!query?.value) return ""; | ||
|
||
const params = new URLSearchParams(); | ||
// TODO: think about how to search in both klantregister and handelsregister for phone / email | ||
|
||
params.set("pagina", page?.toString() ?? "1"); | ||
params.set("type", "hoofdvestiging,nevenvestiging"); | ||
|
||
const searchParams = bedrijfQueryDictionary[query.field](query.value); | ||
|
||
searchParams.forEach((tuple) => { | ||
params.set(...tuple); | ||
}); | ||
|
||
return `${handelsRegisterBaseUrl}/zoeken?${params}`; | ||
}; | ||
|
||
async function mapHandelsRegister(json: any): Promise<Bedrijf> { | ||
const { vestigingsnummer, kvkNummer, handelsnaam, straatnaam, plaats } = | ||
json ?? {}; | ||
|
||
let vestiging: KvkVestiging | undefined; | ||
|
||
if (vestigingsnummer) { | ||
try { | ||
vestiging = await fetchVestiging(getVestingUrl(vestigingsnummer)); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
|
||
return { | ||
_typeOfKlant: "bedrijf", | ||
kvkNummer, | ||
vestigingsnummer, | ||
bedrijfsnaam: handelsnaam, | ||
straatnaam, | ||
woonplaats: plaats, | ||
...(vestiging ?? {}), | ||
}; | ||
} | ||
|
||
type KvkVestiging = { | ||
vestigingsnummer: string; | ||
kvkNummer: string; | ||
handelsnaam: string; | ||
postcode: string; | ||
huisnummer: string; | ||
huisletter: string; | ||
huisnummertoevoeging: string; | ||
}; | ||
|
||
const mapVestiging = ({ | ||
vestigingsnummer, | ||
kvkNummer, | ||
eersteHandelsnaam, | ||
adressen, | ||
}: any): KvkVestiging => { | ||
const { huisnummerToevoeging, postcode, huisnummer, huisletter } = | ||
adressen?.find((x: any) => x?.type === "bezoekadres") ?? | ||
adressen?.[0] ?? | ||
{}; | ||
|
||
return { | ||
vestigingsnummer, | ||
kvkNummer, | ||
handelsnaam: eersteHandelsnaam, | ||
huisnummertoevoeging: huisnummerToevoeging, | ||
postcode, | ||
huisnummer, | ||
huisletter, | ||
}; | ||
}; | ||
|
||
const hasFoutCode = (body: unknown, code: string) => { | ||
if ( | ||
body && | ||
typeof body === "object" && | ||
"fout" in body && | ||
Array.isArray(body.fout) | ||
) { | ||
return body.fout.some((x) => x?.code === code); | ||
} | ||
return false; | ||
}; | ||
|
||
function searchBedrijvenInHandelsRegister(url: string) { | ||
return fetchLoggedIn(url).then(async (r) => { | ||
if (r.status === 404) { | ||
const body = await r.json(); | ||
if (hasFoutCode(body, "IPD5200")) return defaultPagination([]); | ||
} | ||
if (r.status === 400) { | ||
throw new FriendlyError("Invalide zoekopdracht"); | ||
} | ||
throwIfNotOk(r); | ||
const body = await r.json(); | ||
return parseKvkPagination(body); | ||
}); | ||
} | ||
|
||
type SearchBedrijfArguments<K extends SearchCategories> = { | ||
query: BedrijfQuery<K> | undefined; | ||
page: number | undefined; | ||
}; | ||
|
||
export function useSearchBedrijven<K extends SearchCategories>( | ||
getArgs: () => SearchBedrijfArguments<K> | ||
) { | ||
return ServiceResult.fromFetcher( | ||
() => getSearchBedrijvenUrl(getArgs()), | ||
searchBedrijvenInHandelsRegister | ||
); | ||
} | ||
|
||
const getVestingUrl = (vestigingsnummer?: string) => { | ||
if (!vestigingsnummer) return ""; | ||
return handelsRegisterBaseUrl + "/vestigingsprofielen/" + vestigingsnummer; | ||
}; | ||
|
||
const fetchVestiging = (url: string) => | ||
fetchLoggedIn(url).then(throwIfNotOk).then(parseJson).then(mapVestiging); | ||
|
||
export const useBedrijfByVestigingsnummer = ( | ||
getVestigingsnummer: () => string | undefined | ||
) => { | ||
const getUrl = () => { | ||
const vestigingsnummer = getVestigingsnummer(); | ||
if (!vestigingsnummer) return ""; | ||
const searchParams = new URLSearchParams(); | ||
searchParams.set("vestigingsnummer", vestigingsnummer); | ||
return `${handelsRegisterBaseUrl}/zoeken?${searchParams}`; | ||
}; | ||
|
||
const getUniqueId = () => { | ||
const url = getUrl(); | ||
return url && url + "_single"; | ||
}; | ||
|
||
const fetcher = (url: string) => | ||
searchBedrijvenInHandelsRegister(url).then(enforceOneOrZero); | ||
|
||
return ServiceResult.fromFetcher(getUrl, fetcher, { | ||
getUniqueId, | ||
}); | ||
}; | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import type { BedrijfQuery, SearchCategories } from "../types"; | ||
|
||
export const bedrijfQuery = <K extends SearchCategories>( | ||
query: BedrijfQuery<K>, | ||
) => query; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { ServiceResult, enforceOneOrZero } from "@/services"; | ||
import { searchBedrijvenInHandelsRegister } from "./shared/shared"; | ||
|
||
const zoekenUrl = "/api/kvk/v2/zoeken"; | ||
|
||
export const useBedrijfByVestigingsnummer = ( | ||
getVestigingsnummer: () => string | undefined, | ||
) => { | ||
const getUrl = () => { | ||
const vestigingsnummer = getVestigingsnummer(); | ||
if (!vestigingsnummer) return ""; | ||
const searchParams = new URLSearchParams(); | ||
searchParams.set("vestigingsnummer", vestigingsnummer); | ||
return `${zoekenUrl}?${searchParams}`; | ||
}; | ||
|
||
// useBedrijfByVestigingsnummer private ////////////////////////// | ||
|
||
const getUniqueId = () => { | ||
const url = getUrl(); | ||
return url && url + "_single"; | ||
}; | ||
|
||
const fetcher = (url: string) => | ||
searchBedrijvenInHandelsRegister(url).then(enforceOneOrZero); | ||
|
||
return ServiceResult.fromFetcher(getUrl, fetcher, { | ||
getUniqueId, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { ServiceResult } from "@/services"; | ||
|
||
import type { | ||
SearchCategories, | ||
BedrijfQuery, | ||
BedrijfQueryDictionary, | ||
} from "../types"; | ||
import { searchBedrijvenInHandelsRegister, zoekenUrl } from "./shared/shared"; | ||
|
||
export function useSearchBedrijven<K extends SearchCategories>( | ||
getArgs: () => SearchBedrijfArguments<K>, | ||
) { | ||
return ServiceResult.fromFetcher( | ||
() => getSearchBedrijvenUrl(getArgs()), | ||
searchBedrijvenInHandelsRegister, | ||
); | ||
} | ||
|
||
// useSearchBedrijven private /////////////////////////////////////////// | ||
|
||
const getSearchBedrijvenUrl = <K extends SearchCategories>({ | ||
query, | ||
page, | ||
}: SearchBedrijfArguments<K>) => { | ||
if (!query?.value) return ""; | ||
|
||
const params = new URLSearchParams(); | ||
params.set("pagina", page?.toString() ?? "1"); | ||
params.set("type", "hoofdvestiging"); | ||
params.append("type", "nevenvestiging"); | ||
|
||
const searchParams = bedrijfQueryDictionary[query.field](query.value); | ||
|
||
searchParams.forEach((tuple) => { | ||
params.set(...tuple); | ||
}); | ||
|
||
return `${zoekenUrl}?${params}`; | ||
}; | ||
|
||
type SearchBedrijfArguments<K extends SearchCategories> = { | ||
query: BedrijfQuery<K> | undefined; | ||
page: number | undefined; | ||
}; | ||
|
||
const bedrijfQueryDictionary: BedrijfQueryDictionary = { | ||
postcodeHuisnummer: ({ postcode, huisnummer }) => [ | ||
["postcode", postcode.numbers + postcode.digits], | ||
["huisnummer", huisnummer], | ||
], | ||
kvkNummer: (search) => [["kvkNummer", search]], | ||
handelsnaam: (search) => [["naam", search]], | ||
}; |
Oops, something went wrong.