mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-19 15:48:04 +01:00
[#1711] back to Schema.Types.Mixed for user.tasks, this makes me sad :(
This commit is contained in:
@@ -40,7 +40,6 @@ api.marketBuy = function(req, res, next){
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
------------------------------------------------------------------------
|
------------------------------------------------------------------------
|
||||||
Tasks
|
Tasks
|
||||||
@@ -65,12 +64,15 @@ api.verifyTaskExists = function(req, res, next) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
function deleteTask(user, task) {
|
function deleteTask(user, task) {
|
||||||
user[task.type+'s'].id(task.id).remove();
|
var i = _.findIndex(user[task.type+'s'], {id:task.id});
|
||||||
|
if (~i) {
|
||||||
|
user[task.type+'s'].splice(i, 1)
|
||||||
|
//user.markModified(task.type+'s')
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
function addTask(user, task) {
|
function addTask(user, task) {
|
||||||
task = helpers.taskDefaults(task);
|
task = helpers.taskDefaults(task);
|
||||||
task._id = task.id;
|
|
||||||
user[task.type+'s'].unshift(task);
|
user[task.type+'s'].unshift(task);
|
||||||
return task;
|
return task;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ var mongoose = require("mongoose");
|
|||||||
var Schema = mongoose.Schema;
|
var Schema = mongoose.Schema;
|
||||||
var helpers = require('habitrpg-shared/script/helpers');
|
var helpers = require('habitrpg-shared/script/helpers');
|
||||||
var _ = require('lodash');
|
var _ = require('lodash');
|
||||||
var TaskSchema = require('./task').schema;
|
|
||||||
var Group = require('./group').model;
|
var Group = require('./group').model;
|
||||||
|
|
||||||
var ChallengeSchema = new Schema({
|
var ChallengeSchema = new Schema({
|
||||||
@@ -10,10 +9,10 @@ var ChallengeSchema = new Schema({
|
|||||||
name: String,
|
name: String,
|
||||||
shortName: String,
|
shortName: String,
|
||||||
description: String,
|
description: String,
|
||||||
habits: [TaskSchema],
|
habits: [Schema.Types.Mixed],
|
||||||
dailys: [TaskSchema],
|
dailys: [Schema.Types.Mixed],
|
||||||
todos: [TaskSchema],
|
todos: [Schema.Types.Mixed],
|
||||||
rewards: [TaskSchema],
|
rewards: [Schema.Types.Mixed],
|
||||||
leader: {type: String, ref: 'User'},
|
leader: {type: String, ref: 'User'},
|
||||||
group: {type: String, ref: 'Group'},
|
group: {type: String, ref: 'Group'},
|
||||||
timestamp: {type: Date, 'default': Date.now},
|
timestamp: {type: Date, 'default': Date.now},
|
||||||
@@ -31,6 +30,11 @@ ChallengeSchema.virtual('tasks').get(function () {
|
|||||||
// FIXME this isn't always triggered, since we sometimes use update() or findByIdAndUpdate()
|
// FIXME this isn't always triggered, since we sometimes use update() or findByIdAndUpdate()
|
||||||
// @see https://github.com/LearnBoost/mongoose/issues/964
|
// @see https://github.com/LearnBoost/mongoose/issues/964
|
||||||
ChallengeSchema.pre('save', function(next){
|
ChallengeSchema.pre('save', function(next){
|
||||||
|
// @see comment in user.js pre(save)
|
||||||
|
this.markModified('habits');
|
||||||
|
this.markModified('dailys');
|
||||||
|
this.markModified('todos');
|
||||||
|
this.markModified('rewards');
|
||||||
this.memberCount = _.size(this.members);
|
this.memberCount = _.size(this.members);
|
||||||
next()
|
next()
|
||||||
})
|
})
|
||||||
@@ -39,11 +43,11 @@ ChallengeSchema.methods.toJSON = function(){
|
|||||||
var doc = this.toObject();
|
var doc = this.toObject();
|
||||||
doc.memberCount = doc.members ? _.size(doc.members) : doc.memberCount; // @see pre('save') comment above
|
doc.memberCount = doc.members ? _.size(doc.members) : doc.memberCount; // @see pre('save') comment above
|
||||||
doc._isMember = this._isMember;
|
doc._isMember = this._isMember;
|
||||||
_.each(['habits','dailys','todos','rewards'], function(type){
|
// _.each(['habits','dailys','todos','rewards'], function(type){
|
||||||
_.each(doc[type],function(task){
|
// _.each(doc[type],function(task){
|
||||||
task.id = task._id;
|
// task.id = task._id || task.id;
|
||||||
})
|
// })
|
||||||
})
|
// })
|
||||||
return doc;
|
return doc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ var mongoose = require("mongoose");
|
|||||||
var Schema = mongoose.Schema;
|
var Schema = mongoose.Schema;
|
||||||
var helpers = require('habitrpg-shared/script/helpers');
|
var helpers = require('habitrpg-shared/script/helpers');
|
||||||
var _ = require('lodash');
|
var _ = require('lodash');
|
||||||
var TaskSchema = require('./task').schema;
|
|
||||||
|
|
||||||
// User Schema
|
// User Schema
|
||||||
// -----------
|
// -----------
|
||||||
@@ -191,10 +190,10 @@ var UserSchema = new Schema({
|
|||||||
|
|
||||||
challenges: [{type: 'String', ref:'Challenge'}],
|
challenges: [{type: 'String', ref:'Challenge'}],
|
||||||
|
|
||||||
habits: [TaskSchema],
|
habits: [Schema.Types.Mixed],
|
||||||
dailys: [TaskSchema],
|
dailys: [Schema.Types.Mixed],
|
||||||
todos: [TaskSchema],
|
todos: [Schema.Types.Mixed],
|
||||||
rewards: [TaskSchema],
|
rewards: [Schema.Types.Mixed],
|
||||||
|
|
||||||
}, {
|
}, {
|
||||||
strict: true,
|
strict: true,
|
||||||
@@ -209,12 +208,11 @@ UserSchema.methods.toJSON = function() {
|
|||||||
doc.filters = {};
|
doc.filters = {};
|
||||||
doc._tmp = this._tmp; // be sure to send down drop notifs
|
doc._tmp = this._tmp; // be sure to send down drop notifs
|
||||||
|
|
||||||
// TODO why isnt' this happening automatically given the TaskSchema.methods.toJSON above?
|
// _.each(['habits','dailys','todos','rewards'], function(type){
|
||||||
_.each(['habits','dailys','todos','rewards'], function(type){
|
// _.each(doc[type],function(task){
|
||||||
_.each(doc[type],function(task){
|
// task.id = task._id || task.id;
|
||||||
task.id = task._id;
|
// })
|
||||||
})
|
// })
|
||||||
})
|
|
||||||
|
|
||||||
return doc;
|
return doc;
|
||||||
};
|
};
|
||||||
@@ -229,7 +227,16 @@ UserSchema.virtual('tasks').get(function () {
|
|||||||
// Custom setter/getter virtuals?
|
// Custom setter/getter virtuals?
|
||||||
|
|
||||||
UserSchema.pre('save', function(next) {
|
UserSchema.pre('save', function(next) {
|
||||||
//this.markModified('tasks');
|
// I finally figured out when markModified() is required - when using Schema.Type.Mixed or Array. Let's say you
|
||||||
|
// have `tasks: [{text:String, notes:String}]`. Because it's a defined schema, Mongoose can track attribute changes.
|
||||||
|
// But if you have `tasks: [Schema.Types.Mixed]`, Mongoose can't track underlying attr changes - only top-level changes
|
||||||
|
// (via splice(), push/unshift(), etc). Adding a workaround here, which is very inefficient - we'll want to find
|
||||||
|
// each location where a task attr is modified and use `user.markModified(task.type+'s')`
|
||||||
|
// (@see https://github.com/HabitRPG/habitrpg/issues/1711#issuecomment-27626467)
|
||||||
|
this.markModified('habits');
|
||||||
|
this.markModified('dailys');
|
||||||
|
this.markModified('todos');
|
||||||
|
this.markModified('rewards');
|
||||||
|
|
||||||
if (!this.profile.name) {
|
if (!this.profile.name) {
|
||||||
var fb = this.auth.facebook;
|
var fb = this.auth.facebook;
|
||||||
|
|||||||
Reference in New Issue
Block a user