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

POC-873: HTS Module Control flow #1418

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions departments/department-programs-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -212,5 +212,14 @@
"name": "RETENTION PROGRAM"
}
]
},
"uud7": {
"name": "HTS",
"programs": [
{
"uuid": "a0f8382f-df8a-4f1d-8959-9fb6eef90353",
"name": "HTS PROGRAM"
}
]
}
}
51 changes: 51 additions & 0 deletions programs/patient-data-resolver.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const availableKeys = {
hivLastEncounter: getPatientLastEncounter,
patientEnrollment: getPatientEnrollement,
patientEncounters: getPatientEncounters,
isHtsPatientNegative: getPatientLatestHTSInitialEncounter,
isPatientTransferredOut: checkTransferOut,
dcQualifedVisits: getQualifiedDcVisits,
latestCovidAssessment: getLatestCovidAssessment,
Expand All @@ -30,6 +31,7 @@ const def = {
availableKeys: availableKeys,
getPatientLastEncounter: getPatientLastEncounter,
getPatientEncounters: getPatientEncounters,
getPatientLatestHTSInitialEncounter: getPatientLatestHTSInitialEncounter,
checkTransferOut: checkTransferOut,
dcQualifedVisits: getQualifiedDcVisits,
getLatestCovidAssessment: getLatestCovidAssessment,
Expand Down Expand Up @@ -134,6 +136,55 @@ function getPatientEncounters(patientUuid) {
});
}

function getPatientLatestHTSInitialEncounter(patientUuid) {
return new Promise((resolve, reject) => {
encounterService
.getPatientEncounters({
patientUuid,
v:
'custom:(encounterDatetime,encounterType:(uuid,display),obs:(uuid,concept:(uuid,display),value))'
})
.then((encounters) => {
if (!encounters || encounters.length === 0) {
return resolve(false);
}

// HTS Encounter UUID
const HTS_ENCOUNTER_UUID = 'ae9693ff-d341-4997-8166-fa46ac4d38f4';
const TARGET_OBS_UUIDS = [
'a896d2cc-1350-11df-a1f1-0026b9348838',
'a89a7ae4-1350-11df-a1f1-0026b9348838'
];

// Filter for only HTS encounters
const htsEncounters = encounters.filter(
(enc) => enc.encounterType?.uuid === HTS_ENCOUNTER_UUID
);

if (htsEncounters.length === 0) {
return resolve(false);
}

// Get the latest encounter based on encounterDatetime
htsEncounters.sort(
(a, b) =>
new Date(b.encounterDatetime) - new Date(a.encounterDatetime)
);
const latestEncounter = htsEncounters[0];
// Check if the latest encounter contains the required obs concept
const hasTargetObs = latestEncounter.obs?.some((obs) =>
TARGET_OBS_UUIDS.includes(obs.value?.uuid)
);

resolve(hasTargetObs);
})
.catch((e) => {
console.error('An error occurred fetching encounters: ', e);
reject(e);
});
});
}

function gethivLastTenClinicalEncounters(patientUuid, params) {
return new Promise((resolve, reject) => {
etlHivSummary
Expand Down
93 changes: 93 additions & 0 deletions programs/patient-program-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5699,5 +5699,98 @@
]
}
]
},
"a0f8382f-df8a-4f1d-8959-9fb6eef90353": {
"name": "HTS PROGRAM",
"dataDependencies": [
"patient",
"enrollment",
"patientEncounters",
"isHtsPatientNegative"
],
"incompatibleWith": [
"781d85b0-1359-11df-a1f1-0026b9348838",
"781d897a-1359-11df-a1f1-0026b9348838",
"334c9e98-173f-4454-a8ce-f80b20b7fdf0",
"c4246ff0-b081-460c-bcc5-b0678012659e",
"c6bf3625-de80-4a88-a913-38273e300a55",
"8c1a3a68-d91d-488d-8663-ad572673232b",
"f7793d42-11ac-4cfd-9b35-e0a21a7a7c31",
"203571d6-a4f2-4953-9e8b-e1105e2340f5"
],
"visitTypes": [
{
"uuid": "f92e8456-4de6-4776-82c3-c15b81eb8f5d",
"name": "HTS New/Retest Visit",
"message": "HTS retest should have been done.",
"allowedIf": "",
"encounterTypes": [
{
"uuid": "82749926-63b1-467b-9d41-453c7542678a",
"display": "HTSSCREENING",
"allowedIf": "showOnlyHTSScreening"
},
{
"uuid": "ae9693ff-d341-4997-8166-fa46ac4d38f4",
"display": "HTSINITIAL",
"allowedIf": "showOnlyHTSINITIAL"
},
{
"uuid": "16bac581-5a17-43f2-9315-3e12a1f3189a",
"display": "HTSRETEST",
"allowedIf": "showOnlyHTSRetest"
},
{
"uuid": "fbb106cf-d24f-4917-b905-42db7549a788",
"display": "HTSLINKAGE",
"allowedIf": "showOthersHTSEncounters"
},
{
"uuid": "55c10a7a-2732-4063-be25-68d5e1bce1fc",
"display": "HTSREFERRAL",
"allowedIf": "showOthersHTSEncounters"
}
]
},
{
"uuid": "7d2b0fb1-f1d7-4173-81aa-c8b832407656",
"name": "HTS Retest To Confirm Positive Visit",
"message": "HTS retest should have been done.",
"allowedIf": "",
"encounterTypes": [
{
"uuid": "16bac581-5a17-43f2-9315-3e12a1f3189a",
"display": "HTSRETEST",
"allowedIf": "showHTSRetestToConfirmP"
},
{
"uuid": "55c10a7a-2732-4063-be25-68d5e1bce1fc",
"display": "HTSREFERRAL",
"allowedIf": "!showHTSRetestToConfirmP"
},
{
"uuid": "fbb106cf-d24f-4917-b905-42db7549a788",
"display": "HTSLINKAGE",
"allowedIf": "!showHTSRetestToConfirmP"
}
]
},
{
"uuid": "81978aea-61c1-426e-82e6-ade88f4e25ef",
"name": "HTS Linkage Visit",
"message": "HTS retest should have been done.",
"allowedIf": "",
"encounterTypes": [
{
"uuid": "fbb106cf-d24f-4917-b905-42db7549a788",
"display": "HTSLINKAGE"
},
{
"uuid": "55c10a7a-2732-4063-be25-68d5e1bce1fc",
"display": "HTSREFERRAL"
}
]
}
]
}
}
130 changes: 128 additions & 2 deletions programs/scope-builder.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ function buildScope(dataDictionary) {
lastCovidScreeningDate: '',
retroSpective: false,
screenedForCovidToday: false,
isViremicHighVL: false
isViremicHighVL: false,
hasHTSEncounters: false,
showOnlyHTSScreening: false,
showOnlyHTSINITIAL: false,
showOnlyHTSRetest: false,
showHTSRetestToConfirmP: false,
showOthersHTSEncounters: false
};
let isStandardDcVisit = false;

Expand Down Expand Up @@ -272,7 +278,6 @@ function buildScope(dataDictionary) {
} else {
const today = Moment().format('YYYY-MM-DD');
const visitDate = Moment(dataDictionary.visitDate).format('YYYY-MM-DD');

if (dataDictionary.latestCovidAssessment) {
scope['lastCovidScreeningDate'] = dataDictionary.latestCovidAssessment;
const screeningDate = Moment(dataDictionary.latestCovidAssessment).format(
Expand Down Expand Up @@ -305,6 +310,38 @@ function buildScope(dataDictionary) {
) {
scope.isViremicHighVL = true;
}

if (dataDictionary.patientEncounters) {
scope.patientEncounters = dataDictionary.patientEncounters;
scope.programUuid = dataDictionary.programUuid;

// Add HTS scope building alongside other program builders
if (dataDictionary.programUuid === 'a0f8382f-df8a-4f1d-8959-9fb6eef90353') {
buildHTSScopeMembers(
scope,
dataDictionary.patientEncounters,
dataDictionary.isHtsPatientNegative,
dataDictionary
);
}

buildHivScopeMembers(
scope,
dataDictionary.patientEncounters,
dataDictionary?.intendedVisitLocationUuid
);
buildOncologyScopeMembers(
scope,
dataDictionary.patientEncounters,
dataDictionary.programUuid
);
buildMNCHScopeMembers(
scope,
dataDictionary.patientEncounters,
dataDictionary.patientEnrollment
);
}

// add other methods to build the scope objects
return scope;
}
Expand Down Expand Up @@ -546,6 +583,95 @@ function isInitialOncologyVisit(encounters, programUuid) {
return initialOncologyEncounters.length === 0;
}

function buildHTSScopeMembers(
scope,
patientEncounters,
isHtsPatientNegative,
dataDictionary
) {
const HTS_ENCOUNTER_TYPES = {
SCREENING: '82749926-63b1-467b-9d41-453c7542678a',
INITIAL: 'ae9693ff-d341-4997-8166-fa46ac4d38f4',
RETEST: '16bac581-5a17-43f2-9315-3e12a1f3189a',
LINKAGE: 'fbb106cf-d24f-4917-b905-42db7549a788',
REFERRAL: '55c10a7a-2732-4063-be25-68d5e1bce1fc'
};
const visitDate = Moment(dataDictionary.visitDate).format('YYYY-MM-DD');

const today = dataDictionary.retroSpective
? visitDate
: Moment().startOf('day');

const isFromPreviousDate = (encounter) => {
const encounterDate =
encounter?.encounterDatetime &&
Moment(encounter.encounterDatetime).startOf('day');
return encounterDate ? encounterDate.isBefore(today) : false;
};

const getLatestEncounterOfType = (encounters, encounterTypeUuid) =>
encounters
?.filter(({ encounterType }) => encounterType?.uuid === encounterTypeUuid)
.sort(
(a, b) =>
Moment(b.encounterDatetime).valueOf() -
Moment(a.encounterDatetime).valueOf()
)[0];

scope.hasHTSEncounters =
patientEncounters?.some(({ encounterType }) =>
Object.values(HTS_ENCOUNTER_TYPES).includes(encounterType?.uuid)
) || false;

if (!scope.hasHTSEncounters) {
scope.showOnlyHTSScreening = true;
scope.showHTSRetestToConfirmP = true;
return scope;
}

const latestScreening = getLatestEncounterOfType(
patientEncounters,
HTS_ENCOUNTER_TYPES.SCREENING
);
const latestInitial = getLatestEncounterOfType(
patientEncounters,
HTS_ENCOUNTER_TYPES.INITIAL
);
const latestRetest = getLatestEncounterOfType(
patientEncounters,
HTS_ENCOUNTER_TYPES.RETEST
);

const hasOldScreening =
latestScreening && isFromPreviousDate(latestScreening);
const hasOldInitial = latestInitial && isFromPreviousDate(latestInitial);
const hasScreeningToday =
latestScreening && !isFromPreviousDate(latestScreening);
const hasInitialToday = latestInitial && !isFromPreviousDate(latestInitial);
const hasRetestToday = latestRetest && !isFromPreviousDate(latestRetest);

if (isHtsPatientNegative && hasInitialToday) {
scope.showOnlyHTSINITIAL = true;
return scope;
}

if (!hasScreeningToday) {
scope.showOnlyHTSScreening = true;
scope.isFirstHTSNewRetestVisit = true;
} else if (hasScreeningToday && !hasInitialToday) {
scope.showOnlyHTSINITIAL = true;
scope.isFirstHTSNewRetestVisit = false;
} else if (hasInitialToday && !hasRetestToday) {
scope.showOnlyHTSRetest = true;
} else if (hasRetestToday) {
scope.showOthersHTSEncounters = true;
} else {
scope.showOnlyHTSScreening = true;
}

return scope;
}

function buildProgramScopeMembers(scope, programEnrollment) {
if (
programEnrollment &&
Expand Down
Loading