Skip to content

Commit c80616c

Browse files
committed
Sanitise any _unchecked values coming from the clinic booking process
This means swapping individual checkbox values for a Boolean or taking _checked out of arrays where there are multiple checkboxes - in both the booking and appointment models.
1 parent 2cee5f9 commit c80616c

2 files changed

Lines changed: 25 additions & 4 deletions

File tree

app/models/clinic-appointment.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
formatDate,
1616
getDateValueDifference
1717
} from '../utils/date.js'
18+
import { stringToArray, stringToBoolean } from '../utils/string.js'
1819

1920
/**
2021
* @class ClinicAppointment
@@ -52,7 +53,7 @@ export class ClinicAppointment {
5253
this.unmatchedDob = options?.unmatchedDob && new Date(options.unmatchedDob)
5354
this.unmatchedDob_ = options?.unmatchedDob_
5455

55-
this.needsExtraTime = options?.needsExtraTime
56+
this.needsExtraTime = stringToBoolean(options?.needsExtraTime)
5657
this.extraTimeReason = options?.extraTimeReason
5758

5859
this.parentalRelationship = options?.parentalRelationship
@@ -64,8 +65,14 @@ export class ClinicAppointment {
6465
this.startAt = options?.startAt ? new Date(options.startAt) : undefined
6566
this.endAt = options?.endAt ? new Date(options.endAt) : undefined
6667

67-
this.selected_programme_ids = options?.selected_programme_ids || []
68-
this.primary_programme_ids = options?.primary_programme_ids || []
68+
this.selected_programme_ids =
69+
(options?.selected_programme_ids &&
70+
stringToArray(options.selected_programme_ids)) ||
71+
[]
72+
this.primary_programme_ids =
73+
(options?.primary_programme_ids &&
74+
stringToArray(options.primary_programme_ids)) ||
75+
[]
6976
this.healthAnswers = options?.healthAnswers || {}
7077
}
7178

@@ -367,11 +374,18 @@ export class ClinicAppointment {
367374
* @static
368375
*/
369376
static update(uuid, updates, context) {
377+
// Sanitise any _unchecked checkbox values
378+
if (updates?.selected_programme_ids) {
379+
updates.selected_programme_ids = stringToArray(
380+
updates.selected_programme_ids
381+
)
382+
}
383+
384+
// Copy updates into the relevant appointment
370385
const updatedAppointment = _.merge(
371386
ClinicAppointment.findOne(uuid, context),
372387
updates
373388
)
374-
// updatedAppointment.updatedAt = today()
375389

376390
// Remove appointment context
377391
delete updatedAppointment.context

app/models/clinic-booking.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import _ from 'lodash'
44
import allProgrammesData from '../datasets/programmes.js'
55
import { SessionPresets } from '../enums.js'
66
import { ClinicAppointment, Parent, Programme } from '../models.js'
7+
import { stringToBoolean } from '../utils/string.js'
78

89
/**
910
* @class ClinicBooking
@@ -182,6 +183,12 @@ export class ClinicBooking {
182183
* @static
183184
*/
184185
static update(uuid, updates, context) {
186+
// Sanitise any _unchecked checkbox values
187+
if (updates?.parent?.sms) {
188+
updates.parent.sms = stringToBoolean(updates.parent.sms) || false
189+
}
190+
191+
// Copy updates into the relevant booking
185192
const existingBooking = ClinicBooking.findOne(uuid, context)
186193
const updatedBooking = _.merge(existingBooking, updates)
187194

0 commit comments

Comments
 (0)