refactor workflows api
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
import { ApprovalWorkflow } from "@/interfaces/approval.workflow";
|
import { ApprovalWorkflow } from "@/interfaces/approval.workflow";
|
||||||
import { sessionOptions } from "@/lib/session";
|
import { sessionOptions } from "@/lib/session";
|
||||||
import { requestUser } from "@/utils/api";
|
import { requestUser } from "@/utils/api";
|
||||||
import { createConfiguredWorkflow } from "@/utils/approval.workflows.be";
|
import { createApprovalWorkflow } from "@/utils/approval.workflows.be";
|
||||||
import { withIronSessionApiRoute } from "iron-session/next";
|
import { withIronSessionApiRoute } from "iron-session/next";
|
||||||
import type { NextApiRequest, NextApiResponse } from "next";
|
import type { NextApiRequest, NextApiResponse } from "next";
|
||||||
|
|
||||||
@@ -23,5 +23,5 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
const approvalWorkflow: ApprovalWorkflow = req.body;
|
const approvalWorkflow: ApprovalWorkflow = req.body;
|
||||||
|
|
||||||
if (approvalWorkflow)
|
if (approvalWorkflow)
|
||||||
return res.status(201).json(await createConfiguredWorkflow(approvalWorkflow));
|
return res.status(201).json(await createApprovalWorkflow("configured-workflows", approvalWorkflow));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
import { ApprovalWorkflow } from "@/interfaces/approval.workflow";
|
import { ApprovalWorkflow } from "@/interfaces/approval.workflow";
|
||||||
import { sessionOptions } from "@/lib/session";
|
import { sessionOptions } from "@/lib/session";
|
||||||
import { requestUser } from "@/utils/api";
|
import { requestUser } from "@/utils/api";
|
||||||
import { updateConfiguredWorkflow } from "@/utils/approval.workflows.be";
|
import { updateApprovalWorkflow } from "@/utils/approval.workflows.be";
|
||||||
import { withIronSessionApiRoute } from "iron-session/next";
|
import { withIronSessionApiRoute } from "iron-session/next";
|
||||||
import { ObjectId } from "mongodb";
|
import { ObjectId } from "mongodb";
|
||||||
import type { NextApiRequest, NextApiResponse } from "next";
|
import type { NextApiRequest, NextApiResponse } from "next";
|
||||||
@@ -26,7 +26,7 @@ async function put(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
|
|
||||||
if (id && approvalWorkflow) {
|
if (id && approvalWorkflow) {
|
||||||
approvalWorkflow._id = new ObjectId(id);
|
approvalWorkflow._id = new ObjectId(id);
|
||||||
await updateConfiguredWorkflow(approvalWorkflow);
|
await updateApprovalWorkflow("configured-workflows", approvalWorkflow);
|
||||||
return res.status(204).end();
|
return res.status(204).end();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
import { ApprovalWorkflow } from "@/interfaces/approval.workflow";
|
import { ApprovalWorkflow } from "@/interfaces/approval.workflow";
|
||||||
import { sessionOptions } from "@/lib/session";
|
import { sessionOptions } from "@/lib/session";
|
||||||
import { requestUser } from "@/utils/api";
|
import { requestUser } from "@/utils/api";
|
||||||
import { deleteConfiguredWorkflow, updateConfiguredWorkflow } from "@/utils/approval.workflows.be";
|
import { deleteApprovalWorkflow, updateApprovalWorkflow } from "@/utils/approval.workflows.be";
|
||||||
import { withIronSessionApiRoute } from "iron-session/next";
|
import { withIronSessionApiRoute } from "iron-session/next";
|
||||||
import { ObjectId } from "mongodb";
|
import { ObjectId } from "mongodb";
|
||||||
import type { NextApiRequest, NextApiResponse } from "next";
|
import type { NextApiRequest, NextApiResponse } from "next";
|
||||||
@@ -24,7 +24,7 @@ async function del(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
|
|
||||||
const { id } = req.query as { id?: string };
|
const { id } = req.query as { id?: string };
|
||||||
|
|
||||||
if (id) return res.status(200).json(await deleteConfiguredWorkflow(id));
|
if (id) return res.status(200).json(await deleteApprovalWorkflow("configured-workflows", id));
|
||||||
}
|
}
|
||||||
|
|
||||||
async function put(req: NextApiRequest, res: NextApiResponse) {
|
async function put(req: NextApiRequest, res: NextApiResponse) {
|
||||||
@@ -40,7 +40,7 @@ async function put(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
|
|
||||||
if (id && workflow) {
|
if (id && workflow) {
|
||||||
workflow._id = new ObjectId(id);
|
workflow._id = new ObjectId(id);
|
||||||
await updateConfiguredWorkflow(workflow);
|
await updateApprovalWorkflow("configured-workflows", workflow);
|
||||||
return res.status(204).end();
|
return res.status(204).end();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { ApprovalWorkflow } from "@/interfaces/approval.workflow";
|
|||||||
import { Entity } from "@/interfaces/entity";
|
import { Entity } from "@/interfaces/entity";
|
||||||
import { sessionOptions } from "@/lib/session";
|
import { sessionOptions } from "@/lib/session";
|
||||||
import { requestUser } from "@/utils/api";
|
import { requestUser } from "@/utils/api";
|
||||||
import { replaceConfiguredWorkflowsByEntities } from "@/utils/approval.workflows.be";
|
import { replaceApprovalWorkflowsByEntities } from "@/utils/approval.workflows.be";
|
||||||
import { withIronSessionApiRoute } from "iron-session/next";
|
import { withIronSessionApiRoute } from "iron-session/next";
|
||||||
import type { NextApiRequest, NextApiResponse } from "next";
|
import type { NextApiRequest, NextApiResponse } from "next";
|
||||||
|
|
||||||
@@ -31,7 +31,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
const configuredWorkflows: ApprovalWorkflow[] = filteredWorkflows;
|
const configuredWorkflows: ApprovalWorkflow[] = filteredWorkflows;
|
||||||
const entitiesIds: string[] = userEntitiesWithLabel.map((e) => e.id);
|
const entitiesIds: string[] = userEntitiesWithLabel.map((e) => e.id);
|
||||||
|
|
||||||
await replaceConfiguredWorkflowsByEntities(configuredWorkflows, entitiesIds);
|
await replaceApprovalWorkflowsByEntities("configured-workflows", configuredWorkflows, entitiesIds);
|
||||||
|
|
||||||
return res.status(201).json({ ok: true });
|
return res.status(201).json({ ok: true });
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ import { GrClearOption } from "react-icons/gr";
|
|||||||
import { toast, ToastContainer } from "react-toastify";
|
import { toast, ToastContainer } from "react-toastify";
|
||||||
import { useRouter } from "next/router";
|
import { useRouter } from "next/router";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import { getConfiguredWorkflow } from "@/utils/approval.workflows.be";
|
import { getApprovalWorkflow } from "@/utils/approval.workflows.be";
|
||||||
|
|
||||||
export const getServerSideProps = withIronSessionSsr(async ({ req, res, params }) => {
|
export const getServerSideProps = withIronSessionSsr(async ({ req, res, params }) => {
|
||||||
const user = await requestUser(req, res);
|
const user = await requestUser(req, res);
|
||||||
@@ -35,7 +35,7 @@ export const getServerSideProps = withIronSessionSsr(async ({ req, res, params }
|
|||||||
|
|
||||||
const { id } = params as { id: string };
|
const { id } = params as { id: string };
|
||||||
|
|
||||||
const workflow: ApprovalWorkflow | null = await getConfiguredWorkflow(id);
|
const workflow: ApprovalWorkflow | null = await getApprovalWorkflow("configured-workflows", id);
|
||||||
|
|
||||||
if (!workflow)
|
if (!workflow)
|
||||||
return redirect("/approval-workflows")
|
return redirect("/approval-workflows")
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import { CorporateUser, DeveloperUser, MasterCorporateUser, TeacherUser, User }
|
|||||||
import { sessionOptions } from "@/lib/session";
|
import { sessionOptions } from "@/lib/session";
|
||||||
import { redirect, serialize } from "@/utils";
|
import { redirect, serialize } from "@/utils";
|
||||||
import { requestUser } from "@/utils/api";
|
import { requestUser } from "@/utils/api";
|
||||||
import { getConfiguredWorkflow } from "@/utils/approval.workflows.be";
|
import { getApprovalWorkflow } from "@/utils/approval.workflows.be";
|
||||||
import { shouldRedirectHome } from "@/utils/navigation.disabled";
|
import { shouldRedirectHome } from "@/utils/navigation.disabled";
|
||||||
import { getEntityUsers } from "@/utils/users.be";
|
import { getEntityUsers } from "@/utils/users.be";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
@@ -30,7 +30,7 @@ export const getServerSideProps = withIronSessionSsr(async ({ req, res, params }
|
|||||||
|
|
||||||
const { id } = params as { id: string };
|
const { id } = params as { id: string };
|
||||||
|
|
||||||
const workflow: ApprovalWorkflow | null = await getConfiguredWorkflow(id);
|
const workflow: ApprovalWorkflow | null = await getApprovalWorkflow("configured-workflows", id);
|
||||||
|
|
||||||
if (!workflow)
|
if (!workflow)
|
||||||
return redirect("/approval-workflows")
|
return redirect("/approval-workflows")
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import { User } from "@/interfaces/user";
|
|||||||
import { sessionOptions } from "@/lib/session";
|
import { sessionOptions } from "@/lib/session";
|
||||||
import { redirect, serialize } from "@/utils";
|
import { redirect, serialize } from "@/utils";
|
||||||
import { requestUser } from "@/utils/api";
|
import { requestUser } from "@/utils/api";
|
||||||
import { getConfiguredWorkflow } from "@/utils/approval.workflows.be";
|
import { getApprovalWorkflow } from "@/utils/approval.workflows.be";
|
||||||
import { shouldRedirectHome } from "@/utils/navigation.disabled";
|
import { shouldRedirectHome } from "@/utils/navigation.disabled";
|
||||||
import { getSpecificUsers, getUser } from "@/utils/users.be";
|
import { getSpecificUsers, getUser } from "@/utils/users.be";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
@@ -41,7 +41,7 @@ export const getServerSideProps = withIronSessionSsr(async ({ req, res, params }
|
|||||||
|
|
||||||
const { id } = params as { id: string };
|
const { id } = params as { id: string };
|
||||||
|
|
||||||
const workflow: ApprovalWorkflow | null = await getConfiguredWorkflow(id);
|
const workflow: ApprovalWorkflow | null = await getApprovalWorkflow("configured-workflows", id);
|
||||||
|
|
||||||
if (!workflow)
|
if (!workflow)
|
||||||
return redirect("/approval-workflows")
|
return redirect("/approval-workflows")
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import { CorporateUser, DeveloperUser, MasterCorporateUser, TeacherUser, User }
|
|||||||
import { sessionOptions } from "@/lib/session";
|
import { sessionOptions } from "@/lib/session";
|
||||||
import { redirect, serialize } from "@/utils";
|
import { redirect, serialize } from "@/utils";
|
||||||
import { requestUser } from "@/utils/api";
|
import { requestUser } from "@/utils/api";
|
||||||
import { getConfiguredWorkflowsByEntities } from "@/utils/approval.workflows.be";
|
import { getApprovalWorkflowsByEntities } from "@/utils/approval.workflows.be";
|
||||||
import { getEntities } from "@/utils/entities.be";
|
import { getEntities } from "@/utils/entities.be";
|
||||||
import { shouldRedirectHome } from "@/utils/navigation.disabled";
|
import { shouldRedirectHome } from "@/utils/navigation.disabled";
|
||||||
import { getEntitiesUsers } from "@/utils/users.be";
|
import { getEntitiesUsers } from "@/utils/users.be";
|
||||||
@@ -35,7 +35,7 @@ export const getServerSideProps = withIronSessionSsr(async ({ req, res }) => {
|
|||||||
|
|
||||||
const userEntitiesWithLabel = await getEntities(user.entities.map(entity => entity.id));
|
const userEntitiesWithLabel = await getEntities(user.entities.map(entity => entity.id));
|
||||||
|
|
||||||
const allConfiguredWorkflows = await getConfiguredWorkflowsByEntities(userEntitiesWithLabel.map(entity => entity.id));
|
const allConfiguredWorkflows = await getApprovalWorkflowsByEntities("configured-workflows", userEntitiesWithLabel.map(entity => entity.id));
|
||||||
|
|
||||||
return {
|
return {
|
||||||
props: serialize({
|
props: serialize({
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import { User } from "@/interfaces/user";
|
|||||||
import { sessionOptions } from "@/lib/session";
|
import { sessionOptions } from "@/lib/session";
|
||||||
import { redirect, serialize } from "@/utils";
|
import { redirect, serialize } from "@/utils";
|
||||||
import { requestUser } from "@/utils/api";
|
import { requestUser } from "@/utils/api";
|
||||||
import { getConfiguredWorkflows } from "@/utils/approval.workflows.be";
|
import { getApprovalWorkflows } from "@/utils/approval.workflows.be";
|
||||||
import { getEntities } from "@/utils/entities.be";
|
import { getEntities } from "@/utils/entities.be";
|
||||||
import { shouldRedirectHome } from "@/utils/navigation.disabled";
|
import { shouldRedirectHome } from "@/utils/navigation.disabled";
|
||||||
import { getSpecificUsers } from "@/utils/users.be";
|
import { getSpecificUsers } from "@/utils/users.be";
|
||||||
@@ -36,7 +36,7 @@ export const getServerSideProps = withIronSessionSsr(async ({ req, res }) => {
|
|||||||
if (shouldRedirectHome(user) || !["admin", "developer", "teacher", "corporate", "mastercorporate"].includes(user.type))
|
if (shouldRedirectHome(user) || !["admin", "developer", "teacher", "corporate", "mastercorporate"].includes(user.type))
|
||||||
return redirect("/")
|
return redirect("/")
|
||||||
|
|
||||||
const workflows = await getConfiguredWorkflows();
|
const workflows = await getApprovalWorkflows("configured-workflows");
|
||||||
|
|
||||||
const allAssigneeIds: string[] = [
|
const allAssigneeIds: string[] = [
|
||||||
...new Set(
|
...new Set(
|
||||||
|
|||||||
@@ -4,41 +4,41 @@ import { ObjectId } from "mongodb";
|
|||||||
|
|
||||||
const db = client.db(process.env.MONGODB_DB);
|
const db = client.db(process.env.MONGODB_DB);
|
||||||
|
|
||||||
export const getConfiguredWorkflows = async (ids?: string[]) => {
|
export const getApprovalWorkflows = async (collection: string, ids?: string[]) => {
|
||||||
return await db
|
return await db
|
||||||
.collection<ApprovalWorkflow>("configured-workflows")
|
.collection<ApprovalWorkflow>(collection)
|
||||||
.find(ids ? { _id: { $in: ids.map((id) => new ObjectId(id)) } } : {})
|
.find(ids ? { _id: { $in: ids.map((id) => new ObjectId(id)) } } : {})
|
||||||
.toArray();
|
.toArray();
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getConfiguredWorkflow = async (id: string) => {
|
export const getApprovalWorkflow = async (collection: string, id: string) => {
|
||||||
return await db.collection<ApprovalWorkflow>("configured-workflows").findOne({ _id: new ObjectId(id) });
|
return await db.collection<ApprovalWorkflow>(collection).findOne({ _id: new ObjectId(id) });
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getConfiguredWorkflowsByEntities = async (ids: string[]) => {
|
export const getApprovalWorkflowsByEntities = async (collection: string, ids: string[]) => {
|
||||||
return await db
|
return await db
|
||||||
.collection<ApprovalWorkflow>("configured-workflows")
|
.collection<ApprovalWorkflow>(collection)
|
||||||
.find({ entityId: { $in: ids } })
|
.find({ entityId: { $in: ids } })
|
||||||
.toArray();
|
.toArray();
|
||||||
};
|
};
|
||||||
|
|
||||||
export const createConfiguredWorkflow = async (workflow: ApprovalWorkflow) => {
|
export const createApprovalWorkflow = async (collection: string, workflow: ApprovalWorkflow) => {
|
||||||
const { _id, ...workflowWithoutId } = workflow as ApprovalWorkflow;
|
const { _id, ...workflowWithoutId } = workflow as ApprovalWorkflow;
|
||||||
return await db.collection("configured-workflows").insertOne(workflowWithoutId);
|
return await db.collection(collection).insertOne(workflowWithoutId);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const createConfiguredWorkflows = async (workflows: ApprovalWorkflow[]) => {
|
export const createApprovalWorkflows = async (collection: string, workflows: ApprovalWorkflow[]) => {
|
||||||
if (workflows.length === 0) return;
|
if (workflows.length === 0) return;
|
||||||
const workflowsWithoutIds: ApprovalWorkflow[] = workflows.map(({ _id, ...wfs }) => wfs);
|
const workflowsWithoutIds: ApprovalWorkflow[] = workflows.map(({ _id, ...wfs }) => wfs);
|
||||||
return await db.collection("configured-workflows").insertMany(workflowsWithoutIds);
|
return await db.collection(collection).insertMany(workflowsWithoutIds);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const updateConfiguredWorkflow = async (workflow: ApprovalWorkflow) => {
|
export const updateApprovalWorkflow = async (collection: string, workflow: ApprovalWorkflow) => {
|
||||||
const { _id, ...workflowWithoutId } = workflow as ApprovalWorkflow;
|
const { _id, ...workflowWithoutId } = workflow as ApprovalWorkflow;
|
||||||
return await db.collection("configured-workflows").replaceOne({ _id: _id }, workflowWithoutId);
|
return await db.collection(collection).replaceOne({ _id: _id }, workflowWithoutId);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const updateConfiguredWorkflows = async (workflows: ApprovalWorkflow[]) => {
|
export const updateApprovalWorkflows = async (collection: string, workflows: ApprovalWorkflow[]) => {
|
||||||
const bulkOperations = workflows.map((workflow) => {
|
const bulkOperations = workflows.map((workflow) => {
|
||||||
const { _id, ...workflowWithoutId } = workflow;
|
const { _id, ...workflowWithoutId } = workflow;
|
||||||
return {
|
return {
|
||||||
@@ -49,14 +49,14 @@ export const updateConfiguredWorkflows = async (workflows: ApprovalWorkflow[]) =
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
return await db.collection("configured-workflows").bulkWrite(bulkOperations);
|
return await db.collection(collection).bulkWrite(bulkOperations);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const deleteConfiguredWorkflow = async (id: string) => {
|
export const deleteApprovalWorkflow = async (collection: string, id: string) => {
|
||||||
return await db.collection("configured-workflows").deleteOne({ _id: new ObjectId(id) });
|
return await db.collection(collection).deleteOne({ _id: new ObjectId(id) });
|
||||||
};
|
};
|
||||||
|
|
||||||
export const replaceConfiguredWorkflowsByEntities = async (workflows: ApprovalWorkflow[], entityIds: string[]) => {
|
export const replaceApprovalWorkflowsByEntities = async (collection: string, workflows: ApprovalWorkflow[], entityIds: string[]) => {
|
||||||
// 1. Keep track of the _id values of all workflows we want to end up with
|
// 1. Keep track of the _id values of all workflows we want to end up with
|
||||||
const finalIds = new Set<string>();
|
const finalIds = new Set<string>();
|
||||||
|
|
||||||
@@ -64,17 +64,17 @@ export const replaceConfiguredWorkflowsByEntities = async (workflows: ApprovalWo
|
|||||||
for (const workflow of workflows) {
|
for (const workflow of workflows) {
|
||||||
if (workflow._id) {
|
if (workflow._id) {
|
||||||
// Replace the existing ones
|
// Replace the existing ones
|
||||||
await updateConfiguredWorkflow(workflow);
|
await updateApprovalWorkflow(collection, workflow);
|
||||||
finalIds.add(workflow._id.toString());
|
finalIds.add(workflow._id.toString());
|
||||||
} else {
|
} else {
|
||||||
// Insert if no _id
|
// Insert if no _id
|
||||||
const insertResult = await createConfiguredWorkflow(workflow);
|
const insertResult = await createApprovalWorkflow(collection, workflow);
|
||||||
finalIds.add(insertResult.insertedId.toString());
|
finalIds.add(insertResult.insertedId.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Delete any existing workflow (within these entityIds) that wasn't in the final list
|
// 3. Delete any existing workflow (within these entityIds) that wasn't in the final list
|
||||||
await db.collection("configured-workflows").deleteMany({
|
await db.collection(collection).deleteMany({
|
||||||
_id: {
|
_id: {
|
||||||
$nin: Array.from(finalIds).map((id) => new ObjectId(id)),
|
$nin: Array.from(finalIds).map((id) => new ObjectId(id)),
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user