Skip to content

Commit

Permalink
fix(#138): sonar fix
Browse files Browse the repository at this point in the history
  • Loading branch information
witash committed Oct 18, 2024
1 parent 1c7dc2b commit 5cc6525
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions mediator/src/utils/openmrs_sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
});

Expand Down

0 comments on commit 5cc6525

Please sign in to comment.