|
| 1 | +import { ClinicBooking } from '../models.js' |
| 2 | + |
1 | 3 | import { camelToKebabCase } from './string.js' |
2 | 4 |
|
3 | 5 | /** |
@@ -70,62 +72,79 @@ export const getAllAppointmentPaths = (booking) => { |
70 | 72 | * Get the path for a single health question |
71 | 73 | * |
72 | 74 | * @param {string} key |
| 75 | + * @param {ClinicAppointment} appointment |
73 | 76 | * @param {string} pathPrefix |
74 | 77 | * @returns |
75 | 78 | */ |
76 | | -const getHealthQuestionPath = (key, pathPrefix) => { |
77 | | - return `${pathPrefix}health-question-${camelToKebabCase(key)}` |
| 79 | +const getHealthQuestionPath = (key, appointment, pathPrefix) => { |
| 80 | + return `${pathPrefix}${appointment.uuid}/health-question-${camelToKebabCase(key)}` |
78 | 81 | } |
79 | 82 |
|
80 | 83 | /** |
81 | 84 | * Get health question paths for given vaccines |
82 | 85 | * |
83 | 86 | * @param {string} pathPrefix - Path prefix |
84 | | - * @param {import('../models.js').ClinicAppointment} appointment - clinic appointment |
| 87 | + * @param {string} booking_uuid - clinic booking identifier, for access to all appointments |
| 88 | + * @param {object} bookingContext - the data context holding the booking and appointments |
| 89 | + * @param {object} programmeContext - the data context holding the programme and vaccine info |
85 | 90 | * @returns {object} Health question paths |
86 | 91 | */ |
87 | | -export const getHealthQuestionPaths = (pathPrefix, appointment) => { |
88 | | - // Don't worry about it till we've actually made our first appointment |
89 | | - if (!appointment) { |
90 | | - console.log('getHealthQuestionPaths: no appointment') |
| 92 | +export const getHealthQuestionPaths = ( |
| 93 | + pathPrefix, |
| 94 | + booking_uuid, |
| 95 | + bookingContext, |
| 96 | + programmeContext |
| 97 | +) => { |
| 98 | + const paths = {} |
91 | 99 |
|
92 | | - return {} |
| 100 | + const booking = ClinicBooking.findOne(booking_uuid, bookingContext) |
| 101 | + if (!booking) { |
| 102 | + return paths |
93 | 103 | } |
94 | 104 |
|
95 | | - const paths = {} |
96 | | - const healthQuestions = Object.entries( |
97 | | - appointment.healthQuestionsForSelectedProgrammes |
98 | | - ) |
| 105 | + for (const appointment of booking.appointments) { |
| 106 | + const healthQuestions = Object.entries( |
| 107 | + appointment.getHealthQuestionsForSelectedProgrammes(programmeContext) |
| 108 | + ) |
99 | 109 |
|
100 | | - healthQuestions.forEach(([key, question], index) => { |
101 | | - const questionPath = getHealthQuestionPath(key, pathPrefix) |
| 110 | + healthQuestions.forEach(([key, question], index) => { |
| 111 | + const questionPath = getHealthQuestionPath(key, appointment, pathPrefix) |
102 | 112 |
|
103 | | - if (question.conditional) { |
104 | | - const nextQuestion = healthQuestions[index + 1] |
105 | | - if (nextQuestion) { |
106 | | - const forkPath = getHealthQuestionPath(nextQuestion[0], pathPrefix) |
| 113 | + if (question.conditional) { |
| 114 | + const nextQuestion = healthQuestions[index + 1] |
| 115 | + if (nextQuestion) { |
| 116 | + const forkPath = getHealthQuestionPath( |
| 117 | + nextQuestion[0], |
| 118 | + appointment, |
| 119 | + pathPrefix |
| 120 | + ) |
107 | 121 |
|
108 | | - paths[questionPath] = { |
109 | | - [forkPath]: { |
110 | | - data: `appointment.healthAnswers.${key}.answer`, |
111 | | - value: 'No' |
| 122 | + paths[questionPath] = { |
| 123 | + [forkPath]: { |
| 124 | + data: `appointment.healthAnswers.${key}.answer`, |
| 125 | + value: 'No' |
| 126 | + } |
112 | 127 | } |
| 128 | + } else { |
| 129 | + paths[questionPath] = {} |
| 130 | + } |
| 131 | + |
| 132 | + // Add paths for conditional sub-questions |
| 133 | + for (const subKey of Object.keys(question.conditional)) { |
| 134 | + const subQuestionPath = getHealthQuestionPath( |
| 135 | + subKey, |
| 136 | + appointment, |
| 137 | + pathPrefix |
| 138 | + ) |
| 139 | + paths[subQuestionPath] = {} |
113 | 140 | } |
114 | 141 | } else { |
115 | 142 | paths[questionPath] = {} |
116 | 143 | } |
117 | | - |
118 | | - // Add paths for conditional sub-questions |
119 | | - for (const subKey of Object.keys(question.conditional)) { |
120 | | - const subQuestionPath = getHealthQuestionPath(subKey, pathPrefix) |
121 | | - paths[subQuestionPath] = {} |
122 | | - } |
123 | | - } else { |
124 | | - paths[questionPath] = {} |
125 | | - } |
126 | | - }) |
127 | | - |
128 | | - console.log(`getHealthQuestionPaths: ${paths.length} questions`) |
| 144 | + }) |
| 145 | + paths[`${pathPrefix}${appointment.uuid}/impairments`] = {} |
| 146 | + paths[`${pathPrefix}${appointment.uuid}/adjustments`] = {} |
| 147 | + } |
129 | 148 |
|
130 | 149 | return paths |
131 | 150 | } |
0 commit comments