Skip to content

Commit

Permalink
Ensure there are no duplicates in the returned patient results from o…
Browse files Browse the repository at this point in the history
…pen client registry
  • Loading branch information
mherman22 committed Nov 20, 2024
1 parent 2914aa4 commit c0e2899
Showing 1 changed file with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -205,17 +205,26 @@ private List<PatientSearchResults> searchPatientInClientRegistry(String lastName
// convert fhir object to patient search result
PatientSearchResults transformedPatientSearchResult = SpringContext.getBean(FhirTransformService.class)
.transformToOpenElisPatientSearchResults(externalPatient);
// in case the patient object has no NationalId ,
// we can construct a dynamic National ID like "NID-{gender}-{dob}-{initials}"

// Check for null NationalId and generate if needed
if (transformedPatientSearchResult.getNationalId() == null
|| transformedPatientSearchResult.getNationalId().isEmpty()) {
String nationalId = generateDynamicID(transformedPatientSearchResult);
log.info("dynamic national id: {}", nationalId);
transformedPatientSearchResult.setNationalId(nationalId);
}

transformedPatientSearchResult.setDataSourceName(MessageUtil.getMessage("patient.cr.source"));
finalResults.add(transformedPatientSearchResult);
// Skip this patient if it's already in the local database
if (!isPatientDuplicate(transformedPatientSearchResult)) {
transformedPatientSearchResult.setDataSourceName(MessageUtil.getMessage("patient.cr.source"));
finalResults.add(transformedPatientSearchResult);
} else {
LogEvent.logInfo("PatientSearchRestController", "searchPatientInClientRegistry",
String.format("Skipped duplicate patient with NationalId: %s, Name: %s %s",
transformedPatientSearchResult.getNationalId(),
transformedPatientSearchResult.getFirstName(),
transformedPatientSearchResult.getLastName()));
}
}

return finalResults;
Expand Down Expand Up @@ -333,4 +342,29 @@ public IQuery<IBaseBundle> buildPatientSearchQuery(IGenericClient clientRegistry

return query;
}

private boolean isPatientDuplicate(PatientSearchResults externalPatient) {
List<PatientSearchResults> localResults = searchResultsService.getSearchResults(externalPatient.getLastName(),
externalPatient.getFirstName(), null, null, externalPatient.getNationalId(), null, null, null, null,
null);
for (PatientSearchResults localPatient : localResults) {
if (isMatchingPatient(localPatient, externalPatient)) {
return true;
}
}
return false;
}

private boolean isMatchingPatient(PatientSearchResults local, PatientSearchResults external) {
boolean nationalIdMatch = (local.getNationalId() != null && external.getNationalId() != null)
&& local.getNationalId().equalsIgnoreCase(external.getNationalId());

boolean firstNameMatch = (local.getFirstName() != null && external.getFirstName() != null)
&& local.getFirstName().equalsIgnoreCase(external.getFirstName());

boolean lastNameMatch = (local.getLastName() != null && external.getLastName() != null)
&& local.getLastName().equalsIgnoreCase(external.getLastName());

return nationalIdMatch && firstNameMatch && lastNameMatch;
}
}

0 comments on commit c0e2899

Please sign in to comment.