forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(ccdaservice): test and refactor fetchPreviousAddresses funct…
…ion (openemr#6828) * refactor(ccdaservice): test and refactor fetchPreviousAddresses function * refactor(ccdaservice): use extracted fetchPreviousAddresses function
- Loading branch information
1 parent
e5f3e08
commit d25d771
Showing
4 changed files
with
283 additions
and
76 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
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
204
ccdaservice/utils/demographics/previous-addresses.mock.js
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,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; |
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,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(); | ||
}); | ||
}); |