Skip to content

Commit a13b589

Browse files
committed
chore: tidy unused mocks
1 parent 8d68476 commit a13b589

2 files changed

Lines changed: 42 additions & 12 deletions

File tree

packages/updatePrescriptionStatus/src/validation/content.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ export type ValidationOutcome = {
1313
issues: string | undefined;
1414
};
1515

16-
export const ONE_DAY_IN_MS = 86400000
16+
export const ONE_HOUR_IN_MS = 60 * 60 * 1000
17+
export const ONE_DAY_IN_MS = 24 * ONE_HOUR_IN_MS
1718
export const LINE_ITEM_ID_CODESYSTEM =
1819
"https://fhir.nhs.uk/Id/prescription-order-item-number"
1920
export const NHS_NUMBER_CODESYSTEM = "https://fhir.nhs.uk/Id/nhs-number"
@@ -64,16 +65,24 @@ export function entryContent(entry: BundleEntry): Array<string> {
6465

6566
export function lastModified(task: Task): string | undefined {
6667
const lastModified = new Date(task.lastModified!)
67-
return isPastDate(lastModified, "lastModified")
68+
const hasMetaLastUpdated = Boolean(task.meta?.lastUpdated)
69+
// 24 hours if meta.lastUpdated is not provided, otherwise 999 hours
70+
const allowedHours = hasMetaLastUpdated ? 999 : 24
71+
return isWithinHours(lastModified, allowedHours, "lastModified")
6872
}
6973

70-
function isPastDate(date: Date, fieldName: string): string | undefined {
74+
function isWithinHours(
75+
date: Date,
76+
hours: number,
77+
fieldName: string
78+
): string | undefined {
7179
if (isNaN(date.getTime())) {
7280
return `Date format provided for ${fieldName} is invalid.`
7381
}
7482

75-
const today = new Date()
76-
if (date.valueOf() - today.valueOf() > ONE_DAY_IN_MS) {
83+
const now = new Date()
84+
const limitMs = hours * ONE_HOUR_IN_MS
85+
if (date.valueOf() - now.valueOf()> limitMs) {
7786
return `Invalid ${fieldName} value provided.`
7887
}
7988
}
@@ -84,7 +93,7 @@ export function metaLastUpdated(task: Task): string | undefined {
8493
}
8594

8695
const parsed = new Date(task.meta.lastUpdated)
87-
return isPastDate(parsed, "meta.lastUpdated")
96+
return isWithinHours(parsed, 24, "meta.lastUpdated")
8897
}
8998

9099
export function prescriptionID(task: Task): string | undefined {

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

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ import {
3535
} from "../utils/testUtils"
3636

3737
describe("Unit test for overall task validation", () => {
38+
beforeEach(() => {
39+
jest.useFakeTimers().setSystemTime(DEFAULT_DATE)
40+
})
3841
it("When task is valid, should return true with no issues.", async () => {
3942
const expectedOutcome = {valid: true, issues: undefined}
4043
const entry: BundleEntry = {fullUrl: FULL_URL_0, resource: validTask()}
@@ -141,9 +144,6 @@ describe("Unit tests for pre-cast validation of bundle", () => {
141144
})
142145

143146
describe("Unit tests for validation of lastModified", () => {
144-
beforeEach(() => {
145-
jest.useFakeTimers().setSystemTime(DEFAULT_DATE)
146-
})
147147

148148
it("When lastModified is over a day in the future, should return expected issue.", async () => {
149149
const futureDate = new Date(
@@ -175,12 +175,33 @@ describe("Unit tests for validation of lastModified", () => {
175175

176176
expect(actual).toEqual(undefined)
177177
})
178+
179+
it("When meta.lastUpdated present, lastModified <= 999 hours into future should be valid.", async () => {
180+
const withinWindow = new Date(
181+
DEFAULT_DATE.valueOf() - (998 * 60 * 60 * 1000)
182+
)
183+
const task = {lastModified: withinWindow.toISOString(), meta: {lastUpdated: DEFAULT_DATE.toISOString()}}
184+
185+
const actual = lastModified(task as Task)
186+
187+
expect(actual).toEqual(undefined)
188+
})
189+
190+
it("When meta.lastUpdated present, lastModified > 999 hours into future should return expected issue.", async () => {
191+
const futureDate = new Date(
192+
DEFAULT_DATE.valueOf() + (999 * 60 * 60 * 1000 + 1000)
193+
)
194+
const task = {lastModified: futureDate.toISOString(), meta: {lastUpdated: DEFAULT_DATE.toISOString()}}
195+
196+
const expected = "Invalid lastModified value provided."
197+
198+
const actual = lastModified(task as Task)
199+
200+
expect(actual).toEqual(expected)
201+
})
178202
})
179203

180204
describe("Unit tests for validation of metaLastUpdated", () => {
181-
beforeEach(() => {
182-
jest.useFakeTimers().setSystemTime(DEFAULT_DATE)
183-
})
184205

185206
it("When meta.lastUpdated is over a day in the future, should return expected issue.", async () => {
186207
const futureDate = new Date(

0 commit comments

Comments
 (0)