Merge branch 'TheHollidayInn-api-v3-user-set-last-cron-fix' into api-v3

This commit is contained in:
Blade Barringer
2016-05-20 08:03:47 -05:00
6 changed files with 93 additions and 14 deletions

View File

@@ -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",

View File

@@ -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;
});
});

View File

@@ -17,6 +17,7 @@ describe('Settings Controller', function () {
releasePets: sandbox.stub(),
releaseMounts: sandbox.stub(),
releaseBoth: sandbox.stub(),
setCustomDayStart: sandbox.stub(),
user: user
};
@@ -86,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,
'lastCron': expectedTime
});
expect(User.setCustomDayStart).to.be.calledWith(5);
});
});

View File

@@ -77,10 +77,7 @@ habitrpg.controller('SettingsCtrl',
};
$scope.saveDayStart = function() {
User.set({
'preferences.dayStart': Math.floor($scope.dayStart),
'lastCron': +new Date
});
User.setCustomDayStart(Math.floor($scope.dayStart));
};
$scope.language = window.env.language;

View File

@@ -310,6 +310,20 @@ angular.module('habitrpg')
});
},
setCustomDayStart: function (dayStart) {
$http({
method: "POST",
url: 'api/v3/user/custom-day-start',
data: {
dayStart: dayStart
}
})
.then(function (response) {
Notification.text(response.data.data.message);
sync();
});
},
makeAdmin: function () {
$http({
method: "POST",

View File

@@ -1313,4 +1313,31 @@ api.userReset = {
},
};
/**
* @api {post} /api/v3/user/custom-day-start Sets preferences.dayStart for user
* @apiVersion 3.0.0
* @apiName setCustomDayStart
* @apiGroup User
*
* @apiSuccess {Object} data An empty Object
*/
api.setCustomDayStart = {
method: 'POST',
middlewares: [authWithHeaders()],
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, {
message: res.t('customDayStartHasChanged'),
});
},
};
module.exports = api;