Skip to content

Commit 08bbac3

Browse files
committed
Allow use of a previously entered address in clinic booking
This commit also introduces the Child object (without a context) on the ClinicAppointment object, replacing the unmatchedXxxx properties.
1 parent a709e5e commit 08bbac3

7 files changed

Lines changed: 121 additions & 57 deletions

File tree

app/controllers/book-into-a-clinic.js

Lines changed: 78 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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
/**

app/locales/en.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,10 @@ export const en = {
312312
title: 'What is %s’s home address?',
313313
hint: 'Give the child’s primary address. We use this to confirm their identity.'
314314
},
315+
addressSelection: {
316+
title: 'What is %s’s home address?',
317+
hint: 'Select the child’s primary address. We use this to confirm their identity.'
318+
},
315319
parentalRelationship: {
316320
title: 'What is your relationship to %s?',
317321
hasParentalResponsibility: {

app/models/clinic-appointment.js

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,14 @@ import { fakerEN_GB as faker } from '@faker-js/faker'
22
import _ from 'lodash'
33

44
import {
5+
Child,
56
ClinicBooking,
67
Parent,
78
Patient,
89
Programme,
910
Session
1011
} from '../models.js'
11-
import {
12-
convertIsoDateToObject,
13-
convertObjectToIsoDate,
14-
formatDate,
15-
getDateValueDifference
16-
} from '../utils/date.js'
12+
import { formatDate, getDateValueDifference } from '../utils/date.js'
1713
import { stringToArray, stringToBoolean } from '../utils/string.js'
1814

1915
/**
@@ -24,10 +20,7 @@ import { stringToArray, stringToBoolean } from '../utils/string.js'
2420
* @property {string} uuid - Unique ID for this clinic appointment
2521
* @property {string} booking_uuid - Unique ID for the booking in which this appointment was made
2622
* @property {string} [patient_uuid] - Patient UUID (if matched to a patient record)
27-
* @property {string} [unmatchedFirstName] - Child first name, if not matched to a patient record
28-
* @property {string} [unmatchedLastName] - Child last name, if not matched to a patient record
29-
* @property {Date} [unmatchedDob] - Child date of birth, if not matched to a patient record
30-
* @property {object} [unmatchedDob_] - Child date of birth, if not matched to a patient record (for use with decorate)
23+
* @property {import('./child.js').Child} [child] - child details recorded from form input values
3124
* @property {Boolean} needsExtraTime - Does the child need extra time for their vaccinations?
3225
* @property {string} [extraTimeReason] - The reason why the child needs extra time for their appointment
3326
* @property {ParentalRelationship} [parentalRelationship] - The relationship of the person booking the appointment to the child
@@ -47,10 +40,7 @@ export class ClinicAppointment {
4740
this.booking_uuid = options?.booking_uuid
4841

4942
this.patient_uuid = options?.patient_uuid
50-
this.unmatchedFirstName = options?.unmatchedFirstName
51-
this.unmatchedLastName = options?.unmatchedLastName
52-
this.unmatchedDob = options?.unmatchedDob && new Date(options.unmatchedDob)
53-
this.unmatchedDob_ = options?.unmatchedDob_
43+
this.child = (options?.child && new Child(options.child)) || new Child({})
5444

5545
this.needsExtraTime = stringToBoolean(options?.needsExtraTime)
5646
this.extraTimeReason = options?.extraTimeReason
@@ -155,7 +145,7 @@ export class ClinicAppointment {
155145
* @returns {string} Child's first name
156146
*/
157147
get firstName() {
158-
return this.patient ? this.patient.firstName : this.unmatchedFirstName
148+
return this.patient ? this.patient.firstName : this.child.firstName
159149
}
160150

161151
/**
@@ -164,7 +154,7 @@ export class ClinicAppointment {
164154
* @returns {string} Child's last name
165155
*/
166156
get lastName() {
167-
return this.patient ? this.patient.lastName : this.unmatchedLastName
157+
return this.patient ? this.patient.lastName : this.child.lastName
168158
}
169159

170160
/**
@@ -176,26 +166,6 @@ export class ClinicAppointment {
176166
return `${this.firstName} ${this.lastName}`
177167
}
178168

179-
/**
180-
* Get date of birth for `dateInput`
181-
*
182-
* @returns {object|undefined} `dateInput` object
183-
*/
184-
get unmatchedDob_() {
185-
return convertIsoDateToObject(this.unmatchedDob)
186-
}
187-
188-
/**
189-
* Set date of birth from `dateInput`
190-
*
191-
* @param {object} object - dateInput object
192-
*/
193-
set unmatchedDob_(object) {
194-
if (object) {
195-
this.unmatchedDob = convertObjectToIsoDate(object)
196-
}
197-
}
198-
199169
/**
200170
* Get the programmes selected for this appointment
201171
*

app/utils/clinic-appointment.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import { camelToKebabCase } from './string.js'
55
/**
66
* Get wizard journey paths and forking details for all appointments in the given clinic booking
77
*
8+
* @param {object} sessionData - the request.session.data object
89
* @param {ClinicBooking} booking - the clinic booking whose appointment journeys we're mapping
910
* @returns {object} An object containing all relevants page and forks
1011
*/
11-
export const getAllAppointmentPaths = (booking) => {
12+
export const getAllAppointmentPaths = (sessionData, booking) => {
1213
const booking_uuid = booking.uuid
1314
const session_preset_slug = booking.sessionPreset.slug
1415

@@ -19,6 +20,15 @@ export const getAllAppointmentPaths = (booking) => {
1920
{},
2021
[`/${session_preset_slug}/${booking_uuid}/new/${appointment_uuid}/dob`]:
2122
{},
23+
...(booking.appointments_ids[0] !== appointment_uuid
24+
? {
25+
[`/${session_preset_slug}/${booking_uuid}/new/${appointment_uuid}/address-selection`]:
26+
{
27+
[`/${session_preset_slug}/${booking_uuid}/new/${appointment_uuid}/parental-relationship`]:
28+
() => sessionData.transaction.previousAddress !== 'new'
29+
}
30+
}
31+
: {}),
2232
[`/${session_preset_slug}/${booking_uuid}/new/${appointment_uuid}/address`]:
2333
{},
2434
[`/${session_preset_slug}/${booking_uuid}/new/${appointment_uuid}/parental-relationship`]:
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{% extends "_layouts/form.njk" %}
2+
3+
{% set title = __("clinicBooking.addressSelection.title", firstName) %}
4+
5+
{% block form %}
6+
{{ radios({
7+
fieldset: {
8+
legend: {
9+
html: appHeading({
10+
title: title,
11+
caption: __("clinicBooking.appointment.caption", fullName) if childCount > 1
12+
})
13+
}
14+
},
15+
hint: { text: __("clinicBooking.addressSelection.hint") },
16+
items: previousAddressItems,
17+
decorate: "transaction.previousAddress"
18+
}) }}
19+
{% endblock %}

app/views/book-into-a-clinic/form/child.njk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
{{ input({
1818
label: { text: __("clinicBooking.child.firstName.label") },
1919
hint: { text: __("clinicBooking.child.firstName.hint") },
20-
decorate: "appointment.unmatchedFirstName"
20+
decorate: "appointment.child.firstName"
2121
}) }}
2222

2323
{{ input({
2424
label: { text: __("clinicBooking.child.lastName.label") },
2525
hint: { text: __("clinicBooking.child.lastName.hint") },
26-
decorate: "appointment.unmatchedLastName"
26+
decorate: "appointment.child.lastName"
2727
}) }}
2828
{% endblock %}

app/views/book-into-a-clinic/form/dob.njk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
}
1414
},
1515
hint: { text: __("clinicBooking.dob.hint") },
16-
decorate: "appointment.unmatchedDob_"
16+
decorate: "appointment.child.dob_"
1717
}) }}
1818
{% endblock %}

0 commit comments

Comments
 (0)