Skip to content

Commit

Permalink
Merge pull request #107 from gravity-ui/improve-session-helper
Browse files Browse the repository at this point in the history
Improve session helper
  • Loading branch information
vanilla-wave authored Sep 5, 2024
2 parents 4e0514a + 2d547a7 commit fe41482
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 11 deletions.
98 changes: 96 additions & 2 deletions src/promo-manager/core/condition/condition-helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe('ShowOnceForPeriod', function () {
},
};

expect(helper(state, {currentDate, promoType: 'promoGroup1', config})).toBe(false);
expect(helper(state, {currentDate, promoGroup: 'promoGroup1', config})).toBe(false);
});

it('enough time has passed to start -> true', function () {
Expand All @@ -117,7 +117,7 @@ describe('ShowOnceForPeriod', function () {
},
};

expect(helper(state, {currentDate, promoType: 'promoGroup1', config})).toBe(true);
expect(helper(state, {currentDate, promoGroup: 'promoGroup1', config})).toBe(true);
});
});
});
Expand Down Expand Up @@ -161,6 +161,100 @@ describe('ShowOnceForSession', function () {

expect(helper(state, {currentDate, promoSlug: 'somePromo1', config})).toBe(false);
});

it('has run, for group -> false ', function () {
const state = {
base: {
activePromo: null,
activeQueue: [],
},
progress: {
finishedPromos: ['somePromo1'],
progressInfoByPromo: {
somePromo1: {
lastCallTime: currentDate,
},
},
},
};

const helper = ShowOnceForSession();

expect(helper(state, {currentDate, promoGroup: 'promoGroup1', config})).toBe(false);
});

describe('with slugs param', function () {
it('for slug without runs -> true ', function () {
const helper = ShowOnceForSession({
slugs: ['promoWithoutRuns'],
});

const state = {
base: {
activePromo: null,
activeQueue: [],
},
progress: {
finishedPromos: ['somePromo1'],
progressInfoByPromo: {
somePromo1: {
lastCallTime: currentDate,
},
},
},
};

expect(helper(state, {currentDate, config})).toBe(true);
});

it('for slug with runs -> false ', function () {
const helper = ShowOnceForSession({
slugs: ['somePromo1'],
});

const state = {
base: {
activePromo: null,
activeQueue: [],
},
progress: {
finishedPromos: ['somePromo1'],
progressInfoByPromo: {
somePromo1: {
lastCallTime: currentDate,
},
},
},
};

expect(helper(state, {currentDate, config})).toBe(false);
});
});

describe('with slugs param and context -> take slug from params', function () {
it('for slug without runs, ctx with runs -> true ', function () {
const helper = ShowOnceForSession({
slugs: ['promoWithoutRuns'],
});

const state = {
base: {
activePromo: null,
activeQueue: [],
},
progress: {
finishedPromos: ['somePromo1'],
progressInfoByPromo: {
somePromo1: {
lastCallTime: currentDate,
},
},
},
};

expect(helper(state, {currentDate, config, promoSlug: 'somePromo1'})).toBe(true);
});
});
});
describe('LimitFrequency', function () {
const currentDate = new Date('07-15-2024').valueOf();
Expand Down
17 changes: 15 additions & 2 deletions src/promo-manager/core/condition/condition-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,24 @@ export const ShowOnceForPeriod = (interval: DurationParam) => {
};
};

export const ShowOnceForSession = () => {
type SlugsParam = {slugs?: string[]};
export const ShowOnceForSession = ({slugs: slugsFromParams}: SlugsParam = {}) => {
return (state: PromoState, ctx: ConditionContext) => {
const targetInterval = dayjs.duration(performance.now());

return getTimeFromLastCallInMs(state, ctx) > targetInterval.asMilliseconds();
const slugFromContext = ctx.promoGroup || ctx.promoSlug;
const slugs = slugsFromParams ?? [slugFromContext];

for (const slug of slugs) {
if (
getTimeFromLastCallInMs(state, {...ctx, promoSlug: slug}) <
targetInterval.asMilliseconds()
) {
return false;
}
}

return true;
};
};

Expand Down
2 changes: 1 addition & 1 deletion src/promo-manager/core/condition/condition-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const getLastTimeCall = (state: PromoState, ctx: ConditionContext, slug?:
export const getTimeFromLastCallInMs = (state: PromoState, ctx: ConditionContext) => {
const nowDate = dayjs(ctx.currentDate);

const lastTimeCall = getLastTimeCall(state, ctx, ctx.promoSlug || ctx.promoType);
const lastTimeCall = getLastTimeCall(state, ctx, ctx.promoSlug || ctx.promoGroup);

if (!lastTimeCall) {
return Infinity;
Expand Down
10 changes: 5 additions & 5 deletions src/promo-manager/core/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,19 +315,19 @@ export class Controller {
return false;
}

const type = this.getGroupBySlug(slug);
const group = this.getGroupBySlug(slug);

if (!type) {
if (!group) {
return false;
}

const conditionsForType = this.conditions.typeConditions[type] ?? [];
const conditionsForType = this.conditions.typeConditions[group] ?? [];
const conditionsForSlug = this.conditions.promoConditions[slug] ?? [];

const resultForGroup = checkCondition(
this.state,
{
promoType: type,
promoGroup: group,
currentDate: this.dateNow(),
config: this.options.config,
},
Expand All @@ -339,7 +339,7 @@ export class Controller {
const resultForPromo = checkCondition(
this.state,
{
promoType: type,
promoGroup: group,
promoSlug: slug,
currentDate: this.dateNow(),
helpers: this.conditionHelpers,
Expand Down
2 changes: 1 addition & 1 deletion src/promo-manager/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export type PromoOptions = {
};

export type ConditionContext = {
promoType?: PromoGroupSlug;
promoGroup?: PromoGroupSlug;
promoSlug?: PromoSlug;
currentDate: number;
helpers?: Record<string, ConditionHelper>;
Expand Down

0 comments on commit fe41482

Please sign in to comment.