refactor workflows api
This commit is contained in:
@@ -4,41 +4,41 @@ import { ObjectId } from "mongodb";
|
||||
|
||||
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
|
||||
.collection<ApprovalWorkflow>("configured-workflows")
|
||||
.collection<ApprovalWorkflow>(collection)
|
||||
.find(ids ? { _id: { $in: ids.map((id) => new ObjectId(id)) } } : {})
|
||||
.toArray();
|
||||
};
|
||||
|
||||
export const getConfiguredWorkflow = async (id: string) => {
|
||||
return await db.collection<ApprovalWorkflow>("configured-workflows").findOne({ _id: new ObjectId(id) });
|
||||
export const getApprovalWorkflow = async (collection: string, id: string) => {
|
||||
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
|
||||
.collection<ApprovalWorkflow>("configured-workflows")
|
||||
.collection<ApprovalWorkflow>(collection)
|
||||
.find({ entityId: { $in: ids } })
|
||||
.toArray();
|
||||
};
|
||||
|
||||
export const createConfiguredWorkflow = async (workflow: ApprovalWorkflow) => {
|
||||
export const createApprovalWorkflow = async (collection: string, workflow: 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;
|
||||
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;
|
||||
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 { _id, ...workflowWithoutId } = workflow;
|
||||
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) => {
|
||||
return await db.collection("configured-workflows").deleteOne({ _id: new ObjectId(id) });
|
||||
export const deleteApprovalWorkflow = async (collection: string, id: string) => {
|
||||
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
|
||||
const finalIds = new Set<string>();
|
||||
|
||||
@@ -64,17 +64,17 @@ export const replaceConfiguredWorkflowsByEntities = async (workflows: ApprovalWo
|
||||
for (const workflow of workflows) {
|
||||
if (workflow._id) {
|
||||
// Replace the existing ones
|
||||
await updateConfiguredWorkflow(workflow);
|
||||
await updateApprovalWorkflow(collection, workflow);
|
||||
finalIds.add(workflow._id.toString());
|
||||
} else {
|
||||
// Insert if no _id
|
||||
const insertResult = await createConfiguredWorkflow(workflow);
|
||||
const insertResult = await createApprovalWorkflow(collection, 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({
|
||||
await db.collection(collection).deleteMany({
|
||||
_id: {
|
||||
$nin: Array.from(finalIds).map((id) => new ObjectId(id)),
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user