From d25d771cc211874d02ff9052c84807ffd7ea7f15 Mon Sep 17 00:00:00 2001 From: raskolnikov-rodion <22417165+raskolnikov-rodion@users.noreply.github.com> Date: Mon, 11 Sep 2023 00:32:02 +0100 Subject: [PATCH] refactor(ccdaservice): test and refactor fetchPreviousAddresses function (#6828) * refactor(ccdaservice): test and refactor fetchPreviousAddresses function * refactor(ccdaservice): use extracted fetchPreviousAddresses function --- ccdaservice/serveccda.js | 79 +------ .../utils/demographics/previous-addresses.js | 56 +++++ .../demographics/previous-addresses.mock.js | 204 ++++++++++++++++++ .../demographics/previous-addresses.spec.js | 20 ++ 4 files changed, 283 insertions(+), 76 deletions(-) create mode 100644 ccdaservice/utils/demographics/previous-addresses.js create mode 100644 ccdaservice/utils/demographics/previous-addresses.mock.js create mode 100644 ccdaservice/utils/demographics/previous-addresses.spec.js diff --git a/ccdaservice/serveccda.js b/ccdaservice/serveccda.js index 2318a57d18c..5e4c800ad65 100644 --- a/ccdaservice/serveccda.js +++ b/ccdaservice/serveccda.js @@ -23,6 +23,9 @@ const { headReplace } = require('./utils/head-replace/head-replace'); const { fDate, templateDate } = require('./utils/date/date'); const { countEntities } = require('./utils/count-entities/count-entities'); const { populateTimezones } = require('./utils/timezones/timezones'); +const { + fetchPreviousAddresses, +} = require('./utils/demographics/previous-addresses'); var conn = ''; // make our connection scope global to script var oidFacility = ""; @@ -33,82 +36,6 @@ var webRoot = ""; var authorDateTime = ''; var documentLocation = ''; -function fetchPreviousAddresses(pd) { - let addressArray = []; - let pa = pd.previous_addresses.address; - let streetLine = [pd.street[0]]; - if (pd.street[1].length > 0) { - streetLine = [pd.street[0], pd.street[1]]; - } - addressArray.push({ - "use": "HP", - "street_lines": streetLine, - "city": pd.city, - "state": pd.state, - "zip": pd.postalCode, - "country": pd.country || "US", - "date_time": { - // use current date for current residence - "low": { - "date": fDate(""), - "precision": "day" - } - } - }); - let count = countEntities(pa); - // how do we ever get here where we just have one object? - if (count === 1) { - streetLine = [pa.street[0]]; - if (pa.street[1].length > 0) { - streetLine = [pa.street[0], pa.street[1]]; - } - addressArray.push({ - "use": pa.use, - "street_lines": streetLine, - "city": pa.city, - "state": pa.state, - "zip": pa.postalCode, - "country": pa.country || "US", - "date_time": { - "low": { - "date": fDate(pa.period_start), - "precision": "day" - }, - "high": { - "date": fDate(pa.period_end) || fDate(""), - "precision": "day" - } - } - }); - } else if (count > 1) { - for (let i in pa) { - streetLine = [pa[i].street[0]]; - if (pa[i].street[1].length > 0) { - streetLine = [pa[i].street[0], pa[i].street[1]]; - } - addressArray.push({ - "use": pa[i].use, - "street_lines": streetLine, - "city": pa[i].city, - "state": pa[i].state, - "zip": pa[i].postalCode, - "country": pa[i].country || "US", - "date_time": { - "low": { - "date": fDate(pa[i].period_start), - "precision": "day" - }, - "high": { - "date": fDate(pa[i].period_end) || fDate(""), - "precision": "day" - } - } - }); - } - } - return addressArray; -} - function populateDemographic(pd, g) { let first = 'NI'; let middle = 'NI'; diff --git a/ccdaservice/utils/demographics/previous-addresses.js b/ccdaservice/utils/demographics/previous-addresses.js new file mode 100644 index 00000000000..71790f0227b --- /dev/null +++ b/ccdaservice/utils/demographics/previous-addresses.js @@ -0,0 +1,56 @@ +'use strict'; + +const { fDate } = require('../date/date'); +const { countEntities } = require('../count-entities/count-entities'); + +function buildStreetLines(streets) { + const streetLines = [streets[0]]; + if (streets[1]?.length > 0) streetLines.push(streets[1]); + return streetLines; +} + +function buildAddressItem(input, isCurrent = false) { + return { + use: isCurrent ? 'HP' : input.use, + street_lines: buildStreetLines(input.street), + city: input.city, + state: input.state, + zip: input.postalCode, + country: input.country || 'US', + date_time: { + low: { + date: fDate(isCurrent ? '' : input.period_start), + precision: 'day', + }, + ...(isCurrent + ? {} + : { + high: { + date: fDate(input.period_end) || fDate(''), + precision: 'day', + }, + }), + }, + }; +} + +function fetchPreviousAddresses(input) { + const addresses = []; + const currentAddress = buildAddressItem(input, true); + addresses.push(currentAddress); + const previousAddresses = input.previous_addresses.address; + const count = countEntities(previousAddresses); + if (count === 1) { + // how do we ever get here where we just have one object? + const previous = buildAddressItem(previousAddresses); + addresses.push(previous); + } else if (count > 1) { + for (let i in previousAddresses) { + const previous = buildAddressItem(previousAddresses[i]); + addresses.push(previous); + } + } + return addresses; +} + +exports.fetchPreviousAddresses = fetchPreviousAddresses; diff --git a/ccdaservice/utils/demographics/previous-addresses.mock.js b/ccdaservice/utils/demographics/previous-addresses.mock.js new file mode 100644 index 00000000000..7bf46180aa0 --- /dev/null +++ b/ccdaservice/utils/demographics/previous-addresses.mock.js @@ -0,0 +1,204 @@ +const TODAY = '2021-04-06T18:55:55.301Z'; + +const cityA = { + city: 'Blue Bell', + state: 'MA', + postalCode: '02368', + country: 'US', + street: ['17 Daws Rd.'], + use: 'primary home', +}; + +const cityB = { + city: 'Borgo Marino', + state: 'Pavia', + country: 'IT', + postalCode: '65210', + street: ['Borgo Lamberto 942', 'Appartamento 59'], + use: 'primary home', + period_start: '01/07/2019', + period_end: '12/31/2022', +}; + +const cityC = { + city: 'Riley Causeway', + state: 'Hawaii', + postalCode: '93789-5373', + street: ['88792 Myrna Point', 'Suite 258'], + use: 'work place', + period_start: '05/09/2023', +}; + +const TEST_CASES = [ + [ + // INPUT + { + ...cityA, + previous_addresses: { + address: [cityB, cityC], + }, + }, + // RESULT + [ + { + city: cityA.city, + country: cityA.country, + date_time: { + low: { + date: TODAY, + precision: 'day', + }, + }, + state: cityA.state, + street_lines: [cityA.street[0]], + use: 'HP', + zip: cityA.postalCode, + }, + { + city: cityB.city, + country: cityB.country, + date_time: { + high: { + date: '2022-12-31', + precision: 'day', + }, + low: { + date: '2019-01-07', + precision: 'day', + }, + }, + state: cityB.state, + street_lines: [cityB.street[0], cityB.street[1]], + use: 'primary home', + zip: cityB.postalCode, + }, + { + city: cityC.city, + country: 'US', + date_time: { + high: { + date: 'undefined', + precision: 'day', + }, + low: { + date: '2023-05-09', + precision: 'day', + }, + }, + state: cityC.state, + street_lines: [cityC.street[0], cityC.street[1]], + use: 'work place', + zip: cityC.postalCode, + }, + ], + ], + [ + // INPUT + { + ...cityC, + previous_addresses: { + address: [cityA, cityB], + }, + }, + // RESULT + [ + { + city: cityC.city, + country: 'US', + date_time: { + low: { + date: TODAY, + precision: 'day', + }, + }, + state: cityC.state, + street_lines: [cityC.street[0], cityC.street[1]], + use: 'HP', + zip: cityC.postalCode, + }, + { + city: cityA.city, + country: 'US', + date_time: { + high: { + date: 'undefined', + precision: 'day', + }, + low: { + date: 'undefined', + precision: 'day', + }, + }, + state: cityA.state, + street_lines: [cityA.street[0]], + use: cityA.use, + zip: cityA.postalCode, + }, + { + city: cityB.city, + country: cityB.country, + date_time: { + high: { + date: '2022-12-31', + precision: 'day', + }, + low: { + date: '2019-01-07', + precision: 'day', + }, + }, + state: cityB.state, + street_lines: [cityB.street[0], cityB.street[1]], + use: cityB.use, + zip: cityB.postalCode, + }, + ], + ], + [ + // INPUT + { + ...cityB, + previous_addresses: { + address: cityC, + }, + }, + // RESULT + [ + { + city: cityB.city, + country: cityB.country, + date_time: { + low: { + date: TODAY, + precision: 'day', + }, + }, + state: cityB.state, + street_lines: [cityB.street[0], cityB.street[1]], + use: 'HP', + zip: cityB.postalCode, + }, + { + city: cityC.city, + country: 'US', + date_time: { + high: { + date: 'undefined', + precision: 'day', + }, + low: { + date: '2023-05-09', + precision: 'day', + }, + }, + state: cityC.state, + street_lines: [cityC.street[0], cityC.street[1]], + use: cityC.use, + zip: cityC.postalCode, + }, + ], + ], +]; + +exports.TEST_CASES = TEST_CASES; +exports.TODAY = TODAY; diff --git a/ccdaservice/utils/demographics/previous-addresses.spec.js b/ccdaservice/utils/demographics/previous-addresses.spec.js new file mode 100644 index 00000000000..4073c782a4a --- /dev/null +++ b/ccdaservice/utils/demographics/previous-addresses.spec.js @@ -0,0 +1,20 @@ +const fetchPreviousAddresses = + require('./previous-addresses').fetchPreviousAddresses; +const { TEST_CASES, TODAY } = require('./previous-addresses.mock'); + +describe('fetchPreviousAddresses', () => { + beforeAll(() => { + jest.useFakeTimers().setSystemTime(new Date(TODAY)); + }); + + test.each(TEST_CASES)( + 'should return an array containing the addresses in the input object', + (input, result) => { + expect(fetchPreviousAddresses(input)).toEqual(result); + } + ); + + afterAll(() => { + jest.useRealTimers(); + }); +});