diff --git a/common/locales/en/settings.json b/common/locales/en/settings.json index daffc88b5a..727c04774c 100644 --- a/common/locales/en/settings.json +++ b/common/locales/en/settings.json @@ -47,6 +47,7 @@ "customDayStart": "Custom Day Start", "changeCustomDayStart": "Change Custom Day Start?", "sureChangeCustomDayStart": "Are you sure you want to change your custom day start?", + "customDayStartHasChanged": "Your custom day start has changed.", "nextCron": "Your Dailies will next reset the first time you use Habitica after <%= time %>. Make sure you have completed your Dailies before this time!", "customDayStartInfo1": "Habitica defaults to check and reset your Dailies at midnight in your own time zone each day. You can customize that time here.", "misc": "Misc", diff --git a/test/api/v3/integration/user/POST-user-set-custom-day-start.test.js b/test/api/v3/integration/user/POST-user-set-custom-day-start.test.js deleted file mode 100644 index 1dd88bb342..0000000000 --- a/test/api/v3/integration/user/POST-user-set-custom-day-start.test.js +++ /dev/null @@ -1,19 +0,0 @@ -import { - generateUser, -} from '../../../../helpers/api-integration/v3'; - -let user; -let endpoint = '/user/set-custom-day-start'; - -describe('POST /user/set-custom-day-start', () => { - beforeEach(async () => { - user = await generateUser(); - }); - - it('update user.preferences.dayStart', async () => { - expect(user.preferences.dayStart).to.eql(0); - await user.post(endpoint, { dayStart: 1 }); - await user.sync(); - expect(user.preferences.dayStart).to.eql(1); - }); -}); diff --git a/test/api/v3/integration/user/POST-user_custom-day-start.test.js b/test/api/v3/integration/user/POST-user_custom-day-start.test.js new file mode 100644 index 0000000000..868b9ae91d --- /dev/null +++ b/test/api/v3/integration/user/POST-user_custom-day-start.test.js @@ -0,0 +1,47 @@ +import moment from 'moment'; +import { + generateUser, + translate as t, +} from '../../../../helpers/api-integration/v3'; + +let user; +let endpoint = '/user/custom-day-start'; + +describe('POST /user/custom-day-start', () => { + beforeEach(async () => { + user = await generateUser(); + }); + + it('updates user.preferences.dayStart', async () => { + expect(user.preferences.dayStart).to.eql(0); + + await user.post(endpoint, { dayStart: 1 }); + await user.sync(); + + expect(user.preferences.dayStart).to.eql(1); + }); + + it('sets lastCron to the current time to prevent an unexpected cron', async () => { + let oldCron = moment().subtract(7, 'hours'); + + await user.update({lastCron: oldCron}); + await user.post(endpoint, { dayStart: 1 }); + await user.sync(); + + expect(user.lastCron.valueOf()).to.be.gt(oldCron.valueOf()); + }); + + it('returns a confirmation message', async () => { + let {message} = await user.post(endpoint, { dayStart: 1 }); + + expect(message).to.eql(t('customDayStartHasChanged')); + }); + + it('errors if invalid value is passed', async () => { + await expect(user.post(endpoint, { dayStart: 'foo' })) + .to.eventually.be.rejected; + + await expect(user.post(endpoint, { dayStart: 24})) + .to.eventually.be.rejected; + }); +}); diff --git a/test/spec/controllers/settingsCtrlSpec.js b/test/spec/controllers/settingsCtrlSpec.js index 55bbbb3fac..ef960ae204 100644 --- a/test/spec/controllers/settingsCtrlSpec.js +++ b/test/spec/controllers/settingsCtrlSpec.js @@ -17,7 +17,7 @@ describe('Settings Controller', function () { releasePets: sandbox.stub(), releaseMounts: sandbox.stub(), releaseBoth: sandbox.stub(), - setLastCron: sandbox.stub(), + setCustomDayStart: sandbox.stub(), user: user }; @@ -87,19 +87,11 @@ describe('Settings Controller', function () { }); describe('#saveDayStart', function () { - - it('updates user\'s custom day start and last cron', function () { - var fakeCurrentTime = new Date(2013, 3, 1, 8, 12).getTime(); - var expectedTime = fakeCurrentTime; - sandbox.useFakeTimers(fakeCurrentTime); + it('updates user\'s custom day start', function () { scope.dayStart = 5; scope.saveDayStart(); - expect(User.set).to.be.calledOnce; - expect(User.set).to.be.calledWith({ - 'preferences.dayStart': 5, - }); - expect(User.setLastCron).to.be.calledWith(expectedTime); + expect(User.setCustomDayStart).to.be.calledWith(5); }); }); diff --git a/website/client/js/services/userServices.js b/website/client/js/services/userServices.js index 4637c43c22..78e01a7476 100644 --- a/website/client/js/services/userServices.js +++ b/website/client/js/services/userServices.js @@ -313,13 +313,14 @@ angular.module('habitrpg') setCustomDayStart: function (dayStart) { $http({ method: "POST", - url: 'api/v3/user/set-custom-day-start', + url: 'api/v3/user/custom-day-start', data: { dayStart: dayStart } }) .then(function (response) { - Notification.text('Day start updated. Remember to refresh'); + Notification.text(response.data.data.message); + sync(); }); }, diff --git a/website/server/controllers/api-v3/user.js b/website/server/controllers/api-v3/user.js index ea68c48d01..a943ca02c6 100644 --- a/website/server/controllers/api-v3/user.js +++ b/website/server/controllers/api-v3/user.js @@ -1311,26 +1311,29 @@ api.userReset = { }; /** -* @api {post} /api/v3/user/set-custom-day-start Sets preferences.dayStart for user +* @api {post} /api/v3/user/custom-day-start Sets preferences.dayStart for user * @apiVersion 3.0.0 -* @apiName UserSetCustomDayStart +* @apiName setCustomDayStart * @apiGroup User * * @apiSuccess {Object} data An empty Object */ -api.userSetCustomDayStart = { +api.setCustomDayStart = { method: 'POST', middlewares: [authWithHeaders()], - url: '/user/set-custom-day-start', + url: '/user/custom-day-start', async handler (req, res) { let user = res.locals.user; let dayStart = req.body.dayStart; user.preferences.dayStart = dayStart; + user.lastCron = new Date(); await user.save(); - res.respond(200, {}); + res.respond(200, { + message: res.t('customDayStartHasChanged'), + }); }, };