-
Notifications
You must be signed in to change notification settings - Fork 40
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
@@ -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') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
|
@@ -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'){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this include edit mode? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: { | ||
|
@@ -77,8 +90,7 @@ async function constructPatientObjectFromObsData( | |
causeOfDeath: '', | ||
}, | ||
}; | ||
// PTracker ID | ||
const pTrackerId = findChildObsInTree(obsGroup, infantPTrackerId)?.value; | ||
|
||
if (pTrackerId) { | ||
patient.identifiers = [ | ||
{ | ||
|
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ''; | ||
}); | ||
}; |
This file was deleted.
There was a problem hiding this comment.
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
?There was a problem hiding this comment.
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 toawait
for the operation to first finish.