@@ -3,33 +3,31 @@ import { addMinutes } from 'date-fns'
33import _ from 'lodash'
44
55import { ParentalRelationship , SessionType } from '../enums.js'
6- import { ClinicAppointment , Parent } from '../models.js'
6+ import { ClinicAppointment } from '../models.js'
77import { getAge } from '../utils/date.js'
88
9- import { generateParent } from './parent.js'
10-
119const clinicSlotLength = Number ( process . env . CLINIC_SLOT_LENGTH ) || 10
1210
1311/**
1412 * Generate fake clinic appointment
1513 *
16- * @param {string } booking_uuid - The UUID of the booking this appointment belongs to
17- * @param {Parent|null } parentFromFirstAppointment - The parent from the first appointment created in the same booking, if this appointment is not the first
14+ * @param {ClinicBooking } booking - The booking this appointment will belong to
1815 * @param {object } context - The other data already defined (sessions, children, etc.)
1916 * @returns {ClinicAppointment } A new, fake clinic appointment
2017 */
21- export function generateClinicAppointment (
22- booking_uuid ,
23- parentFromFirstAppointment ,
24- context
25- ) {
18+ export function generateClinicAppointment ( booking , context ) {
2619 const uuid = faker . string . uuid ( )
2720
2821 // Choose a clinic session to book this appointment into
2922 const clinicSessions = Object . values ( context . sessions ) . filter (
30- ( s ) => s . type === SessionType . Clinic
23+ ( s ) =>
24+ s . type === SessionType . Clinic &&
25+ s . presetNames . includes ( booking . sessionPreset . name )
3126 )
3227 const clinicSession = faker . helpers . arrayElement ( clinicSessions )
28+ if ( ! clinicSession ) {
29+ return null
30+ }
3331 const session_id = clinicSession . id
3432
3533 // Work out the expected age range for children attending this session
@@ -60,80 +58,87 @@ export function generateClinicAppointment(
6058 ? undefined
6159 : faker . date . birthdate ( { min : minAge , max : maxAge , mode : 'age' } )
6260
63- // Parent details and relationship
64- let parent
65- if ( ! parentFromFirstAppointment ) {
66- // This is the first appointment for the booking, so we're free to CREATE the parent details
67- if ( matchedPatient ) {
68- parent =
69- ( matchedPatient . parent1 || matchedPatient . parent2 ) ??
70- generateParent ( matchedPatient . lastName , faker . datatype . boolean ( 0.5 ) )
71- } else {
72- parent = generateParent ( unmatchedLastName , faker . datatype . boolean ( 0.5 ) )
73- }
74- } else {
75- // Copy parent details from the first appointment and possibly adapt the relationship
76- parent = new Parent ( parentFromFirstAppointment )
77- parent . uuid = faker . string . uuid ( )
78-
79- // Set up the relationship to the child for this appointment
80- if (
81- [ ParentalRelationship . Mum , ParentalRelationship . Dad ] . includes (
82- parentFromFirstAppointment . relationship
83- )
84- ) {
61+ // Set up the relationship to the child for this appointment
62+ const parent = booking . parent
63+ let parentalRelationship ,
64+ parentalRelationshipOther ,
65+ parentHasParentalResponsibility
66+ if ( parent ) {
67+ const mumOrDad = [
68+ ParentalRelationship . Mum ,
69+ ParentalRelationship . Dad
70+ ] . includes ( parent . relationship )
71+ if ( mumOrDad ) {
8572 // Mum or Dad initially, and most likely to stay that way
8673 if ( faker . datatype . boolean ( 0.1 ) ) {
87- parent . relationship = faker . helpers . arrayElement ( [
74+ parentalRelationship = faker . helpers . arrayElement ( [
8875 ParentalRelationship . Fosterer ,
8976 ParentalRelationship . Guardian ,
9077 ParentalRelationship . Other
9178 ] )
92- parent . relationshipOther =
93- parent . relationship === ParentalRelationship . Other
79+ parentalRelationshipOther =
80+ parentalRelationship === ParentalRelationship . Other
9481 ? 'Grandparent'
9582 : undefined
83+ parentHasParentalResponsibility = true
9684 }
9785 } else {
9886 // Fosterer, Guardian or Other
99- parent . relationship = parentFromFirstAppointment . relationship
100- parent . relationshipOther = parentFromFirstAppointment . relationshipOther
87+ parentalRelationship = parent . relationship
88+ parentalRelationshipOther = parent . relationshipOther
89+ parentHasParentalResponsibility = parent . hasParentalResponsibility
10190 }
10291 }
10392
10493 // Slot details (NB: session date is expected to specify midday)
94+ const needsExtraTime = faker . datatype . boolean ( 0.2 )
95+ let extraTimeReason
96+ if ( needsExtraTime ) {
97+ const phobia = faker . helpers . weightedArrayElement ( [
98+ { value : 'needles' , weight : 90 } ,
99+ { value : 'nurses' , weight : 8 } ,
100+ { value : 'vaccines' , weight : 2 }
101+ ] )
102+ extraTimeReason = `Suffers from anxiety regarding ${ phobia } `
103+ }
105104 const startAt = addMinutes (
106105 clinicSession . date ,
107106 faker . number . int ( { min : 0 , max : 60 , multipleOf : clinicSlotLength } )
108107 )
109- const endAt = addMinutes ( startAt , clinicSlotLength )
108+ const endAt = addMinutes ( startAt , clinicSlotLength * ( needsExtraTime ? 2 : 1 ) )
110109
111110 // Have the child signed up for the clinic's primary programme plus a random selection of other programmes
111+ const primary_programme_ids = clinicSession . programme_ids
112112 const additionalProgramme_ids = Object . values ( context . programmes )
113113 . filter ( ( p ) => p . hidden !== true )
114114 . map ( ( p ) => p . id )
115115 . filter (
116116 ( id ) =>
117117 ! clinicSession . programme_ids . includes ( id ) && faker . datatype . boolean ( 0.2 )
118118 )
119- const programme_ids = [
120- ...clinicSession . programme_ids ,
119+ const selected_programme_ids = [
120+ ...primary_programme_ids ,
121121 ...additionalProgramme_ids
122122 ]
123123
124124 return new ClinicAppointment (
125125 {
126126 uuid,
127- booking_uuid,
127+ booking_uuid : booking . uuid ,
128128 patient_uuid,
129129 unmatchedFirstName,
130130 unmatchedLastName,
131131 unmatchedDob,
132- parent,
132+ needsExtraTime,
133+ extraTimeReason,
134+ parentalRelationship,
135+ parentalRelationshipOther,
136+ parentHasParentalResponsibility,
133137 session_id,
134138 startAt,
135139 endAt,
136- programme_ids
140+ selected_programme_ids,
141+ primary_programme_ids
137142 } ,
138143 context
139144 )
0 commit comments