// The rules the Automated Tasks page applies, kept pure so they can be tested without the page. import type { Extension, Questionnaire } from 'fhir/r4'; import { AUTO_ASSIGN_TRIGGER_URL, type AutoAssignTrigger } from '../../../config/autoAssign'; // Both ActivityDefinition and Questionnaire carry the trigger as one extension. Forms settings reads and // writes the same one, so a change here shows there. export const getTrigger = (resource: { extension?: Extension[] }): AutoAssignTrigger => { const code = resource.extension?.find((ext) => ext.url === AUTO_ASSIGN_TRIGGER_URL)?.valueCode; return code === 'all-appointments' || code === 'new-patient-appointments' ? code : 'off'; }; /** The line under the trigger choice, in the deck words. */ export const triggerHelp = (trigger: AutoAssignTrigger | ''): string => { switch (trigger) { case 'all-appointments': return 'Assigned when any appointment is booked.'; case 'new-patient-appointments': return 'Assigned when a new patient appointment is booked.'; case 'off': return 'Off keeps the task saved without assigning it automatically.'; default: return 'Choose when this questionnaire should be assigned. Nothing is selected automatically.'; } }; /** The Assigned when column of a row. Off stays visible rather than hiding the task. */ export const assignedWhen = (trigger: AutoAssignTrigger): string => { switch (trigger) { case 'all-appointments': return 'Any appointment is booked'; case 'new-patient-appointments': return 'New patient appointment is booked'; default: return 'Off'; } }; export type QuestionnaireEligibility = 'eligible' | 'draft' | 'public' | 'assigned' | 'unchecked'; /** * Only an active clinical questionnaire with no rule may become an automated task, and none while * the public forms are unread, so a public form cannot slip through. */ export const questionnaireEligibility = ( questionnaire: Questionnaire, publicQuestionnaireIds: ReadonlySet | null, ): QuestionnaireEligibility => { if (publicQuestionnaireIds === null) return 'unchecked'; if (questionnaire.id && publicQuestionnaireIds.has(questionnaire.id)) return 'public'; if (getTrigger(questionnaire) !== 'off') return 'assigned'; if (questionnaire.status !== 'active') return 'draft'; return 'eligible'; };