Skip to content

Commit

Permalink
MIJN-9702-CHORE/BAG-endpoint-vervangen-ivm-deprecation-huidig-endpoint (
Browse files Browse the repository at this point in the history
#1680)

* Changed bag to use new endpoint

* Removed comments in favor of new variable name

* Somewhere into fixing frontend part

* Works on frontend, but uncertainty with parsing

* Remove unused mock data. The URL is public and called directly

* Found new type of address that worked, so added to tests and fixed
problem with it.

* Replace part with regex

* Added some extra tests and fixed them

* add type

* removed evil snakecase

* Give null when no data instead of empty object

* Update src/server/services/bag.ts

Co-authored-by: Tim van Oostrom <[email protected]>

* Update src/server/services/bag.ts

Co-authored-by: Tim van Oostrom <[email protected]>

* Update src/server/services/bag.ts

Co-authored-by: Tim van Oostrom <[email protected]>

* Added explicit return type

* Fix test

---------

Co-authored-by: Tim van Oostrom <[email protected]>
  • Loading branch information
RoanPaulus and timvanoostrom authored Jan 6, 2025
1 parent 44e78e2 commit d122c67
Show file tree
Hide file tree
Showing 12 changed files with 446 additions and 424 deletions.
170 changes: 0 additions & 170 deletions mocks/fixtures/bag.json

This file was deleted.

30 changes: 20 additions & 10 deletions src/client/components/LocationModal/LocationModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ import { LatLngLiteral } from 'leaflet';

import styles from './LocationModal.module.scss';
import { LOCATION_ZOOM } from '../../../universal/config/myarea-datasets';
import { PUBLIC_API_URLS } from '../../../universal/config/url';
import {
extractAddress,
getLatLngWithAddress,
getLatLonByAddress,
isLocatedInWeesp,
LatLngWithAddress,
} from '../../../universal/helpers/bag';
import { BAGSearchResult, BAGSourceData } from '../../../universal/types/bag';
import {
BAGAdreseerbaarObject,
BAGQueryParams,
BAGSourceData,
} from '../../../universal/types/bag';
import { Modal } from '../../components';
import { BaseLayerType } from '../../components/MyArea/Map/BaseLayerToggle';
import MyAreaLoader from '../../components/MyArea/MyAreaLoader';
Expand All @@ -23,20 +28,25 @@ import { MapLocationMarker } from '../MyArea/MyArea.hooks';

function transformBagSearchResultsResponse(
response: BAGSourceData,
querySearchAddress: string,
querySearchAddress: BAGQueryParams,
isWeesp: boolean
): LatLngWithAddress[] | null {
const results = response?.results ?? [];
const adresseerbareObjecten = response?._embedded.adresseerbareobjecten ?? [];

// Try to get exact match
const latlng = getLatLonByAddress(results, querySearchAddress, isWeesp);
if (latlng && results.length === 1) {
return [latlng];
const latlngWithAddress = getLatLonByAddress(
adresseerbareObjecten,
querySearchAddress,
isWeesp
);

if (latlngWithAddress && adresseerbareObjecten.length === 1) {
return [latlngWithAddress];
}

// No exact match, return all results
if (results.length) {
return results.map((result: BAGSearchResult) =>
if (adresseerbareObjecten.length) {
return adresseerbareObjecten.map((result: BAGAdreseerbaarObject) =>
getLatLngWithAddress(result)
);
}
Expand Down Expand Up @@ -91,10 +101,10 @@ export function LocationModal({
if (isLocationModalOpen) {
const querySearchAddress = extractAddress(address);
const isWeesp = isLocatedInWeesp(address);

// Updates bagApi state
fetchBag({
url: `https://api.data.amsterdam.nl/atlas/search/adres/?features=2&q=${querySearchAddress}`,
url: PUBLIC_API_URLS.BAG_ADRESSEERBARE_OBJECTEN,
params: querySearchAddress,
transformResponse(responseData: BAGSourceData) {
const latlngResults = transformBagSearchResultsResponse(
responseData,
Expand Down
3 changes: 2 additions & 1 deletion src/server/config/source-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { AxiosRequestConfig, AxiosResponse } from 'axios';
import { ONE_HOUR_MS, ONE_MINUTE_MS, ONE_SECOND_MS } from './app';
import { IS_TAP } from '../../universal/config/env';
import { FeatureToggle } from '../../universal/config/feature-toggles';
import { PUBLIC_API_URLS } from '../../universal/config/url';
import { getCert } from '../helpers/cert';
import { getFromEnv } from '../helpers/env';

Expand Down Expand Up @@ -228,7 +229,7 @@ export const ApiConfig: ApiDataRequestConfig = {
}),
},
BAG: {
url: `https://api.data.amsterdam.nl/atlas/search/adres/`,
url: PUBLIC_API_URLS.BAG_ADRESSEERBARE_OBJECTEN,
},
ERFPACHTv2: {
url: getFromEnv('BFF_ERFPACHT_API_URL'),
Expand Down
79 changes: 48 additions & 31 deletions src/server/services/bag.test.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,65 @@
import nock from 'nock';
import { describe, expect, it } from 'vitest';

import { fetchBAG } from './bag';
import bagData from '../../../mocks/fixtures/bag.json';
import { jsonCopy } from '../../universal/helpers/utils';
import { Adres } from '../../universal/types';

describe('BAG service', () => {
const DUMMY_RESPONSE = jsonCopy(bagData);
const REQUEST_ID = 'x';
const ADDRESS = {
straatnaam: 'straatje',
huisnummer: 25,
woonplaatsNaam: 'Amsterdam',
} as unknown as Adres;

it('Bag api should reply correctly', async () => {
nock('https://api.data.amsterdam.nl')
.get('/atlas/search/adres/?q=straatje 25&features=2')
.reply(200, DUMMY_RESPONSE);
// This is only a section of the mock data from the source.
const BAG_MOCK_DATA = {
_embedded: {
adresseerbareobjecten: [
{
identificatie: '0363200012145295',
huisnummer: 1,
huisletter: null,
huisnummertoevoeging: null,
postcode: '1011PN',
woonplaatsNaam: 'Amsterdam',
adresseerbaarObjectPuntGeometrieWgs84: {
type: 'Point',
coordinates: [4.9001655, 52.3676456],
},
},
],
},
};

const address = {
straatnaam: 'straatje',
huisnummer: 25,
woonplaatsNaam: 'Amsterdam',
} as unknown as Adres;
function setupNockResponse(reply: number, response?: object) {
nock('https://api.data.amsterdam.nl')
.get(
'/v1/benkagg/adresseerbareobjecten/?openbareruimteNaam=straatje&huisnummer=25'
)
.reply(reply, response);
}

const rs = await fetchBAG('x', address);
describe('BAG service', () => {
setupNockResponse(200, BAG_MOCK_DATA);

expect(rs).toStrictEqual({
test('Bag api should reply correctly', async () => {
const response = await fetchBAG(REQUEST_ID, ADDRESS);

expect(response).toStrictEqual({
status: 'OK',
content: {
address,
bagNummeraanduidingId: null,
latlng: null,
address: ADDRESS,
bagNummeraanduidingId: '0363200012145295',
latlng: {
lat: 52.3676456,
lng: 4.9001655,
},
},
});
});

it('Bag api should fail correctly', async () => {
nock('http://api.data.amsterdam.nl')
.get('/bag', { params: { q: 'undefined' } })
.reply(500);
// Request non-existing mock url
const rs = await fetchBAG('x', {} as any);

expect(rs).toStrictEqual({
status: 'ERROR',
message: 'Kon geen correct zoek adres opmaken.',
content: null,
});
test('No data in response', async () => {
setupNockResponse(200, {});
const response = await fetchBAG(REQUEST_ID, ADDRESS);
expect(response).toStrictEqual({ status: 'OK', content: null });
});
});
Loading

0 comments on commit d122c67

Please sign in to comment.