61 lines
2.2 KiB
TypeScript
61 lines
2.2 KiB
TypeScript
// 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 { 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) {
|
|
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 });
|
|
}
|
|
|
|
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.");
|
|
}
|
|
|
|
}
|
|
|
|
|
|
}
|