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

Adding Latest visit date and Correct TX Curr #50

Open
wants to merge 1 commit into
base: main
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,73 @@

@Component
public class GetTxCurr {

private final GetDatePatientBecameIIT getDatePatientBecameIIT;

private final GetInterruptedInTreatment getInterruptedInTreatment;

private final GetTxCurrQueries getTxCurrQueries;

public GetTxCurr(GetInterruptedInTreatment getInterruptedInTreatment, GetTxCurrQueries getTxCurrQueries) {
this.getInterruptedInTreatment = getInterruptedInTreatment;
this.getTxCurrQueries = getTxCurrQueries;
public GetTxCurr(GetDatePatientBecameIIT getDatePatientBecameIIT) {
this.getDatePatientBecameIIT = getDatePatientBecameIIT;
}

public List<GetTxNew.PatientEnrollmentData> getTxCurrPatients(Date startDate, Date endDate) {
HashSet<Patient> allPatientsSet = getTxCurrQueries.getTxCurr(startDate, endDate);

// Remove all deceased, IIT, and transferred-out patients
HashSet<Patient> deceasedPatients = getDeceasedPatientsByDateRange(startDate, endDate);
HashSet<Patient> transferredOutPatients = getTransferredOutClients(startDate, endDate);
HashSet<Patient> iitPatients = getInterruptedInTreatment.getIit(startDate, endDate);

allPatientsSet.removeAll(deceasedPatients);
allPatientsSet.removeAll(transferredOutPatients);
allPatientsSet.removeAll(iitPatients);

// Convert patients to PatientEnrollmentData format
List<GetTxNew.PatientEnrollmentData> result = new ArrayList<>();
for (Patient patient : allPatientsSet) {
Obs enrollmentObs = getEnrollmentObsForPatient(patient);
if (enrollmentObs != null) {
result.add(new GetTxNew.PatientEnrollmentData(patient, enrollmentObs.getValueDate()));
// Fetch enrollment dates and patient data
List<Obs> enrollmentDateObs = Context.getObsService().getObservations(null, null,
Collections.singletonList(Context.getConceptService().getConceptByUuid(DATE_OF_ENROLLMENT_UUID)), null, null,
null, null, null, null, null, null, false);

Set<Integer> uniquePatientIds = new HashSet<>();
List<GetTxNew.PatientEnrollmentData> filteredClients = new ArrayList<>();

for (Obs obs : enrollmentDateObs) {
Date enrollmentDate = obs.getValueDate();
Person person = obs.getPerson();

// Check if the person is a patient and add if they haven't been added before
if (enrollmentDate != null && person.isPatient() && uniquePatientIds.add(person.getId())) {
Patient patient = Context.getPatientService().getPatient(person.getId());

// Retrieve death, transfer out, IIT, and RTT dates
Date deathDate = getDeathDate(patient);
Date transferOutDate = getTransferredOutDate(patient);
Date iitDate = getDatePatientBecameIIT.getIitDateForPatient(patient);
Date rttDate = getReturnedToTreatmentDate(patient);

// Determine the earliest exclusion date
Date exclusionDate = null;
if (deathDate != null)
exclusionDate = deathDate;
if (transferOutDate != null)
exclusionDate = (exclusionDate == null || transferOutDate.before(exclusionDate)) ? transferOutDate
: exclusionDate;
if (iitDate != null)
exclusionDate = (exclusionDate == null || iitDate.before(exclusionDate)) ? iitDate : exclusionDate;

// Adjust exclusion logic to consider RTT date
if (exclusionDate != null) {
if (exclusionDate.before(endDate) && exclusionDate.after(enrollmentDate)) {
// If RTT date exists and is after exclusion, re-include patient from RTT onward
if (rttDate != null && !rttDate.after(endDate)) {
GetTxNew.PatientEnrollmentData patientData = new GetTxNew.PatientEnrollmentData(patient,
rttDate);
filteredClients.add(patientData);
System.out.println("Including Patient: " + patient.getPersonName().getFullName()
+ " due to return to treatment date: " + rttDate);
Comment on lines +63 to +64
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Avoid logging patient names to prevent PII leakage

Logging patient names can lead to exposure of sensitive personal information and violate privacy regulations. Replace System.out.println statements with proper logging and avoid including personal identifiers.

Apply this diff to remove patient names from logs and use a logging framework:

- System.out.println("Including Patient: " + patient.getPersonName().getFullName()
-         + " due to return to treatment date: " + rttDate);
+ logger.info("Including patient due to return to treatment date: " + rttDate);

Ensure that a logging framework like SLF4J is used and properly configured:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

private static final Logger logger = LoggerFactory.getLogger(GetTxCurr.class);

continue;
}
System.out.println("Excluding Patient: " + patient.getPersonName().getFullName()
+ " due to exclusion date: " + exclusionDate);
Comment on lines +67 to +68
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Avoid logging patient names to prevent PII leakage

Logging patient names can lead to exposure of sensitive personal information and violate privacy regulations. Replace System.out.println statements with proper logging and avoid including personal identifiers.

Apply this diff to remove patient names from logs and use a logging framework:

- System.out.println("Excluding Patient: " + patient.getPersonName().getFullName()
-         + " due to exclusion date: " + exclusionDate);
+ logger.info("Excluding patient due to exclusion date: " + exclusionDate);

Committable suggestion skipped: line range outside the PR's diff.

continue;
}
}

// Add patient if the enrollment date is within the query range
if (!enrollmentDate.after(endDate)) {
GetTxNew.PatientEnrollmentData patientData = new GetTxNew.PatientEnrollmentData(patient, enrollmentDate);
filteredClients.add(patientData);
}
}
}

// Remove duplicates and return the final list
return new ArrayList<>(new LinkedHashSet<>(result));


return filteredClients;
}

private Obs getEnrollmentObsForPatient(Patient patient) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import static org.openmrs.module.ssemrws.constants.SharedConstants.getVLResults;
Expand Down Expand Up @@ -96,13 +98,19 @@ public Object getAllAvailableFormsForVisit(HttpServletRequest request, @RequestP
return new ResponseEntity<>("The patient has no active visits.", new HttpHeaders(), HttpStatus.NOT_FOUND);
}

activeVisits.sort(Comparator.comparing(Visit::getStartDatetime).reversed());
Visit latestVisit = activeVisits.get(0);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String latestVisitDate = dateFormat.format(latestVisit.getStartDatetime());

ArrayNode formList = JsonNodeFactory.instance.arrayNode();
ObjectNode allFormsObj = JsonNodeFactory.instance.objectNode();

// Add patient details to the response
allFormsObj.put("patientName", patient.getGivenName() + " " + patient.getFamilyName());
allFormsObj.put("patientUuid", patient.getUuid());
allFormsObj.put("patientAge", ageDisplay);
allFormsObj.put("latestVisitDate", latestVisitDate);

// Fetch all published forms
List<Form> availableForms = Context.getFormService().getPublishedForms();
Expand Down