From db50210567ef3e3b542daccb4748726476409934 Mon Sep 17 00:00:00 2001 From: Audrius Vaitonis Date: Mon, 15 Apr 2024 10:42:52 +0100 Subject: [PATCH] feat(DTFS2-7052): fix address line 1 formating --- src/modules/geospatial/geospatial.service.test.ts | 11 +++++++++++ src/modules/geospatial/geospatial.service.ts | 5 +++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/modules/geospatial/geospatial.service.test.ts b/src/modules/geospatial/geospatial.service.test.ts index ec3165dd..7853afcf 100644 --- a/src/modules/geospatial/geospatial.service.test.ts +++ b/src/modules/geospatial/geospatial.service.test.ts @@ -63,5 +63,16 @@ describe('CustomerService', () => { expect(response).toEqual([]); }); + + it('returns addressLine1 formatted correctly even if middle value is missing', async () => { + const [modifiedOrdnanceSurveyResponse] = getAddressOrdnanceSurveyResponse; + modifiedOrdnanceSurveyResponse.results[0].DPA.BUILDING_NUMBER = null; + const address = modifiedOrdnanceSurveyResponse.results[0].DPA; + when(informaticaServiceGetAddressesByPostcode).calledWith(postcode).mockResolvedValueOnce(modifiedOrdnanceSurveyResponse); + + const response = await service.getAddressesByPostcode(postcode); + + expect(response[0].addressLine1).toBe(`${address.BUILDING_NAME} ${address.THOROUGHFARE_NAME}`); + }); }); }); diff --git a/src/modules/geospatial/geospatial.service.ts b/src/modules/geospatial/geospatial.service.ts index b3cb515d..d0385b92 100644 --- a/src/modules/geospatial/geospatial.service.ts +++ b/src/modules/geospatial/geospatial.service.ts @@ -18,11 +18,12 @@ export class GeospatialService { } response.results.forEach((item) => { - // Ordnance survey sends duplicated results with the welsh version too via 'CY' + // Item can have key DPA or LPI, get data dynamicaly, even if we expect key to always be DPA. const item_data = item[Object.keys(item)[0]]; addresses.push({ organisationName: item_data.ORGANISATION_NAME || null, - addressLine1: `${item_data.BUILDING_NAME || ''} ${item_data.BUILDING_NUMBER || ''} ${item_data.THOROUGHFARE_NAME || ''}`.trim(), + // Filter out empty values and join values with single space. + addressLine1: [item_data.BUILDING_NAME, item_data.BUILDING_NUMBER, item_data.THOROUGHFARE_NAME].filter(Boolean).join(' '), addressLine2: item_data.DEPENDENT_LOCALITY || null, addressLine3: null, locality: item_data.POST_TOWN || null,