diff --git a/programs/patient-data-resolver.service.js b/programs/patient-data-resolver.service.js index 5bb7fa37f..8a04056cc 100755 --- a/programs/patient-data-resolver.service.js +++ b/programs/patient-data-resolver.service.js @@ -119,7 +119,10 @@ function checkTransferOut(patientUuid, params) { } function getMedicationRefillVisits(patientUuid) { - const expectedAdultReturnEncounter = '8d5b2be0-c2cc-11de-8d13-0010c6dffd0f'; + const expectedEncounters = [ + '8d5b2be0-c2cc-11de-8d13-0010c6dffd0f', + '4e7553b4-373d-452f-bc89-3f4ad9a01ce7' + ]; const patientEncounters = encounterService.getPatientEncounters({ patientUuid, @@ -134,7 +137,7 @@ function getMedicationRefillVisits(patientUuid) { .filter( (encounter) => encounter.encounterType && - encounter.encounterType.uuid === expectedAdultReturnEncounter + expectedEncounters.includes(encounter.encounterType.uuid) ) .sort( (a, b) => diff --git a/programs/patient-program-config.json b/programs/patient-program-config.json index 5c25e815d..93955e2cc 100755 --- a/programs/patient-program-config.json +++ b/programs/patient-program-config.json @@ -736,8 +736,8 @@ }, { "uuid": "a2a265dc-508d-4f31-94ee-0bf8a58049d7", - "name": "HCW-Peer Delivery Visit", - "allowedIf": "!isEligibleForCommunityVisit", + "name": "HCW/Peer Delivery Visit", + "allowedIf": "!isEligibleForCommunityVisit && !isFirstAMPATHHIVVisit", "encounterTypes": [ { "uuid": "987009c6-6f24-43f7-9640-c285d6553c63", @@ -2980,8 +2980,8 @@ "visitTypes": [ { "uuid": "0becf5b9-0522-47c8-960c-c9d660c56501", - "name": "Facilty Medication Delivery Visit", - "allowedIf": "qualifiesMedicationRefillVisit && isEligibleForMedicationRefill", + "name": "Facility Medication Refill Visit", + "allowedIf": "programLocation === intendedVisitLocationUuid && qualifiesMedicationRefillVisit && isEligibleForMedicationRefill", "message": "Patient has to be in the location he enrolled into the HIV Care and Treatment program ", "encounterTypes": [ { @@ -3309,7 +3309,7 @@ { "uuid": "3c1906a1-6d85-4184-9a43-0912cbac3d74", "name": "Community Medication Delivery Visit", - "allowedIf": "qualifiesMedicationRefillVisit && isEligibleForCommunityVisit", + "allowedIf": "programLocation === intendedVisitLocationUuid && qualifiesMedicationRefillVisit && isEligibleForCommunityVisit", "message": "Patient has to be in the location he enrolled into the HIV Care and Treatment program ", "encounterTypes": [ { @@ -3320,8 +3320,8 @@ }, { "uuid": "a2a265dc-508d-4f31-94ee-0bf8a58049d7", - "name": "HCW-Peer Delivery Visit", - "allowedIf": "!isEligibleForCommunityVisit", + "name": "HCW/Peer Delivery Visit", + "allowedIf": "!isEligibleForCommunityVisit && !isFirstAMPATHHIVVisit", "encounterTypes": [ { "uuid": "987009c6-6f24-43f7-9640-c285d6553c63", diff --git a/programs/scope-builder.service.js b/programs/scope-builder.service.js index 1c0b1b287..a11505f5b 100755 --- a/programs/scope-builder.service.js +++ b/programs/scope-builder.service.js @@ -431,15 +431,21 @@ function validateMedicationRefillEligibility(validateMedicationRefill) { const icadAnswerUuid = '7e5e7759-e9f7-4b16-b904-041fcdddb390'; + // Version 2.0 cutoff date + const VERSION_2_CUTOFF_DATE = new Date('2025-05-02'); + let isEligibleForMedicationRefill = false; let isEligibleForCommunityVisit = false; + let hasMedicationRefillConcept = false; + // Check if the new concepts exist in observations for (const obs of validateMedicationRefill.obs) { const conceptUuid = obs.concept?.uuid; if (!conceptUuid) continue; if (conceptUuid === conceptMap.medicationRefillEligibility) { + hasMedicationRefillConcept = true; isEligibleForMedicationRefill = true; } @@ -451,9 +457,34 @@ function validateMedicationRefillEligibility(validateMedicationRefill) { } } + // Backwards compatibility logic for medicationRefillEligibility + if (!hasMedicationRefillConcept) { + const encounterDate = new Date(validateMedicationRefill.encounterDatetime); + + // For encounters before version 2.0, default to eligible for medication refill + // This maintains the previous workflow where this validation didn't exist + if (encounterDate < VERSION_2_CUTOFF_DATE) { + isEligibleForMedicationRefill = true; + console.log( + 'Legacy encounter detected - defaulting medicationRefill eligibility to true' + ); + } + // For newer encounters without the concept + else { + console.warn( + 'Recent encounter missing medicationRefillEligibility concept' + ); + isEligibleForMedicationRefill = false; + } + } + return { medicationRefill: isEligibleForMedicationRefill, - communityVisit: isEligibleForCommunityVisit + communityVisit: isEligibleForCommunityVisit, + isLegacyEncounter: + !hasMedicationRefillConcept && + new Date(validateMedicationRefill.encounterDatetime) < + VERSION_2_CUTOFF_DATE }; }