Skip to content

Commit

Permalink
Merge pull request #489 from udsm-dhis2-lab/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
josephatJ authored Nov 19, 2024
2 parents 5f7c46d + dd5ac9c commit 4ed6402
Show file tree
Hide file tree
Showing 77 changed files with 1,992 additions and 1,106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,11 @@ String pushDataToExternalMediator(String data, String mediatorKey, String mediat
Map<String, Object> generateVisitsData(Date startDate, Date endDate, Boolean sendToExternalMediator, String uuid)
throws Exception;

List<Map<String, Object>> getPatientVisitsByIdentifier(String id, String idType, String referralNumber,
Integer numberOfVisits) throws Exception;

Map<String, Object> sendReferralDataToMediator(String uuid) throws Exception;

String getSharedRecordsFromExternalMediator(String hfrCode, String id, String idType) throws Exception;
String getSharedRecordsFromExternalMediator(String hfrCode, String id, String idType, String referralNumber)
throws Exception;
}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,9 @@ public ItemPrice saveItemPrice(ItemPrice itemPrice) throws APIException {
itemPrice.setPaymentType(paymentType);
if (itemPrice.getPayable() != null && itemPrice.getPayablePaymentMode() == null) {
throw new APIException("Payment mode for payable not provided");
} else {
} else if (itemPrice.getPayablePaymentMode() != null
&& itemPrice.getPayablePaymentMode().getUuid() != null
&& conceptService.getConceptByUuid(itemPrice.getPayablePaymentMode().getUuid()) != null) {
Concept payablePaymentMode = conceptService.getConceptByUuid(itemPrice.getPayablePaymentMode().getUuid());
itemPrice.setPayablePaymentMode(payablePaymentMode);
}
Expand Down Expand Up @@ -1272,6 +1274,21 @@ public Map<String, Object> generateVisitsData(Date startDate,
return dataTemplateData;
}

public List<Map<String,Object>> getPatientVisitsByIdentifier(String id, String idType, String referralNumber, Integer numberOfVisits) throws Exception {
// 1. Get client from OpenMRS
// TODO: Add support to search visits by referralNumber
List<Visit> visits = dao.getPatientVisitsByIdentifier(id, idType, numberOfVisits);
List<Map<String,Object>> visitsData = new ArrayList<>();
for(Visit visit: visits) {
Map<String,Object> templateData = new HashMap<>();
templateData.put("visitDetails", prepareVisitDetails(visit));
templateData.put("demographicDetails", prepareDemographicDetails(visit));
templateData.put("diagnosisDetails", prepareDiagnosisDetails(visit));
visitsData.add(templateData);
}
return visitsData;
}

public Map<String,Object> sendReferralDataToMediator(String uuid) throws Exception {
Map<String,Object> visitData = new HashMap<>();
List<Visit> visits = dao.getVisitsByStartDateAndEndDate(null, null, uuid);
Expand Down Expand Up @@ -1408,23 +1425,28 @@ private String pushDataToExternalMediator(Map<String, Object> dataTemplateData,
AdministrationService adminService = Context.getService(AdministrationService.class);
String mediatorsConfigs = adminService.getGlobalProperty(ICareConfig.INTEROPERABILITY_MEDIATORS_LIST);

JSONArray mediatorsList = new JSONArray(mediatorsConfigs);
ICareService iCareService = Context.getService(ICareService.class);

for (int count = 0; count < mediatorsList.length(); count++) {
JSONObject mediator = mediatorsList.getJSONObject(count);
if (mediator.optBoolean("isActive") && mediatorKeyType.equals(mediator.getString("mediatorKey"))) {
String mediatorKey = mediator.getString("mediatorKey");
String mediatorUrlPath = mediator.getString("mediatorUrlPath");
String authenticationType = mediator.getString("authenticationType");
authReferenceKey = mediator.getString("") != null ? mediator.getString("authKeyReference") : mediator
.getString("mediatorKey");
return iCareService.pushDataToExternalMediator(new JSONObject(dataTemplateData).toString(), mediatorKey,
mediatorUrlPath, authenticationType, authReferenceKey);
if (mediatorsConfigs != null) {
JSONArray mediatorsList = new JSONArray(mediatorsConfigs);
ICareService iCareService = Context.getService(ICareService.class);

if (!mediatorsList.isEmpty()) {
for (int count = 0; count < mediatorsList.length(); count++) {
JSONObject mediator = mediatorsList.getJSONObject(count);
if (mediator.optBoolean("isActive") && mediatorKeyType.equals(mediator.getString("mediatorKey"))) {
String mediatorKey = mediator.getString("mediatorKey");
String mediatorUrlPath = mediator.getString("mediatorUrlPath");
String authenticationType = mediator.getString("authenticationType");
authReferenceKey = mediator.getString("") != null ? mediator.getString("authKeyReference")
: mediator.getString("mediatorKey");
return iCareService.pushDataToExternalMediator(new JSONObject(dataTemplateData).toString(),
mediatorKey, mediatorUrlPath, authenticationType, authReferenceKey);
}
}
}
}
}
catch (Exception e) {
e.printStackTrace();
throw new Exception(e.getMessage());
}
return "";
Expand Down Expand Up @@ -1476,7 +1498,8 @@ private List<Map<String,Object>> getPatientIdentifiers(Patient patient) {
@Override
public String getSharedRecordsFromExternalMediator(String hfrCode,
String id,
String idType) throws Exception {
String idType,
String referralNumber) throws Exception {
try {
Map<String,Object> responseData = new HashMap<>();
AdministrationService administrationService = Context.getAdministrationService();
Expand All @@ -1500,16 +1523,27 @@ public String getSharedRecordsFromExternalMediator(String hfrCode,

if (id != null) {
mediatorUrl += "?id=" + id;
if (idType != null) {
if (mediatorUrl.contains("?")) {
mediatorUrl += "&idType="+ idType;
}
}
}

if (hfrCode != null) {
if (mediatorUrl.contains("?")) {
mediatorUrl += "&hfrCode="+ hfrCode;
} else {
mediatorUrl += "?hfrCode=" + hfrCode;
}
}
if (idType != null) {


if (referralNumber != null) {
if (mediatorUrl.contains("?")) {
mediatorUrl += "&idType="+ idType;
mediatorUrl += "&referralNumber="+ referralNumber;
} else {
mediatorUrl += "?referralNumber=" + referralNumber;
}
}
// Construct the URL for the GET request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,25 +205,56 @@ public SampleStatus saveSampleStatus(SampleStatus sampleStatus) throws Exception
@Override
public SampleStatus updateSampleStatus(SampleStatus sampleStatus) throws Exception {

// Retrieve the sample from the database by UUID
Sample sample = this.getSampleByUuid(sampleStatus.getSample().getUuid());
if (sample == null) {
throw new Exception("Sample with ID '" + sampleStatus.getSample().getUuid() + "' does not exist.");
}
User user = new User();
if (sampleStatus.getUser() != null) {
Context.getUserService().getUserByUuid(sampleStatus.getUser().getUuid());

User user;
if (sampleStatus.getUser() != null && sampleStatus.getUser().getUuid() != null) {
// Retrieve the existing user by UUID if specified
user = Context.getUserService().getUserByUuid(sampleStatus.getUser().getUuid());
if (user == null) {
throw new Exception("User with UUID '" + sampleStatus.getUser().getUuid() + "' does not exist.");
}
} else {
// Otherwise, use the authenticated user
user = Context.getAuthenticatedUser();
if (user == null) {
throw new Exception("The user is not authenticated.");
}
}

if (user == null) {
throw new Exception("The user is not authenticated.");
}
// Ensure that sample and user are set on the sampleStatus object
sampleStatus.setSample(sample);
sampleStatus.setUser(user);

// Save the sampleStatus entity
return this.sampleStatusDAO.save(sampleStatus);
}

// public SampleStatus updateSampleStatus(SampleStatus sampleStatus) throws Exception {

// Sample sample = this.getSampleByUuid(sampleStatus.getSample().getUuid());
// if (sample == null) {
// throw new Exception("Sample with ID '" + sampleStatus.getSample().getUuid() + "' does not exist.");
// }
// User user = new User();
// if (sampleStatus.getUser() != null) {
// Context.getUserService().getUserByUuid(sampleStatus.getUser().getUuid());
// } else {
// user = Context.getAuthenticatedUser();
// }

// if (user == null) {
// throw new Exception("The user is not authenticated.");
// }
// sampleStatus.setSample(sample);
// sampleStatus.setUser(user);
// return this.sampleStatusDAO.save(sampleStatus);
// }

@Override
public TestAllocation allocateTestWithSample(TestAllocation testAllocation) throws Exception {
//Check preconditions
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.openmrs.module.icare.scheduler.tasks;

import org.openmrs.api.context.Context;
import org.openmrs.module.icare.core.ICareService;
import org.openmrs.scheduler.tasks.AbstractTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

public class AutoGenerateAndSendDataTemplateTask extends AbstractTask {

private static final Logger log = LoggerFactory.getLogger(AutoGenerateAndSendDataTemplateTask.class);

public AutoGenerateAndSendDataTemplateTask() {
}

@Override
public void execute() {

if (!this.isExecuting) {
if (log.isDebugEnabled()) {
log.debug("Starting Auto Generate Data Template Task...");
}

this.startExecuting();

try {
Date endDate = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(endDate);
calendar.add(Calendar.HOUR_OF_DAY, -24);
Date startDate = calendar.getTime();

ICareService iCareService = Context.getService(ICareService.class);
System.out.println(startDate);
System.out.println(endDate);
iCareService.generateVisitsData(startDate, endDate, true, null);
}
catch (Exception e) {
e.printStackTrace();
log.error("Error while auto generating data template and sending to external mediator ", e);
}
finally {
this.stopExecuting();
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1727,7 +1727,31 @@ public Map<String, Object> sendReferralDataToMediator(@RequestBody Map<String, O
@ResponseBody
public String getClientDataFromExternalMediator(@RequestParam(value = "hfrCode", required = false) String hfrCode,
@RequestParam(value = "id", required = true) String id,
@RequestParam(value = "referralNumber", required = false) String referralNumber,
@RequestParam(value = "idType", required = false) String idType) throws Exception {
return iCareService.getSharedRecordsFromExternalMediator(hfrCode, id, idType);
return iCareService.getSharedRecordsFromExternalMediator(hfrCode, id, idType, referralNumber);
}

@RequestMapping(value = "emrHealthRecords", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Map<String,Object> retrieveClientsData(
@RequestParam(value = "hfrCode", required = false) String hfrCode,
@RequestParam(value = "id", required = false) String id,
@RequestParam(value ="referralNumber", required = false) String referralNumber,
@RequestParam(value = "idType", required = false) String idType,
@RequestParam(value = "count", required = true, defaultValue = "1") Integer count) throws Exception {
try {
Map<String,Object> response = new HashMap<>();
Map<String,Object> requestInfo = new HashMap<>();
requestInfo.put("id", id);
requestInfo.put("idType", idType);
requestInfo.put("count", count);
response.put("requestInfo",requestInfo);
response.put("results", iCareService.getPatientVisitsByIdentifier(id, idType, referralNumber, count));
return response;
}catch (Exception e) {
e.printStackTrace();
throw new Exception(e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1045,4 +1045,13 @@ public void testSharedRecordsAPI() throws Exception {
Map<String, Object> responseMap = (new ObjectMapper()).readValue(mockedResponse.getContentAsString(), Map.class);
System.out.println(responseMap);
}

@Test
public void testGetPatientVisitsByIdentifier() throws Exception {
MockHttpServletRequest emrHealthRecordsRequest = newGetRequest("icare/emrHealthRecords", new Parameter("id",
"110620-2/10891/21"), new Parameter("idType", "MRN"), new Parameter("count", "1"));
MockHttpServletResponse mockedResponse = handle(emrHealthRecordsRequest);
Map<String, Object> responseMap = (new ObjectMapper()).readValue(mockedResponse.getContentAsString(), Map.class);
System.out.println(responseMap);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
[isTabularList]="true"
[filterCategory]="'billing'"
[orderBy]="'ENCOUNTER'"
[orderByDirection]="'ASC'"
[orderByDirection]="'DESC'"
(selectPatient)="onSelectPatient($event)"
></app-patient-list>
</div>
Expand Down
Loading

0 comments on commit 4ed6402

Please sign in to comment.