Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CNFT1-2995] Structure of the form and api request for the creation of a patient with extended data #1788

Merged
merged 12 commits into from
Sep 13, 2024
39 changes: 39 additions & 0 deletions apps/modernization-ui/src/apps/patient/add/extended/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {
Administrative,
Name,
Address,
PhoneEmail,
Identification,
Race,
Ethnicity,
Sex,
Birth,
Mortality,
GeneralInformation
} from 'apps/patient/data/api';
import { Maybe } from 'utils';

type NewPatient = {
administrative: Administrative;
names?: Name[];
addresses?: Address[];
phoneEmails?: PhoneEmail[];
identifications?: Identification[];
races?: Race[];
ethnicity?: Maybe<Ethnicity>;
birth?: Maybe<Birth>;
sex?: Maybe<Sex>;
mortality?: Maybe<Mortality>;
general?: Maybe<GeneralInformation>;
};

type CreatedPatient = {
id: number;
shortId: number;
name?: {
first?: string;
last?: string;
};
};

export type { NewPatient, CreatedPatient };
29 changes: 29 additions & 0 deletions apps/modernization-ui/src/apps/patient/add/extended/entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {
AdministrativeEntry,
NameEntry,
AddressEntry,
PhoneEmailEntry,
IdentificationEntry,
RaceEntry,
EthnicityEntry,
SexEntry,
BirthEntry,
MortalityEntry,
GeneralInformationEntry
} from 'apps/patient/data/entry';

type ExtendedNewPatientEntry = {
administrative: AdministrativeEntry;
names?: NameEntry[];
addresses?: AddressEntry[];
phoneEmails?: PhoneEmailEntry[];
identifications?: IdentificationEntry[];
races?: RaceEntry[];
ethnicity?: EthnicityEntry;
birth?: BirthEntry;
sex?: SexEntry;
mortality?: MortalityEntry;
general?: GeneralInformationEntry;
};

export type { ExtendedNewPatientEntry };
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { AddPatientExtended } from './AddPatientExtended';
export type { ExtendedNewPatientEntry } from './entry';
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
import { ExtendedNewPatientEntry } from './entry';
import { transformer } from './transformer';

describe('when transforming entered extended patient data', () => {
it('should transform administrative to a format accepted by the API', () => {
const entry: ExtendedNewPatientEntry = {
administrative: { asOf: '04/13/2017', comment: 'entered-value' }
};

const actual = transformer(entry);

expect(actual).toEqual(
expect.objectContaining({ administrative: { asOf: '04/13/2017', comment: 'entered-value' } })
);
});

it('should transform names', () => {
const entry: ExtendedNewPatientEntry = {
administrative: { asOf: '04/13/2017' },
names: [
{
asOf: '04/13/2017',
type: { value: 'name-type-value', name: 'name-type-name' }
}
]
};

const actual = transformer(entry);

expect(actual).toEqual(
expect.objectContaining({
names: expect.arrayContaining([expect.objectContaining({ type: 'name-type-value' })])
})
);
});

it('should transform addresses', () => {
const entry: ExtendedNewPatientEntry = {
administrative: { asOf: '04/13/2017' },
addresses: [
{
asOf: '04/13/2017',
type: { value: 'address-type-value', name: 'address-type-name' },
use: { value: 'address-use-value', name: 'address-use-name' }
}
]
};

const actual = transformer(entry);

expect(actual).toEqual(
expect.objectContaining({
addresses: expect.arrayContaining([
expect.objectContaining({ type: 'address-type-value', use: 'address-use-value' })
])
})
);
});

it('should transform phone and emails', () => {
const entry: ExtendedNewPatientEntry = {
administrative: { asOf: '04/13/2017' },
phoneEmails: [
{
asOf: '04/13/2017',
type: { value: 'phone-email-type-value', name: 'phone-email-type-name' },
use: { value: 'phone-email-use-value', name: 'phone-email-use-name' }
}
]
};

const actual = transformer(entry);

expect(actual).toEqual(
expect.objectContaining({
phoneEmails: expect.arrayContaining([
expect.objectContaining({ type: 'phone-email-type-value', use: 'phone-email-use-value' })
])
})
);
});

it('should transform identifictions', () => {
const entry: ExtendedNewPatientEntry = {
administrative: { asOf: '04/13/2017' },
identifications: [
{
asOf: '04/13/2017',
type: { value: 'identification-type-value', name: 'identification-type-name' },
id: 'id-value'
}
]
};

const actual = transformer(entry);

expect(actual).toEqual(
expect.objectContaining({
identifications: expect.arrayContaining([
expect.objectContaining({ type: 'identification-type-value' })
])
})
);
});

it('should transform races', () => {
const entry: ExtendedNewPatientEntry = {
administrative: { asOf: '04/13/2017' },
races: [
{
asOf: '04/13/2017',
race: { value: 'race-value', name: 'race-name' },
detailed: []
}
]
};

const actual = transformer(entry);

expect(actual).toEqual(
expect.objectContaining({
races: expect.arrayContaining([expect.objectContaining({ race: 'race-value' })])
})
);
});

it('should transform ethnicity', () => {
const entry: ExtendedNewPatientEntry = {
administrative: { asOf: '04/13/2017' },
ethnicity: {
asOf: '04/13/2017',
ethnicity: { value: 'ethnicity-value', name: 'ethnicity-name' },
detailed: []
}
};

const actual = transformer(entry);

expect(actual).toEqual(
expect.objectContaining({
ethnicity: expect.objectContaining({ ethnicity: 'ethnicity-value' })
})
);
});

it('should transform sex', () => {
const entry: ExtendedNewPatientEntry = {
administrative: { asOf: '04/13/2017' },
sex: {
asOf: '04/13/2017',
current: { value: 'current-sex-value', name: 'current-sex-name' }
}
};

const actual = transformer(entry);

expect(actual).toEqual(
expect.objectContaining({
sex: expect.objectContaining({ current: 'current-sex-value' })
})
);
});

it('should transform birth', () => {
const entry: ExtendedNewPatientEntry = {
administrative: { asOf: '04/13/2017' },
birth: {
asOf: '04/13/2017',
bornOn: '06/17/2003'
}
};

const actual = transformer(entry);

expect(actual).toEqual(
expect.objectContaining({
birth: expect.objectContaining({ bornOn: '06/17/2003' })
})
);
});

it('should transform mortality', () => {
const entry: ExtendedNewPatientEntry = {
administrative: { asOf: '04/13/2017' },
mortality: {
asOf: '04/13/2017',
deceasedOn: '09/07/1976'
}
};

const actual = transformer(entry);

expect(actual).toEqual(
expect.objectContaining({
mortality: expect.objectContaining({ deceasedOn: '09/07/1976' })
})
);
});

it('should transform general information', () => {
const entry: ExtendedNewPatientEntry = {
administrative: { asOf: '04/13/2017' },
general: {
asOf: '04/13/2017',
comment: 'general-information'
}
};

const actual = transformer(entry);

expect(actual).toEqual(
expect.objectContaining({
general: expect.objectContaining({ comment: 'general-information' })
})
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import {
asAddress,
asAdministrative,
asName,
asPhoneEmail,
asIdentification,
asRace,
asEthnicity,
asSex,
asBirth,
asMortality,
asGeneral
} from 'apps/patient/data';
import { ExtendedNewPatientEntry } from './entry';
import { Transformer } from './useAddExtendedPatient';
import { Mapping } from 'utils';
import { NewPatient } from './api';

const maybeMap =
<R, S>(mapping: Mapping<R, S>) =>
(value?: R): S | undefined =>
value ? mapping(value) : undefined;

const maybeMapAll =
<R, S>(mapping: Mapping<R, S>) =>
(value?: R[]): S[] =>
value ? value.map(mapping) : [];

const asNames = maybeMapAll(asName);
const asAddresses = maybeMapAll(asAddress);
const asPhoneEmails = maybeMapAll(asPhoneEmail);
const asIdentifications = maybeMapAll(asIdentification);
const asRaces = maybeMapAll(asRace);

const maybeAsEthnicity = maybeMap(asEthnicity);
const maybeAsSex = maybeMap(asSex);
const maybeBirth = maybeMap(asBirth);
const maybeMortality = maybeMap(asMortality);
const maybeGeneral = maybeMap(asGeneral);

const transformer: Transformer = (entry: ExtendedNewPatientEntry): NewPatient => {
const administrative = asAdministrative(entry.administrative);
const names = asNames(entry.names);
const addresses = asAddresses(entry.addresses);
const phoneEmails = asPhoneEmails(entry.phoneEmails);
const identifications = asIdentifications(entry.identifications);
const races = asRaces(entry.races);

const ethnicity = maybeAsEthnicity(entry.ethnicity);
const sex = maybeAsSex(entry.sex);
const birth = maybeBirth(entry.birth);
const mortality = maybeMortality(entry.mortality);
const general = maybeGeneral(entry.general);

return {
administrative,
names,
addresses,
phoneEmails,
identifications,
races,
ethnicity,
sex,
birth,
mortality,
general
};
};

export { transformer };
Loading
Loading