instanciate all workflows configured for an exam author based on different entities.

This commit is contained in:
Joao Correia
2025-02-05 12:37:53 +00:00
parent f4c7961caa
commit 6692c201e4
9 changed files with 129 additions and 67 deletions

View File

@@ -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 });
}
}