11import { fakerEN_GB as faker } from '@faker-js/faker'
22import { addMinutes } from 'date-fns'
3- import _ from 'lodash'
43
54import { ParentalRelationship , SessionType } from '../enums.js'
65import { ClinicAppointment } from '../models.js'
@@ -11,47 +10,53 @@ const clinicSlotLength = Number(process.env.CLINIC_SLOT_LENGTH) || 10
1110/**
1211 * Generate fake clinic appointment
1312 *
14- * @param {ClinicBooking } booking - The booking this appointment will belong to
13+ * @param {import('../models/clinic-booking.js'). ClinicBooking } booking - The booking this appointment will belong to
1514 * @param {object } context - The other data already defined (sessions, children, etc.)
1615 * @returns {ClinicAppointment } A new, fake clinic appointment
1716 */
1817export function generateClinicAppointment ( booking , context ) {
1918 const uuid = faker . string . uuid ( )
2019
21- // Choose a clinic session to book this appointment into
20+ // Find clinic sessions for this programme
2221 const clinicSessions = Object . values ( context . sessions ) . filter (
23- ( s ) =>
24- s . type === SessionType . Clinic &&
25- s . presetNames . includes ( booking . sessionPreset . name )
22+ ( session ) =>
23+ session . type === SessionType . Clinic &&
24+ session . presetNames . includes ( booking . sessionPreset . name )
2625 )
27- if ( ! clinicSessions . some ( Boolean ) ) {
26+ if ( ! clinicSessions . length ) {
2827 return null
2928 }
29+
30+ // Choose a clinic session to book this appointment into
3031 const clinicSession = faker . helpers . arrayElement ( clinicSessions )
3132 if ( ! clinicSession ) {
3233 return null
3334 }
3435 const session_id = clinicSession . id
3536
3637 // Work out the expected age range for children attending this session
37- const yearGroups = _ . uniq (
38- clinicSession . programmes . flatMap ( ( p ) => p . yearGroups || [ ] )
39- )
40- const ageRanges = yearGroups . map ( ( yg ) => ( { min : yg + 4 , max : yg + 5 } ) )
41- const allAgeLimits = ageRanges . flatMap ( ( ar ) => [ ar . min , ar . max ] )
42- const minAge = Math . min ( allAgeLimits ) || 4
43- const maxAge = Math . max ( allAgeLimits ) || 15
38+ const yearGroups = clinicSession . programmes . flatMap ( ( programme ) => [
39+ ...new Set ( programme . yearGroups )
40+ ] )
41+ const minAge = yearGroups . length ? Math . min ( ...yearGroups ) + 4 : 4
42+ const maxAge = yearGroups . length ? Math . max ( ...yearGroups ) + 5 : 15
4443
45- // Find/create a child of an appropriate age for the chosen clinic and its programmme
44+ // Find/create a child of an appropriate age for the chosen clinic and its programme
4645 let matchedPatient
4746 if ( faker . datatype . boolean ( 0.9 ) ) {
48- const eligiblePatients = Object . values ( context . patients ) . filter ( ( p ) => {
49- const age = getAge ( p . dob )
50- return age >= minAge && age <= maxAge
51- } )
47+ const eligiblePatients = Object . values ( context . patients ) . filter (
48+ ( patient ) => {
49+ const age = getAge ( patient . dob )
50+ return age >= minAge && age <= maxAge
51+ }
52+ )
53+ if ( ! eligiblePatients . length ) {
54+ return null
55+ }
5256 matchedPatient = faker . helpers . arrayElement ( eligiblePatients )
5357 }
5458 const patient_uuid = matchedPatient ?. uuid
59+
5560 // Unmatched child details, if required
5661 const unmatchedFirstName = matchedPatient
5762 ? undefined
0 commit comments