port updateTask, addTask, clearCompleted, taskDefaults, uuid

This commit is contained in:
Matteo Pagliazzi
2016-04-03 21:50:32 +02:00
parent 060e3b1045
commit 382e391fd0
14 changed files with 322 additions and 99 deletions
+4
View File
@@ -121,6 +121,8 @@ import openMysteryItem from './ops/openMysteryItem';
import releasePets from './ops/releasePets';
import releaseBoth from './ops/releaseBoth';
import releaseMounts from './ops/releaseMounts';
import updateTask from './ops/updateTask';
import clearCompleted from './ops/clearCompleted';
api.ops = {
scoreTask,
@@ -143,6 +145,8 @@ api.ops = {
releasePets,
releaseBoth,
releaseMounts,
updateTask,
clearCompleted,
};
import handleTwoHanded from './fns/handleTwoHanded';
+43 -40
View File
@@ -1,71 +1,74 @@
import uuid from './uuid';
import { v4 as uuid } from 'uuid';
import _ from 'lodash';
import moment from 'moment';
/*
Even though Mongoose handles task defaults, we want to make sure defaults are set on the client-side before
sending up to the server for performance
*/
// Even though Mongoose handles task defaults, we want to make sure defaults are set on the client-side before
// sending up to the server for performance
// TODO revisit
// TODO move to client code?
module.exports = function(task) {
var defaults, ref, ref1, ref2;
if (task == null) {
task = {};
}
if (!(task.type && ((ref = task.type) === 'habit' || ref === 'daily' || ref === 'todo' || ref === 'reward'))) {
const tasksTypes = ['habit', 'daily', 'todo', 'reward'];
module.exports = function taskDefaults (task = {}) {
if (!task.type || tasksTypes.indexOf(task.type) === -1) {
task.type = 'habit';
}
defaults = {
id: uuid(),
text: task.id != null ? task.id : '',
let defaultId = uuid();
let defaults = {
_id: defaultId, // TODO convert all occurencies of id to _id
text: task._id || defaultId,
notes: '',
tags: [],
value: task.type === 'reward' ? 10 : 0,
priority: 1,
challenge: {},
reminders: {},
attribute: 'str',
dateCreated: new Date()
createdAt: new Date(), // TODO these are going to be overwritten by the server...
updatedAt: new Date(),
};
_.defaults(task, defaults);
if (task.type === 'habit' || task.type === 'daily') {
_.defaults(task, {
history: [],
});
}
if (task.type === 'todo' || task.type === 'daily') {
_.defaults(task, {
completed: false,
collapseChecklist: false,
checklist: [],
});
}
if (task.type === 'habit') {
_.defaults(task, {
up: true,
down: true
});
}
if ((ref1 = task.type) === 'habit' || ref1 === 'daily') {
_.defaults(task, {
history: []
});
}
if ((ref2 = task.type) === 'daily' || ref2 === 'todo') {
_.defaults(task, {
completed: false
down: true,
});
}
if (task.type === 'daily') {
_.defaults(task, {
streak: 0,
repeat: {
su: true,
m: true,
t: true,
w: true,
th: true,
f: true,
s: true
}
}, {
startDate: new Date(),
s: true,
su: true,
},
startDate: moment().startOf('day').toDate(),
everyX: 1,
frequency: 'weekly'
frequency: 'weekly',
});
}
task._id = task.id;
if (task.value == null) {
task.value = task.type === 'reward' ? 10 : 0;
}
if (!_.isNumber(task.priority)) {
task.priority = 1;
}
return task;
};
+4 -9
View File
@@ -1,9 +1,4 @@
// TODO use node-uuid module
module.exports = function() {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
var r, v;
r = Math.random() * 16 | 0;
v = (c === "x" ? r : r & 0x3 | 0x8);
return v.toString(16);
});
};
import uuid from 'uuid';
// TODO remove this file completely
module.exports = uuid.v4;
+9 -14
View File
@@ -1,27 +1,22 @@
import taskDefaults from '../libs/taskDefaults';
import i18n from '../i18n';
module.exports = function(user, req, cb) {
var task;
task = taskDefaults(req.body);
if (user.tasks[task.id] != null) {
return typeof cb === "function" ? cb({
code: 409,
message: i18n.t('messageDuplicateTaskID', req.language)
}) : void 0;
}
user[task.type + "s"].unshift(task);
// TODO move to client since it's only used there?
module.exports = function addTask (user, req = {body: {}}) {
let task = taskDefaults(req.body);
user.tasksOrder[`${task.type}s`].unshift(task._id);
if (user.preferences.newTaskEdit) {
task._editing = true;
}
if (user.preferences.tagsCollapsed) {
task._tags = true;
}
if (!user.preferences.advancedCollapsed) {
task._advanced = true;
}
if (typeof cb === "function") {
cb(null, task);
}
return task;
};
+6 -8
View File
@@ -1,12 +1,10 @@
import _ from 'lodash';
module.exports = function(user, req, cb) {
_.remove(user.todos, function(t) {
var ref;
return t.completed && !((ref = t.challenge) != null ? ref.id : void 0);
// TODO move to client since it's only used there?
// TODO rename file to clearCompletedTodos
module.exports = function clearCompletedTodos (todos) {
_.remove(todos, todo => {
return todo.completed && (!todo.challenge || !todo.challenge.id || todo.challenge.broken);
});
if (typeof user.markModified === "function") {
user.markModified('todos');
}
return typeof cb === "function" ? cb(null, user.todos) : void 0;
};
+19 -16
View File
@@ -1,23 +1,26 @@
import i18n from '../i18n';
import _ from 'lodash';
module.exports = function(user, req, cb) {
var ref, task;
if (!(task = user.tasks[(ref = req.params) != null ? ref.id : void 0])) {
return typeof cb === "function" ? cb({
code: 404,
message: i18n.t('messageTaskNotFound', req.language)
}) : void 0;
}
_.merge(task, _.omit(req.body, ['checklist', 'reminders', 'id', 'type']));
if (req.body.checklist) {
task.checklist = req.body.checklist;
}
// From server pass task.toObject() not the task document directly
module.exports = function updateTask (task, req = {}) {
// If reminders are updated -> replace the original ones
if (req.body.reminders) {
task.reminders = req.body.reminders;
delete req.body.reminders;
}
if (typeof task.markModified === "function") {
task.markModified('tags');
// If checklist is updated -> replace the original one
if (req.body.checklist) {
task.checklist = req.body.checklist;
delete req.body.checklist;
}
return typeof cb === "function" ? cb(null, task) : void 0;
// If tags are updated -> replace the original ones
if (req.body.tags) {
task.tags = req.body.tags;
delete req.body.tags;
}
_.merge(task, _.omit(req.body, ['_id', 'id', 'type']));
return task;
};