Skip to content

Commit

Permalink
Merge pull request #1319 from mherman22/Issue-1308
Browse files Browse the repository at this point in the history
Ensure there are no duplicates in the returned patient results from open client registry
  • Loading branch information
mozzy11 authored Jan 6, 2025
2 parents 60a34a6 + 78c54cf commit f6d388a
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 f6d388a

Please sign in to comment.