Skip to content

Commit

Permalink
refactor(ccdaservice): test and refactor fetchPreviousAddresses funct…
Browse files Browse the repository at this point in the history
…ion (openemr#6828)

* refactor(ccdaservice): test and refactor fetchPreviousAddresses function

* refactor(ccdaservice): use extracted fetchPreviousAddresses function
  • Loading branch information
raskolnikov-rodion authored Sep 10, 2023
1 parent e5f3e08 commit d25d771
Show file tree
Hide file tree
Showing 4 changed files with 283 additions and 76 deletions.
79 changes: 3 additions & 76 deletions ccdaservice/serveccda.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand All @@ -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';
Expand Down
56 changes: 56 additions & 0 deletions ccdaservice/utils/demographics/previous-addresses.js
Original file line number Diff line number Diff line change
@@ -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;
204 changes: 204 additions & 0 deletions ccdaservice/utils/demographics/previous-addresses.mock.js
Original file line number Diff line number Diff line change
@@ -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;
20 changes: 20 additions & 0 deletions ccdaservice/utils/demographics/previous-addresses.spec.js
Original file line number Diff line number Diff line change
@@ -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();
});
});

0 comments on commit d25d771

Please sign in to comment.