diff --git a/SAMtemplates/functions/main.yaml b/SAMtemplates/functions/main.yaml index 30f4cdd17..1e2094203 100644 --- a/SAMtemplates/functions/main.yaml +++ b/SAMtemplates/functions/main.yaml @@ -115,7 +115,7 @@ Resources: Environment: Variables: LOG_LEVEL: !Ref LogLevel - GET_STATUS_UPDATES: !Ref ToggleGetStatusUpdates + EXPECT_STATUS_UPDATES: !Ref ToggleGetStatusUpdates Metadata: BuildMethod: esbuild BuildProperties: diff --git a/packages/enrichPrescriptions/src/statusUpdates.ts b/packages/enrichPrescriptions/src/statusUpdates.ts index 5c6df683c..6c73c14e6 100644 --- a/packages/enrichPrescriptions/src/statusUpdates.ts +++ b/packages/enrichPrescriptions/src/statusUpdates.ts @@ -18,7 +18,7 @@ export const TEMPORARILY_UNAVAILABLE_STATUS = "Tracking Temporarily Unavailable" export const APPROVED_STATUS = "Prescriber Approved" export const CANCELLED_STATUS = "Prescriber Cancelled" -export const expectStatusUpdates = () => process.env.GET_STATUS_UPDATES === "true" +export const expectStatusUpdates = () => process.env.EXPECT_STATUS_UPDATES === "true" type MedicationRequestStatus = "completed" | "active" diff --git a/packages/enrichPrescriptions/tests/testHandler.test.ts b/packages/enrichPrescriptions/tests/testHandler.test.ts index e1d801770..4fa589582 100644 --- a/packages/enrichPrescriptions/tests/testHandler.test.ts +++ b/packages/enrichPrescriptions/tests/testHandler.test.ts @@ -20,7 +20,7 @@ import {lambdaHandler} from "../src/enrichPrescriptions" describe("Unit tests for handler", function () { beforeEach(() => { jest.useFakeTimers().setSystemTime(SYSTEM_DATETIME) - process.env.GET_STATUS_UPDATES = "true" + process.env.EXPECT_STATUS_UPDATES = "true" }) it("when event contains a bundle with one prescription, one MedicationRequest and status updates, updates are applied", async () => { @@ -53,17 +53,26 @@ describe("Unit tests for handler", function () { expect(actualResponse).toEqual(expectedResponse) }) - it("when no status update data (call to GetStatusUpdates toggled-off), no updates are applied", async () => { + it("when no status update data (GetStatusUpdates toggled-off), no updates are applied", async () => { + process.env.EXPECT_STATUS_UPDATES = "false" const {event, expectedResponse} = noUpdateDataEventAndResponse() const actualResponse = await lambdaHandler(event) expect(actualResponse).toEqual(expectedResponse) }) - it("when status update is unsuccessful (call to GetStatusUpdates fails), temporary updates are applied", async () => { + it("when status updates are expected but unsuccessful (GetStatusUpdates fails at code level), temporary updates are applied", async () => { const {event, expectedResponse} = getStatusUpdatesFailedEventAndResponse() const actualResponse = await lambdaHandler(event) expect(actualResponse).toEqual(expectedResponse) }) + + it("when status updates are expected but not present (GetStatusUpdates fails at state machine level), temporary updates are applied", async () => { + const {event, expectedResponse} = getStatusUpdatesFailedEventAndResponse() + delete event.StatusUpdates + const actualResponse = await lambdaHandler(event) + + expect(actualResponse).toEqual(expectedResponse) + }) }) diff --git a/packages/enrichPrescriptions/tests/testStatusUpdate.test.ts b/packages/enrichPrescriptions/tests/testStatusUpdate.test.ts index 5d7b874ea..9a4e150a9 100644 --- a/packages/enrichPrescriptions/tests/testStatusUpdate.test.ts +++ b/packages/enrichPrescriptions/tests/testStatusUpdate.test.ts @@ -28,10 +28,12 @@ import { ONE_WEEK_IN_MS, StatusUpdates, TEMPORARILY_UNAVAILABLE_STATUS, + UpdatesScenario, applyStatusUpdates, applyTemporaryStatusUpdates, delayWithPharmacyStatus, - getStatusDate + getStatusDate, + getUpdatesScenario } from "../src/statusUpdates" import {Bundle, MedicationRequest} from "fhir/r4" import {Logger} from "@aws-lambda-powertools/logger" @@ -482,4 +484,17 @@ describe("Unit tests for statusUpdate", function () { expect(allMedicationRequests.length).toEqual(6) expect(allMedicationRequestsWithTemporaryUpdates.length).toEqual(3) }) + + it.each([ + {expectUpdates: true, updatesPresent: true, expected: UpdatesScenario.Present}, + {expectUpdates: true, updatesPresent: false, expected: UpdatesScenario.ExpectedButAbsent}, + {expectUpdates: false, updatesPresent: false, expected: UpdatesScenario.NotExpected} + ])("getUpdatesScenario returns as expected", async ({expectUpdates, updatesPresent, expected}) => { + process.env.EXPECT_STATUS_UPDATES = expectUpdates ? "true" : "false" + const statusUpdates = updatesPresent ? {isSuccess: true, prescriptions: [], schemaVersion: 1} : undefined + + const scenario = getUpdatesScenario(statusUpdates) + + expect(scenario).toEqual(expected) + }) })