fix(groups): clean up groups to copy tasks

Fixes #14916
This commit is contained in:
SabreCat
2023-10-10 11:33:28 -05:00
parent b539dd2b2e
commit 616b1e1486
+9 -5
View File
@@ -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.mirrorGroupTasks = _.intersection(groupsToMirror, matchingGroupsArray);
} else if (acceptablePUTPaths[key]) {
let adjustedVal = val;
if (key === 'stats.lvl' && val > common.constants.MAX_LEVEL_HARD_CAP) {