Skip to content

Commit

Permalink
fix-3809 race condition in Subscription extension fhir-path-criteria-…
Browse files Browse the repository at this point in the history
…expression %previous value lookup (medplum#3810)

* fix-3809 race condition in Subscription extension fhir-path-criteria-expression %previous value lookup

* remove redundant check
  • Loading branch information
dillonstreator committed Jan 29, 2024
1 parent 3d29602 commit 9ec221f
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions packages/server/src/workers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,25 @@ export async function isFhirCriteriaMet(subscription: Subscription, currentResou
if (!criteria?.valueString) {
return true;
}
const history = await systemRepo.readHistory(currentResource.resourceType, currentResource?.id as string);
const previous = await getPreviousResource(currentResource);
const evalInput = {
'%current': toTypedValue(currentResource),
'%previous': toTypedValue({}),
'%previous': toTypedValue(previous ?? {}),
};
const previousResource = history.entry?.[1]?.resource as Resource;
if (previousResource) {
evalInput['%previous'] = toTypedValue(previousResource);
}
const evalValue = evalFhirPathTyped(criteria.valueString, [toTypedValue(currentResource)], evalInput);
if (evalValue?.[0]?.value === true) {
return true;
}
return false;
return evalValue?.[0]?.value === true;
}

async function getPreviousResource(currentResource: Resource): Promise<Resource | undefined> {
const history = await systemRepo.readHistory(currentResource.resourceType, currentResource?.id as string);

return history.entry?.find((_, idx) => {
if (idx === 0) {
return false;
}

return history.entry?.[idx - 1]?.resource?.meta?.versionId === currentResource.meta?.versionId;
})?.resource;
}

export function isJobSuccessful(subscription: Subscription, status: number): boolean {
Expand Down

0 comments on commit 9ec221f

Please sign in to comment.