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(() => {
|
useEffect(() => {
|
||||||
const filters: Array<(workflow: ApprovalWorkflow) => boolean> = [];
|
const filters: Array<(workflow: ApprovalWorkflow) => boolean> = [];
|
||||||
|
|
||||||
if (statusFilter && statusFilter !== undefined) {
|
if (statusFilter && statusFilter !== undefined) {
|
||||||
const statusOption = STATUS_OPTIONS.find((x) => x.value === statusFilter);
|
const statusOption = STATUS_OPTIONS.find((x) => x.value === statusFilter);
|
||||||
if (statusOption && statusOption.filter) {
|
if (statusOption && statusOption.filter) {
|
||||||
filters.push(statusOption.filter);
|
filters.push(statusOption.filter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (entityFilter && entityFilter !== undefined) {
|
if (entityFilter && entityFilter !== undefined) {
|
||||||
const entityOption = ENTITY_OPTIONS.find((x) => x.value === entityFilter);
|
const entityOption = ENTITY_OPTIONS.find((x) => x.value === entityFilter);
|
||||||
if (entityOption && entityOption.filter) {
|
if (entityOption && entityOption.filter) {
|
||||||
filters.push(entityOption.filter);
|
filters.push(entityOption.filter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nameFilter.trim() !== "") {
|
if (nameFilter.trim() !== "") {
|
||||||
const nameFilterFunction = (workflow: ApprovalWorkflow) =>
|
const nameFilterFunction = (workflow: ApprovalWorkflow) =>
|
||||||
workflow.name.toLowerCase().includes(nameFilter.toLowerCase());
|
workflow.name.toLowerCase().includes(nameFilter.toLowerCase());
|
||||||
filters.push(nameFilterFunction);
|
filters.push(nameFilterFunction);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply all filters
|
// Apply all filters
|
||||||
const filtered = workflows.filter(workflow => filters.every(filterFn => filterFn(workflow)));
|
const filtered = workflows.filter(workflow => filters.every(filterFn => filterFn(workflow)));
|
||||||
setFilteredWorkflows(filtered);
|
setFilteredWorkflows(filtered);
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
}, [workflows, statusFilter, entityFilter, nameFilter]);
|
}, [workflows, statusFilter, entityFilter, nameFilter]);
|
||||||
|
|
||||||
|
|
||||||
const handleNameFilterChange = (name: ApprovalWorkflow["name"]) => {
|
const handleNameFilterChange = (name: ApprovalWorkflow["name"]) => {
|
||||||
setNameFilter(name);
|
setNameFilter(name);
|
||||||
@@ -150,7 +150,12 @@ export default function ApprovalWorkflows({ user, workflows, workflowsAssignees,
|
|||||||
|
|
||||||
axios
|
axios
|
||||||
.delete(`/api/approval-workflows/${id}`)
|
.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) => {
|
.catch((reason) => {
|
||||||
if (reason.response.status === 404) {
|
if (reason.response.status === 404) {
|
||||||
toast.error("Approval Workflow not found!");
|
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.");
|
toast.error("Something went wrong, please try again later.");
|
||||||
})
|
})
|
||||||
/* .finally(reload); */
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const columns = [
|
const columns = [
|
||||||
|
|||||||
@@ -15,14 +15,18 @@ export const getApprovalWorkflow = async (id: string) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const createApprovalWorkflow = async (workflow: ApprovalWorkflow) => {
|
export const createApprovalWorkflow = async (workflow: ApprovalWorkflow) => {
|
||||||
await db.collection("approval-workflows").insertOne(workflow);
|
return await db.collection("approval-workflows").insertOne(workflow);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const createApprovalWorkflows = async (workflows: ApprovalWorkflow[]) => {
|
export const createApprovalWorkflows = async (workflows: ApprovalWorkflow[]) => {
|
||||||
if (workflows.length === 0) return;
|
if (workflows.length === 0) return;
|
||||||
await db.collection<ApprovalWorkflow>("approval-workflows").insertMany(workflows);
|
return await db.collection<ApprovalWorkflow>("approval-workflows").insertMany(workflows);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const updateApprovalWorkflow = async (id: string, workflow: ApprovalWorkflow) => {
|
export const updateApprovalWorkflow = async (id: string, workflow: ApprovalWorkflow) => {
|
||||||
await db.collection("approval-workflows").replaceOne({ id }, workflow);
|
return await db.collection("approval-workflows").replaceOne({ id }, workflow);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const deleteApprovalWorkflow = async (id: string) => {
|
||||||
|
return await db.collection("approval-workflows").deleteOne({ id });
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user