From 6692c201e427e68a937128a8674a8bc3efb7ff52 Mon Sep 17 00:00:00 2001 From: Joao Correia Date: Wed, 5 Feb 2025 12:37:53 +0000 Subject: [PATCH] instanciate all workflows configured for an exam author based on different entities. --- .../ExamEditor/SettingsEditor/level.tsx | 30 +++++++----- .../SettingsEditor/listening/index.tsx | 30 +++++++----- .../SettingsEditor/reading/index.tsx | 20 +++++--- .../SettingsEditor/speaking/index.tsx | 30 +++++++----- .../SettingsEditor/writing/index.tsx | 26 ++++++---- src/pages/api/approval-workflows/index.ts | 48 +++++++++++++------ src/pages/approval-workflows/create.tsx | 3 +- src/pages/approval-workflows/index.tsx | 5 +- src/utils/approval.workflows.be.ts | 4 +- 9 files changed, 129 insertions(+), 67 deletions(-) diff --git a/src/components/ExamEditor/SettingsEditor/level.tsx b/src/components/ExamEditor/SettingsEditor/level.tsx index e55e6c15..20c8fd81 100644 --- a/src/components/ExamEditor/SettingsEditor/level.tsx +++ b/src/components/ExamEditor/SettingsEditor/level.tsx @@ -215,24 +215,32 @@ const LevelSettings: React.FC = () => { }); const requestBody = await (async () => { - const handledExam = await getExamById("level", result.data.id); - + const handledExam = await getExamById("writing", result.data.id); return { examAuthor: handledExam?.createdBy ?? "Unknown Author", + examEntities: handledExam?.entities ?? [], examId: handledExam?.id ?? "Unknown ID", - examModule: "level" + examModule: "writing" }; })(); - await axios.post(`/api/approval-workflows`, requestBody) - .then(() => { - toast.success(`Approval Workflow for exam has been created`); + await axios + .post(`/api/approval-workflows`, requestBody) + .then((response) => { + if (response.status === 200) { + toast.success(`Approval Workflows for exam have been successfully created`); + } else if (response.status === 207) { + toast.warning( + `Approval Workflows were partially created. Exam author might not have a configured workflow for all its entities.` + ); + } }) .catch((reason) => { - if (reason.response.status === 404) { - toast.error("No configured workflow found for examAuthor."); - } - else { - toast.error("Something went wrong while creating approval workflow, please try again later."); + if (reason.response?.status === 404) { + toast.error("No configured workflow found for examAuthor for any of its entities."); + } else { + toast.error( + "Something went wrong while creating approval workflow, please try again later." + ); } }); diff --git a/src/components/ExamEditor/SettingsEditor/listening/index.tsx b/src/components/ExamEditor/SettingsEditor/listening/index.tsx index 489d9c3d..efbdfb6d 100644 --- a/src/components/ExamEditor/SettingsEditor/listening/index.tsx +++ b/src/components/ExamEditor/SettingsEditor/listening/index.tsx @@ -153,24 +153,32 @@ const ListeningSettings: React.FC = () => { toast.success(`Submitted Exam ID: ${result.data.id}`); const requestBody = await (async () => { - const handledExam = await getExamById("listening", result.data.id); - + const handledExam = await getExamById("writing", result.data.id); return { examAuthor: handledExam?.createdBy ?? "Unknown Author", + examEntities: handledExam?.entities ?? [], examId: handledExam?.id ?? "Unknown ID", - examModule: "listening" + examModule: "writing" }; })(); - await axios.post(`/api/approval-workflows`, requestBody) - .then(() => { - toast.success(`Approval Workflow for exam has been created`); + await axios + .post(`/api/approval-workflows`, requestBody) + .then((response) => { + if (response.status === 200) { + toast.success(`Approval Workflows for exam have been successfully created`); + } else if (response.status === 207) { + toast.warning( + `Approval Workflows were partially created. Exam author might not have a configured workflow for all its entities.` + ); + } }) .catch((reason) => { - if (reason.response.status === 404) { - toast.error("No configured workflow found for examAuthor."); - } - else { - toast.error("Something went wrong while creating approval workflow, please try again later."); + if (reason.response?.status === 404) { + toast.error("No configured workflow found for examAuthor for any of its entities."); + } else { + toast.error( + "Something went wrong while creating approval workflow, please try again later." + ); } }); diff --git a/src/components/ExamEditor/SettingsEditor/reading/index.tsx b/src/components/ExamEditor/SettingsEditor/reading/index.tsx index 0597dea3..4a16ff12 100644 --- a/src/components/ExamEditor/SettingsEditor/reading/index.tsx +++ b/src/components/ExamEditor/SettingsEditor/reading/index.tsx @@ -90,27 +90,35 @@ const ReadingSettings: React.FC = () => { .then((result) => { playSound("sent"); toast.success(`Submitted Exam ID: ${result.data.id}`); - return getExamById("reading", result.data.id); }) .then((handledExam) => { const requestBody = { examAuthor: handledExam?.createdBy ?? "Unknown Author", + examEntities: handledExam?.entities ?? [], examId: handledExam?.id ?? "Unknown ID", examModule: "reading" }; return axios.post(`/api/approval-workflows`, requestBody); }) - .then(() => { - toast.success(`Approval Workflow for exam has been created`); + .then((response) => { + if (response.status === 200) { + toast.success(`Approval Workflows for exam have been successfully created`); + } else if (response.status === 207) { + toast.warning( + `Approval Workflows were partially created. Exam author might not have a configured workflow for all its entities.` + ); + } }) .catch((error) => { if (error.response && error.response.status === 404) { - toast.error("No configured workflow found for examAuthor."); + toast.error("No configured workflow found for examAuthor for any of its entities."); } else { - // This error could come from either of the requests - toast.error(error.response?.data?.error || "Something went wrong, please try again later."); + toast.error( + error.response?.data?.error || + "Something went wrong, please try again later." + ); } }); } diff --git a/src/components/ExamEditor/SettingsEditor/speaking/index.tsx b/src/components/ExamEditor/SettingsEditor/speaking/index.tsx index d187e2e4..0b9570b8 100644 --- a/src/components/ExamEditor/SettingsEditor/speaking/index.tsx +++ b/src/components/ExamEditor/SettingsEditor/speaking/index.tsx @@ -197,24 +197,32 @@ const SpeakingSettings: React.FC = () => { }); const requestBody = await (async () => { - const handledExam = await getExamById("speaking", result.data.id); - + const handledExam = await getExamById("writing", result.data.id); return { examAuthor: handledExam?.createdBy ?? "Unknown Author", + examEntities: handledExam?.entities ?? [], examId: handledExam?.id ?? "Unknown ID", - examModule: "speaking" + examModule: "writing" }; })(); - await axios.post(`/api/approval-workflows`, requestBody) - .then(() => { - toast.success(`Approval Workflow for exam has been created`); + await axios + .post(`/api/approval-workflows`, requestBody) + .then((response) => { + if (response.status === 200) { + toast.success(`Approval Workflows for exam have been successfully created`); + } else if (response.status === 207) { + toast.warning( + `Approval Workflows were partially created. Exam author might not have a configured workflow for all its entities.` + ); + } }) .catch((reason) => { - if (reason.response.status === 404) { - toast.error("No configured workflow found for examAuthor."); - } - else { - toast.error("Something went wrong while creating approval workflow, please try again later."); + if (reason.response?.status === 404) { + toast.error("No configured workflow found for examAuthor for any of its entities."); + } else { + toast.error( + "Something went wrong while creating approval workflow, please try again later." + ); } }); diff --git a/src/components/ExamEditor/SettingsEditor/writing/index.tsx b/src/components/ExamEditor/SettingsEditor/writing/index.tsx index 2147f8ec..d0fd0bbf 100644 --- a/src/components/ExamEditor/SettingsEditor/writing/index.tsx +++ b/src/components/ExamEditor/SettingsEditor/writing/index.tsx @@ -144,23 +144,31 @@ const WritingSettings: React.FC = () => { const requestBody = await (async () => { const handledExam = await getExamById("writing", result.data.id); - return { examAuthor: handledExam?.createdBy ?? "Unknown Author", + examEntities: handledExam?.entities ?? [], examId: handledExam?.id ?? "Unknown ID", examModule: "writing" }; })(); - await axios.post(`/api/approval-workflows`, requestBody) - .then(() => { - toast.success(`Approval Workflow for exam has been created`); + await axios + .post(`/api/approval-workflows`, requestBody) + .then((response) => { + if (response.status === 200) { + toast.success(`Approval Workflows for exam have been successfully created`); + } else if (response.status === 207) { + toast.warning( + `Approval Workflows were partially created. Exam author might not have a configured workflow for all its entities.` + ); + } }) .catch((reason) => { - if (reason.response.status === 404) { - toast.error("No configured workflow found for examAuthor."); - } - else { - toast.error("Something went wrong while creating approval workflow, please try again later."); + if (reason.response?.status === 404) { + toast.error("No configured workflow found for examAuthor for any of its entities."); + } else { + toast.error( + "Something went wrong while creating approval workflow, please try again later." + ); } }); diff --git a/src/pages/api/approval-workflows/index.ts b/src/pages/api/approval-workflows/index.ts index 9cffb13d..26636186 100644 --- a/src/pages/api/approval-workflows/index.ts +++ b/src/pages/api/approval-workflows/index.ts @@ -9,10 +9,11 @@ import type { NextApiRequest, NextApiResponse } from "next"; export default withIronSessionApiRoute(handler, sessionOptions); interface PostRequestBody { - examAuthor: string, - examId: string, - examName: string, - examModule: Module, + examAuthor: string; + examEntities: string[]; + examId: string; + examName: string; + examModule: Module; } async function handler(req: NextApiRequest, res: NextApiResponse) { @@ -39,22 +40,39 @@ async function post(req: NextApiRequest, res: NextApiResponse) { return res.status(403).json({ ok: false }); } - const { examAuthor, examId, examModule } = req.body as PostRequestBody; + const { examAuthor, examEntities, examId, examModule } = req.body as PostRequestBody; + + const results = await Promise.all( + examEntities.map(async (entity) => { + const configuredWorkflow = await getApprovalWorkflowByFormIntaker(entity, examAuthor); + if (!configuredWorkflow) { + return { entity, created: false, error: "No configured workflow found for examAuthor." }; + } - if (examAuthor) { - const configuredWorkflow = await getApprovalWorkflowByFormIntaker(examAuthor); - if(configuredWorkflow) { configuredWorkflow.modules.push(examModule); configuredWorkflow.name = `${examId}`; configuredWorkflow.examId = examId; + configuredWorkflow.entityId = entity; configuredWorkflow.startDate = Date.now(); - - return res.status(201).json(await createApprovalWorkflow("active-workflows", configuredWorkflow)); - } else { - return res.status(404).json("No configured workflow found for examAuthor."); - } + try { + const creationResponse = await createApprovalWorkflow("active-workflows", configuredWorkflow); + return { entity, created: true, creationResponse }; + } catch (error) { + const err = error as Error; + return { entity, created: false, error: err.message }; + } + }) + ); + + const successCount = results.filter((r) => r.created).length; + const totalCount = examEntities.length; + + if (successCount === totalCount) { + return res.status(200).json({ ok: true, results }); + } else if (successCount > 0) { + return res.status(207).json({ ok: true, results }); + } else { + return res.status(404).json({ ok: false, message: "No workflows were created", results }); } - - } diff --git a/src/pages/approval-workflows/create.tsx b/src/pages/approval-workflows/create.tsx index cf12d216..49bdae1b 100644 --- a/src/pages/approval-workflows/create.tsx +++ b/src/pages/approval-workflows/create.tsx @@ -256,8 +256,9 @@ export default function Home({ user, allConfiguredWorkflows, userEntitiesWithLab -
+ +