mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 15:17:25 +01:00
refactor api.daysSince to a cron.js function not exposed on api
This commit is contained in:
@@ -47,7 +47,6 @@ export function startOfWeek (options) {
|
||||
return moment(o.now).startOf('week');
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
This is designed for use with any date that has an important time portion (e.g., when comparing the current date-time with the previous cron's date-time for determing if cron should run now).
|
||||
It changes the time portion of the date-time to be the Custom Day Start hour, so that the date-time is now the user's correct start of day.
|
||||
@@ -68,3 +67,13 @@ export function startOfDay (options) {
|
||||
return dayStart;
|
||||
}
|
||||
|
||||
/*
|
||||
Absolute diff from "yesterday" till now
|
||||
*/
|
||||
|
||||
export function daysSince (yesterday, options = {}) {
|
||||
let o = sanitizeOptions(options);
|
||||
|
||||
return startOfDay(_.defaults({ now: o.now }, o)).diff(startOfDay(_.defaults({ now: yesterday }, o)), 'days');
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ import {
|
||||
DAY_MAPPING, // temporary, pending further refactoring
|
||||
sanitizeOptions, // temporary, pending further refactoring
|
||||
startOfDay, // temporary, pending further refactoring
|
||||
daysSince,
|
||||
} from '../../common/script/cron';
|
||||
|
||||
var $w, _, api, content, i18n, moment, preenHistory, sortOrder,
|
||||
@@ -74,24 +75,6 @@ api.planGemLimits = {
|
||||
------------------------------------------------------
|
||||
*/
|
||||
|
||||
/*
|
||||
Absolute diff from "yesterday" till now
|
||||
*/
|
||||
|
||||
api.daysSince = function(yesterday, options) {
|
||||
var o;
|
||||
if (options == null) {
|
||||
options = {};
|
||||
}
|
||||
o = sanitizeOptions(options);
|
||||
return startOfDay(_.defaults({
|
||||
now: o.now
|
||||
}, o)).diff(startOfDay(_.defaults({
|
||||
now: yesterday
|
||||
}, o)), 'days');
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
Should the user do this task on this date, given the task's repeat options and user.preferences.dayStart?
|
||||
*/
|
||||
@@ -2276,7 +2259,7 @@ api.wrap = function(user, main) {
|
||||
}
|
||||
}
|
||||
dropMultiplier = ((ref1 = user.purchased) != null ? (ref2 = ref1.plan) != null ? ref2.customerId : void 0 : void 0) ? 2 : 1;
|
||||
if ((api.daysSince(user.items.lastDrop.date, user.preferences) === 0) && (user.items.lastDrop.count >= dropMultiplier * (5 + Math.floor(user._statsComputed.per / 25) + (user.contributor.level || 0)))) {
|
||||
if ((daysSince(user.items.lastDrop.date, user.preferences) === 0) && (user.items.lastDrop.count >= dropMultiplier * (5 + Math.floor(user._statsComputed.per / 25) + (user.contributor.level || 0)))) {
|
||||
return;
|
||||
}
|
||||
if (((ref3 = user.flags) != null ? ref3.dropsEnabled : void 0) && user.fns.predictableRandom(user.stats.exp) < chance) {
|
||||
@@ -2483,7 +2466,7 @@ api.wrap = function(user, main) {
|
||||
options = {};
|
||||
}
|
||||
now = +options.now || +(new Date);
|
||||
daysMissed = api.daysSince(user.lastCron, _.defaults({
|
||||
daysMissed = daysSince(user.lastCron, _.defaults({
|
||||
now: now
|
||||
}, user.preferences));
|
||||
if (!(daysMissed > 0)) {
|
||||
|
||||
@@ -3,6 +3,7 @@ import {
|
||||
DAY_MAPPING,
|
||||
startOfWeek,
|
||||
startOfDay,
|
||||
daysSince,
|
||||
} from '../../common/script/cron';
|
||||
|
||||
let expect = require('expect.js');
|
||||
@@ -1208,7 +1209,7 @@ describe('Cron', () => {
|
||||
});
|
||||
it('daysSince cron before, now after', () => {
|
||||
let lastCron = moment('2014-10-09 02: 30: 00');
|
||||
let days = shared.daysSince(lastCron, {
|
||||
let days = daysSince(lastCron, {
|
||||
now: moment('2014-10-09 11: 30: 00'),
|
||||
dayStart,
|
||||
});
|
||||
@@ -1217,7 +1218,7 @@ describe('Cron', () => {
|
||||
});
|
||||
it('daysSince cron before, now before', () => {
|
||||
let lastCron = moment('2014-10-09 02: 30: 00');
|
||||
let days = shared.daysSince(lastCron, {
|
||||
let days = daysSince(lastCron, {
|
||||
now: moment('2014-10-09 03: 30: 00'),
|
||||
dayStart,
|
||||
});
|
||||
@@ -1226,7 +1227,7 @@ describe('Cron', () => {
|
||||
});
|
||||
it('daysSince cron after, now after', () => {
|
||||
let lastCron = moment('2014-10-09 05: 30: 00');
|
||||
let days = shared.daysSince(lastCron, {
|
||||
let days = daysSince(lastCron, {
|
||||
now: moment('2014-10-09 06: 30: 00'),
|
||||
dayStart,
|
||||
});
|
||||
@@ -1235,7 +1236,7 @@ describe('Cron', () => {
|
||||
});
|
||||
it('daysSince cron after, now tomorrow before', () => {
|
||||
let lastCron = moment('2014-10-09 12: 30: 00');
|
||||
let days = shared.daysSince(lastCron, {
|
||||
let days = daysSince(lastCron, {
|
||||
now: moment('2014-10-10 01: 30: 00'),
|
||||
dayStart,
|
||||
});
|
||||
@@ -1244,7 +1245,7 @@ describe('Cron', () => {
|
||||
});
|
||||
it('daysSince cron after, now tomorrow after', () => {
|
||||
let lastCron = moment('2014-10-09 12: 30: 00');
|
||||
let days = shared.daysSince(lastCron, {
|
||||
let days = daysSince(lastCron, {
|
||||
now: moment('2014-10-10 10: 30: 00'),
|
||||
dayStart,
|
||||
});
|
||||
@@ -1253,7 +1254,7 @@ describe('Cron', () => {
|
||||
});
|
||||
xit('daysSince, last cron before new dayStart', () => {
|
||||
let lastCron = moment('2014-10-09 01: 00: 00');
|
||||
let days = shared.daysSince(lastCron, {
|
||||
let days = daysSince(lastCron, {
|
||||
now: moment('2014-10-09 05: 00: 00'),
|
||||
dayStart,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user