implement initialization of approval workflows on exam creation.
This commit is contained in:
@@ -25,7 +25,7 @@ async function del(req: NextApiRequest, res: NextApiResponse) {
|
||||
|
||||
const { id } = req.query as { id?: string };
|
||||
|
||||
if (id) return res.status(200).json(await deleteApprovalWorkflow("configured-workflows", id));
|
||||
if (id) return res.status(200).json(await deleteApprovalWorkflow("active-workflows", id));
|
||||
}
|
||||
|
||||
async function put(req: NextApiRequest, res: NextApiResponse) {
|
||||
@@ -41,7 +41,7 @@ async function put(req: NextApiRequest, res: NextApiResponse) {
|
||||
|
||||
if (id && workflow) {
|
||||
workflow._id = new ObjectId(id);
|
||||
await updateApprovalWorkflow("configured-workflows", workflow);
|
||||
await updateApprovalWorkflow("active-workflows", workflow);
|
||||
return res.status(204).end();
|
||||
}
|
||||
}
|
||||
@@ -57,6 +57,6 @@ async function get(req: NextApiRequest, res: NextApiResponse) {
|
||||
const { id } = req.query as { id?: string };
|
||||
|
||||
if (id) {
|
||||
return res.status(200).json(await getApprovalWorkflow("configured-workflows", id));
|
||||
return res.status(200).json(await getApprovalWorkflow("active-workflows", id));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,5 +33,5 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
|
||||
await replaceApprovalWorkflowsByEntities(configuredWorkflows, entitiesIds);
|
||||
|
||||
return res.status(201).json({ ok: true });
|
||||
return res.status(204).end();
|
||||
}
|
||||
|
||||
@@ -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.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ export const getServerSideProps = withIronSessionSsr(async ({ req, res, params }
|
||||
|
||||
const { id } = params as { id: string };
|
||||
|
||||
const workflow: ApprovalWorkflow | null = await getApprovalWorkflow("configured-workflows", id);
|
||||
const workflow: ApprovalWorkflow | null = await getApprovalWorkflow("active-workflows", id);
|
||||
|
||||
if (!workflow)
|
||||
return redirect("/approval-workflows")
|
||||
@@ -161,7 +161,11 @@ export default function Home({ user, initialWorkflow, id, workflowAssignees, wor
|
||||
return;
|
||||
})
|
||||
|
||||
handleStepClick(selectedStepIndex + 1, currentWorkflow.steps[selectedStepIndex + 1]);
|
||||
if (selectedStepIndex + 1 < currentWorkflow.steps.length){
|
||||
handleStepClick(selectedStepIndex + 1, currentWorkflow.steps[selectedStepIndex + 1]);
|
||||
} else {
|
||||
setIsPanelOpen(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleRejectStep = () => {
|
||||
|
||||
@@ -20,7 +20,6 @@ import clsx from "clsx";
|
||||
import { withIronSessionSsr } from "iron-session/next";
|
||||
import Head from "next/head";
|
||||
import Link from "next/link";
|
||||
import { useRouter } from "next/router";
|
||||
import { useEffect, useState } from "react";
|
||||
import { BsTrash } from "react-icons/bs";
|
||||
import { FaRegEdit } from "react-icons/fa";
|
||||
@@ -98,6 +97,8 @@ export default function ApprovalWorkflows({ user, initialWorkflows, workflowsAss
|
||||
const {workflows, reload} = useApprovalWorkflows();
|
||||
const currentWorkflows = workflows || initialWorkflows;
|
||||
|
||||
console.log(currentWorkflows);
|
||||
|
||||
const [filteredWorkflows, setFilteredWorkflows] = useState<ApprovalWorkflow[]>([]);
|
||||
|
||||
const [statusFilter, setStatusFilter] = useState<CustomStatus>(undefined);
|
||||
|
||||
Reference in New Issue
Block a user