implement delete workflow
This commit is contained in:
27
src/pages/api/approval-workflows/[id]/index.ts
Normal file
27
src/pages/api/approval-workflows/[id]/index.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||
import { ApprovalWorkflow } from "@/interfaces/approval.workflow";
|
||||
import { sessionOptions } from "@/lib/session";
|
||||
import { requestUser } from "@/utils/api";
|
||||
import { createApprovalWorkflows, deleteApprovalWorkflow, updateApprovalWorkflow } from "@/utils/approval.workflows.be";
|
||||
import { withIronSessionApiRoute } from "iron-session/next";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||
|
||||
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (req.method === "DELETE") return await del(req, res);
|
||||
}
|
||||
|
||||
async function del(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 { id } = req.query as { id?: string };
|
||||
|
||||
if (id)
|
||||
return res.status(200).json(await deleteApprovalWorkflow(id));
|
||||
}
|
||||
@@ -113,33 +113,33 @@ export default function ApprovalWorkflows({ user, workflows, workflowsAssignees,
|
||||
|
||||
useEffect(() => {
|
||||
const filters: Array<(workflow: ApprovalWorkflow) => boolean> = [];
|
||||
|
||||
|
||||
if (statusFilter && statusFilter !== undefined) {
|
||||
const statusOption = STATUS_OPTIONS.find((x) => x.value === statusFilter);
|
||||
if (statusOption && statusOption.filter) {
|
||||
filters.push(statusOption.filter);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (entityFilter && entityFilter !== undefined) {
|
||||
const entityOption = ENTITY_OPTIONS.find((x) => x.value === entityFilter);
|
||||
if (entityOption && entityOption.filter) {
|
||||
filters.push(entityOption.filter);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (nameFilter.trim() !== "") {
|
||||
const nameFilterFunction = (workflow: ApprovalWorkflow) =>
|
||||
workflow.name.toLowerCase().includes(nameFilter.toLowerCase());
|
||||
filters.push(nameFilterFunction);
|
||||
}
|
||||
|
||||
|
||||
// Apply all filters
|
||||
const filtered = workflows.filter(workflow => filters.every(filterFn => filterFn(workflow)));
|
||||
setFilteredWorkflows(filtered);
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [workflows, statusFilter, entityFilter, nameFilter]);
|
||||
|
||||
|
||||
|
||||
const handleNameFilterChange = (name: ApprovalWorkflow["name"]) => {
|
||||
setNameFilter(name);
|
||||
@@ -150,7 +150,12 @@ export default function ApprovalWorkflows({ user, workflows, workflowsAssignees,
|
||||
|
||||
axios
|
||||
.delete(`/api/approval-workflows/${id}`)
|
||||
.then(() => toast.success(`Deleted ${name} Approval Workflow.`))
|
||||
.then(() => {
|
||||
toast.success(`Successfully deleted ${name} Approval Workflow.`);
|
||||
setTimeout(() => {
|
||||
window.location.reload();
|
||||
}, 2000);
|
||||
})
|
||||
.catch((reason) => {
|
||||
if (reason.response.status === 404) {
|
||||
toast.error("Approval Workflow not found!");
|
||||
@@ -164,7 +169,6 @@ export default function ApprovalWorkflows({ user, workflows, workflowsAssignees,
|
||||
|
||||
toast.error("Something went wrong, please try again later.");
|
||||
})
|
||||
/* .finally(reload); */
|
||||
};
|
||||
|
||||
const columns = [
|
||||
|
||||
Reference in New Issue
Block a user