diff --git a/api/src/main/java/org/openmrs/module/kenyaemr/metadata/FacilityMetadata.java b/api/src/main/java/org/openmrs/module/kenyaemr/metadata/FacilityMetadata.java index 20fe25c48..959eaf649 100755 --- a/api/src/main/java/org/openmrs/module/kenyaemr/metadata/FacilityMetadata.java +++ b/api/src/main/java/org/openmrs/module/kenyaemr/metadata/FacilityMetadata.java @@ -47,6 +47,7 @@ public static final class _LocationAttributeType { public static final String TELEPHONE_FAX = "29e1e758-d03e-4e84-a55e-288fa63d533a"; public static final String SHA_ACCREDITATION = "7dbbfe5d-8a5a-4b24-897d-0cc5299c3dbb"; public static final String SHA_CONTRACTED_FACILITY = "68d9200e-a469-482f-8cc8-9d0953a3c917"; + public static final String SHA_FACILITY_EXPIRY_DATE = "8e1ec5d4-4810-466a-9c90-b801bae9d063"; } /** @@ -73,6 +74,13 @@ public void install() throws Exception { FreeTextDatatype.class, "", 0, 1, _LocationAttributeType.SHA_CONTRACTED_FACILITY )); + + install(locationAttributeType( + "Facility Expiry Date", "Expiry date for SHA contracted Facility", + FreeTextDatatype.class, "", 0, 1, + _LocationAttributeType.SHA_FACILITY_EXPIRY_DATE + )); + } /** diff --git a/api/src/main/java/org/openmrs/module/kenyaemr/task/FacilityStatusTask.java b/api/src/main/java/org/openmrs/module/kenyaemr/task/FacilityStatusTask.java index 0c7689dfa..289202580 100644 --- a/api/src/main/java/org/openmrs/module/kenyaemr/task/FacilityStatusTask.java +++ b/api/src/main/java/org/openmrs/module/kenyaemr/task/FacilityStatusTask.java @@ -120,28 +120,52 @@ private static String getAuthCredentials() { private static Map extractFacilityStatus(String response) { Map statusMap = new HashMap<>(); - statusMap.put("operationalStatus", "Unknown"); - statusMap.put("approved", "Unknown"); + statusMap.put("operationalStatus", "--"); + statusMap.put("approved", "--"); + statusMap.put("shaFacilityExpiryDate", "--"); try { JSONObject jsonResponse = new JSONObject(response); + log.info("JSON Response: {}", jsonResponse.toString(2)); + JSONArray extensions = jsonResponse.optJSONArray("extension"); if (extensions != null) { for (int i = 0; i < extensions.length(); i++) { JSONObject extension = extensions.optJSONObject(i); if (extension != null) { - String url = extension.getString("url"); - if ("https://shr.tiberbuapps.com/fhir/StructureDefinition/operational-status".equals(url)) { - statusMap.put("operationalStatus", extension.getJSONObject("valueCoding").getString("display")); - } else if ("https://shr.tiberbuapps.com/fhir/StructureDefinition/approved".equals(url)) { - statusMap.put("approved", extension.getJSONObject("valueCoding").getString("display")); + String url = extension.optString("url", ""); + log.debug("Processing extension URL: {}", url); + + switch (url) { + case "https://shr.tiberbuapps.com/fhir/StructureDefinition/operational-status": + statusMap.put("operationalStatus", extension.getJSONObject("valueCoding").optString("display", "--")); + break; + + case "https://shr.tiberbuapps.com/fhir/StructureDefinition/approved": + statusMap.put("approved", extension.getJSONObject("valueCoding").optString("display", "--")); + break; + + case "https://shr.tiberbuapps.com/fhir/StructureDefinition/facility-license-info": + JSONArray licenseExtensions = extension.optJSONArray("extension"); + if (licenseExtensions != null) { + for (int j = 0; j < licenseExtensions.length(); j++) { + JSONObject licenseExtension = licenseExtensions.optJSONObject(j); + if (licenseExtension != null && + "https://shr.tiberbuapps.com/fhir/StructureDefinition/last-expiry-date".equals(licenseExtension.optString("url", ""))) { + statusMap.put("shaFacilityExpiryDate", licenseExtension.optString("valueString", "--")); + } + } + } + break; } } } } } catch (JSONException e) { + log.error("Error parsing facility status JSON: {}", e.getMessage()); logDiagnostics(response); } + return statusMap; } @@ -173,8 +197,9 @@ public void saveFacilityStatus() { if (responseEntity.getStatusCode().is2xxSuccessful()) { Map facilityStatus = extractFacilityStatus(responseEntity.getBody()); - String operationalStatus = facilityStatus.getOrDefault("operationalStatus", "Unknown"); - String approved = facilityStatus.getOrDefault("approved", "Unknown"); + String operationalStatus = facilityStatus.getOrDefault("operationalStatus", "--"); + String approved = facilityStatus.getOrDefault("approved", "--"); + String facilityExpiryDate = facilityStatus.getOrDefault("facilityExpiryDate", "--"); final Location LOCATION = locationService.getLocation(LOCATION_ID); @@ -184,6 +209,8 @@ public void saveFacilityStatus() { // Handle SHA Facility Attribute handleSHAFacilityAttribute(LOCATION, approved); + //Handle SHA facility expiry date + handleSHAFacilityExpiryDateAttribute(LOCATION, facilityExpiryDate); } else { log.error("Failed to save facility status: {}", responseEntity.getBody()); } @@ -246,4 +273,31 @@ public void handleSHAFacilityAttribute(Location LOCATION, String approved) { locationService.saveLocation(LOCATION); log.info("Facility status for MFL Code {} saved successfully: , Approved: {}", MFL_CODE, approved); } + + public void handleSHAFacilityExpiryDateAttribute(Location LOCATION, String facilityExpiryDate) { + LocationAttributeType facilityExpiryDateAttributeType = MetadataUtils.existing(LocationAttributeType.class, FacilityMetadata._LocationAttributeType.SHA_FACILITY_EXPIRY_DATE); + + LocationAttribute facilityExpiryDateAttribute = LOCATION.getActiveAttributes(facilityExpiryDateAttributeType) + .stream() + .filter(attr -> attr.getAttributeType().equals(facilityExpiryDateAttributeType)) + .findFirst() + .orElse(null); + + if (facilityExpiryDateAttribute == null) { + facilityExpiryDateAttribute = new LocationAttribute(); + facilityExpiryDateAttribute.setAttributeType(facilityExpiryDateAttributeType); + facilityExpiryDateAttribute.setValue(facilityExpiryDate); + LOCATION.addAttribute(facilityExpiryDateAttribute); + log.info("SHA License expiry date attribute updated to new value: {}", facilityExpiryDate); + } else { + if (!facilityExpiryDate.equals(facilityExpiryDateAttribute.getValue())) { + facilityExpiryDateAttribute.setValue(facilityExpiryDate); + log.info("SHA Facility attribute updated to new value: {}", facilityExpiryDate); + } else { + log.info("No update needed.SHA Facility License expiry date attribute value is the same: {}", facilityExpiryDate); + } + } + locationService.saveLocation(LOCATION); + log.info("Facility SHA License expiry date for MFL Code {} saved successfully: , License expiry date: {}", MFL_CODE, facilityExpiryDate); + } } \ No newline at end of file diff --git a/omod/src/main/java/org/openmrs/module/kenyaemr/web/controller/KenyaemrCoreRestController.java b/omod/src/main/java/org/openmrs/module/kenyaemr/web/controller/KenyaemrCoreRestController.java index 17312d9a0..c297b8e71 100644 --- a/omod/src/main/java/org/openmrs/module/kenyaemr/web/controller/KenyaemrCoreRestController.java +++ b/omod/src/main/java/org/openmrs/module/kenyaemr/web/controller/KenyaemrCoreRestController.java @@ -429,6 +429,12 @@ public Object getDefaultConfiguredFacility() { .findFirst() .orElse(null); + LocationAttribute shaFacilityExpiryDate = location.getActiveAttributes(MetadataUtils.existing(LocationAttributeType.class, FacilityMetadata._LocationAttributeType.SHA_FACILITY_EXPIRY_DATE)) + .stream() + .filter(attr -> attr.getAttributeType().equals(MetadataUtils.existing(LocationAttributeType.class, FacilityMetadata._LocationAttributeType.SHA_FACILITY_EXPIRY_DATE))) + .findFirst() + .orElse(null); + ObjectNode locationNode = JsonNodeFactory.instance.objectNode(); locationNode.put("locationId", location.getLocationId()); @@ -437,6 +443,7 @@ public Object getDefaultConfiguredFacility() { locationNode.put("operationalStatus", operationalStatusAttribute != null ? operationalStatusAttribute.getValue().toString() : "--"); locationNode.put("operationalStatus", operationalStatusAttribute != null ? operationalStatusAttribute.getValue().toString() : "--"); locationNode.put("shaContracted", isSHAFacilityAttribute != null ? isSHAFacilityAttribute.getValue().toString() : "--" ); + locationNode.put("shaFacilityExpiryDate", shaFacilityExpiryDate != null ? shaFacilityExpiryDate.getValue().toString() : "--" ); return locationNode.toString();