API: return computed stats for members routes (#7870)

* api: return computed stats for members responses

* add integration tests for computed stats

* add unit tests for computed stats

* clarify test name

* add missing query parameter to test case

* reset test database before running API tests for the Hall
This commit is contained in:
Matteo Pagliazzi
2016-08-04 19:56:00 +02:00
committed by GitHub
parent d097868cad
commit d7ccf2bbe1
8 changed files with 96 additions and 17 deletions

View File

@@ -50,7 +50,10 @@ api.getMember = {
if (!member) throw new NotFound(res.t('userWithIDNotFound', {userId: memberId}));
// manually call toJSON with minimize: true so empty paths aren't returned
res.respond(200, member.toJSON({minimize: true}));
let memberToJSON = member.toJSON({minimize: true});
member.addComputedStatsToJSONObj(memberToJSON);
res.respond(200, memberToJSON);
},
};
@@ -100,6 +103,7 @@ function _getMembersForItem (type) {
let query = {};
let fields = nameFields;
let addComputedStats = false; // add computes stats to the member info when items and stats are available
if (type === 'challenge-members') {
query.challenges = challenge._id;
@@ -111,6 +115,7 @@ function _getMembersForItem (type) {
if (req.query.includeAllPublicFields === 'true') {
fields = memberFields;
addComputedStats = true;
}
}
} else if (type === 'group-invites') {
@@ -138,7 +143,13 @@ function _getMembersForItem (type) {
.exec();
// manually call toJSON with minimize: true so empty paths aren't returned
res.respond(200, members.map(member => member.toJSON({minimize: true})));
let membersToJSON = members.map(member => {
let memberToJSON = member.toJSON({minimize: true});
if (addComputedStats) member.addComputedStatsToJSONObj(memberToJSON);
return memberToJSON;
});
res.respond(200, membersToJSON);
};
}

View File

@@ -30,19 +30,14 @@ api.getUser = {
middlewares: [authWithHeaders()],
url: '/user',
async handler (req, res) {
let user = res.locals.user.toJSON();
let user = res.locals.user;
let userToJSON = user.toJSON();
// Remove apiToken from response TODO make it private at the user level? returned in signup/login
delete user.apiToken;
delete userToJSON.apiToken;
// TODO move to model? (maybe virtuals, maybe in toJSON)
// NOTE: if an item is manually added to user.stats common/fns/predictableRandom must be tweaked
// so it's not considered. Otherwise the client will have it while the server won't and the results will be different.
user.stats.toNextLevel = common.tnl(user.stats.lvl);
user.stats.maxHealth = common.maxHealth;
user.stats.maxMP = common.statsComputed(user).maxMP;
return res.respond(200, user);
user.addComputedStatsToJSONObj(userToJSON);
return res.respond(200, userToJSON);
},
};