mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-19 07:37:25 +01:00
add support for user._tmp, disable user.filters since it is not used
This commit is contained in:
@@ -175,7 +175,6 @@ export default function scoreTask (options = {}, req = {}) {
|
|||||||
exp: user.stats.exp,
|
exp: user.stats.exp,
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO return or pass to cb, don't add to user object
|
|
||||||
// This is for setting one-time temporary flags, such as streakBonus or itemDropped. Useful for notifying
|
// This is for setting one-time temporary flags, such as streakBonus or itemDropped. Useful for notifying
|
||||||
// the API consumer, then cleared afterwards
|
// the API consumer, then cleared afterwards
|
||||||
user._tmp = {};
|
user._tmp = {};
|
||||||
|
|||||||
@@ -72,10 +72,11 @@ describe('Base model plugin', () => {
|
|||||||
schema.plugin(baseModel, options);
|
schema.plugin(baseModel, options);
|
||||||
|
|
||||||
let objToTransform = {ok: true, amPrivate: true};
|
let objToTransform = {ok: true, amPrivate: true};
|
||||||
let privatized = schema.options.toJSON.transform({}, objToTransform);
|
let doc = {doc: true};
|
||||||
|
let privatized = schema.options.toJSON.transform(doc, objToTransform);
|
||||||
|
|
||||||
expect(privatized).to.equals(true);
|
expect(privatized).to.equals(true);
|
||||||
expect(options.toJSONTransform).to.be.calledWith(objToTransform);
|
expect(options.toJSONTransform).to.be.calledWith(objToTransform, doc);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('accepts a transform function for sanitize', () => {
|
it('accepts a transform function for sanitize', () => {
|
||||||
|
|||||||
33
test/api/v3/unit/models/user.test.js
Normal file
33
test/api/v3/unit/models/user.test.js
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import { model as User } from '../../../../../website/src/models/user';
|
||||||
|
|
||||||
|
describe('User Model', () => {
|
||||||
|
it('keeps user._tmp when calling .toJSON', () => {
|
||||||
|
let user = new User({
|
||||||
|
auth: {
|
||||||
|
local: {
|
||||||
|
username: 'username',
|
||||||
|
lowerCaseUsername: 'username',
|
||||||
|
email: 'email@email.email',
|
||||||
|
salt: 'salt',
|
||||||
|
hashed_password: 'hashed_password', // eslint-disable-line camelcase
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
user._tmp = {ok: true};
|
||||||
|
user._nonTmp = {ok: true};
|
||||||
|
|
||||||
|
expect(user._tmp).to.eql({ok: true});
|
||||||
|
expect(user._nonTmp).to.eql({ok: true});
|
||||||
|
|
||||||
|
let toObject = user.toObject();
|
||||||
|
let toJSON = user.toJSON();
|
||||||
|
|
||||||
|
expect(toObject).to.not.have.keys('_tmp');
|
||||||
|
expect(toObject).to.not.have.keys('_nonTmp');
|
||||||
|
|
||||||
|
expect(toJSON).to.have.any.key('_tmp');
|
||||||
|
expect(toJSON._tmp).to.eql({ok: true});
|
||||||
|
expect(toJSON).to.not.have.keys('_nonTmp');
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -55,7 +55,7 @@ export default function baseModel (schema, options = {}) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Allow an additional toJSON transform function to be used
|
// Allow an additional toJSON transform function to be used
|
||||||
return options.toJSONTransform ? options.toJSONTransform(plainObj) : plainObj;
|
return options.toJSONTransform ? options.toJSONTransform(plainObj, doc) : plainObj;
|
||||||
};
|
};
|
||||||
|
|
||||||
schema.statics.getModelPaths = function getModelPaths () {
|
schema.statics.getModelPaths = function getModelPaths () {
|
||||||
|
|||||||
@@ -105,9 +105,10 @@ export let schema = new Schema({
|
|||||||
},
|
},
|
||||||
|
|
||||||
balance: {type: Number, default: 0},
|
balance: {type: Number, default: 0},
|
||||||
filters: {type: Schema.Types.Mixed, default: () => {
|
// Not saved on the user TODO remove with migration
|
||||||
|
/* filters: {type: Schema.Types.Mixed, default: () => {
|
||||||
return {};
|
return {};
|
||||||
}},
|
}}, */
|
||||||
|
|
||||||
purchased: {
|
purchased: {
|
||||||
ads: {type: Boolean, default: false},
|
ads: {type: Boolean, default: false},
|
||||||
@@ -516,13 +517,11 @@ schema.plugin(baseModel, {
|
|||||||
'auth.local.salt', 'tasksOrder', 'tags', 'stats', 'challenges', 'guilds', 'party._id', 'party.quest',
|
'auth.local.salt', 'tasksOrder', 'tags', 'stats', 'challenges', 'guilds', 'party._id', 'party.quest',
|
||||||
'invitations', 'balance', 'backer', 'contributor'],
|
'invitations', 'balance', 'backer', 'contributor'],
|
||||||
private: ['auth.local.hashed_password', 'auth.local.salt'],
|
private: ['auth.local.hashed_password', 'auth.local.salt'],
|
||||||
toJSONTransform: function userToJSON (doc) {
|
toJSONTransform: function userToJSON (plainObj, originalDoc) {
|
||||||
// FIXME? Is this a reference to `doc.filters` or just disabled code? Remove?
|
// doc.filters = {}; Not saved
|
||||||
// TODO this works?
|
plainObj._tmp = originalDoc._tmp; // be sure to send down drop notifs TODO how to test?
|
||||||
// doc.filters = {};
|
|
||||||
// doc._tmp = this._tmp; // be sure to send down drop notifs
|
|
||||||
|
|
||||||
return doc;
|
return plainObj;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user