From e57f9bb080aed497924f3f0bc3d7c96d268a1d0a Mon Sep 17 00:00:00 2001 From: Njuguna Ndung'u Date: Tue, 5 Nov 2024 13:14:37 +0300 Subject: [PATCH] feat: check if OpenMRS patient exists before creating a new one --- mediator/src/utils/openmrs_sync.ts | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/mediator/src/utils/openmrs_sync.ts b/mediator/src/utils/openmrs_sync.ts index af8efd6e..bb66bba0 100644 --- a/mediator/src/utils/openmrs_sync.ts +++ b/mediator/src/utils/openmrs_sync.ts @@ -1,14 +1,14 @@ import { - getFhirResourcesSince, - updateFhirResource, - createFhirResource, - getIdType, addId, + addSourceMeta, copyIdToNamedIdentifier, - getFhirResource, - replaceReference, + createFhirResource, getFHIRPatientResource, - addSourceMeta + getFhirResourceByIdentifier, + getFhirResourcesSince, + getIdType, + replaceReference, + updateFhirResource } from './fhir' import { SYNC_INTERVAL } from '../../config' import { getOpenMRSResourcesSince, createOpenMRSResource } from './openmrs' @@ -126,6 +126,12 @@ export async function compare( And forward to CHT if successful */ async function sendPatientToFhir(patient: fhir4.Patient) { + // check if patient already exists + const openMRSPatient = await getFhirResourceByIdentifier(getIdType(patient, openMRSIdentifierType), 'Patient'); + if (openMRSPatient.data?.total > 0) { + logger.error(`Patient with the same patient_id already exists`); + return { status: 200, data: { message: `Patient with the same ${openMRSIdentifierType} already exists`} }; + } logger.info(`Sending Patient ${patient.id} to FHIR`); copyIdToNamedIdentifier(patient, patient, openMRSIdentifierType); addSourceMeta(patient, openMRSSource);