From 207dbf35d61c92eb9e1f744bed5df42fb76c4d21 Mon Sep 17 00:00:00 2001 From: MathWhiz Date: Fri, 3 Mar 2017 07:57:57 -0600 Subject: [PATCH] Add tags to default tasks (#8419) * Add ability to add tags to default tasks * fix missing semicolon * fix nesting callbacks error * Add tags to default tasks * fix default tags * Start test * Finish test * Fix tests * Move test * Fix padded-bock * Fix test * Fix request * fix requests * fix test * fix lint * Refine test * Fix test * Fix Test * Fix tests * Please work :( * Fix stupid mistake * Fix lint * Fixes * fix function * fix lint * fix lint --- .../user/auth/POST-register_local.test.js | 33 +++++++++++++++++++ website/common/script/content/index.js | 12 +++++++ website/server/models/user/hooks.js | 11 +++++++ 3 files changed, 56 insertions(+) diff --git a/test/api/v3/integration/user/auth/POST-register_local.test.js b/test/api/v3/integration/user/auth/POST-register_local.test.js index 3960a07477..a749f19a5d 100644 --- a/test/api/v3/integration/user/auth/POST-register_local.test.js +++ b/test/api/v3/integration/user/auth/POST-register_local.test.js @@ -5,6 +5,7 @@ import { createAndPopulateGroup, getProperty, } from '../../../../../helpers/api-integration/v3'; +import { ApiUser } from '../../../../../helpers/api-integration/api-classes'; import { v4 as generateRandomUserName } from 'uuid'; import { each } from 'lodash'; import { encrypt } from '../../../../../../website/server/libs/encryption'; @@ -416,5 +417,37 @@ describe('POST /user/auth/local/register', () => { expect(user.tags).to.not.be.empty; }); + + it('adds the correct tags to the correct tasks', async () => { + let user = await api.post('/user/auth/local/register', { + username, + email, + password, + confirmPassword: password, + }); + + let requests = new ApiUser(user); + + let habits = await requests.get('/tasks/user?type=habits'); + let todos = await requests.get('/tasks/user?type=todos'); + + function findTag (tagName) { + let tag = user.tags.find((userTag) => { + return userTag.name === t(tagName); + }); + return tag.id; + } + + expect(habits[0].tags).to.have.a.lengthOf(3); + expect(habits[0].tags).to.include.members(['defaultTag1', 'defaultTag4', 'defaultTag6'].map(findTag)); + + expect(habits[1].tags).to.have.a.lengthOf(1); + expect(habits[1].tags).to.include.members(['defaultTag3'].map(findTag)); + + expect(habits[2].tags).to.have.a.lengthOf(2); + expect(habits[2].tags).to.include.members(['defaultTag2', 'defaultTag3'].map(findTag)); + + expect(todos[0].tags).to.have.a.lengthOf(0); + }); }); }); diff --git a/website/common/script/content/index.js b/website/common/script/content/index.js index 9db1613d0d..85a535880b 100644 --- a/website/common/script/content/index.js +++ b/website/common/script/content/index.js @@ -2999,6 +2999,11 @@ api.userDefaults = { up: true, down: false, attribute: 'per', + tags: [ + t('defaultTag1'), // Work + t('defaultTag4'), // School + t('defaultTag6'), // Chores + ], }, { type: 'habit', text: t('defaultHabit2Text'), @@ -3006,6 +3011,9 @@ api.userDefaults = { up: false, down: true, attribute: 'str', + tags: [ + t('defaultTag3'), // Health + Wellness + ], }, { type: 'habit', text: t('defaultHabit3Text'), @@ -3013,6 +3021,10 @@ api.userDefaults = { up: true, down: true, attribute: 'str', + tags: [ + t('defaultTag2'), // Exercise + t('defaultTag3'), // Health + Wellness + ], }, ], dailys: [], diff --git a/website/server/models/user/hooks.js b/website/server/models/user/hooks.js index 5c15db3733..1663d97cf5 100644 --- a/website/server/models/user/hooks.js +++ b/website/server/models/user/hooks.js @@ -23,6 +23,13 @@ schema.post('init', function postInitUser (doc) { shared.wrap(doc); }); +function findTag (user, tagName) { + let tagID = _.find(user.tags, (userTag) => { + return userTag.name === tagName(user.preferences.language); + }); + return tagID.id; +} + function _populateDefaultTasks (user, taskTypes) { let tagsI = taskTypes.indexOf('tag'); @@ -59,6 +66,10 @@ function _populateDefaultTasks (user, taskTypes) { }); } + if (taskDefaults.tags) { + newTask.tags = _.compact(_.map(taskDefaults.tags, _.partial(findTag, user))); + } + return newTask.save(); });