38 lines
1.5 KiB
TypeScript
38 lines
1.5 KiB
TypeScript
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
|
import { ApprovalWorkflow } from "@/interfaces/approval.workflow";
|
|
import { Entity } from "@/interfaces/entity";
|
|
import { sessionOptions } from "@/lib/session";
|
|
import { requestUser } from "@/utils/api";
|
|
import { replaceApprovalWorkflowsByEntities } from "@/utils/approval.workflows.be";
|
|
import { withIronSessionApiRoute } from "iron-session/next";
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
export default withIronSessionApiRoute(handler, sessionOptions);
|
|
|
|
interface ReplaceApprovalWorkflowsRequest {
|
|
filteredWorkflows: ApprovalWorkflow[];
|
|
userEntitiesWithLabel: Entity[];
|
|
}
|
|
|
|
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
if (req.method === "POST") return await post(req, res);
|
|
}
|
|
|
|
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", "teacher", "corporate", "mastercorporate"].includes(user.type)) {
|
|
return res.status(403).json({ ok: false });
|
|
}
|
|
|
|
const { filteredWorkflows, userEntitiesWithLabel } = req.body as ReplaceApprovalWorkflowsRequest;
|
|
|
|
const configuredWorkflows: ApprovalWorkflow[] = filteredWorkflows;
|
|
const entitiesIds: string[] = userEntitiesWithLabel.map((e) => e.id);
|
|
|
|
await replaceApprovalWorkflowsByEntities(configuredWorkflows, entitiesIds);
|
|
|
|
return res.status(204).end();
|
|
}
|