|
| 1 | +import { |
| 2 | + expect, |
| 3 | + describe, |
| 4 | + it, |
| 5 | + afterEach, |
| 6 | + jest |
| 7 | +} from "@jest/globals" |
| 8 | + |
| 9 | +import {Logger} from "@aws-lambda-powertools/logger" |
| 10 | + |
| 11 | +import {PSUDataItem} from "@psu-common/commonTypes" |
| 12 | + |
| 13 | +import {PostDatedProcessingResult, PostDatedSQSMessageWithExistingRecords} from "../src/types" |
| 14 | +import {createMockPostModifiedDataItem} from "./testUtils" |
| 15 | + |
| 16 | +type BusinessLogicModule = typeof import("../src/businessLogic") |
| 17 | + |
| 18 | +const ORIGINAL_ENV = {...process.env} |
| 19 | + |
| 20 | +async function loadBusinessLogic( |
| 21 | + envOverrides = {} |
| 22 | +): Promise<BusinessLogicModule> { |
| 23 | + // Makes sure that the environment is set before import each time |
| 24 | + jest.resetModules() |
| 25 | + process.env = {...ORIGINAL_ENV, ...envOverrides} |
| 26 | + return import("../src/businessLogic") |
| 27 | +} |
| 28 | + |
| 29 | +function createPSURecord(overrides: Partial<PSUDataItem> = {}): PSUDataItem { |
| 30 | + const baseRecord: PSUDataItem = { |
| 31 | + LastModified: new Date("2026-01-01T00:00:00.000Z").toISOString(), |
| 32 | + LineItemID: "line-item", |
| 33 | + PatientNHSNumber: "0123456789", |
| 34 | + PharmacyODSCode: "ABC123", |
| 35 | + PrescriptionID: "RX123", |
| 36 | + RepeatNo: 0, |
| 37 | + RequestID: "req-123", |
| 38 | + Status: "ready to collect", |
| 39 | + TaskID: "task-123", |
| 40 | + TerminalStatus: "terminal", |
| 41 | + ApplicationName: "post-dated-tests", |
| 42 | + ExpiryTime: 0, |
| 43 | + PostDatedLastModifiedSetAt: "2026-01-01T00:00:00.000Z" |
| 44 | + } |
| 45 | + |
| 46 | + return { |
| 47 | + ...baseRecord, |
| 48 | + ...overrides |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +function createMessage( |
| 53 | + overrides: Partial<PostDatedSQSMessageWithExistingRecords> = {} |
| 54 | +): PostDatedSQSMessageWithExistingRecords { |
| 55 | + const prescData = createMockPostModifiedDataItem({}) |
| 56 | + const baseMessage: PostDatedSQSMessageWithExistingRecords = { |
| 57 | + MessageId: "msg-123", |
| 58 | + ReceiptHandle: "receipt-123", |
| 59 | + Body: JSON.stringify(prescData), |
| 60 | + Attributes: { |
| 61 | + |
| 62 | + }, |
| 63 | + prescriptionData: prescData, |
| 64 | + // In theory, this should contain the record corresponding to prescData, but for testing purposes it's fine |
| 65 | + existingRecords: [createPSURecord()] |
| 66 | + } |
| 67 | + |
| 68 | + return { |
| 69 | + ...baseMessage, |
| 70 | + ...overrides, |
| 71 | + prescriptionData: overrides.prescriptionData ?? baseMessage.prescriptionData, |
| 72 | + existingRecords: overrides.existingRecords ?? baseMessage.existingRecords |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +afterEach(() => { |
| 77 | + process.env = {...ORIGINAL_ENV} |
| 78 | + jest.useRealTimers() |
| 79 | +}) |
| 80 | + |
| 81 | +describe("businessLogic", () => { |
| 82 | + describe("getMostRecentRecord", () => { |
| 83 | + it("should return the record with the latest timestamp, when all records are post-dated", async () => { |
| 84 | + const {getMostRecentRecord} = await loadBusinessLogic() |
| 85 | + const records = [ |
| 86 | + createPSURecord({ |
| 87 | + LineItemID: "line-old", |
| 88 | + PostDatedLastModifiedSetAt: "2026-01-01T00:00:00.000Z", |
| 89 | + LastModified: "2026-01-02T00:00:00.000Z" // The time it's scheduled to mature |
| 90 | + }), |
| 91 | + createPSURecord({ |
| 92 | + LineItemID: "line-new", |
| 93 | + PostDatedLastModifiedSetAt: "2026-01-01T12:00:00.000Z", // submitted 12 hours later |
| 94 | + LastModified: "2026-01-01T18:00:00.000Z" // but last modified is earlier |
| 95 | + }) |
| 96 | + ] |
| 97 | + |
| 98 | + const result = getMostRecentRecord(records) |
| 99 | + |
| 100 | + expect(result.LineItemID).toBe("line-new") |
| 101 | + }) |
| 102 | + |
| 103 | + it("Should return the latest record when only one record is post-dated", async () => { |
| 104 | + const {getMostRecentRecord} = await loadBusinessLogic() |
| 105 | + const records = [ |
| 106 | + createPSURecord({ // post-dated record submitted first |
| 107 | + LineItemID: "line-old", |
| 108 | + PostDatedLastModifiedSetAt: "2026-01-01T00:00:00.000Z", |
| 109 | + LastModified: "2026-01-15T00:00:00.000Z" // The time it's scheduled to mature |
| 110 | + }), |
| 111 | + createPSURecord({ // contemporary record submitted second |
| 112 | + LineItemID: "line-new", |
| 113 | + PostDatedLastModifiedSetAt: undefined, |
| 114 | + LastModified: "2026-01-02T00:00:00.000Z" // The time the prescription was actually ready to collect |
| 115 | + }) |
| 116 | + ] |
| 117 | + |
| 118 | + const result = getMostRecentRecord(records) |
| 119 | + |
| 120 | + expect(result.LineItemID).toBe("line-new") |
| 121 | + }) |
| 122 | + |
| 123 | + it("should return the latest record when no records are post-dated", async () => { |
| 124 | + const {getMostRecentRecord} = await loadBusinessLogic() |
| 125 | + const records = [ |
| 126 | + createPSURecord({ |
| 127 | + LineItemID: "line-old", |
| 128 | + PostDatedLastModifiedSetAt: undefined, |
| 129 | + LastModified: "2026-01-01T00:00:00.000Z" |
| 130 | + }), |
| 131 | + createPSURecord({ |
| 132 | + LineItemID: "line-new", |
| 133 | + PostDatedLastModifiedSetAt: undefined, |
| 134 | + LastModified: "2026-02-01T00:00:00.000Z" |
| 135 | + }) |
| 136 | + ] |
| 137 | + |
| 138 | + const result = getMostRecentRecord(records) |
| 139 | + |
| 140 | + expect(result.LineItemID).toBe("line-new") |
| 141 | + }) |
| 142 | + }) |
| 143 | + |
| 144 | + describe("processMessage", () => { |
| 145 | + it("should return the override value when override mode is enabled", async () => { |
| 146 | + const {processMessage} = await loadBusinessLogic({ |
| 147 | + POST_DATED_OVERRIDE: "true", |
| 148 | + POST_DATED_OVERRIDE_VALUE: "immature" |
| 149 | + }) |
| 150 | + const logger = new Logger({serviceName: "post-dated-tests"}) |
| 151 | + |
| 152 | + const result = processMessage(logger, createMessage()) |
| 153 | + |
| 154 | + expect(result).toBe(PostDatedProcessingResult.IMMATURE) |
| 155 | + }) |
| 156 | + |
| 157 | + it("should ignore messages that have no existing records", async () => { |
| 158 | + const {processMessage} = await loadBusinessLogic() |
| 159 | + const logger = new Logger({serviceName: "post-dated-tests"}) |
| 160 | + const message = createMessage({existingRecords: []}) |
| 161 | + |
| 162 | + const result = processMessage(logger, message) |
| 163 | + |
| 164 | + expect(result).toBe(PostDatedProcessingResult.IGNORE) |
| 165 | + }) |
| 166 | + |
| 167 | + it("should ignore messages when the most recent record is not post-dated", async () => { |
| 168 | + const {processMessage} = await loadBusinessLogic() |
| 169 | + const logger = new Logger({serviceName: "post-dated-tests"}) |
| 170 | + const message = createMessage({ |
| 171 | + existingRecords: [ |
| 172 | + createPSURecord({ |
| 173 | + LineItemID: "line-no-post-date", |
| 174 | + PostDatedLastModifiedSetAt: undefined |
| 175 | + }) |
| 176 | + ] |
| 177 | + }) |
| 178 | + |
| 179 | + const result = processMessage(logger, message) |
| 180 | + |
| 181 | + expect(result).toBe(PostDatedProcessingResult.IGNORE) |
| 182 | + }) |
| 183 | + |
| 184 | + it("should ignore messages when the status is not notifiable", async () => { |
| 185 | + const {processMessage} = await loadBusinessLogic() |
| 186 | + const logger = new Logger({serviceName: "post-dated-tests"}) |
| 187 | + const message = createMessage({ |
| 188 | + existingRecords: [ |
| 189 | + createPSURecord({ |
| 190 | + Status: "dispensed", |
| 191 | + LineItemID: "line-not-notifiable" |
| 192 | + }) |
| 193 | + ] |
| 194 | + }) |
| 195 | + |
| 196 | + const result = processMessage(logger, message) |
| 197 | + |
| 198 | + expect(result).toBe(PostDatedProcessingResult.IGNORE) |
| 199 | + }) |
| 200 | + |
| 201 | + it("should classify a message as immature when LastModified is in the future", async () => { |
| 202 | + jest.useFakeTimers() |
| 203 | + jest.setSystemTime(new Date("2026-01-01T12:00:00.000Z")) |
| 204 | + const {processMessage} = await loadBusinessLogic() |
| 205 | + const logger = new Logger({serviceName: "post-dated-tests"}) |
| 206 | + const message = createMessage({ |
| 207 | + existingRecords: [ |
| 208 | + createPSURecord({ |
| 209 | + LastModified: "2026-01-02T12:00:00.000Z", |
| 210 | + PostDatedLastModifiedSetAt: "2026-01-02T12:00:00.000Z", |
| 211 | + LineItemID: "line-future" |
| 212 | + }) |
| 213 | + ] |
| 214 | + }) |
| 215 | + |
| 216 | + const result = processMessage(logger, message) |
| 217 | + |
| 218 | + expect(result).toBe(PostDatedProcessingResult.IMMATURE) |
| 219 | + }) |
| 220 | + |
| 221 | + it("should classify a message as matured when LastModified is in the past", async () => { |
| 222 | + jest.useFakeTimers() |
| 223 | + jest.setSystemTime(new Date("2026-01-03T12:00:00.000Z")) |
| 224 | + const {processMessage} = await loadBusinessLogic() |
| 225 | + const logger = new Logger({serviceName: "post-dated-tests"}) |
| 226 | + const message = createMessage({ |
| 227 | + existingRecords: [ |
| 228 | + createPSURecord({ |
| 229 | + LastModified: "2026-01-02T12:00:00.000Z", |
| 230 | + PostDatedLastModifiedSetAt: "2026-01-02T12:00:00.000Z", |
| 231 | + LineItemID: "line-past" |
| 232 | + }) |
| 233 | + ] |
| 234 | + }) |
| 235 | + |
| 236 | + const result = processMessage(logger, message) |
| 237 | + |
| 238 | + expect(result).toBe(PostDatedProcessingResult.MATURED) |
| 239 | + }) |
| 240 | + |
| 241 | + it("should use the most recent record when determining maturity", async () => { |
| 242 | + jest.useFakeTimers() |
| 243 | + jest.setSystemTime(new Date("2026-01-05T12:00:00.000Z")) |
| 244 | + const {processMessage} = await loadBusinessLogic() |
| 245 | + const logger = new Logger({serviceName: "post-dated-tests"}) |
| 246 | + const message = createMessage({ |
| 247 | + existingRecords: [ |
| 248 | + createPSURecord({ |
| 249 | + LineItemID: "line-old", |
| 250 | + LastModified: "2026-01-01T12:00:00.000Z", |
| 251 | + PostDatedLastModifiedSetAt: "2026-01-01T12:00:00.000Z", |
| 252 | + Status: "ready to collect - partial" |
| 253 | + }), |
| 254 | + createPSURecord({ |
| 255 | + LineItemID: "line-new", |
| 256 | + LastModified: "2026-01-06T12:00:00.000Z", |
| 257 | + PostDatedLastModifiedSetAt: "2026-01-06T12:00:00.000Z", |
| 258 | + Status: "ready to collect" |
| 259 | + }) |
| 260 | + ] |
| 261 | + }) |
| 262 | + |
| 263 | + const result = processMessage(logger, message) |
| 264 | + |
| 265 | + expect(result).toBe(PostDatedProcessingResult.IMMATURE) |
| 266 | + }) |
| 267 | + }) |
| 268 | +}) |
0 commit comments