From 0dbd597d0bc750928aec47166aa8f558b0ba06a0 Mon Sep 17 00:00:00 2001 From: SabreCat Date: Mon, 18 Dec 2023 15:50:58 -0600 Subject: [PATCH] Squashed commit of the following: commit d03bdee783deff98ee24cc211f1498525bf36c70 Merge: b31ba93233 70f5aa1f55 Author: SabreCat Date: Mon Dec 18 15:47:29 2023 -0600 Merge branch 'release' into sabrecat/group-task-fixes commit b31ba932333efaaaf1f3d69ed9d0bce6e5bcaf8a Author: SabreCat Date: Tue Oct 10 12:02:41 2023 -0500 fix(groups): missing dot path step Also sync assignments on unassign as well as assign commit 8012f0294daedaccae1767fe6bf0e4686cfd8268 Author: SabreCat Date: Tue Oct 10 11:47:59 2023 -0500 fix(groups): use assigned keys as source of truth commit 616b1e1486e0111281538ed3f4516cac95e96fc3 Author: SabreCat Date: Tue Oct 10 11:33:28 2023 -0500 fix(groups): clean up groups to copy tasks Fixes #14916 --- website/server/libs/user/index.js | 14 +++++++++----- website/server/models/group.js | 10 +++------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/website/server/libs/user/index.js b/website/server/libs/user/index.js index e3f9e8d604..118e0586d9 100644 --- a/website/server/libs/user/index.js +++ b/website/server/libs/user/index.js @@ -147,8 +147,10 @@ export async function update (req, res, { isV3 = false }) { await checkNewInputForProfanity(user, res, newBlurb); } + let groupsToMirror; + let matchingGroupsArray; if (req.body['preferences.tasks.mirrorGroupTasks'] !== undefined) { - const groupsToMirror = req.body['preferences.tasks.mirrorGroupTasks']; + groupsToMirror = req.body['preferences.tasks.mirrorGroupTasks']; if (!Array.isArray(groupsToMirror)) { throw new BadRequest('Groups to copy tasks from must be an array.'); } @@ -160,7 +162,7 @@ export async function update (req, res, { isV3 = false }) { } } - const matchingGroupsCount = await Groups.countDocuments({ + const matchingGroups = await Groups.find({ _id: { $in: groupsToMirror }, 'purchased.plan.customerId': { $exists: true }, $or: [ @@ -168,11 +170,11 @@ export async function update (req, res, { isV3 = false }) { { 'purchased.plan.dateTerminated': null }, { 'purchased.plan.dateTerminated': { $gt: new Date() } }, ], + }, { + _id: 1, }).exec(); - if (matchingGroupsCount !== groupsToMirror.length) { - throw new BadRequest('Groups to copy tasks from must have subscriptions.'); - } + matchingGroupsArray = _.map(matchingGroups, groupRecord => groupRecord._id); } _.each(req.body, (val, key) => { @@ -234,6 +236,8 @@ export async function update (req, res, { isV3 = false }) { if (lastNewsPost) { user.flags.lastNewStuffRead = lastNewsPost._id; } + } else if (key === 'preferences.tasks.mirrorGroupTasks') { + user.preferences.tasks.mirrorGroupTasks = _.intersection(groupsToMirror, matchingGroupsArray); } else if (acceptablePUTPaths[key]) { let adjustedVal = val; if (key === 'stats.lvl' && val > common.constants.MAX_LEVEL_HARD_CAP) { diff --git a/website/server/models/group.js b/website/server/models/group.js index 6ca51cc955..4324a3545a 100644 --- a/website/server/models/group.js +++ b/website/server/models/group.js @@ -1432,11 +1432,6 @@ schema.methods.syncTask = async function groupSyncTask (taskToSync, users, assig completed: false, }; - if (!taskToSync.group.assignedUsers) { - taskToSync.group.assignedUsers = []; - } - taskToSync.group.assignedUsers.push(user._id); - if (!taskToSync.group.assignedUsersDetail) { taskToSync.group.assignedUsersDetail = {}; } @@ -1445,6 +1440,8 @@ schema.methods.syncTask = async function groupSyncTask (taskToSync, users, assig } taskToSync.markModified('group.assignedUsersDetail'); + taskToSync.group.assignedUsers = _.keys(taskToSync.group.assignedUsersDetail); + // Sync tags const userTags = user.tags; const i = _.findIndex(userTags, { id: group._id }); @@ -1478,8 +1475,7 @@ schema.methods.unlinkTask = async function groupUnlinkTask ( }; delete unlinkingTask.group.assignedUsersDetail[user._id]; - const assignedUserIndex = unlinkingTask.group.assignedUsers.indexOf(user._id); - unlinkingTask.group.assignedUsers.splice(assignedUserIndex, 1); + unlinkingTask.group.assignedUsers = _.keys(unlinkingTask.group.assignedUsersDetail); unlinkingTask.markModified('group'); const promises = [unlinkingTask.save()];