Skip to content

Commit d791351

Browse files
committed
fix: validate meta.lastUpdated
1 parent 95b9ff2 commit d791351

File tree

5 files changed

+75
-14
lines changed

5 files changed

+75
-14
lines changed

packages/updatePrescriptionStatus/src/updatePrescriptionStatus.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,11 @@ export function buildDataItems(
381381
TaskID: task.id!,
382382
TerminalStatus: task.status,
383383
ApplicationName: applicationName,
384-
ExpiryTime: (Math.floor(+new Date() / 1000) + TTL_DELTA),
385-
OptumPostDatedLastModifiedSetAt: task.meta?.lastUpdated
384+
ExpiryTime: (Math.floor(+new Date() / 1000) + TTL_DELTA)
385+
}
386+
387+
if (task.meta?.lastUpdated) {
388+
(dataItem as any).OptumPostDatedLastModifiedSetAt = task.meta.lastUpdated
386389
}
387390

388391
dataItems.push(dataItem)

packages/updatePrescriptionStatus/src/validation/content.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,30 @@ export function entryContent(entry: BundleEntry): Array<string> {
6363
}
6464

6565
export function lastModified(task: Task): string | undefined {
66-
const today = new Date()
6766
const lastModified = new Date(task.lastModified!)
67+
return isPastDate(lastModified, "lastModified")
68+
}
6869

69-
if (isNaN(lastModified.getTime())) {
70-
return "Date format provided for lastModified is invalid."
70+
function isPastDate(date: Date, fieldName: string): string | undefined {
71+
if (isNaN(date.getTime())) {
72+
return `Date format provided for ${fieldName} is invalid.`
7173
}
7274

73-
if (lastModified.valueOf() - today.valueOf() > ONE_DAY_IN_MS) {
74-
return "Invalid last modified value provided."
75+
const today = new Date()
76+
if (date.valueOf() - today.valueOf() > ONE_DAY_IN_MS) {
77+
return `Invalid ${fieldName} value provided.`
7578
}
7679
}
7780

81+
export function metaLastUpdated(task: Task): string | undefined {
82+
if (!task.meta?.lastUpdated) {
83+
return undefined
84+
}
85+
86+
const parsed = new Date(task.meta.lastUpdated)
87+
return isPastDate(parsed, "meta.lastUpdated")
88+
}
89+
7890
export function prescriptionID(task: Task): string | undefined {
7991
const message = "Prescription ID is invalid."
8092
const prescriptionID = task.basedOn?.[0].identifier?.value
@@ -183,6 +195,7 @@ export function taskContent(task: Task): Array<string> {
183195
status,
184196
businessStatus,
185197
lastModified,
198+
metaLastUpdated,
186199
nhsNumber,
187200
prescriptionID,
188201
resourceType,

packages/updatePrescriptionStatus/tests/testUpdatePrescriptionStatus.test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ describe("buildDataItems", () => {
290290

291291
it("should include OptumPostDatedLastModifiedSetAt in data item when meta.lastUpdated is defined", () => {
292292
const task = validTask()
293-
const lastUpdated = "2024-01-15T10:30:00.000Z"
293+
const lastUpdated = new Date(DEFAULT_DATE.valueOf() - (24 * 60 * 60 * 1000)).toISOString()
294294
task.meta = {
295295
lastUpdated: lastUpdated
296296
}
@@ -301,8 +301,7 @@ describe("buildDataItems", () => {
301301
}
302302

303303
const dataItems = buildDataItems([requestEntry], "", "")
304-
console.log(dataItems[0])
305-
306-
expect(dataItems[0].OptumPostDatedLastModifiedSetAt).toEqual(lastUpdated)
304+
const first: any = dataItems[0]
305+
expect(first.OptumPostDatedLastModifiedSetAt).toEqual(lastUpdated)
307306
})
308307
})

packages/updatePrescriptionStatus/tests/validation/testRequestContentValidation.test.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
businessStatus,
1212
codeSystems,
1313
lastModified,
14+
metaLastUpdated,
1415
nhsNumber,
1516
ONE_DAY_IN_MS,
1617
prescriptionID,
@@ -150,7 +151,7 @@ describe("Unit tests for validation of lastModified", () => {
150151
)
151152
const task = {lastModified: futureDate.toISOString()}
152153

153-
const expected = "Invalid last modified value provided."
154+
const expected = "Invalid lastModified value provided."
154155

155156
const actual = lastModified(task as Task)
156157

@@ -176,6 +177,51 @@ describe("Unit tests for validation of lastModified", () => {
176177
})
177178
})
178179

180+
describe("Unit tests for validation of metaLastUpdated", () => {
181+
beforeEach(() => {
182+
jest.useFakeTimers().setSystemTime(DEFAULT_DATE)
183+
})
184+
185+
it("When meta.lastUpdated is over a day in the future, should return expected issue.", async () => {
186+
const futureDate = new Date(
187+
DEFAULT_DATE.valueOf() + (ONE_DAY_IN_MS + 1000)
188+
)
189+
const task = {meta: {lastUpdated: futureDate.toISOString()}}
190+
191+
const expected = "Invalid meta.lastUpdated value provided."
192+
193+
const actual = metaLastUpdated(task as Task)
194+
195+
expect(actual).toEqual(expected)
196+
})
197+
198+
it("When meta.lastUpdated date format is invalid, should return expected issue.", async () => {
199+
const task = {meta: {lastUpdated: "invalid date"}}
200+
201+
const expected = "Date format provided for meta.lastUpdated is invalid."
202+
203+
const actual = metaLastUpdated(task as Task)
204+
205+
expect(actual).toEqual(expected)
206+
})
207+
208+
it("When meta.lastUpdated is valid and not in the future, should return undefined.", async () => {
209+
const task = {meta: {lastUpdated: DEFAULT_DATE.toISOString()}}
210+
211+
const actual = metaLastUpdated(task as Task)
212+
213+
expect(actual).toEqual(undefined)
214+
})
215+
216+
it("When meta.lastUpdated is not present, should return undefined.", async () => {
217+
const task = {}
218+
219+
const actual = metaLastUpdated(task as Task)
220+
221+
expect(actual).toEqual(undefined)
222+
})
223+
})
224+
179225
describe("Unit tests for validation of prescription ID", () => {
180226
it.each([
181227
{

packages/updatePrescriptionStatus/tests/validation/testRequestValidationViaHandler.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe("Integration tests for validation via updatePrescriptionStatus handler"
5555
const event: APIGatewayProxyEvent = generateMockEvent(body)
5656

5757
const expected = bundleWrap([
58-
badRequest("Invalid last modified value provided.", FULL_URL_0),
58+
badRequest("Invalid lastModified value provided.", FULL_URL_0),
5959
accepted(FULL_URL_1)
6060
])
6161

@@ -77,7 +77,7 @@ describe("Integration tests for validation via updatePrescriptionStatus handler"
7777
const event: APIGatewayProxyEvent = generateMockEvent(body)
7878

7979
const expected = bundleWrap([
80-
badRequest("Invalid last modified value provided.", FULL_URL_0),
80+
badRequest("Invalid lastModified value provided.", FULL_URL_0),
8181
badRequest("Missing required field(s) - FullUrl, PrescriptionID.")
8282
])
8383

0 commit comments

Comments
 (0)