From 8ea05c3c46eaf435975a2f7d0d98f7d181c093c1 Mon Sep 17 00:00:00 2001 From: Matteo Pagliazzi Date: Sun, 10 Apr 2016 19:09:53 +0200 Subject: [PATCH] v3 adapt v2: fix a few bugs with batchUpdate and tasks --- website/src/controllers/api-v2/user.js | 24 +++++++++++++----------- website/src/models/task.js | 12 ++++++++++++ 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/website/src/controllers/api-v2/user.js b/website/src/controllers/api-v2/user.js index f1aab61023..f1b0cd9ecc 100644 --- a/website/src/controllers/api-v2/user.js +++ b/website/src/controllers/api-v2/user.js @@ -820,6 +820,7 @@ api.deleteTask = function(req, res, next) { api.updateTask = function(req, res, next) { var user = res.locals.user; + req.body = Tasks.Task.fromJSONV2(req.body); Tasks.Task.findOne({ _id: req.params.id, @@ -845,6 +846,7 @@ api.addTask = function(req, res, next) { var user = res.locals.user; req.body.type = req.body.type || 'habit'; 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)); @@ -920,8 +922,8 @@ api.batchUpdate = function(req, res, next) { var oldJson = res.json; // Stash user.save, we'll queue the save op till the end (so we don't overload the server) - var oldSave = user.save; - user.save = function(cb){cb(null,user)} + //var oldSave = user.save; + //user.save = function(cb){cb(null,user)} // Setup the array of functions we're going to call in parallel with async res.locals.ops = []; @@ -943,13 +945,13 @@ api.batchUpdate = function(req, res, next) { }); }) // Finally, save user at the end - .concat(function(){ + .concat(/*function(){ user.save = oldSave; user.save(arguments[arguments.length-1]); - }); + }*/); // 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.send = oldSend; if (err) return next(err); @@ -957,14 +959,14 @@ api.batchUpdate = function(req, res, next) { var response; // return only drops & streaks - if (_user._tmp && _user._tmp.drop){ - response = _user.toJSON(); + if (user._tmp && user._tmp.drop){ + response = user.toJSON(); res.status(200).json({_tmp: {drop: response._tmp.drop}, _v: response._v}); // Fetch full user object } else if (res.locals.wasModified){ // 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); response = transformedData; @@ -973,12 +975,12 @@ api.batchUpdate = function(req, res, next) { }); // return only the version number } else{ - response = _user.toJSON(); + response = user.toJSON(); res.status(200).json({_v: response._v}); } - user.fns.nullify(); - user = res.locals.user = oldSend = oldJson = oldSave = null; + //user.fns.nullify(); + user = res.locals.user = oldSend = oldJson = null; }); }; diff --git a/website/src/models/task.js b/website/src/models/task.js index 3d296eb973..354f8ed033 100644 --- a/website/src/models/task.js +++ b/website/src/models/task.js @@ -123,6 +123,18 @@ TaskSchema.methods.toJSONV2 = function toJSONV2 () { 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 export let Task = mongoose.model('Task', TaskSchema);