Skip to content

Commit

Permalink
code review
Browse files Browse the repository at this point in the history
  • Loading branch information
CynthiaKamau committed Jan 9, 2025
1 parent 561f6fe commit 025ca74
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 56 deletions.
39 changes: 2 additions & 37 deletions src/adapters/encounter-diagnosis-adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const formContext = {
customDependencies: {
patientPrograms: [],
},
deletedFields: [],
getFormField: jest.fn(),
addFormField: jest.fn(),
updateFormField: jest.fn(),
Expand All @@ -39,6 +40,7 @@ const formContext = {
removeInvalidField: jest.fn(),
setInvalidFields: jest.fn(),
setForm: jest.fn(),
setDeletedFields: jest.fn(),
} as FormContextProps;

const field = {
Expand Down Expand Up @@ -214,41 +216,4 @@ describe('EncounterDiagnosisAdapter', () => {
});
expect(field.meta.submission.voidedValue).toBe(undefined);
});

it('should handle deleting a diagnosis', () => {
formContext.sessionMode = 'edit';

const value = null;
field.meta = {
previousValue: {
patient: '833db896-c1f0-11eb-8529-0242ac130003',
condition: null,
diagnosis: {
coded: '127133AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
},
certainty: 'PROVISIONAL',
rank: 1,
formFieldPath: 'rfe-forms-DiagNosIS',
formFieldNamespace: 'rfe-forms',
uuid: '0e20bb67-5d7f-41e0-96a1-751efc21a96f',
},
initialValue: {
omrsObject: {
uuid: '0e20bb67-5d7f-41e0-96a1-751efc21a96f',
diagnosis: {
coded: {
uuid: '127133AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
display: 'Schistosoma Mansonii Infection',
},
},
},
refinedValue: null,
},
repeat: {
wasDeleted: true,
},
};
EncounterDiagnosisAdapter.transformFieldValue(field, value, formContext);
expect(field.meta.submission.voidedValue).toEqual({ uuid: '0e20bb67-5d7f-41e0-96a1-751efc21a96f', voided: true });
});
});
21 changes: 5 additions & 16 deletions src/adapters/encounter-diagnosis-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,28 +68,17 @@ const constructNewDiagnosis = (value: any, field: FormField, patientUuid: string
if (uuid && uuid.trim() !== '') {
diagnosis.uuid = uuid;
}

return diagnosis;
};

function editDiagnosis(newEncounterDiagnosis: any, field: FormField, patientUuid: string) {
if (newEncounterDiagnosis === field.meta.previousValue?.diagnosis?.coded?.uuid && !field.meta.repeat?.wasDeleted) {
if (newEncounterDiagnosis === field.meta.previousValue?.diagnosis?.coded?.uuid) {
clearSubmission(field);
return null;
}
const oldDiagnosis = field.meta.initialValue?.omrsObject as OpenmrsResource;

//the field has been deleted
if (field.meta.repeat?.wasDeleted) {
const voided = {
uuid: field.meta.previousValue?.uuid,
voided: true,
};
gracefullySetSubmission(field, constructNewDiagnosis(newEncounterDiagnosis, field, null), voided);
return field.meta.submission.newValue || null;
} else {
const oldDiagnosis = field.meta.initialValue?.omrsObject as OpenmrsResource;
const newValue = constructNewDiagnosis(newEncounterDiagnosis, field, patientUuid, oldDiagnosis.uuid);
gracefullySetSubmission(field, newValue, null);
return newValue;
}
const newValue = constructNewDiagnosis(newEncounterDiagnosis, field, patientUuid, oldDiagnosis?.uuid);
gracefullySetSubmission(field, newValue, null);
return newValue;
}
28 changes: 25 additions & 3 deletions src/processors/encounter/encounter-processor-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export function prepareEncounter(
prepareObs(obsForSubmission, allFormFields);
const ordersForSubmission = prepareOrders(allFormFields);
const diagnosesForSubmission = prepareDiagnosis(allFormFields);

let encounterForSubmission: OpenmrsEncounter = {};

if (encounter) {
Expand Down Expand Up @@ -376,7 +377,28 @@ export async function hydrateRepeatField(

function prepareDiagnosis(fields: FormField[]) {
return fields
.filter((field) => field.type === 'diagnosis' && hasSubmission(field))
.flatMap((field) => [field.meta.submission.newValue, field.meta.submission.voidedValue])
.filter((d) => d);
.filter((field) => field.type === 'diagnosis')
.map((field) => {
if (field?.meta?.repeat?.wasDeleted) {
const voided = {
uuid: field?.meta?.previousValue?.uuid,
voided: true,
};
if (field.meta?.submission) {
field.meta.submission.voidedValue = voided;
}
return field;
}
return field;
})
.flatMap((field) => {
const values = [];
if (field?.meta?.submission?.newValue) {
values.push(field.meta.submission.newValue);
}
if (field?.meta?.submission?.voidedValue) {
values.push(field.meta.submission.voidedValue);
}
return values;
});
}
13 changes: 13 additions & 0 deletions src/transformers/default-schema-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,19 @@ function handleDiagnosis(question: FormField) {
class: question.questionOptions.diagnosis?.conceptClasses,
},
};
if (question.questionOptions.diagnosis?.conceptSet) {
question.questionOptions = {
...question.questionOptions,
concept: question.questionOptions.diagnosis?.conceptSet,
datasource: {
name: 'problem_datasource',
config: {
useSetMembersByConcept: true,
},
},
};
}

delete question.questionOptions['dataSource'];
}
}
1 change: 1 addition & 0 deletions src/types/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ export interface FormQuestionOptions {
rank?: number;
isConfirmed?: boolean;
conceptClasses?: Array<string>;
conceptSet?: string;
};
}

Expand Down

0 comments on commit 025ca74

Please sign in to comment.