Skip to content

Commit

Permalink
fix(promo-manager): fix period with short month
Browse files Browse the repository at this point in the history
  • Loading branch information
vanilla-wave committed Jan 14, 2025
1 parent 02d1132 commit 0f8eb83
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 15 deletions.
14 changes: 8 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"force-release": "echo 43"
},
"dependencies": {
"dayjs": "^1.11.11"
"dayjs": "^1.11.13"
},
"devDependencies": {
"@commitlint/cli": "^17.0.0",
Expand Down
41 changes: 35 additions & 6 deletions src/promo-manager/core/condition/condition-helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ const config: PromoOptions['config'] = {
};
describe('ShowOnceForPeriod', function () {
const currentDate = new Date('07-15-2024').valueOf();
// there are bug with weeks, so use days https://github.com/iamkun/dayjs/issues/2750
const promoPeriod = {days: 2};

it('empty state -> true', function () {
const helper = ShowOnceForPeriod({weeks: 1});
const helper = ShowOnceForPeriod(promoPeriod);

const state = {
base: {
Expand All @@ -33,9 +35,36 @@ describe('ShowOnceForPeriod', function () {
expect(helper(state, {currentDate, promoSlug: 'somePromo1', config})).toBe(true);
});

it('should work for short month', () => {
const helper = ShowOnceForPeriod({months: 2});

const state = {
base: {
activePromo: null,
activeQueue: [],
},
progress: {
finishedPromos: ['somePromo1'],
progressInfoByPromo: {
somePromo1: {
lastCallTime: new Date('01-01-2025').valueOf(),
},
},
},
};

expect(
helper(state, {
currentDate: new Date('03-02-2025').valueOf(),
promoSlug: 'somePromo1',
config,
}),
).toBe(true);
});

describe('pick by promo', function () {
it('not enough time has passed to start -> false', function () {
const helper = ShowOnceForPeriod({weeks: 1});
const helper = ShowOnceForPeriod(promoPeriod);

const state = {
base: {
Expand All @@ -55,8 +84,8 @@ describe('ShowOnceForPeriod', function () {
expect(helper(state, {currentDate, promoSlug: 'somePromo1', config})).toBe(false);
});

it('enough time has passed to start -> false', function () {
const helper = ShowOnceForPeriod({weeks: 1});
it('enough time has passed to start -> true', function () {
const helper = ShowOnceForPeriod(promoPeriod);

const state = {
base: {
Expand All @@ -79,7 +108,7 @@ describe('ShowOnceForPeriod', function () {

describe('pick by group', function () {
it('not enough time has passed to start -> false', function () {
const helper = ShowOnceForPeriod({weeks: 1});
const helper = ShowOnceForPeriod(promoPeriod);

const state = {
base: {
Expand All @@ -100,7 +129,7 @@ describe('ShowOnceForPeriod', function () {
});

it('enough time has passed to start -> true', function () {
const helper = ShowOnceForPeriod({weeks: 1});
const helper = ShowOnceForPeriod(promoPeriod);

const state = {
base: {
Expand Down
11 changes: 9 additions & 2 deletions src/promo-manager/core/condition/condition-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {ConditionContext} from '../types';

import dayjs from 'dayjs';
import duration, {DurationUnitsObjectType} from 'dayjs/plugin/duration';
import {getTimeFromLastCallInMs} from './condition-utils';
import {getLastTimeCall, getTimeFromLastCallInMs} from './condition-utils';

dayjs.extend(duration);

Expand All @@ -14,10 +14,17 @@ export const PromoInCurrentDay = (date: Date) => {
};
export const ShowOnceForPeriod = (interval: DurationParam) => {
return (state: PromoState, ctx: ConditionContext) => {
const lastTimeCall = getLastTimeCall(state, ctx, ctx.promoSlug || ctx.promoGroup);
if (!lastTimeCall) {
return true;
}

// @ts-ignore
const targetInterval = dayjs.duration(interval);
const availableAtDate = dayjs(lastTimeCall).add(targetInterval);

return getTimeFromLastCallInMs(state, ctx) > targetInterval.asMilliseconds();
const nowDate = dayjs(ctx.currentDate);
return availableAtDate.isBefore(nowDate);
};
};

Expand Down
3 changes: 3 additions & 0 deletions src/promo-manager/tests/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ describe('periodic runs', function () {
config: {
promoGroups: [pollWithConditions],
},
logger: {
level: 'debug' as const,
},
};

it('cancel promo and request after 2 months -> show again', async () => {
Expand Down
1 change: 1 addition & 0 deletions src/tests/controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ describe('wrong data', function () {
describe('ignoreUnknownPresets=true', () => {
const prepareOptions = () => {
const options = getOptions();
// @ts-ignore
options.ignoreUnknownPresets = true;

return options;
Expand Down

0 comments on commit 0f8eb83

Please sign in to comment.