- Fix bug where workflows were being created again after exam update
- Moved createWorkflows function into an helper file instead of a post request. - Moved the workflow creation logic into the post of exam creation instead of a seperate post in each exam module
This commit is contained in:
@@ -1,24 +1,14 @@
|
||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||
import { Module } from "@/interfaces";
|
||||
import { sessionOptions } from "@/lib/session";
|
||||
import { requestUser } from "@/utils/api";
|
||||
import { createApprovalWorkflow, getApprovalWorkflowByFormIntaker, getApprovalWorkflows } from "@/utils/approval.workflows.be";
|
||||
import { getApprovalWorkflows } from "@/utils/approval.workflows.be";
|
||||
import { withIronSessionApiRoute } from "iron-session/next";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||
|
||||
interface PostRequestBody {
|
||||
examAuthor: string;
|
||||
examEntities: string[];
|
||||
examId: string;
|
||||
examName: string;
|
||||
examModule: Module;
|
||||
}
|
||||
|
||||
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (req.method === "GET") return await get(req, res);
|
||||
if (req.method === "POST") return await post(req, res);
|
||||
}
|
||||
|
||||
async function get(req: NextApiRequest, res: NextApiResponse) {
|
||||
@@ -30,49 +20,4 @@ async function get(req: NextApiRequest, res: NextApiResponse) {
|
||||
}
|
||||
|
||||
return res.status(200).json(await getApprovalWorkflows("active-workflows"));
|
||||
}
|
||||
|
||||
async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
const user = await requestUser(req, res);
|
||||
if (!user) return res.status(401).json({ ok: false });
|
||||
|
||||
if (!["admin", "developer", "corporate", "mastercorporate"].includes(user.type)) {
|
||||
return res.status(403).json({ ok: false });
|
||||
}
|
||||
|
||||
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." };
|
||||
}
|
||||
|
||||
configuredWorkflow.modules.push(examModule);
|
||||
configuredWorkflow.name = `${examId}`;
|
||||
configuredWorkflow.examId = examId;
|
||||
configuredWorkflow.entityId = entity;
|
||||
configuredWorkflow.startDate = Date.now();
|
||||
|
||||
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