mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-19 15:48:04 +01:00
v3 adapt v2: port res.respond, challenge.getMember and challenge.csv
This commit is contained in:
@@ -20,7 +20,7 @@ var utils = require('../../libs/api-v2/utils');
|
|||||||
var api = module.exports;
|
var api = module.exports;
|
||||||
var pushNotify = require('./pushNotifications');
|
var pushNotify = require('./pushNotifications');
|
||||||
import Q from 'q';
|
import Q from 'q';
|
||||||
|
import v3MembersController from '../api-v3/members';
|
||||||
/*
|
/*
|
||||||
------------------------------------------------------------------------
|
------------------------------------------------------------------------
|
||||||
Challenges
|
Challenges
|
||||||
@@ -100,86 +100,37 @@ api.get = async function(req, res, next) {
|
|||||||
|
|
||||||
api.csv = function(req, res, next) {
|
api.csv = function(req, res, next) {
|
||||||
var cid = req.params.cid;
|
var cid = req.params.cid;
|
||||||
var challenge;
|
req.params.challengeId = cid;
|
||||||
async.waterfall([
|
v3MembersController.exportChallengeCsv.handler(req, res, next).catch(next);
|
||||||
function(cb){
|
|
||||||
Challenge.findById(cid,cb)
|
|
||||||
},
|
|
||||||
function(_challenge,cb) {
|
|
||||||
challenge = _challenge;
|
|
||||||
if (!challenge) return cb('Challenge ' + cid + ' not found');
|
|
||||||
User.aggregate([
|
|
||||||
{$match:{'_id':{ '$in': challenge.members}}}, //yes, we want members
|
|
||||||
{$project:{'profile.name':1,tasks:{$setUnion:["$habits","$dailys","$todos","$rewards"]}}},
|
|
||||||
{$unwind:"$tasks"},
|
|
||||||
{$match:{"tasks.challenge.id":cid}},
|
|
||||||
{$sort:{'tasks.type':1,'tasks.id':1}},
|
|
||||||
{$group:{_id:"$_id", "tasks":{$push:"$tasks"},"name":{$first:"$profile.name"}}}
|
|
||||||
], cb);
|
|
||||||
}
|
|
||||||
],function(err,users){
|
|
||||||
if(err) return next(err);
|
|
||||||
var output = ['UUID','name'];
|
|
||||||
_.each(challenge.tasks,function(t){
|
|
||||||
//output.push(t.type+':'+t.text);
|
|
||||||
//not the right order yet
|
|
||||||
output.push('Task');
|
|
||||||
output.push('Value');
|
|
||||||
output.push('Notes');
|
|
||||||
})
|
|
||||||
output = [output];
|
|
||||||
_.each(users, function(u){
|
|
||||||
var uData = [u._id,u.name];
|
|
||||||
_.each(u.tasks,function(t){
|
|
||||||
uData = uData.concat([t.type+':'+t.text, t.value, t.notes]);
|
|
||||||
})
|
|
||||||
output.push(uData);
|
|
||||||
});
|
|
||||||
|
|
||||||
res.set({
|
|
||||||
'Content-Type': 'text/csv',
|
|
||||||
'Content-disposition': `attachment; filename=${cid}.csv`,
|
|
||||||
});
|
|
||||||
|
|
||||||
csvStringify(output, (err, csv) => {
|
|
||||||
if (err) return next(err);
|
|
||||||
res.status(200).send(csv);
|
|
||||||
challenge = cid = null;
|
|
||||||
});
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
api.getMember = function(req, res, next) {
|
api.getMember = function(req, res, next) {
|
||||||
var cid = req.params.cid;
|
var cid = req.params.cid;
|
||||||
var uid = req.params.uid;
|
var uid = req.params.uid;
|
||||||
|
|
||||||
// We need to start using the aggregation framework instead of in-app filtering, see http://docs.mongodb.org/manual/aggregation/
|
req.params.memberId = uid;
|
||||||
// See code at 32c0e75 for unwind/group example
|
req.params.challengeId = cid;
|
||||||
|
v3MembersController.getChallengeMemberProgress.handler(req, res, next)
|
||||||
|
.then(result => {
|
||||||
|
let newResult = {
|
||||||
|
profile: {
|
||||||
|
name: result.profile.name,
|
||||||
|
},
|
||||||
|
habits: [],
|
||||||
|
dailys: [],
|
||||||
|
todos: [],
|
||||||
|
rewards: [],
|
||||||
|
};
|
||||||
|
|
||||||
//http://stackoverflow.com/questions/24027213/how-to-match-multiple-array-elements-without-using-unwind
|
let tasks = result.tasks;
|
||||||
var proj = {'profile.name':'$profile.name'};
|
tasks.forEach(task => {
|
||||||
_.each(['habits','dailys','todos','rewards'], function(type){
|
let taskObj = task.toJSONV2();
|
||||||
proj[type] = {
|
newResult[taskObj.type + 's'].push(taskObj);
|
||||||
$setDifference: [{
|
});
|
||||||
$map: {
|
|
||||||
input: '$'+type,
|
res.json(newResult);
|
||||||
as: "el",
|
})
|
||||||
in: {
|
.catch(next);
|
||||||
$cond: [{$eq: ["$$el.challenge.id", cid]}, '$$el', false]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, [false]]
|
|
||||||
}
|
|
||||||
});
|
|
||||||
User.aggregate()
|
|
||||||
.match({_id: uid})
|
|
||||||
.project(proj)
|
|
||||||
.exec(function(err, member){
|
|
||||||
if (err) return next(err);
|
|
||||||
if (!member) return res.status(404).json({err: 'Member '+uid+' for challenge '+cid+' not found'});
|
|
||||||
res.json(member[0]);
|
|
||||||
uid = cid = null;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CREATE
|
// CREATE
|
||||||
|
|||||||
@@ -871,13 +871,9 @@ _.each(shared.ops, function(op,k){
|
|||||||
if (k === 'reroll') k = 'userReroll';
|
if (k === 'reroll') k = 'userReroll';
|
||||||
// if (k === 'reset') k = 'resetUser';
|
// if (k === 'reset') k = 'resetUser';
|
||||||
|
|
||||||
api[k] = async function (req, res, next) {
|
api[k] = function (req, res, next) {
|
||||||
try {
|
req.v2 = true;
|
||||||
req.v2 = true;
|
v3UserController[k].handler(req, res, next).catch(next);
|
||||||
await v3UserController[k](req, res, next);
|
|
||||||
} catch (err) {
|
|
||||||
next(err);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else if (!api[k]) {
|
} else if (!api[k]) {
|
||||||
api[k] = function(req, res, next) {
|
api[k] = function(req, res, next) {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import swagger from 'swagger-node-express';
|
|||||||
// import shared from '../../../../common';
|
// import shared from '../../../../common';
|
||||||
import express from 'express';
|
import express from 'express';
|
||||||
import analytics from './analytics';
|
import analytics from './analytics';
|
||||||
|
import responseHandler from './response';
|
||||||
|
|
||||||
const v2app = express();
|
const v2app = express();
|
||||||
|
|
||||||
@@ -13,6 +14,8 @@ v2app.set('view engine', 'jade');
|
|||||||
v2app.set('views', `${__dirname}/../../../views`);
|
v2app.set('views', `${__dirname}/../../../views`);
|
||||||
|
|
||||||
v2app.use(analytics);
|
v2app.use(analytics);
|
||||||
|
v2app.use(responseHandler);
|
||||||
|
|
||||||
|
|
||||||
// Custom Directives
|
// Custom Directives
|
||||||
v2app.use('/', require('../../routes/api-v2/auth'));
|
v2app.use('/', require('../../routes/api-v2/auth'));
|
||||||
|
|||||||
Reference in New Issue
Block a user