diff --git a/common/script/index.js b/common/script/index.js index 9aa76c3e9b..6d26b32c48 100644 --- a/common/script/index.js +++ b/common/script/index.js @@ -99,9 +99,11 @@ api.count = count; // TODO As ops and fns are ported, exported them through the api object import scoreTask from './ops/scoreTask'; +import sleep from './ops/sleep'; api.ops = { scoreTask, + sleep, }; api.fns = {}; diff --git a/common/script/ops/sleep.js b/common/script/ops/sleep.js index dec8095ad3..e23ee22d8e 100644 --- a/common/script/ops/sleep.js +++ b/common/script/ops/sleep.js @@ -1,4 +1,4 @@ -module.exports = function(user, req, cb) { +module.exports = function sleep (user) { user.preferences.sleep = !user.preferences.sleep; - return typeof cb === "function" ? cb(null, {}) : void 0; + return user.preferences.sleep; }; diff --git a/package.json b/package.json index 0dcc4968b4..caf7681d20 100644 --- a/package.json +++ b/package.json @@ -106,8 +106,8 @@ "test:api-v3:integration": "gulp test:api-v3:integration", "test:api-v3:integration:separate-server": "gulp test:api-v3:integration:separate-server", "test:api-legacy": "istanbul cover -i \"website/src/**\" --dir coverage/api ./node_modules/mocha/bin/_mocha test/api-legacy", - "test:common": "mocha test/common", - "test:content": "mocha test/content", + "test:common": "mocha test/common --recursive", + "test:content": "mocha test/content --recursive", "test:karma": "karma start --single-run", "test:karma:watch": "karma start", "test:prepare:webdriver": "webdriver-manager update", diff --git a/tasks/gulp-eslint.js b/tasks/gulp-eslint.js index eb75321f3c..6d8c1ca2dd 100644 --- a/tasks/gulp-eslint.js +++ b/tasks/gulp-eslint.js @@ -51,7 +51,6 @@ const COMMON_FILES = [ '!./common/script/ops/reset.js', '!./common/script/ops/revive.js', '!./common/script/ops/sell.js', - '!./common/script/ops/sleep.js', '!./common/script/ops/sortTag.js', '!./common/script/ops/sortTask.js', '!./common/script/ops/unlock.js', diff --git a/tasks/gulp-tests.js b/tasks/gulp-tests.js index a517d5a460..56a759bdfe 100644 --- a/tasks/gulp-tests.js +++ b/tasks/gulp-tests.js @@ -104,7 +104,7 @@ gulp.task('test:common:clean', (cb) => { }); gulp.task('test:common:watch', ['test:common:clean'], () => { - gulp.watch(['common/script/**', 'test/common/**'], ['test:common:clean']); + gulp.watch(['common/script/**/*', 'test/common/**/*'], ['test:common:clean']); }); gulp.task('test:common:safe', ['test:prepare:build'], (cb) => { diff --git a/test/common/ops/sleep.js b/test/common/ops/sleep.js new file mode 100644 index 0000000000..57cca033b3 --- /dev/null +++ b/test/common/ops/sleep.js @@ -0,0 +1,18 @@ +import sleep from '../../../common/script/ops/sleep'; +import { + generateUser, +} from '../../helpers/common.helper'; + +describe('shared.ops.sleep', () => { + it('changes user.preferences.sleep and returns the new value', () => { + let user = generateUser(); + + let res = sleep(user); + expect(res).to.equal(true); + expect(user.preferences.sleep).to.equal(true); + + let res2 = sleep(user); + expect(res2).to.equal(false); + expect(user.preferences.sleep).to.equal(false); + }); +}); diff --git a/website/src/controllers/api-v3/user.js b/website/src/controllers/api-v3/user.js index c4a68df4e9..a2895c4759 100644 --- a/website/src/controllers/api-v3/user.js +++ b/website/src/controllers/api-v3/user.js @@ -13,6 +13,8 @@ import Q from 'q'; import _ from 'lodash'; import * as passwordUtils from '../../libs/api-v3/password'; +const sleep = common.ops.sleep; + let api = {}; /** @@ -277,4 +279,27 @@ api.castSpell = { }, }; +/** + * @api {post} /user/sleep Put the user in the inn. + * @apiVersion 3.0.0 + * @apiName UserSleep + * @apiGroup User + * + * @apiSuccess {Object} Will return an object with the new `user.preferences.sleep` value. Example `{preferences: {sleep: true}}` + */ +api.sleep = { + method: 'POST', + middlewares: [authWithHeaders(), cron], + url: '/user/sleep', + async handler (req, res) { + let user = res.locals.user; + let sleepVal = sleep(user); + return res.respond(200, { + preferences: { + sleep: sleepVal, + }, + }); + }, +}; + module.exports = api;