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

OHRI-1963 Enable editing of L&D form infant details in OHRI #1689

Merged
merged 4 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/esm-ohri-pmtct-app/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ export function getEstimatedDeliveryDate(patientUuid: string, pTrackerId: string
});
}

export function getIdentifierInfo(identifier: string){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kajambiya this function looks solid to me but the only thing missing here is HTTP request & response error handling, would be good if you can add to this function and we show this to the other devs otherwise we will keep having those errors that emerge out of the blue and the user doesn't understand. Here in case an HTTP request or response error happens I would expect to see a message like Failed to retrieve identifier information. Also shouldn't this function be marked as async ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ebambo, All the error handling regarding api calls including http error handling is done in the openmrsFetch function
We don't need to mark the function async because we do not care when it finishes processing. We would have to add async if there was need to await for the operation to first finish.

return openmrsFetch(
`${BASE_WS_API_URL}patient?identifier=${identifier}&v=custom:(identifiers:(identifier,identifierType:(uuid,display)),person:(display))`,
).then(({ data }) => {
if (data) {
return data;
}
return null;
});
}

export function fetchMotherHIVStatus(patientUuid: string, pTrackerId: string) {
return openmrsFetch(
`${BASE_WS_API_URL}reportingrest/dataSet/${motherHivStatusReport}?person_uuid=${patientUuid}&ptracker_id=${pTrackerId}`,
Expand Down
2 changes: 1 addition & 1 deletion packages/esm-ohri-pmtct-app/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
OHRIWelcomeSection,
createConditionalDashboardGroup,
} from '@ohri/openmrs-esm-ohri-commons-lib';
import { generateInfantPTrackerId } from './utils/ptracker-forms-helpers';
import { generateInfantPTrackerId } from './utils/pmtct-helpers';

export const importTranslation = require.context('../translations', false, /.json$/, 'lazy');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Patient, PatientIdentifier } from '../api/types';
import { findObsByConcept, findChildObsInTree, getObsValueCoded } from '../utils/obs-encounter-utils';
import { updatePatientPtracker } from './current-ptracker-action';
import { getConfig } from '@openmrs/esm-framework';
import { getIdentifierAssignee } from '../utils/pmtct-helpers';

// necessary data points about an infact captured at birth
const infantDetailsGroup = '1c70c490-cafa-4c95-9fdd-a30b62bb78b8';
Expand All @@ -21,15 +22,15 @@ export const MotherToChildLinkageSubmissionAction: PostSubmissionAction = {
const encounter = encounters[0];
const encounterLocation = encounter.location['uuid'];
// only do this the first time the form is entered
if (sessionMode !== 'enter') {
if (sessionMode === 'view') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the comment or delete it to indicate the hook runs on both enter and edit

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return;
}

const preferredIdentifierSource = await getPreferredIdentifierSource();
await updatePatientPtracker(encounter, encounterLocation, patient.id);
const infantsToCreate = await Promise.all(
findObsByConcept(encounter, infantDetailsGroup).map(async (obsGroup) =>
constructPatientObjectFromObsData(obsGroup, encounterLocation, preferredIdentifierSource),
constructPatientObjectFromObsData(obsGroup, encounterLocation, preferredIdentifierSource, sessionMode),
),
);
const newInfantsToCreate = await Promise.all(infantsToCreate.filter((infant) => infant !== null));
Expand All @@ -53,11 +54,23 @@ async function constructPatientObjectFromObsData(
obsGroup,
encounterLocation: string,
preferredIdentifierSource: string,
sessionMode: string,
): Promise<Patient> {
// check if infant is alive
const lifeStatusAtBirth = findChildObsInTree(obsGroup, infantLifeStatus);
// the infant is alive hence eligible for registration
if (getObsValueCoded(lifeStatusAtBirth) == aliveStatus) {
// the infant is alive hence eligible for registration

const pTrackerId = findChildObsInTree(obsGroup, infantPTrackerId)?.value;
const existingpTrackerAssignee = await getIdentifierAssignee(pTrackerId, PtrackerIdentifierType);
if(existingpTrackerAssignee){
if(sessionMode === 'enter'){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this include edit mode?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adjusted accordingly

throw new Error(`P Tracker Id (${pTrackerId}) already assigned to patient (${existingpTrackerAssignee})`);
}
else{
return null;
}
kajambiya marked this conversation as resolved.
Show resolved Hide resolved
}
const patient: Patient = {
identifiers: [],
person: {
Expand All @@ -77,8 +90,7 @@ async function constructPatientObjectFromObsData(
causeOfDeath: '',
},
};
// PTracker ID
const pTrackerId = findChildObsInTree(obsGroup, infantPTrackerId)?.value;

if (pTrackerId) {
patient.identifiers = [
{
Expand Down
26 changes: 26 additions & 0 deletions packages/esm-ohri-pmtct-app/src/utils/pmtct-helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { getIdentifierInfo } from '../api/api';

export const generateInfantPTrackerId = (fieldId: string, motherPtrackerId: string): string | undefined => {
if (!fieldId || !motherPtrackerId) return;
return fieldId === 'infantPtrackerid'
? motherPtrackerId + '1'
: fieldId.includes('_')
? motherPtrackerId.concat((Number(fieldId.split('_')[1]) + 1).toString())
: undefined;
};

export const getIdentifierAssignee = async (identifier: string, identifierType: string) => {
return getIdentifierInfo(identifier).then((data) => {
if (data) {
for (const result of data.results) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if data has no results attribute?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment also applies to the lines below, you better explicitly check or add the question marks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, by the time we get here, all the other possible errors were checked and the call is fine. If there's no data, it only means that there's no patient with that identifier meaning it's ok to go ahead and assign it to the current patient.

for (const identifierObj of result.identifiers) {
if (identifierObj.identifier === identifier && identifierObj.identifierType.uuid === identifierType) {
return result.person.display;
}
}
}
return '';
}
return '';
});
};
11 changes: 0 additions & 11 deletions packages/esm-ohri-pmtct-app/src/utils/ptracker-forms-helpers.ts

This file was deleted.