Merge branch 'develop' into sabrecat/teams-rebase

This commit is contained in:
SabreCat
2022-04-07 14:55:16 -05:00
45 changed files with 1632 additions and 1274 deletions
+50
View File
@@ -250,3 +250,53 @@ export function shouldDo (day, dailyTask, options = {}) {
}
return false;
}
export function getPlanMonths (plan) {
// NB gift subscriptions don't have a planID
// (which doesn't matter because we don't need to reapply perks
// for them and by this point they should have expired anyway)
if (!plan.planId) return 1;
const planIdRegExp = new RegExp('_([0-9]+)mo'); // e.g., matches 'google_6mo' / 'basic_12mo' and captures '6' / '12'
const match = plan.planId.match(planIdRegExp);
if (match !== null && match[0] !== null) {
// 3 for 3-month recurring subscription, etc
return match[1]; // eslint-disable-line prefer-destructuring
}
return 1;
}
/*
* This is a helper method to get all the needed informations of the plan
*
* currently used in cron and the "next hourglass in" feature
*/
export function getPlanContext (user, now) {
const { plan } = user.purchased;
defaults(plan.consecutive, {
count: 0, offset: 0, trinkets: 0, gemCapExtra: 0,
});
const nowMoment = moment(now);
const subscriptionEndDate = moment(plan.dateTerminated).isBefore()
? moment(plan.dateTerminated).startOf('month')
: nowMoment.startOf('month');
const dateUpdatedMoment = moment(plan.dateUpdated).startOf('month');
const elapsedMonths = moment(subscriptionEndDate).diff(dateUpdatedMoment, 'months');
const monthsTillNextHourglass = plan.consecutive.offset || (3 - (plan.consecutive.count % 3));
const possibleNextHourglassDate = moment(plan.dateUpdated)
.add(monthsTillNextHourglass, 'months');
return {
plan,
subscriptionEndDate,
dateUpdatedMoment,
elapsedMonths,
offset: plan.consecutive.offset, // months until the new hourglass is added
nextHourglassDate: possibleNextHourglassDate,
};
}
+13 -8
View File
@@ -25,7 +25,9 @@ import {
import content from './content/index';
import * as count from './count';
// TODO under api.libs.cron?
import { daysSince, DAY_MAPPING, shouldDo } from './cron';
import {
daysSince, DAY_MAPPING, shouldDo, getPlanContext, getPlanMonths,
} from './cron';
import apiErrors from './errors/apiErrorMessages';
import commonErrors from './errors/commonErrorMessages';
import autoAllocate from './fns/autoAllocate';
@@ -93,13 +95,16 @@ import { unEquipByType } from './ops/unequip';
import getOfficialPinnedItems from './libs/getOfficialPinnedItems';
import { sleepAsync } from './libs/sleepAsync';
const api = {};
api.content = content;
api.errors = errors;
api.i18n = i18n;
api.shouldDo = shouldDo;
api.daysSince = daysSince;
api.DAY_MAPPING = DAY_MAPPING;
const api = {
content,
errors,
i18n,
shouldDo,
getPlanContext,
getPlanMonths,
daysSince,
DAY_MAPPING,
};
api.constants = {
MAX_INCENTIVES,