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

Ensure there are no duplicates in the returned patient results from open client registry #1319

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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 @@ -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;
}
}
Loading