- Filter available form intakers so that no form intaker can be in two workflows at once.

- add getApprovalWorkflowByIntaker to prepare workflow start after exam creation.
- fix builder bug with step keys
- ignore edit view for now because it will only be available for active workflows and not configured workflows.
This commit is contained in:
Joao Correia
2025-02-02 22:40:05 +00:00
parent 16545d2075
commit 835a9bee03
5 changed files with 97 additions and 31 deletions

View File

@@ -22,6 +22,39 @@ export const getApprovalWorkflowsByEntities = async (collection: string, ids: st
.toArray();
};
export const getApprovalWorkflowByFormIntaker = async (entityId: string, formIntakerId: string) => {
return await db.collection<ApprovalWorkflow>("configured-workflows").findOne({
entityId,
steps: {
$elemMatch: {
stepNumber: 1,
assignees: formIntakerId,
},
},
});
};
export const getFormIntakersByEntity = async (entityId: string) => {
const results = await db
.collection<ApprovalWorkflow>("configured-workflows")
.aggregate([
// 1. Match workflows with the provided entityId
{ $match: { entityId } },
// 2. Unwind the steps array to process each step individually
{ $unwind: "$steps" },
// 3. Filter for the first step (you could also check for a "firstStep" flag if you prefer)
{ $match: { "steps.stepNumber": 1 } },
// 4. Unwind the assignees array so that each assignee is handled separately
{ $unwind: "$steps.assignees" },
// 5. Group by null (i.e. all documents) and add each assignee to a set to remove duplicates
{ $group: { _id: null, assignees: { $addToSet: "$steps.assignees" } } },
])
.toArray();
// Return the assignees if the aggregation found any; otherwise return an empty array
return results.length > 0 ? results[0].assignees : [];
};
export const createApprovalWorkflow = async (collection: string, workflow: ApprovalWorkflow) => {
const { _id, ...workflowWithoutId } = workflow as ApprovalWorkflow;
return await db.collection(collection).insertOne(workflowWithoutId);
@@ -35,7 +68,7 @@ export const createApprovalWorkflows = async (collection: string, workflows: App
export const updateApprovalWorkflow = async (collection: string, workflow: ApprovalWorkflow) => {
const { _id, ...workflowWithoutId } = workflow as ApprovalWorkflow;
return await db.collection(collection).replaceOne({ _id: _id }, workflowWithoutId);
return await db.collection(collection).replaceOne({ _id: new ObjectId(_id) }, workflowWithoutId);
};
export const updateApprovalWorkflows = async (collection: string, workflows: ApprovalWorkflow[]) => {
@@ -56,25 +89,25 @@ export const deleteApprovalWorkflow = async (collection: string, id: string) =>
return await db.collection(collection).deleteOne({ _id: new ObjectId(id) });
};
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>();
// 2. Process incoming workflows
for (const workflow of workflows) {
if (workflow._id) {
// Replace the existing ones
await updateApprovalWorkflow(collection, workflow);
finalIds.add(workflow._id.toString());
} else {
// Insert if no _id
const insertResult = await createApprovalWorkflow(collection, workflow);
finalIds.add(insertResult.insertedId.toString());
}
}
export const replaceApprovalWorkflowsByEntities = 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 updateApprovalWorkflow("configured-workflows", workflow);
finalIds.add(workflow._id.toString());
} else {
// Insert if no _id
const insertResult = await createApprovalWorkflow("configured-workflows", workflow);
finalIds.add(insertResult.insertedId.toString());
}
}
// 3. Delete any existing workflow (within these entityIds) that wasn't in the final list
await db.collection(collection).deleteMany({
await db.collection("configured-workflows").deleteMany({
_id: {
$nin: Array.from(finalIds).map((id) => new ObjectId(id)),
},