Skip to content

Commit

Permalink
Merge pull request #2263 from HHS/mb/TTAHUB-3172/refactor-find-or-cre…
Browse files Browse the repository at this point in the history
…ate-collab

[TTAHUB-3172] Refactor hook to not commit early when approving a large report
  • Loading branch information
thewatermethod authored Jul 11, 2024
2 parents d897fc7 + c7cdf2f commit 9bf70fc
Showing 1 changed file with 30 additions and 29 deletions.
59 changes: 30 additions & 29 deletions src/models/hooks/activityReport.js
Original file line number Diff line number Diff line change
Expand Up @@ -803,36 +803,37 @@ const autoPopulateUtilizer = async (sequelize, instance, options) => {
...collaborators, // Spread the elements of the 'collaborators' array into a new array
{ userId: instance.userId }, // Add an object with a 'userId' property to the new array
].filter(({ userId }) => userId);
await Promise.all([
...users
// Use flatMap to iterate over each element in the new array asynchronously
.flatMap(async ({ userId }) => goals
// Use map to iterate over each element in the 'goals' array asynchronously
// Call the 'findOrCreateCollaborator' function with the following arguments
.map(async ({ goalId }) => findOrCreateCollaborator(
'goal',
sequelize, // The 'sequelize' variable
options.transaction, // The 'options' variable
goalId, // The 'goalId' from the current iteration of the 'goals' array
userId, // The 'userId' from the current iteration of the new array
GOAL_COLLABORATORS.UTILIZER, // The 'GOAL_COLLABORATORS.UTILIZER' constant
{ activityReportIds: [instance.id] },
))),
...users
// Use flatMap to iterate over each element in the new array asynchronously
.flatMap(async ({ userId }) => objectives
// Use map to iterate over each element in the 'objectives' array asynchronously
// Call the 'findOrCreateCollaborator' function with the following arguments
.map(async ({ objectiveId }) => findOrCreateCollaborator(
'objective',
sequelize, // The 'sequelize' variable
options.transaction, // The 'options' variable
objectiveId, // The 'objectiveId' from the current iteration of the 'objectives' array
userId, // The 'userId' from the current iteration of the new array
OBJECTIVE_COLLABORATORS.UTILIZER, // The 'OBJECTIVE_COLLABORATORS.UTILIZER' constant
{ activityReportIds: [instance.id] },
))),

const findOrCreateCollaboratorOptions = users.flatMap((user) => [
...goals.map((goal) => ({
type: 'goal',
sequelize,
transaction: options.transaction,
associatedId: goal.goalId,
userId: user.userId,
collaboratorType: GOAL_COLLABORATORS.UTILIZER,
metadata: { activityReportIds: [instance.id] },
})),
...objectives.map((objective) => ({
type: 'objective',
sequelize,
transaction: options.transaction,
associatedId: objective.objectiveId,
userId: user.userId,
collaboratorType: OBJECTIVE_COLLABORATORS.UTILIZER,
metadata: { activityReportIds: [instance.id] },
})),
]);

await Promise.all(findOrCreateCollaboratorOptions.map((option) => findOrCreateCollaborator(
option.type,
option.sequelize,
option.transaction,
option.associatedId,
option.userId,
option.collaboratorType,
option.metadata,
)));
}
};

Expand Down

0 comments on commit 9bf70fc

Please sign in to comment.