implement initialization of approval workflows on exam creation.
This commit is contained in:
@@ -1,14 +1,23 @@
|
||||
// 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 { getApprovalWorkflows } from "@/utils/approval.workflows.be";
|
||||
import { createApprovalWorkflow, getApprovalWorkflowByFormIntaker, 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,
|
||||
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) {
|
||||
@@ -19,5 +28,33 @@ async function get(req: NextApiRequest, res: NextApiResponse) {
|
||||
return res.status(403).json({ ok: false });
|
||||
}
|
||||
|
||||
return res.status(200).json(await getApprovalWorkflows("configured-workflows"));
|
||||
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, examId, examModule } = req.body as PostRequestBody;
|
||||
|
||||
if (examAuthor) {
|
||||
const configuredWorkflow = await getApprovalWorkflowByFormIntaker(examAuthor);
|
||||
if(configuredWorkflow) {
|
||||
configuredWorkflow.modules.push(examModule);
|
||||
configuredWorkflow.name = `${examId}`;
|
||||
configuredWorkflow.examId = examId;
|
||||
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.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user