Skip to content

Commit 99dc341

Browse files
authored
Merge pull request #1027 from joshunrau/phq9
2 parents 6f287e7 + 802ae08 commit 99dc341

File tree

1 file changed

+39
-25
lines changed
  • packages/instrument-library/src/forms/patient-health-questionnaire-9

1 file changed

+39
-25
lines changed

packages/instrument-library/src/forms/patient-health-questionnaire-9/index.ts

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
/* eslint-disable perfectionist/sort-objects */
22

33
import { defineInstrument } from '/runtime/v1/@opendatacapture/runtime-core';
4-
import { sum } from '/runtime/v1/[email protected]';
4+
import { omit, sum } from '/runtime/v1/[email protected]';
55
import { z } from '/runtime/v1/[email protected]';
66

7+
const $Response = z.number().int().min(0).max(3);
8+
79
export default defineInstrument({
810
kind: 'FORM',
911
language: ['en', 'fr'],
1012
tags: {
11-
en: ['Health'],
12-
fr: ['Santé']
13+
en: ['Health', 'Depression'],
14+
fr: ['Santé', 'Dépression']
1315
},
1416
internal: {
1517
edition: 1,
@@ -90,16 +92,16 @@ export default defineInstrument({
9092
},
9193
options: {
9294
en: {
93-
0: 'Not at all',
94-
1: 'Several days',
95-
2: 'More than half of days',
95+
0: 'Not at All',
96+
1: 'Several Days',
97+
2: 'More than half the days',
9698
3: 'Nearly every day'
9799
},
98100
fr: {
99-
0: 'Not at all',
100-
1: 'Several days',
101-
2: 'More than half of days',
102-
3: 'Nearly every day'
101+
0: 'Jamais',
102+
1: 'Plusieurs jours',
103+
2: 'Plus de la moitié des jours',
104+
3: 'Presque tous les jours'
103105
}
104106
},
105107
variant: 'likert'
@@ -216,21 +218,33 @@ export default defineInstrument({
216218
totalScore: {
217219
kind: 'computed',
218220
label: { en: 'Total Score', fr: 'Score total' },
219-
value: ({ questions }) => sum(Object.values(questions))
221+
value: ({ questions }) => sum(Object.values(omit(questions, 'impactOnFunctioning')))
220222
}
221223
},
222-
validationSchema: z.object({
223-
questions: z.object({
224-
interestPleasure: z.number().int().min(0).max(3),
225-
feelingDown: z.number().int().min(0).max(3),
226-
sleepIssues: z.number().int().min(0).max(3),
227-
energyLevel: z.number().int().min(0).max(3),
228-
appetiteChanges: z.number().int().min(0).max(3),
229-
selfWorth: z.number().int().min(0).max(3),
230-
concentrationIssues: z.number().int().min(0).max(3),
231-
psychomotorChanges: z.number().int().min(0).max(3),
232-
suicidalThoughts: z.number().int().min(0).max(3)
233-
}),
234-
impactOnFunctioning: z.number().int().min(0).max(3).optional()
235-
})
224+
validationSchema: z
225+
.object({
226+
questions: z.object({
227+
interestPleasure: $Response,
228+
feelingDown: $Response,
229+
sleepIssues: $Response,
230+
energyLevel: $Response,
231+
appetiteChanges: $Response,
232+
selfWorth: $Response,
233+
concentrationIssues: $Response,
234+
psychomotorChanges: $Response,
235+
suicidalThoughts: $Response
236+
}),
237+
impactOnFunctioning: $Response.optional()
238+
})
239+
.superRefine(({ impactOnFunctioning, questions }, ctx) => {
240+
const isAnyNonZero = sum(Object.values(questions)) > 0;
241+
// If any response is not zero, then impactOnFunctioning is required
242+
if (isAnyNonZero && impactOnFunctioning === undefined) {
243+
ctx.addIssue({
244+
code: 'custom',
245+
message: 'This question is required / Cette question est obligatoire',
246+
path: ['impactOnFunctioning']
247+
});
248+
}
249+
})
236250
});

0 commit comments

Comments
 (0)