Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions packages/updatePrescriptionStatus/src/validation/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export type ValidationOutcome = {
issues: string | undefined;
};

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

export function lastModified(task: Task): string | undefined {
const lastModified = new Date(task.lastModified!)
return isPastDate(lastModified, "lastModified")
const hasMetaLastUpdated = Boolean(task.meta?.lastUpdated)
// 24 hours if meta.lastUpdated is not provided, otherwise 999 hours
const allowedHours = hasMetaLastUpdated ? 999 : 24
return isWithinHours(lastModified, allowedHours, "lastModified")
}

function isPastDate(date: Date, fieldName: string): string | undefined {
function isWithinHours(
date: Date,
hours: number,
fieldName: string
): string | undefined {
if (isNaN(date.getTime())) {
return `Date format provided for ${fieldName} is invalid.`
}

const today = new Date()
if (date.valueOf() - today.valueOf() > ONE_DAY_IN_MS) {
const now = new Date()
const limitMs = hours * ONE_HOUR_IN_MS
if (date.valueOf() - now.valueOf()> limitMs) {
return `Invalid ${fieldName} value provided.`
}
}
Expand All @@ -84,7 +93,7 @@ export function metaLastUpdated(task: Task): string | undefined {
}

const parsed = new Date(task.meta.lastUpdated)
return isPastDate(parsed, "meta.lastUpdated")
return isWithinHours(parsed, 24, "meta.lastUpdated")
}

export function prescriptionID(task: Task): string | undefined {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ import {
} from "../utils/testUtils"

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

describe("Unit tests for validation of lastModified", () => {
beforeEach(() => {
jest.useFakeTimers().setSystemTime(DEFAULT_DATE)
})

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

expect(actual).toEqual(undefined)
})

it("When meta.lastUpdated present, lastModified <= 999 hours into future should be valid.", async () => {
const withinWindow = new Date(
DEFAULT_DATE.valueOf() - (998 * 60 * 60 * 1000)
)
const task = {lastModified: withinWindow.toISOString(), meta: {lastUpdated: DEFAULT_DATE.toISOString()}}

const actual = lastModified(task as Task)

expect(actual).toEqual(undefined)
})

it("When meta.lastUpdated present, lastModified > 999 hours into future should return expected issue.", async () => {
const futureDate = new Date(
DEFAULT_DATE.valueOf() + (999 * 60 * 60 * 1000 + 1000)
)
const task = {lastModified: futureDate.toISOString(), meta: {lastUpdated: DEFAULT_DATE.toISOString()}}

const expected = "Invalid lastModified value provided."

const actual = lastModified(task as Task)

expect(actual).toEqual(expected)
})
})

describe("Unit tests for validation of metaLastUpdated", () => {
beforeEach(() => {
jest.useFakeTimers().setSystemTime(DEFAULT_DATE)
})

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