diff --git a/mediator/src/utils/openmrs_sync.ts b/mediator/src/utils/openmrs_sync.ts index 38283285..2b78aad3 100644 --- a/mediator/src/utils/openmrs_sync.ts +++ b/mediator/src/utils/openmrs_sync.ts @@ -84,13 +84,16 @@ export async function compare( const fhirIds = new Map(comparison.fhirResources.map(resource => [getKey(resource), resource])); function isValidDate(resource: fhir4.Resource) { - // if lastUpdated is not valid, cannot proceed, throw an error - const lastUpdated = new Date(resource.meta?.lastUpdated!); + // if lastUpdated is missing or invalid, cannot proceed, throw an error + if (!resource.meta?.lastUpdated) { + throw new Error("Last updated missing"); + } + const lastUpdated = new Date(resource.meta.lastUpdated); if (isNaN(lastUpdated.getTime()) || isNaN(startTime.getTime())) { throw new Error("Invalid date format"); } - // dont sync resources created with 2 * SYNC_INTERVAL of start time + // don't sync resources created with 2 * SYNC_INTERVAL of start time const syncWindow = (Number(SYNC_INTERVAL) * 1000) * 2 const diff = lastUpdated.getTime() - startTime.getTime(); return diff > syncWindow; @@ -99,13 +102,10 @@ export async function compare( comparison.openMRSResources.forEach((openMRSResource) => { const key = getKey(openMRSResource); if (fhirIds.has(key)) { - // ok so the fhir server already has it results.toupdate.push(openMRSResource); fhirIds.delete(key); - } else { - if (isValidDate(openMRSResource)){ - results.incoming.push(openMRSResource); - } + } else if (isValidDate(openMRSResource)){ + results.incoming.push(openMRSResource); } });