mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-16 14:17:22 +01:00
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:
@@ -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);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user