Skip to content

Commit

Permalink
feat: add promo manager
Browse files Browse the repository at this point in the history
  • Loading branch information
iapolya committed Mar 4, 2024
1 parent eca2fc1 commit 45f9409
Show file tree
Hide file tree
Showing 20 changed files with 1,360 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"jest": true
},
"rules": {
"@typescript-eslint/no-explicit-any": ["off"]
"@typescript-eslint/no-explicit-any": ["off"],
"new-cap": ["off"]
},
"ignorePatterns": "dist/**/*"
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,4 @@ export function createOnboarding<T extends InitOptions<any, any, any>>(options:
}

export * from './types';
export * from './promo-manager/index';
9 changes: 7 additions & 2 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@ type Logger = {
export type LoggerOptions = {
level?: LogLevel;
logger?: Logger;
context?: string;
};

const noop = () => {};

export const createLogger = ({level = 'error', logger = console}: LoggerOptions) => {
export const createLogger = ({
level = 'error',
logger = console,
context = 'Onboarding',
}: LoggerOptions) => {
return {
debug:
level === 'debug' ? (...args: unknown[]) => logger.log('[Onboarding]', ...args) : noop,
level === 'debug' ? (...args: unknown[]) => logger.log(`[${context}]`, ...args) : noop,
error: logger.error,
};
};
27 changes: 27 additions & 0 deletions src/promo-manager/core/conditions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type {ConditionParams} from './types';

const getLastTimeCall = ({type, slug, state, byType}: ConditionParams) => {
return byType
? state.progress?.progressInfoByType[type]?.lastCallTime
: state.progress?.progressInfoByPromo[slug]?.lastCallTime;
};

export const PromoInCurrentDay = (date: Date) => {
return () => date.toDateString() === new Date().toDateString();
};

export const ShowOncePerMonths = (months: number) => {
return (params: ConditionParams) => {
const dateNow = new Date(params.date);

dateNow.setMonth(dateNow.getMonth() - months);

const lastTimeCall = getLastTimeCall(params);

if (!lastTimeCall) {
return true;
}

return dateNow.getTime() >= lastTimeCall;
};
};
Loading

0 comments on commit 45f9409

Please sign in to comment.