From 637695ee8a0d7a0df7e70dc81642a9b8bfe2b2f1 Mon Sep 17 00:00:00 2001 From: Olim Saidov Date: Thu, 30 Jan 2025 11:00:37 +0500 Subject: [PATCH] Allow amending and check questionnaire usage before delete --- .../app/(authorized)/questionnaires/page.tsx | 36 +++++++++++++++++++ .../src/components/forms-renderer.tsx | 1 + .../src/components/questionnaires-actions.tsx | 16 ++++++--- .../src/lib/server/aidbox.ts | 3 ++ aidbox-forms-smart-launch-2/types.d.ts | 1 + 5 files changed, 52 insertions(+), 5 deletions(-) diff --git a/aidbox-forms-smart-launch-2/src/app/(authorized)/questionnaires/page.tsx b/aidbox-forms-smart-launch-2/src/app/(authorized)/questionnaires/page.tsx index 509d4be..b97b1f9 100644 --- a/aidbox-forms-smart-launch-2/src/app/(authorized)/questionnaires/page.tsx +++ b/aidbox-forms-smart-launch-2/src/app/(authorized)/questionnaires/page.tsx @@ -61,6 +61,41 @@ export default async function QuestionnairesPage({ searchParams }: PageProps) { const total = response.total || 0; const totalPages = Math.ceil(total / pageSize); + async function checkQuestionnaireUsage(questionnaire: Questionnaire) { + "use server"; + + let total = 0; + const aidbox = await getCurrentAidbox(); + + if (questionnaire.url) { + const response = await aidbox + .get("fhir/QuestionnaireResponse", { + searchParams: { + _summary: 'count', + questionnaire: questionnaire.url, + }, + }) + .json>(); + + total += response.total || 0; + + if (questionnaire.version && total === 0) { + const response = await aidbox + .get("fhir/QuestionnaireResponse", { + searchParams: { + _summary: 'count', + questionnaire: `${questionnaire.url}|${questionnaire.version}`, + }, + }) + .json>(); + + total += response.total || 0; + } + } + + return total !== 0; + } + async function deleteQuestionnaire(questionnaire: Questionnaire) { "use server"; @@ -183,6 +218,7 @@ export default async function QuestionnairesPage({ searchParams }: PageProps) { diff --git a/aidbox-forms-smart-launch-2/src/components/forms-renderer.tsx b/aidbox-forms-smart-launch-2/src/components/forms-renderer.tsx index bcb28ae..f4d5698 100644 --- a/aidbox-forms-smart-launch-2/src/components/forms-renderer.tsx +++ b/aidbox-forms-smart-launch-2/src/components/forms-renderer.tsx @@ -64,6 +64,7 @@ export function FormsRenderer({ ref={ref} questionnaire={JSON.stringify(questionnaire)} questionnaire-response={JSON.stringify(questionnaireResponse)} + config={JSON.stringify({ form: { "allow-amend": true } })} style={{ width: "100%", height: "100%", diff --git a/aidbox-forms-smart-launch-2/src/components/questionnaires-actions.tsx b/aidbox-forms-smart-launch-2/src/components/questionnaires-actions.tsx index 49ee4c2..d4a871c 100644 --- a/aidbox-forms-smart-launch-2/src/components/questionnaires-actions.tsx +++ b/aidbox-forms-smart-launch-2/src/components/questionnaires-actions.tsx @@ -36,12 +36,14 @@ import { useRouter } from "next/navigation"; export function QuestionnairesActions({ questionnaire, library, + onCheckQuestionnaireUsageAction, onDeleteAction, onImportAction, onCreateResponseAction, }: { questionnaire: Questionnaire; library?: boolean; + onCheckQuestionnaireUsageAction?: (questionnaire: Questionnaire) => Promise; onDeleteAction?: (questionnaire: Questionnaire) => Promise; onImportAction?: (questionnaire: Questionnaire) => Promise; onCreateResponseAction?: ( @@ -105,12 +107,16 @@ export function QuestionnairesActions({ className="text-destructive focus:text-destructive" onClick={async () => { if (onDeleteAction) { - await withRunning(onDeleteAction(questionnaire)); + const used = onCheckQuestionnaireUsageAction ? await onCheckQuestionnaireUsageAction(questionnaire) : false; - toast({ - title: "Questionnaire deleted", - description: `Questionnaire deleted successfully`, - }); + if (confirm(`Are you sure you want to delete this questionnaire? ${used ? `\n\nWarning: This questionnaire is used in responses. Deleting it will make them invalid.` : ""}`)) { + await withRunning(onDeleteAction(questionnaire)); + + toast({ + title: "Questionnaire deleted", + description: `Questionnaire deleted successfully`, + }); + } } }} > diff --git a/aidbox-forms-smart-launch-2/src/lib/server/aidbox.ts b/aidbox-forms-smart-launch-2/src/lib/server/aidbox.ts index c4ce406..da67dc5 100644 --- a/aidbox-forms-smart-launch-2/src/lib/server/aidbox.ts +++ b/aidbox-forms-smart-launch-2/src/lib/server/aidbox.ts @@ -64,10 +64,13 @@ export const getOrganizationalAidbox = cache(async (serverUrl: string) => { "[aidbox]", response.status, response.headers.get("content-type"), + ); + console.dir( await response .clone() .json() .catch(() => null), + { depth: 1000 }, ); }, ], diff --git a/aidbox-forms-smart-launch-2/types.d.ts b/aidbox-forms-smart-launch-2/types.d.ts index 8598b36..2c3e1fc 100644 --- a/aidbox-forms-smart-launch-2/types.d.ts +++ b/aidbox-forms-smart-launch-2/types.d.ts @@ -16,6 +16,7 @@ declare global { > & { questionnaire?: string; "questionnaire-response"?: string; + config?: string; }; "aidbox-form-builder": React.DetailedHTMLProps< React.IframeHTMLAttributes,