add support for user._tmp, disable user.filters since it is not used

This commit is contained in:
Matteo Pagliazzi
2016-02-29 23:20:13 +01:00
parent a04bf4c045
commit e5224a7b2a
5 changed files with 44 additions and 12 deletions

View File

@@ -175,7 +175,6 @@ export default function scoreTask (options = {}, req = {}) {
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
// the API consumer, then cleared afterwards
user._tmp = {};

View File

@@ -72,10 +72,11 @@ describe('Base model plugin', () => {
schema.plugin(baseModel, options);
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(options.toJSONTransform).to.be.calledWith(objToTransform);
expect(options.toJSONTransform).to.be.calledWith(objToTransform, doc);
});
it('accepts a transform function for sanitize', () => {

View 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');
});
});

View File

@@ -55,7 +55,7 @@ export default function baseModel (schema, options = {}) {
});
// 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 () {

View File

@@ -105,9 +105,10 @@ export let schema = new Schema({
},
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 {};
}},
}}, */
purchased: {
ads: {type: Boolean, default: false},
@@ -516,13 +517,11 @@ schema.plugin(baseModel, {
'auth.local.salt', 'tasksOrder', 'tags', 'stats', 'challenges', 'guilds', 'party._id', 'party.quest',
'invitations', 'balance', 'backer', 'contributor'],
private: ['auth.local.hashed_password', 'auth.local.salt'],
toJSONTransform: function userToJSON (doc) {
// FIXME? Is this a reference to `doc.filters` or just disabled code? Remove?
// TODO this works?
// doc.filters = {};
// doc._tmp = this._tmp; // be sure to send down drop notifs
toJSONTransform: function userToJSON (plainObj, originalDoc) {
// doc.filters = {}; Not saved
plainObj._tmp = originalDoc._tmp; // be sure to send down drop notifs TODO how to test?
return doc;
return plainObj;
},
});