1- import { buildResult } from "../src/getStatusUpdates"
1+ import { filterOutFutureReduceToLatestUpdates } from "../src/getStatusUpdates"
22import { inputPrescriptionType } from "../src/schema/request"
33import { outputPrescriptionType , itemType } from "../src/schema/response"
44
@@ -8,6 +8,8 @@ type scenariosType = {
88 queryResults : Array < itemType >
99 expectedResult : outputPrescriptionType
1010}
11+ const now = new Date ( )
12+ const futureDateTime = new Date ( now . valueOf ( ) + ( 24 * 60 * 60 * 1000 ) ) . toISOString ( )
1113const scenarios : Array < scenariosType > = [
1214 {
1315 scenarioDescription : "should return correct data when a matched prescription found" ,
@@ -83,7 +85,7 @@ const scenarios: Array<scenariosType> = [
8385 }
8486 } ,
8587 {
86- scenarioDescription : "should return correct data for multiple items " ,
88+ scenarioDescription : "should return latest item for multiple updates for each of multiple statuses " ,
8789 inputPrescriptions : {
8890 prescriptionID : "abc" ,
8991 odsCode : "123"
@@ -97,9 +99,9 @@ const scenarios: Array<scenariosType> = [
9799 } ,
98100 {
99101 itemId : "item_1" ,
100- latestStatus : "latest_item_1_status " ,
102+ latestStatus : "item_1_status " ,
101103 isTerminalState : true ,
102- lastUpdateDateTime : "1972-01-01T00:00:00Z"
104+ lastUpdateDateTime : "1972-01-01T00:00:00Z" // newer update for item_1
103105 } ,
104106 {
105107 itemId : "item_2" ,
@@ -109,9 +111,9 @@ const scenarios: Array<scenariosType> = [
109111 } ,
110112 {
111113 itemId : "item_2" ,
112- latestStatus : "early_item_2_status " ,
114+ latestStatus : "item_2_status " ,
113115 isTerminalState : true ,
114- lastUpdateDateTime : "1970-01-01T00:00:00Z"
116+ lastUpdateDateTime : "1970-01-01T00:00:00Z" // older update for item_2
115117 }
116118 ] ,
117119 expectedResult : {
@@ -120,7 +122,7 @@ const scenarios: Array<scenariosType> = [
120122 items : [
121123 {
122124 itemId : "item_1" ,
123- latestStatus : "latest_item_1_status " ,
125+ latestStatus : "item_1_status " ,
124126 isTerminalState : true ,
125127 lastUpdateDateTime : "1972-01-01T00:00:00Z"
126128 } ,
@@ -132,11 +134,137 @@ const scenarios: Array<scenariosType> = [
132134 }
133135 ]
134136 }
137+ } ,
138+ {
139+ scenarioDescription : "should exclude item when post-dated update hasn't matured" ,
140+ inputPrescriptions : {
141+ prescriptionID : "abc" ,
142+ odsCode : "123"
143+ } ,
144+ queryResults : [
145+ {
146+ itemId : "item_1" ,
147+ latestStatus : "Ready to collect" ,
148+ isTerminalState : false ,
149+ lastUpdateDateTime : "2030-01-01T00:00:00Z" , // Future, no fallback
150+ postDatedLastModifiedSetAt :"1972-01-01T00:00:00Z"
151+ }
152+ ] ,
153+ expectedResult : {
154+ prescriptionID : "abc" ,
155+ onboarded : true ,
156+ items : [ ]
157+ }
158+ } ,
159+ {
160+ scenarioDescription : "should use latest post-dated update when multiple have matured" ,
161+ inputPrescriptions : {
162+ prescriptionID : "abc" ,
163+ odsCode : "123"
164+ } ,
165+ queryResults : [
166+ {
167+ itemId : "item_1" ,
168+ latestStatus : "With pharmacy" ,
169+ isTerminalState : false ,
170+ lastUpdateDateTime : "1970-01-01T00:00:00Z"
171+ } ,
172+ {
173+ itemId : "item_1" ,
174+ latestStatus : "Ready to collect" ,
175+ isTerminalState : false ,
176+ lastUpdateDateTime : "1970-01-02T00:00:00Z" ,
177+ postDatedLastModifiedSetAt : "1970-01-01T00:00:00Z" // first RTC: post-dated and matured
178+ } ,
179+ {
180+ itemId : "item_1" ,
181+ latestStatus : "Ready to collect" ,
182+ isTerminalState : false ,
183+ lastUpdateDateTime : futureDateTime ,
184+ postDatedLastModifiedSetAt : "1970-01-02T00:00:00Z" // second RTC: post-dated and yet to mature
185+ } ,
186+ {
187+ itemId : "item_1" ,
188+ latestStatus : "With pharmacy" ,
189+ isTerminalState : false ,
190+ lastUpdateDateTime : "1970-01-03T00:00:00Z" // Back to 'With pharmacy'
191+ } ,
192+ {
193+ itemId : "item_1" ,
194+ latestStatus : "Ready to collect" ,
195+ isTerminalState : false ,
196+ lastUpdateDateTime : "1970-01-04T00:00:00Z" ,
197+ postDatedLastModifiedSetAt : "1970-01-03T00:00:00Z" // third RTC: post-dated and matured
198+ }
199+ ] ,
200+ expectedResult : {
201+ prescriptionID : "abc" ,
202+ onboarded : true ,
203+ items : [
204+ {
205+ itemId : "item_1" ,
206+ latestStatus : "With pharmacy" ,
207+ isTerminalState : false ,
208+ lastUpdateDateTime : "1970-01-03T00:00:00Z"
209+ } ,
210+ {
211+ itemId : "item_1" ,
212+ latestStatus : "Ready to collect" ,
213+ isTerminalState : false ,
214+ lastUpdateDateTime : "1970-01-04T00:00:00Z" ,
215+ postDatedLastModifiedSetAt : "1970-01-03T00:00:00Z"
216+ }
217+ ]
218+ }
219+ } ,
220+ {
221+ scenarioDescription : "should return an item when it _has_ matured even though the post-dated time is in the future" ,
222+ inputPrescriptions : {
223+ prescriptionID : "abc" ,
224+ odsCode : "123"
225+ } ,
226+ queryResults : [
227+ {
228+ itemId : "item_1" ,
229+ latestStatus : "Ready to collect" ,
230+ isTerminalState : false ,
231+ lastUpdateDateTime : "1970-01-01T00:00:00Z" ,
232+ postDatedLastModifiedSetAt : futureDateTime
233+ }
234+ ] ,
235+ expectedResult : {
236+ prescriptionID : "abc" ,
237+ onboarded : true ,
238+ items : [
239+ {
240+ itemId : "item_1" ,
241+ latestStatus : "Ready to collect" ,
242+ isTerminalState : false ,
243+ lastUpdateDateTime : "1970-01-01T00:00:00Z" ,
244+ postDatedLastModifiedSetAt : futureDateTime
245+ }
246+ ]
247+ }
248+ } ,
249+ {
250+ scenarioDescription : "should return no items when empty item status are found" ,
251+ inputPrescriptions : {
252+ prescriptionID : "abc" ,
253+ odsCode : "123"
254+ } ,
255+ queryResults : [ ] ,
256+ expectedResult : {
257+ prescriptionID : "abc" ,
258+ onboarded : false ,
259+ items : [ ]
260+ }
135261 }
136262]
137263describe ( "Unit tests for buildResults" , ( ) => {
138264 it . each < scenariosType > ( scenarios ) ( "$scenarioDescription" , ( { inputPrescriptions, queryResults, expectedResult} ) => {
139- const result = buildResult ( inputPrescriptions , queryResults )
265+ // Use a fixed time of 2000-01-01 for tests (946684800000 ms since epoch)
266+ const fixedCurrentTime = new Date ( "2000-01-01T00:00:00Z" ) . getTime ( )
267+ const result = filterOutFutureReduceToLatestUpdates ( inputPrescriptions , queryResults , fixedCurrentTime )
140268 expect ( result ) . toMatchObject ( expectedResult )
141269 } )
142270} )
0 commit comments