From 4c977c221a5d0a2395425538ee7495f3044b16d0 Mon Sep 17 00:00:00 2001 From: Jonathan Johnson Date: Thu, 14 Sep 2023 22:08:00 -0700 Subject: [PATCH] Save learner group changes in two batches We were optimizing our network changes here by combining the remove and the add step for groups, however this doesn't always result in consistent data and sometimes results in errors in the API. Instead we should first remove and then add users to groups. This is less efficient, but prevents data errors so it's worth doing here. --- app/components/learner-group/root.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/app/components/learner-group/root.js b/app/components/learner-group/root.js index afaef4ef9d..889e3a64ab 100644 --- a/app/components/learner-group/root.js +++ b/app/components/learner-group/root.js @@ -200,8 +200,8 @@ export default class LearnerGroupRootComponent extends Component { const topLevelGroup = yield learnerGroup.topLevelGroup; const removeGroups = yield topLevelGroup.removeUserFromGroupAndAllDescendants(user); const addGroups = yield learnerGroup.addUserToGroupAndAllParents(user); - const groups = [].concat(removeGroups).concat(addGroups); - yield all(groups.map((group) => group.save())); + yield Promise.all(removeGroups.map((g) => g.save())); + yield Promise.all(addGroups.map((g) => g.save())); this.usersToPassToManager = yield this.createUsersToPassToManager.perform(); this.usersToPassToCohortManager = yield this.createUsersToPassToCohortManager.perform(); } @@ -219,14 +219,20 @@ export default class LearnerGroupRootComponent extends Component { *addUsersToGroup(users) { const learnerGroup = this.args.learnerGroup; const topLevelGroup = yield learnerGroup.topLevelGroup; - let groupsToSave = []; + let addGroups = []; + let removeGroups = []; for (let i = 0; i < users.length; i++) { const user = users[i]; - const removeGroups = yield topLevelGroup.removeUserFromGroupAndAllDescendants(user); - const addGroups = yield learnerGroup.addUserToGroupAndAllParents(user); - groupsToSave = [...groupsToSave, ...removeGroups, ...addGroups]; + removeGroups = [ + ...removeGroups, + ...(yield topLevelGroup.removeUserFromGroupAndAllDescendants(user)), + ]; + addGroups = [...addGroups, ...(yield learnerGroup.addUserToGroupAndAllParents(user))]; } - yield all(uniqueValues(groupsToSave).map((group) => group.save())); + + yield Promise.all(uniqueValues(removeGroups).map((g) => g.save())); + yield Promise.all(uniqueValues(addGroups).map((g) => g.save())); + this.usersToPassToManager = yield this.createUsersToPassToManager.perform(); this.usersToPassToCohortManager = yield this.createUsersToPassToCohortManager.perform(); }