From e2d7c90c0913cc852d83af31da01b582dc11f7b2 Mon Sep 17 00:00:00 2001 From: Adam Loup <124325935+adamloup-enquizit@users.noreply.github.com> Date: Tue, 14 Jan 2025 09:41:47 -0600 Subject: [PATCH] [MU-13] ensures that date of birth is pre-populated from search (#2174) --- .../add/basic/asBasicNewPatientEntry.spec.ts | 37 +++++++++++++++++++ .../add/basic/asBasicNewPatientEntry.ts | 19 +++------- .../patient/add/asNewPatientEntry.spec.ts | 28 +++++++++++++- .../search/patient/add/asNewPatientEntry.ts | 12 +++--- .../src/design-system/date/entry.ts | 18 ++++++++- .../src/design-system/date/index.ts | 2 +- 6 files changed, 91 insertions(+), 25 deletions(-) create mode 100644 apps/modernization-ui/src/apps/patient/add/basic/asBasicNewPatientEntry.spec.ts diff --git a/apps/modernization-ui/src/apps/patient/add/basic/asBasicNewPatientEntry.spec.ts b/apps/modernization-ui/src/apps/patient/add/basic/asBasicNewPatientEntry.spec.ts new file mode 100644 index 0000000000..a25b14f2b8 --- /dev/null +++ b/apps/modernization-ui/src/apps/patient/add/basic/asBasicNewPatientEntry.spec.ts @@ -0,0 +1,37 @@ +import { asBasicNewPatientEntry } from './asBasicNewPatientEntry'; + +describe('when adding a new patient from a patient search', () => { + it('should populate the date of birth from the date equals criteria with a full date', () => { + const criteria = { bornOn: { equals: { month: 6, day: 5, year: 2025 } } }; + + const actual = asBasicNewPatientEntry(criteria); + + expect(actual).toEqual( + expect.objectContaining({ personalDetails: expect.objectContaining({ bornOn: '06/05/2025' }) }) + ); + }); + + it('should populate the date of birth from the date equals criteria with only a month', () => { + const criteria = { bornOn: { equals: { month: 6 } } }; + + const actual = asBasicNewPatientEntry(criteria); + + expect(actual.personalDetails?.bornOn).toBeUndefined(); + }); + + it('should populate the date of birth from the date equals criteria with only a day', () => { + const criteria = { bornOn: { equals: { day: 5 } } }; + + const actual = asBasicNewPatientEntry(criteria); + + expect(actual.personalDetails?.bornOn).toBeUndefined(); + }); + + it('should populate the date of birth from the date equals criteria with only a year', () => { + const criteria = { bornOn: { equals: { year: 2025 } } }; + + const actual = asBasicNewPatientEntry(criteria); + + expect(actual.personalDetails?.bornOn).toBeUndefined(); + }); +}); diff --git a/apps/modernization-ui/src/apps/patient/add/basic/asBasicNewPatientEntry.ts b/apps/modernization-ui/src/apps/patient/add/basic/asBasicNewPatientEntry.ts index df6b7164b4..ab9e324f2c 100644 --- a/apps/modernization-ui/src/apps/patient/add/basic/asBasicNewPatientEntry.ts +++ b/apps/modernization-ui/src/apps/patient/add/basic/asBasicNewPatientEntry.ts @@ -1,4 +1,6 @@ import { asSelectable } from 'options'; +import { internalizeDate } from 'date'; +import { Mapping } from 'utils'; import { BasicAddressEntry, BasicNewPatientEntry, @@ -8,11 +10,9 @@ import { BasicEthnicityRace, BasicIdentificationEntry } from './entry'; -import { Mapping } from 'utils'; -import { PatientCriteriaEntry } from 'apps/search/patient/criteria'; import { asTextCriteriaValue, TextCriteria } from 'options/operator'; -import { internalizeDate } from 'date'; -import { isDateEqualsCriteria } from 'design-system/date/entry'; +import { resolveDate } from 'design-system/date/entry'; +import { PatientCriteriaEntry } from 'apps/search/patient/criteria'; const mapOr = (mapping: Mapping, fallback: O) => @@ -42,16 +42,7 @@ const nameBasic = (initial: Partial): NameInformationEntry }); const personalDetailsBasic = (initial: Partial): BasicPersonalDetailsEntry => { - const bornOn = - initial.bornOn && - isDateEqualsCriteria(initial.bornOn) && - initial.bornOn.equals.year && - initial.bornOn.equals.month && - initial.bornOn.equals.day - ? internalizeDate( - new Date(initial.bornOn.equals.year, initial.bornOn.equals.month - 1, initial.bornOn.equals.day) - ) - : undefined; + const bornOn = resolveDate(initial.bornOn); return { bornOn, diff --git a/apps/modernization-ui/src/apps/search/patient/add/asNewPatientEntry.spec.ts b/apps/modernization-ui/src/apps/search/patient/add/asNewPatientEntry.spec.ts index 77da67f14f..fd4e1b8f06 100644 --- a/apps/modernization-ui/src/apps/search/patient/add/asNewPatientEntry.spec.ts +++ b/apps/modernization-ui/src/apps/search/patient/add/asNewPatientEntry.spec.ts @@ -17,14 +17,38 @@ describe('when adding a new patient from a patient search', () => { expect(actual).toEqual(expect.objectContaining({ lastName: 'last-name' })); }); - it('should populate the date of birth that was searched for', () => { - const criteria = { dateOfBirth: '06/05/2025' }; + it('should populate the date of birth from the date equals criteria with a full date', () => { + const criteria = { bornOn: { equals: { month: 6, day: 5, year: 2025 } } }; const actual = asNewPatientEntry(criteria); expect(actual).toEqual(expect.objectContaining({ dateOfBirth: '06/05/2025' })); }); + it('should populate the date of birth from the date equals criteria with only a month', () => { + const criteria = { bornOn: { equals: { month: 6 } } }; + + const actual = asNewPatientEntry(criteria); + + expect(actual.dateOfBirth).toBeUndefined(); + }); + + it('should populate the date of birth from the date equals criteria with only a day', () => { + const criteria = { bornOn: { equals: { day: 5 } } }; + + const actual = asNewPatientEntry(criteria); + + expect(actual.dateOfBirth).toBeUndefined(); + }); + + it('should populate the date of birth from the date equals criteria with only a year', () => { + const criteria = { bornOn: { equals: { year: 2025 } } }; + + const actual = asNewPatientEntry(criteria); + + expect(actual.dateOfBirth).toBeUndefined(); + }); + it('should populate the gender that was searched for', () => { const criteria = { gender: { name: 'Female', label: 'Female', value: 'F' } }; diff --git a/apps/modernization-ui/src/apps/search/patient/add/asNewPatientEntry.ts b/apps/modernization-ui/src/apps/search/patient/add/asNewPatientEntry.ts index 9d50c76850..31977a537c 100644 --- a/apps/modernization-ui/src/apps/search/patient/add/asNewPatientEntry.ts +++ b/apps/modernization-ui/src/apps/search/patient/add/asNewPatientEntry.ts @@ -1,10 +1,10 @@ -import { NewPatientEntry } from 'apps/patient/add'; -import { PatientCriteriaEntry, Identification, RaceEthnicity, Contact } from '../criteria'; -import { internalizeDate } from 'date'; +import { today } from 'date'; import { asValue } from 'options'; import { orNull } from 'utils'; -import { IdentificationEntry, EmailEntry } from 'apps/patient/add'; +import { resolveDate } from 'design-system/date/entry'; import { TextCriteria, asTextCriteriaValue } from 'options/operator'; +import { PatientCriteriaEntry, Identification, RaceEthnicity, Contact } from 'apps/search/patient/criteria'; +import { IdentificationEntry, EmailEntry, NewPatientEntry } from 'apps/patient/add'; const resolveIdentification = (entry: Identification): IdentificationEntry[] => entry.identification && entry.identificationType @@ -25,10 +25,10 @@ const resolveCriteria = (textCriteria?: TextCriteria): string | null => orNull(a const asNewPatientEntry = (criteria: Partial): NewPatientEntry => { return { - asOf: internalizeDate(new Date()), + asOf: today(), firstName: resolveCriteria(criteria.name?.first), lastName: resolveCriteria(criteria.name?.last), - dateOfBirth: criteria.dateOfBirth, + dateOfBirth: resolveDate(criteria.bornOn), currentGender: orNull(asValue(criteria.gender)), streetAddress1: resolveCriteria(criteria.location?.street), city: resolveCriteria(criteria.location?.city), diff --git a/apps/modernization-ui/src/design-system/date/entry.ts b/apps/modernization-ui/src/design-system/date/entry.ts index bbce873331..a2157e5016 100644 --- a/apps/modernization-ui/src/design-system/date/entry.ts +++ b/apps/modernization-ui/src/design-system/date/entry.ts @@ -22,7 +22,21 @@ type DateCriteria = DateEqualsCriteria | DateBetweenCriteria; const isDateEqualsCriteria = (criteria: DateCriteria): criteria is DateEqualsCriteria => 'equals' in criteria; const isDateBetweenCriteria = (criteria: DateCriteria): criteria is DateBetweenCriteria => 'between' in criteria; -export { isDateEqualsCriteria, isDateBetweenCriteria }; +/** + * Resolves the string representation of date equals criteria if the + * criteria contains a date with a month, day, and year. + * + * @param {DateCriteria | undefined} criteria The DateCriteria to resolve a date from + * @return {string | undefined} A MM/DD/YYYY formatted string that represents a Date. + */ +const resolveDate = (criteria?: DateCriteria) => { + if (criteria && isDateEqualsCriteria(criteria)) { + const date = criteria.equals; + return date.day && date.month && date.year ? displayDateEntry(date) : undefined; + } +}; + +export { isDateEqualsCriteria, isDateBetweenCriteria, resolveDate }; export type { DateCriteria, DateBetweenCriteria, DateEqualsCriteria }; const maybeNumber = maybeMap(Number); @@ -69,7 +83,7 @@ const asISODate = (value?: DateEntry): string | undefined => { const asDate = (value?: DateEntry) => { if (value?.year && value.month && value.day) { - return new Date(value.year, value.month + 1, value.day); + return new Date(value.year, value.month - 1, value.day); } }; diff --git a/apps/modernization-ui/src/design-system/date/index.ts b/apps/modernization-ui/src/design-system/date/index.ts index 3af60f2eca..ff64af1fa7 100644 --- a/apps/modernization-ui/src/design-system/date/index.ts +++ b/apps/modernization-ui/src/design-system/date/index.ts @@ -1,4 +1,4 @@ -export { asDateEntry, asDate, displayDateEntry, asISODate } from './entry'; +export { asDateEntry, asDate, displayDateEntry, asISODate, resolveDate } from './entry'; export type { DateEntry } from './entry'; export { validateDate } from './validateDate';