diff --git a/common/script/libs/preenTodos.js b/common/script/libs/preenTodos.js index f07a49c9d0..121bdae008 100644 --- a/common/script/libs/preenTodos.js +++ b/common/script/libs/preenTodos.js @@ -1,14 +1,12 @@ import moment from 'moment'; import _ from 'lodash'; -/* - Preen 3-day past-completed To-Dos from Angular & mobile app - */ +// TODO used only in v2 client -module.exports = function(tasks) { - return _.filter(tasks, function(t) { - return !t.completed || (t.challenge && t.challenge.id) || moment(t.dateCompleted).isAfter(moment().subtract({ - days: 3 +module.exports = function preenTodos (tasks) { + return _.filter(tasks, (t) => { + return !t.completed || t.challenge && t.challenge.id || moment(t.dateCompleted).isAfter(moment().subtract({ + days: 3, })); }); }; diff --git a/common/script/ops/addTag.js b/common/script/ops/addTag.js index 2b97e67f38..b44ead8d2e 100644 --- a/common/script/ops/addTag.js +++ b/common/script/ops/addTag.js @@ -1,14 +1,17 @@ import uuid from '../libs/uuid'; +import _ from 'lodash'; // TODO used only in client, move there? -module.exports = function(user, req, cb) { - if (user.tags == null) { +module.exports = function addTag (user, req = {}) { + if (!user.tags) { user.tags = []; } + user.tags.push({ name: req.body.name, - id: req.body.id || uuid() + id: _.get(req, 'body.id') || uuid(), }); - return typeof cb === "function" ? cb(null, user.tags) : void 0; + + return user.tags; }; diff --git a/common/script/ops/deleteTag.js b/common/script/ops/deleteTag.js index 275aa3e5bd..c40fe79ba5 100644 --- a/common/script/ops/deleteTag.js +++ b/common/script/ops/deleteTag.js @@ -1,28 +1,32 @@ import i18n from '../i18n'; import _ from 'lodash'; +import { NotFound } from '../libs/errors'; // TODO used only in client, move there? -module.exports = function(user, req, cb) { - var i, tag, tid; - tid = req.params.id; - i = _.findIndex(user.tags, { - id: tid +module.exports = function deleteTag (user, req = {}) { + let tid = _.get(req, 'params.id'); + + let index = _.findIndex(user.tags, { + id: tid, }); - if (!~i) { - return typeof cb === "function" ? cb({ - code: 404, - message: i18n.t('messageTagNotFound', req.language) - }) : void 0; + + if (index === -1) { + throw new NotFound(i18n.t('messageTagNotFound', req.language)); } - tag = user.tags[i]; + + let tag = user.tags[index]; delete user.filters[tag.id]; - user.tags.splice(i, 1); - _.each(user.tasks, function(task) { + + user.tags.splice(index, 1); + + _.each(user.tasks, (task) => { return delete task.tags[tag.id]; }); - _.each(['habits', 'dailys', 'todos', 'rewards'], function(type) { - return typeof user.markModified === "function" ? user.markModified(type) : void 0; + + _.each(['habits', 'dailys', 'todos', 'rewards'], (type) => { + user.markModified(type); }); - return typeof cb === "function" ? cb(null, user.tags) : void 0; + + return user.tags; }; diff --git a/common/script/ops/deleteTask.js b/common/script/ops/deleteTask.js index a830e19257..b818cf7ed4 100644 --- a/common/script/ops/deleteTask.js +++ b/common/script/ops/deleteTask.js @@ -1,19 +1,21 @@ import i18n from '../i18n'; +import { NotFound } from '../libs/errors'; +import _ from 'lodash'; // TODO used only in client, move there? -module.exports = function(user, req, cb) { - var i, ref, task; - task = user.tasks[(ref = req.params) != null ? ref.id : void 0]; +module.exports = function deleteTask (user, req = {}) { + let tid = _.get(req, 'params.id'); + let task = user.tasks[tid]; + if (!task) { - return typeof cb === "function" ? cb({ - code: 404, - message: i18n.t('messageTaskNotFound', req.language) - }) : void 0; + throw new NotFound(i18n.t('messageTaskNotFound', req.language)); } - i = user[task.type + "s"].indexOf(task); - if (~i) { - user[task.type + "s"].splice(i, 1); + + let index = user[`${task.type}s`].indexOf(task); + if (index !== -1) { + user[`${task.type}s`].splice(index, 1); } - return typeof cb === "function" ? cb(null, {}) : void 0; + + return {}; }; diff --git a/common/script/ops/getTag.js b/common/script/ops/getTag.js index b54133c2c7..4a7db63128 100644 --- a/common/script/ops/getTag.js +++ b/common/script/ops/getTag.js @@ -1,19 +1,18 @@ import _ from 'lodash'; import i18n from '../i18n'; +import { NotFound } from '../libs/errors'; // TODO used only in client, move there? -module.exports = function(user, req, cb) { - var i, tid; - tid = req.params.id; - i = _.findIndex(user.tags, { - id: tid +module.exports = function getTag (user, req = {}) { + let tid = _.get(req, 'params.id'); + + let index = _.findIndex(user.tags, { + id: tid, }); - if (!~i) { - return typeof cb === "function" ? cb({ - code: 404, - message: i18n.t('messageTagNotFound', req.language) - }) : void 0; + if (index === -1) { + throw new NotFound(i18n.t('messageTagNotFound', req.language)); } - return typeof cb === "function" ? cb(null, user.tags[i]) : void 0; + + return user.tags[index]; }; diff --git a/common/script/ops/getTags.js b/common/script/ops/getTags.js index b379f13578..a96589a831 100644 --- a/common/script/ops/getTags.js +++ b/common/script/ops/getTags.js @@ -1,5 +1,5 @@ // TODO used only in client, move there? -module.exports = function(user, req, cb) { - return typeof cb === "function" ? cb(null, user.tags) : void 0; +module.exports = function getTags (user) { + return user.tags; }; diff --git a/common/script/ops/sortTag.js b/common/script/ops/sortTag.js index d47083ad3f..29756a8b82 100644 --- a/common/script/ops/sortTag.js +++ b/common/script/ops/sortTag.js @@ -1,11 +1,16 @@ +import { BadRequest } from '../libs/errors'; +import _ from 'lodash'; + // TODO used only in client, move there? -module.exports = function(user, req, cb) { - var from, ref, to; - ref = req.query, to = ref.to, from = ref.from; - if (!((to != null) && (from != null))) { - return typeof cb === "function" ? cb('?to=__&from=__ are required') : void 0; +module.exports = function sortTag (user, req = {}) { + let to = _.get(req, 'query.to'); + let fromParam = _.get(req, 'query.from'); + + if (!to || !fromParam) { + throw new BadRequest('?to=__&from=__ are required'); } - user.tags.splice(to, 0, user.tags.splice(from, 1)[0]); - return typeof cb === "function" ? cb(null, user.tags) : void 0; + + user.tags.splice(to, 0, user.tags.splice(fromParam, 1)[0]); + return user.tags; }; diff --git a/common/script/ops/sortTask.js b/common/script/ops/sortTask.js index 212eaf4de2..ca79b03e24 100644 --- a/common/script/ops/sortTask.js +++ b/common/script/ops/sortTask.js @@ -1,41 +1,50 @@ import i18n from '../i18n'; import preenTodos from '../libs/preenTodos'; +import { + NotFound, + BadRequest, +} from '../libs/errors'; +import _ from 'lodash'; // TODO used only in client, move there? -module.exports = function(user, req, cb) { - var from, id, movedTask, preenedTasks, ref, task, tasks, to; - id = req.params.id; - ref = req.query, to = ref.to, from = ref.from; - task = user.tasks[id]; +module.exports = function sortTag (user, req = {}) { + let id = _.get(req, 'params.id'); + let to = _.get(req, 'query.to'); + let fromParam = _.get(req, 'query.from'); + + let task = user.tasks[id]; + if (!task) { - return typeof cb === "function" ? cb({ - code: 404, - message: i18n.t('messageTaskNotFound', req.language) - }) : void 0; + throw new NotFound(i18n.t('messageTaskNotFound', req.language)); } - if (!((to != null) && (from != null))) { - return typeof cb === "function" ? cb('?to=__&from=__ are required') : void 0; + if (!to && !fromParam) { + throw new BadRequest('?to=__&from=__ are required'); } - tasks = user[task.type + "s"]; - if (task.type === 'todo' && tasks[from] !== task) { - preenedTasks = preenTodos(tasks); + + let tasks = user[`${task.type}s`]; + + if (task.type === 'todo' && tasks[fromParam] !== task) { + let preenedTasks = preenTodos(tasks); + if (to !== -1) { to = tasks.indexOf(preenedTasks[to]); } - from = tasks.indexOf(preenedTasks[from]); + + fromParam = tasks.indexOf(preenedTasks[fromParam]); } - if (tasks[from] !== task) { - return typeof cb === "function" ? cb({ - code: 404, - message: i18n.t('messageTaskNotFound', req.language) - }) : void 0; + + if (tasks[fromParam] !== task) { + throw new NotFound(i18n.t('messageTaskNotFound', req.language)); } - movedTask = tasks.splice(from, 1)[0]; + + let movedTask = tasks.splice(fromParam, 1)[0]; + if (to === -1) { tasks.push(movedTask); } else { tasks.splice(to, 0, movedTask); } - return typeof cb === "function" ? cb(null, tasks) : void 0; + + return tasks; }; diff --git a/common/script/ops/update.js b/common/script/ops/update.js index 12a100e372..c41e74180e 100644 --- a/common/script/ops/update.js +++ b/common/script/ops/update.js @@ -1,9 +1,11 @@ import _ from 'lodash'; -module.exports = function(user, req, cb) { - _.each(req.body, function(v, k) { - user.fns.dotSet(k, v); - return true; +// TODO used only in client, move there? + +module.exports = function updateUser (user, req = {}) { + _.each(req.body, (val, key) => { + _.set(user, key, val); }); - return typeof cb === "function" ? cb(null, user) : void 0; + + return user; }; diff --git a/common/script/ops/updateTag.js b/common/script/ops/updateTag.js index 5e61ff8c6b..ade87f916d 100644 --- a/common/script/ops/updateTag.js +++ b/common/script/ops/updateTag.js @@ -1,20 +1,20 @@ import i18n from '../i18n'; import _ from 'lodash'; +import { NotFound } from '../libs/errors'; // TODO used only in client, move there? -module.exports = function(user, req, cb) { - var i, tid; - tid = req.params.id; - i = _.findIndex(user.tags, { - id: tid +module.exports = function updateTag (user, req = {}) { + let tid = _.get(req, 'params.id'); + + let index = _.findIndex(user.tags, { + id: tid, }); - if (!~i) { - return typeof cb === "function" ? cb({ - code: 404, - message: i18n.t('messageTagNotFound', req.language) - }) : void 0; + + if (index === -1) { + throw new NotFound(i18n.t('messageTagNotFound', req.language)); } - user.tags[i].name = req.body.name; - return typeof cb === "function" ? cb(null, user.tags[i]) : void 0; + + user.tags[index].name = _.get(req, 'body.name'); + return user.tags[index]; }; diff --git a/tasks/gulp-eslint.js b/tasks/gulp-eslint.js index fc542d4faf..998a8c040e 100644 --- a/tasks/gulp-eslint.js +++ b/tasks/gulp-eslint.js @@ -11,16 +11,11 @@ const COMMON_FILES = [ // @TODO remove these negations as the files are converted over. '!./common/script/content/index.js', '!./common/script/ops/addPushDevice.js', - '!./common/script/ops/addTag.js', '!./common/script/ops/addWebhook.js', '!./common/script/ops/blockUser.js', '!./common/script/ops/clearPMs.js', '!./common/script/ops/deletePM.js', - '!./common/script/ops/deleteTag.js', - '!./common/script/ops/deleteTask.js', '!./common/script/ops/deleteWebhook.js', - '!./common/script/ops/getTag.js', - '!./common/script/ops/getTags.js', '!./common/script/ops/rebirth.js', '!./common/script/ops/releaseBoth.js', '!./common/script/ops/releaseMounts.js', @@ -29,11 +24,8 @@ const COMMON_FILES = [ '!./common/script/ops/reset.js', '!./common/script/ops/revive.js', '!./common/script/ops/sell.js', - '!./common/script/ops/sortTag.js', - '!./common/script/ops/sortTask.js', '!./common/script/ops/unlock.js', '!./common/script/ops/update.js', - '!./common/script/ops/updateTag.js', '!./common/script/ops/updateWebhook.js', '!./common/script/fns/crit.js', '!./common/script/fns/cron.js', @@ -55,7 +47,6 @@ const COMMON_FILES = [ '!./common/script/libs/percent.js', '!./common/script/libs/planGemLimits.js', '!./common/script/libs/preenHistory.js', - '!./common/script/libs/preenTodos.js', '!./common/script/libs/removeWhitespace.js', '!./common/script/libs/silver.js', '!./common/script/libs/splitWhitespace.js', diff --git a/test/api/v2/members/POST-members_id_gift.test.js b/test/api/v2/members/POST-members_id_gift.test.js index c67a7ef79e..d36306a79d 100644 --- a/test/api/v2/members/POST-members_id_gift.test.js +++ b/test/api/v2/members/POST-members_id_gift.test.js @@ -2,7 +2,7 @@ import { generateUser, } from '../../../helpers/api-integration/v2'; -xdescribe('POST /members/id/gift', () => { +describe('POST /members/id/gift', () => { let userWithBalance, userWithoutBalance; beforeEach(async () => { diff --git a/test/api/v2/members/POST-members_id_message.test.js b/test/api/v2/members/POST-members_id_message.test.js index 12d77cd1f4..ffbc02266a 100644 --- a/test/api/v2/members/POST-members_id_message.test.js +++ b/test/api/v2/members/POST-members_id_message.test.js @@ -2,7 +2,7 @@ import { generateUser, } from '../../../helpers/api-integration/v2'; -xdescribe('POST /members/id/message', () => { +describe('POST /members/id/message', () => { let sender, recipient; beforeEach(async () => {