Skip to content

Commit

Permalink
Group patient states by Program (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmale authored May 28, 2024
1 parent d47607c commit c690eab
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 12 deletions.
73 changes: 73 additions & 0 deletions src/components/encounter/encounter-form-manager.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { type PatientProgram, type FormField } from '../../types';
import { EncounterFormManager } from './encounter-form-manager';

describe('EncounterFormManager', () => {
describe('preparePatientPrograms', () => {
const fields: FormField[] = [
{
label: 'State 1',
type: 'programState',
questionOptions: { rendering: 'select', programUuid: 'program-1-uuid' },
meta: { submission: { newValue: { state: 'state-1' } } },
id: 'state_1',
},
{
label: 'State 2',
type: 'programState',
questionOptions: { rendering: 'select', programUuid: 'program-1-uuid' },
meta: { submission: { newValue: { state: 'state-2' } } },
id: 'state_2',
},
{
label: 'State 3',
type: 'programState',
questionOptions: { rendering: 'select', programUuid: 'program-2-uuid' },
meta: { submission: { newValue: { state: 'state-3' } } },
id: 'state_3',
},
];

it('should group program states by program', () => {
const patient = { id: 'patient-1' } as fhir.Patient;
const currentPatientPrograms = [
{
program: {
uuid: 'program-1-uuid',
},
uuid: 'existing-enrollment-1-uuid',
},
{
program: {
uuid: 'program-2-uuid',
},
uuid: 'existing-enrollment-2-uuid',
},
] as Array<PatientProgram>;

const result = EncounterFormManager.preparePatientPrograms(fields, patient, currentPatientPrograms);
expect(result).toEqual([
{
uuid: 'existing-enrollment-1-uuid',
states: [{ state: 'state-1' }, { state: 'state-2' }],
},
{
uuid: 'existing-enrollment-2-uuid',
states: [{ state: 'state-3' }],
},
]);
});

it('should enroll in a new program if none exists', () => {
const patient = { id: 'patient-1' } as fhir.Patient;
const result = EncounterFormManager.preparePatientPrograms([fields[0]], patient, []);
expect(result).toEqual([
{
patient: 'patient-1',
program: 'program-1-uuid',
states: [{ state: 'state-1' }],
dateEnrolled: expect.any(String),
},
]);
});
});
});
37 changes: 25 additions & 12 deletions src/components/encounter/encounter-form-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,23 +116,36 @@ export class EncounterFormManager {
patient: fhir.Patient,
currentPatientPrograms: Array<PatientProgram>,
): Array<PatientProgramPayload> {
const programFields = fields.filter((field) => field.type === 'programState' && hasSubmission(field));
return programFields.map((field) => {
const programStateFields = fields.filter((field) => field.type === 'programState' && hasSubmission(field));
const programMap = new Map<string, PatientProgramPayload>();
programStateFields.forEach((field) => {
const programUuid = field.questionOptions.programUuid;
const newState = field.meta.submission.newValue;
const existingProgramEnrollment = currentPatientPrograms.find((program) => program.program.uuid === programUuid);

if (existingProgramEnrollment) {
return {
uuid: existingProgramEnrollment.uuid,
states: [field.meta.submission.newValue],
};
if (programMap.has(programUuid)) {
programMap.get(programUuid).states.push(newState);
} else {
programMap.set(programUuid, {
uuid: existingProgramEnrollment.uuid,
states: [newState],
});
}
} else {
if (programMap.has(programUuid)) {
programMap.get(programUuid).states.push(newState);
} else {
programMap.set(programUuid, {
patient: patient.id,
program: programUuid,
states: [newState],
dateEnrolled: dayjs().format(),
});
}
}
return {
patient: patient.id,
program: programUuid,
states: [field.meta.submission.newValue],
dateEnrolled: dayjs().format(),
};
});
return Array.from(programMap.values());
}

static savePatientPrograms = (patientPrograms: PatientProgramPayload[]) => {
Expand Down

0 comments on commit c690eab

Please sign in to comment.