major change on how workflow builder works. It now fetches in edit mode all the currently configured workflows
This commit is contained in:
@@ -1,36 +1,83 @@
|
||||
import { ObjectId } from "mongodb";
|
||||
import { ApprovalWorkflow } from "@/interfaces/approval.workflow";
|
||||
import client from "@/lib/mongodb";
|
||||
import { ObjectId } from "mongodb";
|
||||
|
||||
const db = client.db(process.env.MONGODB_DB);
|
||||
|
||||
export const getApprovalWorkflows = async (ids?: string[]) => {
|
||||
export const getConfiguredWorkflows = async (ids?: string[]) => {
|
||||
return await db
|
||||
.collection<ApprovalWorkflow>("approval-workflows")
|
||||
.collection<ApprovalWorkflow>("configured-workflows")
|
||||
.find(ids ? { _id: { $in: ids.map((id) => new ObjectId(id)) } } : {})
|
||||
.toArray();
|
||||
};
|
||||
|
||||
export const getApprovalWorkflow = async (id: string) => {
|
||||
return await db.collection<ApprovalWorkflow>("approval-workflows").findOne({ _id: new ObjectId(id) });
|
||||
export const getConfiguredWorkflow = async (id: string) => {
|
||||
return await db.collection<ApprovalWorkflow>("configured-workflows").findOne({ _id: new ObjectId(id) });
|
||||
};
|
||||
|
||||
export const createApprovalWorkflow = async (workflow: ApprovalWorkflow) => {
|
||||
const { _id, ...workflowWithoutId } = workflow as ApprovalWorkflow;
|
||||
return await db.collection("approval-workflows").insertOne(workflowWithoutId);
|
||||
export const getConfiguredWorkflowsByEntities = async (ids: string[]) => {
|
||||
return await db
|
||||
.collection<ApprovalWorkflow>("configured-workflows")
|
||||
.find({ entityId: { $in: ids } })
|
||||
.toArray();
|
||||
};
|
||||
|
||||
export const createApprovalWorkflows = async (workflows: ApprovalWorkflow[]) => {
|
||||
export const createConfiguredWorkflow = async (workflow: ApprovalWorkflow) => {
|
||||
const { _id, ...workflowWithoutId } = workflow as ApprovalWorkflow;
|
||||
return await db.collection("configured-workflows").insertOne(workflowWithoutId);
|
||||
};
|
||||
|
||||
export const createConfiguredWorkflows = async (workflows: ApprovalWorkflow[]) => {
|
||||
if (workflows.length === 0) return;
|
||||
const workflowsWithoutIds: ApprovalWorkflow[] = workflows.map(({_id, ...wfs}) => wfs)
|
||||
return await db.collection("approval-workflows").insertMany(workflowsWithoutIds);
|
||||
const workflowsWithoutIds: ApprovalWorkflow[] = workflows.map(({ _id, ...wfs }) => wfs);
|
||||
return await db.collection("configured-workflows").insertMany(workflowsWithoutIds);
|
||||
};
|
||||
|
||||
export const updateApprovalWorkflow = async (id: string, workflow: ApprovalWorkflow) => {
|
||||
const { _id, ...workflowWithoutId } = workflow as ApprovalWorkflow;
|
||||
return await db.collection("approval-workflows").replaceOne({ _id: new ObjectId(id) }, workflowWithoutId);
|
||||
export const updateConfiguredWorkflow = async (workflow: ApprovalWorkflow) => {
|
||||
const { _id, ...workflowWithoutId } = workflow as ApprovalWorkflow;
|
||||
return await db.collection("configured-workflows").replaceOne({ _id: new ObjectId(_id) }, workflowWithoutId);
|
||||
};
|
||||
|
||||
export const deleteApprovalWorkflow = async (id: string) => {
|
||||
return await db.collection("approval-workflows").deleteOne({ _id: new ObjectId(id) });
|
||||
export const updateConfiguredWorkflows = async (workflows: ApprovalWorkflow[]) => {
|
||||
const bulkOperations = workflows.map((workflow) => {
|
||||
const { _id, ...workflowWithoutId } = workflow;
|
||||
return {
|
||||
replaceOne: {
|
||||
filter: { _id: new ObjectId(_id) },
|
||||
replacement: workflowWithoutId,
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
return await db.collection("configured-workflows").bulkWrite(bulkOperations);
|
||||
};
|
||||
|
||||
export const deleteConfiguredWorkflow = async (id: string) => {
|
||||
return await db.collection("configured-workflows").deleteOne({ _id: new ObjectId(id) });
|
||||
};
|
||||
|
||||
export const replaceConfiguredWorkflowsByEntities = async (workflows: ApprovalWorkflow[], entityIds: string[]) => {
|
||||
// 1. Keep track of the _id values of all workflows we want to end up with
|
||||
const finalIds = new Set<string>();
|
||||
|
||||
// 2. Process incoming workflows
|
||||
for (const workflow of workflows) {
|
||||
if (workflow._id) {
|
||||
// Replace the existing ones
|
||||
await updateConfiguredWorkflow(workflow);
|
||||
finalIds.add(workflow._id.toString());
|
||||
} else {
|
||||
// Insert if no _id
|
||||
const insertResult = await createConfiguredWorkflow(workflow);
|
||||
finalIds.add(insertResult.insertedId.toString());
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Delete any existing workflow (within these entityIds) that wasn't in the final list
|
||||
await db.collection("configured-workflows").deleteMany({
|
||||
_id: {
|
||||
$nin: Array.from(finalIds).map((id) => new ObjectId(id)),
|
||||
},
|
||||
entityId: { $in: entityIds },
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user