major change on how workflow builder works. It now fetches in edit mode all the currently configured workflows

This commit is contained in:
Joao Correia
2025-02-01 22:36:42 +00:00
parent a0229cd971
commit ac539332e6
12 changed files with 159 additions and 56 deletions

View File

@@ -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 },
});
};