From c0e28999c005026d1fea000d64c18edc196fa6ac Mon Sep 17 00:00:00 2001 From: mherman22 Date: Wed, 20 Nov 2024 11:05:03 +0300 Subject: [PATCH] Ensure there are no duplicates in the returned patient results from open client registry --- .../provider/PatientSearchRestController.java | 42 +++++++++++++++++-- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/openelisglobal/common/rest/provider/PatientSearchRestController.java b/src/main/java/org/openelisglobal/common/rest/provider/PatientSearchRestController.java index 52d02541dc..cdec05185e 100644 --- a/src/main/java/org/openelisglobal/common/rest/provider/PatientSearchRestController.java +++ b/src/main/java/org/openelisglobal/common/rest/provider/PatientSearchRestController.java @@ -205,8 +205,8 @@ private List 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); @@ -214,8 +214,17 @@ private List searchPatientInClientRegistry(String lastName 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; @@ -333,4 +342,29 @@ public IQuery buildPatientSearchQuery(IGenericClient clientRegistry return query; } + + private boolean isPatientDuplicate(PatientSearchResults externalPatient) { + List 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; + } }