v3 adapt v2: fix a few bugs with batchUpdate and tasks

This commit is contained in:
Matteo Pagliazzi
2016-04-10 19:09:53 +02:00
parent c6879aa5df
commit 8ea05c3c46
2 changed files with 25 additions and 11 deletions

View File

@@ -820,6 +820,7 @@ api.deleteTask = function(req, res, next) {
api.updateTask = function(req, res, next) { api.updateTask = function(req, res, next) {
var user = res.locals.user; var user = res.locals.user;
req.body = Tasks.Task.fromJSONV2(req.body);
Tasks.Task.findOne({ Tasks.Task.findOne({
_id: req.params.id, _id: req.params.id,
@@ -845,6 +846,7 @@ api.addTask = function(req, res, next) {
var user = res.locals.user; var user = res.locals.user;
req.body.type = req.body.type || 'habit'; req.body.type = req.body.type || 'habit';
req.body.text = req.body.text || 'text'; req.body.text = req.body.text || 'text';
req.body = Tasks.Task.fromJSONV2(req.body);
var task = new Tasks[req.body.type](Tasks.Task.sanitizeCreate(req.body)); var task = new Tasks[req.body.type](Tasks.Task.sanitizeCreate(req.body));
@@ -920,8 +922,8 @@ api.batchUpdate = function(req, res, next) {
var oldJson = res.json; var oldJson = res.json;
// Stash user.save, we'll queue the save op till the end (so we don't overload the server) // Stash user.save, we'll queue the save op till the end (so we don't overload the server)
var oldSave = user.save; //var oldSave = user.save;
user.save = function(cb){cb(null,user)} //user.save = function(cb){cb(null,user)}
// Setup the array of functions we're going to call in parallel with async // Setup the array of functions we're going to call in parallel with async
res.locals.ops = []; res.locals.ops = [];
@@ -943,13 +945,13 @@ api.batchUpdate = function(req, res, next) {
}); });
}) })
// Finally, save user at the end // Finally, save user at the end
.concat(function(){ .concat(/*function(){
user.save = oldSave; user.save = oldSave;
user.save(arguments[arguments.length-1]); user.save(arguments[arguments.length-1]);
}); }*/);
// call all the operations, then return the user object to the requester // call all the operations, then return the user object to the requester
asyncM.waterfall(ops, function(err,_user) { asyncM.waterfall(ops, function(err) {
res.json = oldJson; res.json = oldJson;
res.send = oldSend; res.send = oldSend;
if (err) return next(err); if (err) return next(err);
@@ -957,14 +959,14 @@ api.batchUpdate = function(req, res, next) {
var response; var response;
// return only drops & streaks // return only drops & streaks
if (_user._tmp && _user._tmp.drop){ if (user._tmp && user._tmp.drop){
response = _user.toJSON(); response = user.toJSON();
res.status(200).json({_tmp: {drop: response._tmp.drop}, _v: response._v}); res.status(200).json({_tmp: {drop: response._tmp.drop}, _v: response._v});
// Fetch full user object // Fetch full user object
} else if (res.locals.wasModified){ } else if (res.locals.wasModified){
// Preen 3-day past-completed To-Dos from Angular & mobile app // Preen 3-day past-completed To-Dos from Angular & mobile app
_user.getTransformedData(function(err, transformedData){ user.getTransformedData(function(err, transformedData){
if (err) next(err); if (err) next(err);
response = transformedData; response = transformedData;
@@ -973,12 +975,12 @@ api.batchUpdate = function(req, res, next) {
}); });
// return only the version number // return only the version number
} else{ } else{
response = _user.toJSON(); response = user.toJSON();
res.status(200).json({_v: response._v}); res.status(200).json({_v: response._v});
} }
user.fns.nullify(); //user.fns.nullify();
user = res.locals.user = oldSend = oldJson = oldSave = null; user = res.locals.user = oldSend = oldJson = null;
}); });
}; };

View File

@@ -123,6 +123,18 @@ TaskSchema.methods.toJSONV2 = function toJSONV2 () {
return toJSON; return toJSON;
}; };
TaskSchema.statics.fromJSONV2 = function toJSONV2 (taskObj) {
taskObj._id = taskObj.id;
let v2Tags = taskObj.tags || {};
taskObj.tags = [];
taskObj.tags = _.map(v2Tags, (tag, key) => key)
return taskObj;
};
// END of API v2 methods // END of API v2 methods
export let Task = mongoose.model('Task', TaskSchema); export let Task = mongoose.model('Task', TaskSchema);