77 lines
2.2 KiB
TypeScript
77 lines
2.2 KiB
TypeScript
/**
|
|
* Monthly commission cap checks per referrer (beneficiary).
|
|
*/
|
|
|
|
export type MonthlyCapInput = {
|
|
currentReservedAmount: bigint;
|
|
currentReservedCount: number;
|
|
newCommissionAmount: bigint;
|
|
maxAmountRaw: bigint;
|
|
maxCount: number;
|
|
};
|
|
|
|
export type LevelCapConfig = {
|
|
monthlyMaxAmountRaw: string | bigint;
|
|
monthlyMaxCount: number;
|
|
};
|
|
|
|
export type EffectiveMonthlyCaps = {
|
|
maxAmountRaw: bigint;
|
|
maxCount: number;
|
|
source: 'global' | 'level';
|
|
};
|
|
|
|
export type MonthlyCapResult = {
|
|
exceeded: boolean;
|
|
reason?: 'amount' | 'count';
|
|
};
|
|
|
|
/** Level caps of 0 inherit from global program settings. */
|
|
export function resolveEffectiveLevelCaps(
|
|
global: { maxAmountRaw: bigint; maxCount: number },
|
|
level?: LevelCapConfig | null
|
|
): EffectiveMonthlyCaps {
|
|
const levelAmount = level ? BigInt(level.monthlyMaxAmountRaw) : 0n;
|
|
const levelCount = level?.monthlyMaxCount ?? 0;
|
|
|
|
if (levelAmount > 0n || levelCount > 0) {
|
|
return {
|
|
maxAmountRaw: levelAmount > 0n ? levelAmount : global.maxAmountRaw,
|
|
maxCount: levelCount > 0 ? levelCount : global.maxCount,
|
|
source: 'level',
|
|
};
|
|
}
|
|
|
|
return { ...global, source: 'global' };
|
|
}
|
|
|
|
/** Returns whether adding a commission would exceed configured monthly caps (0 = unlimited). */
|
|
export function wouldExceedMonthlyCommissionCap(input: MonthlyCapInput): MonthlyCapResult {
|
|
if (input.maxCount > 0 && input.currentReservedCount + 1 > input.maxCount) {
|
|
return { exceeded: true, reason: 'count' };
|
|
}
|
|
if (
|
|
input.maxAmountRaw > 0n &&
|
|
input.currentReservedAmount + input.newCommissionAmount > input.maxAmountRaw
|
|
) {
|
|
return { exceeded: true, reason: 'amount' };
|
|
}
|
|
return { exceeded: false };
|
|
}
|
|
|
|
/** Validates reserved totals already include the payout being settled (no double-count). */
|
|
export function wouldExceedMonthlyCapAtSettlement(input: {
|
|
reservedAmount: bigint;
|
|
reservedCount: number;
|
|
maxAmountRaw: bigint;
|
|
maxCount: number;
|
|
}): MonthlyCapResult {
|
|
if (input.maxCount > 0 && input.reservedCount > input.maxCount) {
|
|
return { exceeded: true, reason: 'count' };
|
|
}
|
|
if (input.maxAmountRaw > 0n && input.reservedAmount > input.maxAmountRaw) {
|
|
return { exceeded: true, reason: 'amount' };
|
|
}
|
|
return { exceeded: false };
|
|
}
|