fix(challenges): revert to working

This commit is contained in:
SabreCat 2023-08-07 22:00:47 -05:00
parent 39477c6f11
commit c44b1670cf
5 changed files with 9 additions and 26 deletions

View file

@ -38,8 +38,7 @@ describe('GET /challenges/:challengeId', () => {
group = populatedGroup.group;
members = populatedGroup.members;
challengeLeader = members[0]; // eslint-disable-line prefer-destructuring
otherMember = members[1]; // eslint-disable-line prefer-destructuring
[challengeLeader, otherMember] = members;
challenge = await generateChallenge(challengeLeader, group);
});

View file

@ -14,7 +14,6 @@ import {
import {
NotFound,
NotAuthorized,
BadRequest,
} from '../../libs/errors';
import * as Tasks from '../../models/task';
import csvStringify from '../../libs/csvStringify';
@ -267,12 +266,7 @@ api.joinChallenge = {
const group = await Group.getGroup({
user, groupId: challenge.group, fields: basicGroupFields, optionalMembership: true,
});
if (!group || (group.type === 'party' && group._id !== user.party._id)) {
throw new NotFound(res.t('challengeNotFound'));
}
if (group.type === 'guild' && group._id !== TAVERN_ID && !group.hasActiveGroupPlan()) {
throw new BadRequest(res.t('featureRetired'));
}
if (!group || !challenge.canJoin(user, group)) throw new NotFound(res.t('challengeNotFound'));
const addedSuccessfully = await challenge.addToUser(user);
if (!addedSuccessfully) {
@ -409,9 +403,8 @@ api.getUserChallenges = {
orOptions.push({ leader: user._id });
if (!req.query.member) {
const userGroups = await user.getGroups();
orOptions.push({
group: { $in: userGroups },
group: { $in: user.getGroups() },
}); // Challenges in groups where I'm a member
}

View file

@ -5,7 +5,6 @@ import { authWithHeaders } from '../../middlewares/auth';
import {
model as Group,
basicFields as basicGroupFields,
TAVERN_ID,
} from '../../models/group';
import {
model as User,
@ -412,7 +411,7 @@ api.getGroup = {
const { groupId } = req.params;
const group = await Group.getGroup({ user, groupId, populateLeader: false });
if (!group || (group.type === 'guild' && group._id !== TAVERN_ID && !group.hasActiveGroupPlan())) {
if (!group) {
throw new NotFound(res.t('groupNotFound'));
}
@ -565,7 +564,6 @@ api.joinGroup = {
if (!group) throw new NotFound(res.t('groupNotFound'));
let isUserInvited = false;
const seekingParty = Boolean(user.party.seeking);
if (group.type === 'party') {
// Check if was invited to party
@ -731,7 +729,7 @@ api.joinGroup = {
invited: isUserInvited,
};
if (group.type === 'party') {
analyticsObject.seekingParty = seekingParty;
analyticsObject.seekingParty = Boolean(user.party.seeking);
}
if (group.privacy === 'public') {
analyticsObject.groupName = group.name;
@ -1339,7 +1337,7 @@ api.getGroupPlans = {
async handler (req, res) {
const { user } = res.locals;
const userGroups = await user.getGroups();
const userGroups = user.getGroups();
const groups = await Group
.find({

View file

@ -8,7 +8,6 @@ import {
TAVERN_ID,
} from '../../models/group';
import {
BadRequest,
NotFound,
NotAuthorized,
} from '../errors';
@ -42,9 +41,6 @@ export async function createChallenge (user, req, res) {
});
if (!group) throw new NotFound(res.t('groupNotFound'));
if (!group.isMember(user)) throw new NotAuthorized(res.t('mustBeGroupMember'));
if (group.type === 'guild' && group._id !== TAVERN_ID && !group.hasActiveGroupPlan()) {
throw new BadRequest(res.t('featureRetired'));
}
if (group.leaderOnly && group.leaderOnly.challenges && group.leader !== user._id) {
throw new NotAuthorized(res.t('onlyGroupLeaderChal'));

View file

@ -45,11 +45,8 @@ schema.methods.hasCancelled = function hasCancelled () {
};
// Get an array of groups ids the user is member of
schema.methods.getGroups = async function getUserGroups () {
const userGroups = await Group.getGroups({
user: this,
types: ['party', 'guilds', 'tavern'],
});
schema.methods.getGroups = function getUserGroups () {
const userGroups = this.guilds.slice(0); // clone this.guilds so we don't modify the original
if (this.party._id) userGroups.push(this.party._id);
userGroups.push(TAVERN_ID);
return userGroups;
@ -468,7 +465,7 @@ schema.methods.daysUserHasMissed = function daysUserHasMissed (now, req = {}) {
};
async function getUserGroupData (user) {
const userGroups = await user.getGroups();
const userGroups = user.getGroups();
const groups = await Group
.find({