instanciate all workflows configured for an exam author based on different entities.
This commit is contained in:
@@ -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 });
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user