@@ -94,7 +94,9 @@ export const bookIntoClinicController = {
9494 *
9595 * The nature of the journey here is complex, as there are two separate sections in which we need to
9696 * iterate over children. Or over appointments, if you want to think of it that way (each child has
97- * their own appointment).
97+ * their own appointment). And the second iteration - the health questions - has pages that are
98+ * dependent on the answers given during the appointment booking (specifically, the choice of vaccines
99+ * per child).
98100 *
99101 * So, it goes:
100102 * - Start page
@@ -104,17 +106,13 @@ export const bookIntoClinicController = {
104106 * - ...
105107 * - Appointment time <-- final page of the per-child appointment journey; iterate to next child if required
106108 * - Parent info
109+ * - Check answers
107110 * - Health questions?
108111 * - Health question 1 <-- first page of the per-child health question journey
109112 * - ...
110113 * - Health question n <-- final page of the per-child health question journey; iterate to next child if required
111- * - Check answers
112114 * - Confirmation
113115 *
114- * So, at the point where we start the health questions journey, we need to set up the iteration again, overriding
115- * the default paths.next given to us by the wizard() function so that we can re-inject the appointment_uuid into
116- * the path (the "Health questions?" page won't have that parameter).
117- *
118116 * */
119117
120118 // Create objects on the global context to allow us to check branching conditions, etc.
@@ -136,8 +134,7 @@ export const bookIntoClinicController = {
136134 response . locals . childNumber =
137135 booking . appointments_ids . indexOf ( appointment . uuid ) + 1
138136 response . locals . childCount = booking . appointments_ids . length
139- response . locals . firstName =
140- appointment . unmatchedFirstName || 'your child'
137+ response . locals . firstName = appointment . firstName || 'your child'
141138 response . locals . fullName = appointment . fullName || 'your child'
142139 }
143140 }
@@ -154,7 +151,7 @@ export const bookIntoClinicController = {
154151 [ `/${ session_preset_slug } /${ booking_uuid } /new/child-count` ] : { } ,
155152
156153 // Appointment journey; once per child
157- ...getAllAppointmentPaths ( booking ) ,
154+ ...getAllAppointmentPaths ( request . session . data , booking ) ,
158155
159156 // Parent journey
160157 [ `/${ session_preset_slug } /${ booking_uuid } /new/parent` ] : {
@@ -163,9 +160,12 @@ export const bookIntoClinicController = {
163160 } ,
164161 [ `/${ session_preset_slug } /${ booking_uuid } /new/contact-preference` ] : { } ,
165162
163+ // Check answers
164+ [ `/${ session_preset_slug } /${ booking_uuid } /new/check-answers` ] : { } ,
165+
166166 // Health questions (optional)
167167 [ `/${ session_preset_slug } /${ booking_uuid } /new/offer-health-questions` ] : {
168- [ `/${ session_preset_slug } /${ booking_uuid } /new/check-answers ` ] : {
168+ [ `/${ session_preset_slug } /${ booking_uuid } /new/confirmation ` ] : {
169169 data : 'transaction.optedIntoHealthQuestions' ,
170170 value : 'false'
171171 }
@@ -180,9 +180,6 @@ export const bookIntoClinicController = {
180180 data
181181 ) ,
182182
183- // Check answers
184- [ `/${ session_preset_slug } /${ booking_uuid } /new/check-answers` ] : { } ,
185-
186183 // Confirmation! \o/
187184 [ `/${ session_preset_slug } /${ booking_uuid } /new/confirmation` ] : { }
188185 }
@@ -212,7 +209,7 @@ export const bookIntoClinicController = {
212209 */
213210 showForm ( request , response ) {
214211 const { appointment } = response . locals
215- let { view } = request . params
212+ let { booking_uuid , view } = request . params
216213
217214 // All health questions use the same view
218215 let key
@@ -227,6 +224,50 @@ export const bookIntoClinicController = {
227224 request . session . data
228225 ) [ key ] ?. conditional
229226
227+ // Build the options for the selection of a home address address from those already entered
228+ if ( view === 'address-selection' ) {
229+ const booking = ClinicBooking . findOne (
230+ booking_uuid ,
231+ request . session . data . wizard
232+ )
233+ const previousAddressItems = booking . appointments
234+ . map ( ( appointment ) => {
235+ if ( appointment . child ?. address ) {
236+ const oneLineAddress = Object . values ( appointment . child . address )
237+ . filter ( ( string ) => string )
238+ . join ( ', ' )
239+ return {
240+ text : oneLineAddress ,
241+ value : appointment . uuid
242+ }
243+ }
244+
245+ return null
246+ } )
247+ . filter ( Boolean )
248+
249+ response . locals . previousAddressItems = [
250+ ...previousAddressItems ,
251+ {
252+ divider : 'or'
253+ } ,
254+ {
255+ text : 'Enter a different address' ,
256+ value : 'new'
257+ }
258+ ]
259+ }
260+
261+ /////////////////////
262+ // console.log(`view: ${view}`)
263+ // console.log(
264+ // `data.wizard: ${JSON.stringify(request.session.data.wizard, null, 2)}`
265+ // )
266+ // console.log(
267+ // `data.appointment: ${JSON.stringify(request.session.data.appointment, null, 2)}`
268+ // )
269+ /////////////////////
270+
230271 response . render ( `book-into-a-clinic/form/${ view } ` , { key, hasSubQuestions } )
231272 } ,
232273
@@ -237,7 +278,7 @@ export const bookIntoClinicController = {
237278 * @param {* } response
238279 */
239280 updateForm ( request , response ) {
240- const { booking_uuid, appointment_uuid } = request . params
281+ const { booking_uuid, appointment_uuid, view } = request . params
241282 const { data } = request . session
242283 const { paths } = response . locals
243284
@@ -257,8 +298,10 @@ export const bookIntoClinicController = {
257298 _ . merge ( data . wizard . transaction , request . body . transaction )
258299 }
259300
260- // If we've just set the child count, create the appointments we'll need
261- if ( request . originalUrl . endsWith ( '/new/child-count' ) ) {
301+ let nextUrl = paths . next
302+
303+ if ( view === 'child-count' ) {
304+ // We've just set the child count, so create the appointments we'll need
262305 const booking = ClinicBooking . findOne ( booking_uuid , data . wizard )
263306
264307 let desiredCount = Number ( data . wizard . transaction . childCount )
@@ -280,19 +323,35 @@ export const bookIntoClinicController = {
280323 ClinicAppointment . delete ( appointment_uuid , data . wizard )
281324 }
282325
283- // NB: request.session.save was needed to avoid race condition issues on heroku
284- // Flush to session store and start the appointment journey for the first child
326+ // Start the appointment journey for the first child
285327 const firstAppointment = booking . appointments [ 0 ]
286328 const firstAppointmentUrl = `${ request . baseUrl } /${ booking . bookingUri } /new/${ firstAppointment . appointmentUri } /child`
287- request . session . save ( ( err ) => {
288- if ( ! err ) response . redirect ( firstAppointmentUrl )
289- } )
290- } else {
291- // Flush to session store and continue to the next page in the journey
292- request . session . save ( ( err ) => {
293- if ( ! err ) response . redirect ( paths . next )
294- } )
329+ nextUrl = firstAppointmentUrl
330+ } else if (
331+ view === 'address-selection' &&
332+ request . body . transaction . previousAddress !== 'new'
333+ ) {
334+ // We've just selected a previous child's address for the current appointment, so copy
335+ // that detail to the child record
336+ const previous_appointment_uuid = request . body . transaction . previousAddress
337+ const previousAppointment = ClinicAppointment . findOne (
338+ previous_appointment_uuid ,
339+ data . wizard
340+ )
341+ const currentAppointment = ClinicAppointment . findOne (
342+ appointment_uuid ,
343+ data . wizard
344+ )
345+
346+ if ( previousAppointment && currentAppointment ) {
347+ currentAppointment . child . address = previousAppointment . child . address
348+ }
295349 }
350+
351+ // NB: request.session.save was needed to avoid race condition issues on heroku
352+ request . session . save ( ( err ) => {
353+ if ( ! err ) response . redirect ( nextUrl )
354+ } )
296355 } ,
297356
298357 /**
0 commit comments