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
12 changes: 11 additions & 1 deletion packages/gsul/src/getStatusUpdates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,20 @@ export const filterOutFutureReduceToLatestUpdates = (
})

// flatten both regular and post-dated updates into single array
// but exclude post-dated updates if they have been revoked by a subsequent regular update
const uniqueItems: Array<itemType> = []
Object.values(itemGroups).forEach(group => {
if (group.regular) uniqueItems.push(group.regular)
if (group.postDated) uniqueItems.push(group.postDated)
if (group.postDated) {
// Only include post-dated update if there's no regular update that came after it was set
const postDatedSetTime = Date.parse(group.postDated.postDatedLastModifiedSetAt)
const regularUpdateTime = group.regular ? Date.parse(group.regular.lastUpdateDateTime) : 0

// If the regular update came after the post-dated was set, it revokes the post-dated update
if (!group.regular || regularUpdateTime <= postDatedSetTime) {
uniqueItems.push(group.postDated)
}
}
})

const result: outputPrescriptionType = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ type scenariosType = {
inputPrescriptions: inputPrescriptionType
queryResults: Array<itemType>
expectedResult: outputPrescriptionType
currentTime: number
}
const now = new Date()
const futureDateTime = new Date(now.valueOf() + (24 * 60 * 60 * 1000)).toISOString()
const scenarios: Array<scenariosType> = [
{
scenarioDescription: "should return correct data when a matched prescription found",
currentTime: new Date("2000-01-01T00:00:00Z").getTime(),
inputPrescriptions: {
prescriptionID: "abc",
odsCode: "123"
Expand Down Expand Up @@ -40,6 +42,7 @@ const scenarios: Array<scenariosType> = [
},
{
scenarioDescription: "should return no items when empty item status are found",
currentTime: new Date("2000-01-01T00:00:00Z").getTime(),
inputPrescriptions: {
prescriptionID: "abc",
odsCode: "123"
Expand All @@ -53,6 +56,7 @@ const scenarios: Array<scenariosType> = [
},
{
scenarioDescription: "should return latest data when a multiple updates found",
currentTime: new Date("2000-01-01T00:00:00Z").getTime(),
inputPrescriptions: {
prescriptionID: "abc",
odsCode: "123"
Expand Down Expand Up @@ -86,6 +90,7 @@ const scenarios: Array<scenariosType> = [
},
{
scenarioDescription: "should return latest item for multiple updates for each of multiple statuses",
currentTime: new Date("2000-01-01T00:00:00Z").getTime(),
inputPrescriptions: {
prescriptionID: "abc",
odsCode: "123"
Expand Down Expand Up @@ -137,6 +142,7 @@ const scenarios: Array<scenariosType> = [
},
{
scenarioDescription: "should exclude item when post-dated update hasn't matured",
currentTime: new Date("2000-01-01T00:00:00Z").getTime(),
inputPrescriptions: {
prescriptionID: "abc",
odsCode: "123"
Expand All @@ -158,6 +164,7 @@ const scenarios: Array<scenariosType> = [
},
{
scenarioDescription: "should use latest post-dated update when multiple have matured",
currentTime: new Date("2000-01-01T00:00:00Z").getTime(),
inputPrescriptions: {
prescriptionID: "abc",
odsCode: "123"
Expand Down Expand Up @@ -187,7 +194,7 @@ const scenarios: Array<scenariosType> = [
itemId: "item_1",
latestStatus: "With pharmacy",
isTerminalState: false,
lastUpdateDateTime: "1970-01-03T00:00:00Z" // Back to 'With pharmacy'
lastUpdateDateTime: "1970-01-03T00:00:00Z" // revoke RTC, back to 'With pharmacy'
},
{
itemId: "item_1",
Expand Down Expand Up @@ -219,6 +226,7 @@ const scenarios: Array<scenariosType> = [
},
{
scenarioDescription: "should return an item when it _has_ matured even though the post-dated time is in the future",
currentTime: new Date("2000-01-01T00:00:00Z").getTime(),
inputPrescriptions: {
prescriptionID: "abc",
odsCode: "123"
Expand Down Expand Up @@ -248,6 +256,7 @@ const scenarios: Array<scenariosType> = [
},
{
scenarioDescription: "should return no items when empty item status are found",
currentTime: new Date("2000-01-01T00:00:00Z").getTime(),
inputPrescriptions: {
prescriptionID: "abc",
odsCode: "123"
Expand All @@ -258,13 +267,89 @@ const scenarios: Array<scenariosType> = [
onboarded: false,
items: []
}
},
{
scenarioDescription: "should return With pharmacy when RTC has been revoked",
currentTime: new Date("2025-12-11T12:00:00Z").getTime(),
inputPrescriptions: {
prescriptionID: "abc",
odsCode: "123"
},
queryResults: [
{
itemId: "item_1",
latestStatus: "Ready to collect",
isTerminalState: false,
lastUpdateDateTime: "2025-12-11T10:00:00Z",
postDatedLastModifiedSetAt: "2025-12-10T10:00:00Z"
},
{
itemId: "item_1",
latestStatus: "With pharmacy",
isTerminalState: false,
lastUpdateDateTime: "2025-12-10T11:00:00Z"
}
],
expectedResult: {
prescriptionID: "abc",
onboarded: true,
items: [
{
itemId: "item_1",
latestStatus: "With pharmacy",
isTerminalState: false,
lastUpdateDateTime: "2025-12-10T11:00:00Z"
}
]
}
},
{
scenarioDescription: "should return With pharmacy when RTC has been revoked but later RTC has matured",
currentTime: new Date("2025-12-11T19:00:00Z").getTime(),
inputPrescriptions: {
prescriptionID: "abc",
odsCode: "123"
},
queryResults: [
{
itemId: "item_1",
latestStatus: "Ready to collect",
isTerminalState: false,
lastUpdateDateTime: "2025-12-11T19:00:00Z",
postDatedLastModifiedSetAt: "2025-12-10T13:00:00Z"
},
{
itemId: "item_1",
latestStatus: "With pharmacy",
isTerminalState: false,
lastUpdateDateTime: "2025-12-10T11:00:00Z"
}
],
expectedResult: {
prescriptionID: "abc",
onboarded: true,
items: [
{
itemId: "item_1",
latestStatus: "With pharmacy",
isTerminalState: false,
lastUpdateDateTime: "2025-12-10T11:00:00Z"
},
{
itemId: "item_1",
latestStatus: "Ready to collect",
isTerminalState: false,
lastUpdateDateTime: "2025-12-11T19:00:00Z",
postDatedLastModifiedSetAt: "2025-12-10T13:00:00Z"
}
]
}
}
]
describe("Unit tests for buildResults", () => {
it.each<scenariosType>(scenarios)("$scenarioDescription", ({inputPrescriptions, queryResults, expectedResult}) => {
// Use a fixed time of 2000-01-01 for tests (946684800000 ms since epoch)
const fixedCurrentTime = new Date("2000-01-01T00:00:00Z").getTime()
const result = filterOutFutureReduceToLatestUpdates(inputPrescriptions, queryResults, fixedCurrentTime)
expect(result).toMatchObject(expectedResult)
})
it.each<scenariosType>(scenarios)("$scenarioDescription",
({inputPrescriptions, queryResults, expectedResult, currentTime}) => {
const result = filterOutFutureReduceToLatestUpdates(inputPrescriptions, queryResults, currentTime)
expect(result).toMatchObject(expectedResult)
})
})