Skip to content

Commit

Permalink
feat(promo-manager): add repeated promos
Browse files Browse the repository at this point in the history
  • Loading branch information
vanilla-wave committed Oct 1, 2024
1 parent 0966d01 commit 50d28b2
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 11 deletions.
20 changes: 13 additions & 7 deletions src/promo-manager/core/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,15 @@ export class Controller {
}

if (this.isFinished(slug)) {
return 'finished';
if (!this.helpers.promoBySlug[slug].repeatable) {
return 'finished';
}

if (this.checkPromoConditions(slug)) {
return 'canReRun';
} else {
return 'forbidden';
}
}

if (this.isPending(slug)) {
Expand Down Expand Up @@ -524,7 +532,9 @@ export class Controller {
}

private isAbleToRun = (slug: PromoSlug) => {
return this.getPromoStatus(slug) === 'canRun';
const status = this.getPromoStatus(slug);

return status === 'canRun' || status === 'canReRun';
};

private isActive = (slug: PromoSlug) => {
Expand Down Expand Up @@ -575,11 +585,7 @@ export class Controller {
this.logger.debug('Add promo to the queue', slug);
this.assertProgressLoaded();

if (
this.state.progress.finishedPromos.includes(slug) ||
this.state.base.activeQueue.includes(slug) ||
!this.checkPromoConditions(slug)
) {
if (this.state.base.activeQueue.includes(slug) || !this.checkPromoConditions(slug)) {
return;
}

Expand Down
3 changes: 2 additions & 1 deletion src/promo-manager/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {LoggerOptions} from '../../logger';
import {Controller as OnboardingController} from '../../controller';
import {Controller} from './controller';

export type PromoStatus = 'forbidden' | 'canRun' | 'active' | 'finished' | 'pending';
export type PromoStatus = 'forbidden' | 'canRun' | 'canReRun' | 'active' | 'finished' | 'pending';
export type Priority = 'high';
export type PromoManagerStatus = 'idle' | 'initialized';

Expand All @@ -18,6 +18,7 @@ export type Promo<T = PromoMeta> = {
priority?: Priority;
meta?: T;
trigger?: Trigger;
repeatable?: true;
};

export type PromoGroup<Config = PromoMeta> = {
Expand Down
26 changes: 25 additions & 1 deletion src/promo-manager/tests/controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('active promo', () => {
expect(controller.state.base.activePromo).toBe('ganttPoll');
});

it('run 2 duplicates -> finish promo -> not trigger next', async () => {
it('2 request and finish promo -> not trigger next', async () => {
await controller.requestStart('boardPoll');
await controller.requestStart('boardPoll');

Expand All @@ -77,6 +77,30 @@ describe('active promo', () => {
});
});

describe('repeatred runs', function () {
it('common promo -> cannot run', async function () {
const controller = new Controller(testOptions);

await controller.requestStart('boardPoll');
await controller.finishPromo('boardPoll');

await controller.requestStart('boardPoll');

expect(controller.state.base.activePromo).toBe(null);
});

it('repeated promo -> can rerun', async function () {
const controller = new Controller(testOptions);

await controller.requestStart('boardPollRepeat');
await controller.finishPromo('boardPollRepeat');

await controller.requestStart('boardPollRepeat');

expect(controller.state.base.activePromo).toBe('boardPollRepeat');
});
});

describe('trigger subscribe', () => {
let controller: Controller;

Expand Down
4 changes: 2 additions & 2 deletions src/promo-manager/tests/options.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type {PromoProgressState} from '../core/types';

import {pollGroup, pollGroup2, pollWithConditions} from './promoGroups';
import {pollGroup, pollGroup2, pollWithConditions, repeatedPoll} from './promoGroups';

export const testOptions = {
config: {
promoGroups: [pollGroup, pollGroup2, pollWithConditions],
promoGroups: [pollGroup, pollGroup2, pollWithConditions, repeatedPoll],
init: {
initType: 'timeout' as const,
timeout: 0,
Expand Down
17 changes: 17 additions & 0 deletions src/promo-manager/tests/promoGroups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,23 @@ export const pollGroup2: PromoGroup = {
],
};

export const repeatedPoll: PromoGroup = {
slug: 'pollRepeat',
conditions: [],
promos: [
{
slug: 'boardPollRepeat',
repeatable: true,
conditions: [],
},
{
slug: 'ganttPollRepeat',
repeatable: true,
conditions: [],
},
],
};

export const pollWithConditions: PromoGroup = {
slug: 'ask',
conditions: [ShowOnceForPeriod({months: 1})],
Expand Down

0 comments on commit 50d28b2

Please sign in to comment.