fix: Add _legacyId prop to tasks to support non-uuid identifiers

This commit is contained in:
Blade Barringer
2016-05-17 15:12:44 -05:00
parent 4f1d738272
commit 5931aee26b
2 changed files with 46 additions and 18 deletions

View File

@@ -1,6 +1,7 @@
var url = require('url'); var url = require('url');
var ipn = require('paypal-ipn'); var ipn = require('paypal-ipn');
var _ = require('lodash'); var _ = require('lodash');
var validator = require('validator');
var nconf = require('nconf'); var nconf = require('nconf');
var asyncM = require('async'); var asyncM = require('async');
var shared = require('../../../../common'); var shared = require('../../../../common');
@@ -89,6 +90,7 @@ api.score = function(req, res, next) {
direction = req.params.direction, direction = req.params.direction,
user = res.locals.user, user = res.locals.user,
body = req.body || {}, body = req.body || {},
taskQuery = { userId: user._id },
task; task;
// Send error responses for improper API call // Send error responses for improper API call
@@ -98,23 +100,33 @@ api.score = function(req, res, next) {
return res.json(400, {err: ":direction must be 'up' or 'down'"}); return res.json(400, {err: ":direction must be 'up' or 'down'"});
} }
Tasks.Task.findOne({ if (validator.isUUID(id)) {
_id: id, taskQuery._id = id;
userId: user._id } else {
}, function(err, task){ taskQuery._legacyId = id;
}
Tasks.Task.findOne(taskQuery, function(err, task){
if(err) return next(err); if(err) return next(err);
// If exists already, score it // If exists already, score it
if (!task) { if (!task) {
// If it doesn't exist, this is likely a 3rd party up/down - create a new one, then score it // If it doesn't exist, this is likely a 3rd party up/down - create a new one, then score it
// Defaults. Other defaults are handled in user.ops.addTask() // Defaults. Other defaults are handled in user.ops.addTask()
task = new Tasks.Task({ var taskOptions = {
_id: id, // TODO this might easily lead to conflicts as ids are now unique db-wide
type: body.type || 'habit', type: body.type || 'habit',
text: body.text || id, text: body.text || id,
userId: user._id, userId: user._id,
notes: body.notes || "This task was created by a third-party service. Feel free to edit, it won't harm the connection to that service. Additionally, multiple services may piggy-back off this task." // TODO translate notes: body.notes || "This task was created by a third-party service. Feel free to edit, it won't harm the connection to that service. Additionally, multiple services may piggy-back off this task." // TODO translate
}); }
if (validator.isUUID(id)) {
taskOptions._id = id; // TODO this might easily lead to conflicts as ids are now unique db-wide
} else {
taskOptions._legacyId = id;
}
task = new Tasks.Task(taskOptions);
user.tasksOrder[task.type + 's'].unshift(task._id); user.tasksOrder[task.type + 's'].unshift(task._id);
} }
@@ -206,12 +218,17 @@ api.getTasks = function(req, res, next) {
* Get Task * Get Task
*/ */
api.getTask = function(req, res, next) { api.getTask = function(req, res, next) {
var user = res.locals.user; var user = res.locals.user,
id = req.params.id,
taskQuery = { userId: user._id };
Tasks.Task.findOne({ if (validator.isUUID(id)) {
userId: user._id, taskQuery._id = id;
_id: req.params.id, } else {
}, function (err, task) { taskQuery._legacyId = id;
}
Tasks.Task.findOne(taskQuery, function (err, task) {
if (err) return next(err); if (err) return next(err);
if (!task) return res.status(404).json({err: shared.i18n.t('messageTaskNotFound')}); if (!task) return res.status(404).json({err: shared.i18n.t('messageTaskNotFound')});
res.status(200).json(task.toJSONV2()); res.status(200).json(task.toJSONV2());
@@ -830,13 +847,19 @@ 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,
taskQuery = { userId: user._id },
id = req.params.id;
req.body = Tasks.Task.fromJSONV2(req.body); req.body = Tasks.Task.fromJSONV2(req.body);
Tasks.Task.findOne({ if (validator.isUUID(id)) {
_id: req.params.id, taskQuery._id = id;
userId: user._id } else {
}, function(err, task) { taskQuery._legacyId = id;
}
Tasks.Task.findOne(taskQuery, function(err, task) {
if(err) return next(err); if(err) return next(err);
if(!task) return res.status(404).json({err: 'Task not found.'}) if(!task) return res.status(404).json({err: 'Task not found.'})

View File

@@ -17,6 +17,7 @@ export let tasksTypes = ['habit', 'daily', 'todo', 'reward'];
// Important // Important
// When something changes here remember to update the client side model at common/script/libs/taskDefaults // When something changes here remember to update the client side model at common/script/libs/taskDefaults
export let TaskSchema = new Schema({ export let TaskSchema = new Schema({
_legacyId: String, // TODO Remove when v2 is deprecated
type: {type: String, enum: tasksTypes, required: true, default: tasksTypes[0]}, type: {type: String, enum: tasksTypes, required: true, default: tasksTypes[0]},
text: {type: String, required: true}, text: {type: String, required: true},
notes: {type: String, default: ''}, notes: {type: String, default: ''},
@@ -119,7 +120,11 @@ TaskSchema.methods.scoreChallengeTask = async function scoreChallengeTask (delta
// toJSON for API v2 // toJSON for API v2
TaskSchema.methods.toJSONV2 = function toJSONV2 () { TaskSchema.methods.toJSONV2 = function toJSONV2 () {
let toJSON = this.toJSON(); let toJSON = this.toJSON();
toJSON.id = toJSON._id; if (toJSON._legacyId) {
toJSON.id = toJSON._legacyId;
} else {
toJSON.id = toJSON._id;
}
let v3Tags = this.tags; let v3Tags = this.tags;