mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-19 15:48:04 +01:00
[#1741] remove _id from TaskSchema (we're too deep in our legacy task.id
requirement, we can revisit later)
This commit is contained in:
@@ -366,7 +366,7 @@ function unlink(user, cid, keep, tid) {
|
||||
user.tasks[tid].challenge = {};
|
||||
break;
|
||||
case 'remove':
|
||||
user[user.tasks[tid].type+'s'].id(tid).remove();
|
||||
user.deleteTask(tid);
|
||||
break;
|
||||
case 'keep-all':
|
||||
_.each(user.tasks, function(t){
|
||||
@@ -378,7 +378,7 @@ function unlink(user, cid, keep, tid) {
|
||||
case 'remove-all':
|
||||
_.each(user.tasks, function(t){
|
||||
if (t.challenge && t.challenge.id == cid) {
|
||||
user[t.type+'s'].id(t.id).remove();
|
||||
user.deleteTask(t.id);
|
||||
}
|
||||
})
|
||||
break;
|
||||
|
||||
@@ -64,11 +64,6 @@ api.verifyTaskExists = function(req, res, next) {
|
||||
return next();
|
||||
};
|
||||
|
||||
function deleteTask(user, task) {
|
||||
var t = user[task.type+'s'].id(task.id);
|
||||
if (t) t.remove();
|
||||
};
|
||||
|
||||
function addTask(user, task) {
|
||||
task = helpers.taskDefaults(task);
|
||||
user[task.type+'s'].unshift(task);
|
||||
@@ -169,8 +164,9 @@ api.getTask = function(req, res, next) {
|
||||
* Delete Task
|
||||
*/
|
||||
api.deleteTask = function(req, res, next) {
|
||||
deleteTask(res.locals.user, res.locals.task);
|
||||
res.locals.user.save(function(err) {
|
||||
var user = res.locals.user;
|
||||
user.deleteTask(res.locals.task.id);
|
||||
user.save(function(err) {
|
||||
if (err) return res.json(500, {err: err});
|
||||
res.send(204);
|
||||
});
|
||||
@@ -200,7 +196,7 @@ api.updateTasks = function(req, res, next) {
|
||||
if (task.id) {
|
||||
// delete
|
||||
if (task.del) {
|
||||
deleteTask(user, task);
|
||||
user.deleteTask(task.id);
|
||||
task = {deleted: true};
|
||||
} else {
|
||||
// Update
|
||||
|
||||
@@ -39,11 +39,6 @@ ChallengeSchema.methods.toJSON = function(){
|
||||
var doc = this.toObject();
|
||||
doc.memberCount = doc.members ? _.size(doc.members) : doc.memberCount; // @see pre('save') comment above
|
||||
doc._isMember = this._isMember;
|
||||
_.each(['habits','dailys','todos','rewards'], function(type){
|
||||
_.each(doc[type],function(task){
|
||||
task.id = task.id || task._id;
|
||||
})
|
||||
})
|
||||
return doc;
|
||||
}
|
||||
|
||||
|
||||
@@ -13,8 +13,8 @@ var _ = require('lodash');
|
||||
// -----------
|
||||
|
||||
var TaskSchema = new Schema({
|
||||
_id:{type: String,'default': helpers.uuid},
|
||||
id: String,
|
||||
//_id:{type: String,'default': helpers.uuid},
|
||||
id: {type: String,'default': helpers.uuid},
|
||||
history: Array, // [{date:Date, value:Number}], // this causes major performance problems
|
||||
text: String,
|
||||
date: Date, // due date for todos
|
||||
@@ -34,11 +34,8 @@ var TaskSchema = new Schema({
|
||||
winner: String // user.profile.name
|
||||
// group: {type: 'Strign', ref: 'Group'} // if we restore this, rename `id` above to `challenge`
|
||||
}
|
||||
},{
|
||||
_id: false
|
||||
});
|
||||
|
||||
TaskSchema.pre('save', function(next){
|
||||
this.id = this.id || this._id;
|
||||
next()
|
||||
})
|
||||
|
||||
module.exports.schema = TaskSchema;
|
||||
@@ -201,6 +201,13 @@ var UserSchema = new Schema({
|
||||
minimize: false // So empty objects are returned
|
||||
});
|
||||
|
||||
UserSchema.methods.deleteTask = function(tid) {
|
||||
//user[t.type+'s'].id(t.id).remove();
|
||||
var task = this.tasks[tid];
|
||||
var i = this[task.type+'s'].indexOf(task);
|
||||
if (~i) this[task.type+'s'].splice(i,1);
|
||||
}
|
||||
|
||||
UserSchema.methods.toJSON = function() {
|
||||
var doc = this.toObject();
|
||||
doc.id = doc._id;
|
||||
@@ -209,12 +216,6 @@ UserSchema.methods.toJSON = function() {
|
||||
doc.filters = {};
|
||||
doc._tmp = this._tmp; // be sure to send down drop notifs
|
||||
|
||||
_.each(['habits','dailys','todos','rewards'], function(type){
|
||||
_.each(doc[type],function(task){
|
||||
task.id = task.id || task._id;
|
||||
})
|
||||
})
|
||||
|
||||
return doc;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user