diff --git a/packages/server/src/workers/utils.ts b/packages/server/src/workers/utils.ts index 77011651cc..9fb3b2b53e 100644 --- a/packages/server/src/workers/utils.ts +++ b/packages/server/src/workers/utils.ts @@ -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 { + 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 {