mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 14:47:53 +01:00
* 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
54 lines
1.9 KiB
JavaScript
54 lines
1.9 KiB
JavaScript
import common from '../../../../common';
|
|
import Bluebird from 'bluebird';
|
|
import {
|
|
chatDefaults,
|
|
TAVERN_ID,
|
|
} from '../group';
|
|
import { defaults } from 'lodash';
|
|
|
|
import schema from './schema';
|
|
|
|
schema.methods.isSubscribed = function isSubscribed () {
|
|
return !!this.purchased.plan.customerId; // eslint-disable-line no-implicit-coercion
|
|
};
|
|
|
|
// Get an array of groups ids the user is member of
|
|
schema.methods.getGroups = function getUserGroups () {
|
|
let userGroups = this.guilds.slice(0); // clone user.guilds so we don't modify the original
|
|
if (this.party._id) userGroups.push(this.party._id);
|
|
userGroups.push(TAVERN_ID);
|
|
return userGroups;
|
|
};
|
|
|
|
schema.methods.sendMessage = async function sendMessage (userToReceiveMessage, message) {
|
|
let sender = this;
|
|
|
|
common.refPush(userToReceiveMessage.inbox.messages, chatDefaults(message, sender));
|
|
userToReceiveMessage.inbox.newMessages++;
|
|
userToReceiveMessage._v++;
|
|
userToReceiveMessage.markModified('inbox.messages');
|
|
|
|
common.refPush(sender.inbox.messages, defaults({sent: true}, chatDefaults(message, userToReceiveMessage)));
|
|
sender.markModified('inbox.messages');
|
|
|
|
let promises = [userToReceiveMessage.save(), sender.save()];
|
|
await Bluebird.all(promises);
|
|
};
|
|
|
|
schema.methods.addNotification = function addUserNotification (type, data = {}) {
|
|
this.notifications.push({
|
|
type,
|
|
data,
|
|
});
|
|
};
|
|
|
|
// Add stats.toNextLevel, stats.maxMP and stats.maxHealth
|
|
// to a JSONified User object
|
|
schema.methods.addComputedStatsToJSONObj = function addComputedStatsToUserJSONObj (obj) {
|
|
// NOTE: if an item is manually added to user.stats then
|
|
// common/fns/predictableRandom must be tweaked so the new item is not considered.
|
|
// Otherwise the client will have it while the server won't and the results will be different.
|
|
obj.stats.toNextLevel = common.tnl(this.stats.lvl);
|
|
obj.stats.maxHealth = common.maxHealth;
|
|
obj.stats.maxMP = common.statsComputed(this).maxMP;
|
|
}; |