Files
mtdb_movie/common/resources/client/billing/pricing-table/find-best-price.ts
maher 703f50a09d
Some checks failed
Build / run (push) Has been cancelled
first commit
2025-10-29 11:42:25 +01:00

43 lines
966 B
TypeScript
Executable File

import {Price} from '../price';
export type UpsellBillingCycle = 'monthly' | 'yearly';
export function findBestPrice(
token: UpsellBillingCycle,
prices: Price[]
): Price | undefined {
if (token === 'monthly') {
const match = findMonthlyPrice(prices);
if (match) return match;
}
if (token === 'yearly') {
const match = findYearlyPrice(prices);
if (match) return match;
}
return prices[0];
}
function findYearlyPrice(prices: Price[]) {
return prices.find(price => {
if (price.interval === 'month' && price.interval_count >= 12) {
return price;
}
if (price.interval === 'year' && price.interval_count >= 1) {
return price;
}
});
}
function findMonthlyPrice(prices: Price[]) {
return prices.find(price => {
if (price.interval === 'day' && price.interval_count >= 30) {
return price;
}
if (price.interval === 'month' && price.interval_count >= 1) {
return price;
}
});
}