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 = {};
|
user.tasks[tid].challenge = {};
|
||||||
break;
|
break;
|
||||||
case 'remove':
|
case 'remove':
|
||||||
user[user.tasks[tid].type+'s'].id(tid).remove();
|
user.deleteTask(tid);
|
||||||
break;
|
break;
|
||||||
case 'keep-all':
|
case 'keep-all':
|
||||||
_.each(user.tasks, function(t){
|
_.each(user.tasks, function(t){
|
||||||
@@ -378,7 +378,7 @@ function unlink(user, cid, keep, tid) {
|
|||||||
case 'remove-all':
|
case 'remove-all':
|
||||||
_.each(user.tasks, function(t){
|
_.each(user.tasks, function(t){
|
||||||
if (t.challenge && t.challenge.id == cid) {
|
if (t.challenge && t.challenge.id == cid) {
|
||||||
user[t.type+'s'].id(t.id).remove();
|
user.deleteTask(t.id);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -64,11 +64,6 @@ api.verifyTaskExists = function(req, res, next) {
|
|||||||
return next();
|
return next();
|
||||||
};
|
};
|
||||||
|
|
||||||
function deleteTask(user, task) {
|
|
||||||
var t = user[task.type+'s'].id(task.id);
|
|
||||||
if (t) t.remove();
|
|
||||||
};
|
|
||||||
|
|
||||||
function addTask(user, task) {
|
function addTask(user, task) {
|
||||||
task = helpers.taskDefaults(task);
|
task = helpers.taskDefaults(task);
|
||||||
user[task.type+'s'].unshift(task);
|
user[task.type+'s'].unshift(task);
|
||||||
@@ -169,8 +164,9 @@ api.getTask = function(req, res, next) {
|
|||||||
* Delete Task
|
* Delete Task
|
||||||
*/
|
*/
|
||||||
api.deleteTask = function(req, res, next) {
|
api.deleteTask = function(req, res, next) {
|
||||||
deleteTask(res.locals.user, res.locals.task);
|
var user = res.locals.user;
|
||||||
res.locals.user.save(function(err) {
|
user.deleteTask(res.locals.task.id);
|
||||||
|
user.save(function(err) {
|
||||||
if (err) return res.json(500, {err: err});
|
if (err) return res.json(500, {err: err});
|
||||||
res.send(204);
|
res.send(204);
|
||||||
});
|
});
|
||||||
@@ -200,7 +196,7 @@ api.updateTasks = function(req, res, next) {
|
|||||||
if (task.id) {
|
if (task.id) {
|
||||||
// delete
|
// delete
|
||||||
if (task.del) {
|
if (task.del) {
|
||||||
deleteTask(user, task);
|
user.deleteTask(task.id);
|
||||||
task = {deleted: true};
|
task = {deleted: true};
|
||||||
} else {
|
} else {
|
||||||
// Update
|
// Update
|
||||||
|
|||||||
@@ -39,11 +39,6 @@ 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(doc[type],function(task){
|
|
||||||
task.id = task.id || task._id;
|
|
||||||
})
|
|
||||||
})
|
|
||||||
return doc;
|
return doc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,8 +13,8 @@ var _ = require('lodash');
|
|||||||
// -----------
|
// -----------
|
||||||
|
|
||||||
var TaskSchema = new Schema({
|
var TaskSchema = new Schema({
|
||||||
_id:{type: String,'default': helpers.uuid},
|
//_id:{type: String,'default': helpers.uuid},
|
||||||
id: String,
|
id: {type: String,'default': helpers.uuid},
|
||||||
history: Array, // [{date:Date, value:Number}], // this causes major performance problems
|
history: Array, // [{date:Date, value:Number}], // this causes major performance problems
|
||||||
text: String,
|
text: String,
|
||||||
date: Date, // due date for todos
|
date: Date, // due date for todos
|
||||||
@@ -34,11 +34,8 @@ var TaskSchema = new Schema({
|
|||||||
winner: String // user.profile.name
|
winner: String // user.profile.name
|
||||||
// group: {type: 'Strign', ref: 'Group'} // if we restore this, rename `id` above to `challenge`
|
// 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;
|
module.exports.schema = TaskSchema;
|
||||||
@@ -201,6 +201,13 @@ var UserSchema = new Schema({
|
|||||||
minimize: false // So empty objects are returned
|
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() {
|
UserSchema.methods.toJSON = function() {
|
||||||
var doc = this.toObject();
|
var doc = this.toObject();
|
||||||
doc.id = doc._id;
|
doc.id = doc._id;
|
||||||
@@ -209,12 +216,6 @@ 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
|
||||||
|
|
||||||
_.each(['habits','dailys','todos','rewards'], function(type){
|
|
||||||
_.each(doc[type],function(task){
|
|
||||||
task.id = task.id || task._id;
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
return doc;
|
return doc;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user