From 1a869437114e5d84ec820b5ff4ac632d9ae52927 Mon Sep 17 00:00:00 2001 From: SabreCat Date: Mon, 12 Sep 2022 15:17:39 -0500 Subject: [PATCH 1/3] fix(subscriptions): better next-hourglass logic --- website/common/script/cron.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/website/common/script/cron.js b/website/common/script/cron.js index 6e678f144b..10f8c89195 100644 --- a/website/common/script/cron.js +++ b/website/common/script/cron.js @@ -8,6 +8,7 @@ import defaults from 'lodash/defaults'; import invert from 'lodash/invert'; import moment from 'moment'; import 'moment-recur'; +import subscriptionBlocks from './content/subscriptionBlocks'; export const DAY_MAPPING = { 0: 'su', @@ -286,7 +287,13 @@ export function getPlanContext (user, now) { 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 planMonths = subscriptionBlocks[plan.planId].months || 1; + let monthsTillNextHourglass; + if (planMonths > 1) { + monthsTillNextHourglass = plan.consecutive.offset + 1; + } else { + monthsTillNextHourglass = 3 - (plan.consecutive.count % 3); + } const possibleNextHourglassDate = moment(plan.dateUpdated) .add(monthsTillNextHourglass, 'months'); From f75a6eb11d81070c347038f78a4257950709e1a9 Mon Sep 17 00:00:00 2001 From: SabreCat Date: Tue, 20 Sep 2022 15:25:37 -0500 Subject: [PATCH 2/3] fix(hourglass): handle missing planId --- website/common/script/cron.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/common/script/cron.js b/website/common/script/cron.js index 10f8c89195..bbe115a5c5 100644 --- a/website/common/script/cron.js +++ b/website/common/script/cron.js @@ -287,7 +287,7 @@ export function getPlanContext (user, now) { const dateUpdatedMoment = moment(plan.dateUpdated).startOf('month'); const elapsedMonths = moment(subscriptionEndDate).diff(dateUpdatedMoment, 'months'); - const planMonths = subscriptionBlocks[plan.planId].months || 1; + const planMonths = subscriptionBlocks[plan.planId] ? subscriptionBlocks[plan.planId].months : 1; let monthsTillNextHourglass; if (planMonths > 1) { monthsTillNextHourglass = plan.consecutive.offset + 1; From 6baf08d461e932985b27cfcde079b13e102fe359 Mon Sep 17 00:00:00 2001 From: SabreCat Date: Fri, 23 Sep 2022 16:10:11 -0500 Subject: [PATCH 3/3] fix(test): update expectations for new logic --- test/common/libs/cron.test.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/test/common/libs/cron.test.js b/test/common/libs/cron.test.js index bd84de1e80..d082e1e7b0 100644 --- a/test/common/libs/cron.test.js +++ b/test/common/libs/cron.test.js @@ -213,7 +213,7 @@ describe('cron utility functions', () => { }; } - it('offset 0, next date in 3 months', () => { + it('monthly plan, next date in 3 months', () => { const user = baseUserData(60, 0, 'group_plan_auto'); const planContext = getPlanContext(user, now); @@ -222,8 +222,8 @@ describe('cron utility functions', () => { .to.be.sameMoment('2022-08-10T02:00:00.144Z'); }); - it('offset 1, next date in 1 months', () => { - const user = baseUserData(60, 1, 'group_plan_auto'); + it('monthly plan, next date in 1 month', () => { + const user = baseUserData(62, 0, 'group_plan_auto'); const planContext = getPlanContext(user, now); @@ -231,8 +231,17 @@ describe('cron utility functions', () => { .to.be.sameMoment('2022-06-10T02:00:00.144Z'); }); - it('offset 2, next date in 2 months - with any plan', () => { - const user = baseUserData(60, 2, 'basic_3mo'); + it('multi-month plan, no offset', () => { + const user = baseUserData(60, 0, 'basic_3mo'); + + const planContext = getPlanContext(user, now); + + expect(planContext.nextHourglassDate) + .to.be.sameMoment('2022-06-10T02:00:00.144Z'); + }); + + it('multi-month plan with offset', () => { + const user = baseUserData(60, 1, 'basic_3mo'); const planContext = getPlanContext(user, now);