From 01e2bb56df07ccde8db0666c5a6c5899e4cffbc5 Mon Sep 17 00:00:00 2001 From: Matteo Pagliazzi Date: Mon, 28 Dec 2015 11:18:58 +0100 Subject: [PATCH] port getChallenges --- website/src/controllers/api-v3/challenges.js | 46 ++++++++++++++++++++ website/src/models/group.js | 1 + 2 files changed, 47 insertions(+) create mode 100644 website/src/controllers/api-v3/challenges.js diff --git a/website/src/controllers/api-v3/challenges.js b/website/src/controllers/api-v3/challenges.js new file mode 100644 index 0000000000..72c920eb3d --- /dev/null +++ b/website/src/controllers/api-v3/challenges.js @@ -0,0 +1,46 @@ +import { authWithHeaders } from '../../middlewares/api-v3/auth'; +import cron from '../../middlewares/api-v3/cron'; +import { model as Challenge } from '../../models/challenge'; + +let api = {}; + +/** + * @api {get} /challenges Get challenges for a user + * @apiVersion 3.0.0 + * @apiName GetChallenges + * @apiGroup Challenge + * + * @apiSuccess {Array} challenges An array of challenges + */ +api.getChallenges = { + method: 'GET', + url: '/challenges', + middlewares: [authWithHeaders(), cron], + handler (req, res, next) { + let user = res.locals.user; + + let groups = user.guilds || []; + if (user.party._id) groups.push(user.party._id); + groups.push('habitrpg'); // Public challenges + + Challenge.find({ + $or: [ + {_id: {$in: user.challenges}}, // Challenges where the user is participating + {group: {$in: groups}}, // Challenges in groups where I'm a member + {leader: user._id}, // Challenges where I'm the leader + ], + _id: {$ne: '95533e05-1ff9-4e46-970b-d77219f199e9'}, // remove the Spread the Word Challenge for now, will revisit when we fix the closing-challenge bug TODO revisit + }) + .sort('-official -timestamp') + // TODO populate + // .populate('group', '_id name type') + // .populate('leader', 'profile.name') + .exec() + .then(challenges => { + res.respond(200, challenges); + }) + .catch(next); + }, +}; + +export default api; diff --git a/website/src/models/group.js b/website/src/models/group.js index 2c5cdecd35..5c26330cc7 100644 --- a/website/src/models/group.js +++ b/website/src/models/group.js @@ -13,6 +13,7 @@ let Schema = mongoose.Schema; // NOTE once Firebase is enabled any change to groups' members in MongoDB will have to be run through the API // changes made directly to the db will cause Firebase to get out of sync export let schema = new Schema({ + // TODO don't break validation on _id === 'habitrpg' name: {type: String, required: true}, description: String, leader: {type: String, ref: 'User', validate: [validator.isUUID, 'Invalid uuid.'], required: true},