@@ -134,8 +134,7 @@ export const bookIntoClinicController = {
134134 response . locals . childNumber =
135135 booking . appointments_ids . indexOf ( appointment . uuid ) + 1
136136 response . locals . childCount = booking . appointments_ids . length
137- response . locals . firstName =
138- appointment . unmatchedFirstName || 'your child'
137+ response . locals . firstName = appointment . firstName || 'your child'
139138 response . locals . fullName = appointment . fullName || 'your child'
140139 }
141140 }
@@ -152,7 +151,7 @@ export const bookIntoClinicController = {
152151 [ `/${ session_preset_slug } /${ booking_uuid } /new/child-count` ] : { } ,
153152
154153 // Appointment journey; once per child
155- ...getAllAppointmentPaths ( booking ) ,
154+ ...getAllAppointmentPaths ( request . session . data , booking ) ,
156155
157156 // Parent journey
158157 [ `/${ session_preset_slug } /${ booking_uuid } /new/parent` ] : {
@@ -210,7 +209,7 @@ export const bookIntoClinicController = {
210209 */
211210 showForm ( request , response ) {
212211 const { appointment } = response . locals
213- let { view } = request . params
212+ let { booking_uuid , view } = request . params
214213
215214 // All health questions use the same view
216215 let key
@@ -225,6 +224,50 @@ export const bookIntoClinicController = {
225224 request . session . data
226225 ) [ key ] ?. conditional
227226
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+
228271 response . render ( `book-into-a-clinic/form/${ view } ` , { key, hasSubQuestions } )
229272 } ,
230273
@@ -235,7 +278,7 @@ export const bookIntoClinicController = {
235278 * @param {* } response
236279 */
237280 updateForm ( request , response ) {
238- const { booking_uuid, appointment_uuid } = request . params
281+ const { booking_uuid, appointment_uuid, view } = request . params
239282 const { data } = request . session
240283 const { paths } = response . locals
241284
@@ -255,8 +298,10 @@ export const bookIntoClinicController = {
255298 _ . merge ( data . wizard . transaction , request . body . transaction )
256299 }
257300
258- // If we've just set the child count, create the appointments we'll need
259- 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
260305 const booking = ClinicBooking . findOne ( booking_uuid , data . wizard )
261306
262307 let desiredCount = Number ( data . wizard . transaction . childCount )
@@ -278,19 +323,35 @@ export const bookIntoClinicController = {
278323 ClinicAppointment . delete ( appointment_uuid , data . wizard )
279324 }
280325
281- // NB: request.session.save was needed to avoid race condition issues on heroku
282- // Flush to session store and start the appointment journey for the first child
326+ // Start the appointment journey for the first child
283327 const firstAppointment = booking . appointments [ 0 ]
284328 const firstAppointmentUrl = `${ request . baseUrl } /${ booking . bookingUri } /new/${ firstAppointment . appointmentUri } /child`
285- request . session . save ( ( err ) => {
286- if ( ! err ) response . redirect ( firstAppointmentUrl )
287- } )
288- } else {
289- // Flush to session store and continue to the next page in the journey
290- request . session . save ( ( err ) => {
291- if ( ! err ) response . redirect ( paths . next )
292- } )
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+ }
293349 }
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+ } )
294355 } ,
295356
296357 /**
0 commit comments