62 lines
2.4 KiB
TypeScript
62 lines
2.4 KiB
TypeScript
import { TFunction } from "i18next";
|
|
import { SubscriptionBillingPeriod } from "~/application/enums/subscriptions/SubscriptionBillingPeriod";
|
|
import { SubscriptionPriceWithProduct } from "../db/subscriptionProducts.db.server";
|
|
import DateUtils from "../shared/DateUtils";
|
|
import { getFormattedPriceInCurrency } from "../helpers/PricingHelper";
|
|
import { TenantSubscriptionProductWithDetails } from "../db/subscriptions/tenantSubscriptionProducts.db.server";
|
|
|
|
function formatSubscriptionPrice(price: number, currency?: string | null) {
|
|
return getFormattedPriceInCurrency({ price, currency: currency ?? "usd" });
|
|
}
|
|
|
|
function getProductTitle({ t, item }: { t: TFunction; item: TenantSubscriptionProductWithDetails }) {
|
|
let title =
|
|
t(item.subscriptionProduct.title) +
|
|
" " +
|
|
item.prices
|
|
.map(
|
|
(f) =>
|
|
`${formatSubscriptionPrice(Number(f.subscriptionPrice?.price ?? 0), f.subscriptionPrice?.currency)} - ${getBillingPeriodDescription(
|
|
t,
|
|
f.subscriptionPrice!.billingPeriod
|
|
)}`
|
|
)
|
|
.join(", ");
|
|
if (item.endsAt) {
|
|
title += ` (${t("settings.subscription.endsRelative", { date: DateUtils.dateAgo(item.endsAt) })})`;
|
|
}
|
|
return title;
|
|
}
|
|
|
|
function getPriceDescription(t: TFunction, item: SubscriptionPriceWithProduct) {
|
|
return `${t(item.subscriptionProduct.title)} - ${formatSubscriptionPrice(Number(item.price), item.currency)} - ${getBillingPeriodDescription(t, item.billingPeriod)}`;
|
|
}
|
|
|
|
function getBillingPeriodDescription(t: TFunction, billingPeriod: SubscriptionBillingPeriod) {
|
|
return t("pricing." + SubscriptionBillingPeriod[billingPeriod].toString()).toString();
|
|
}
|
|
|
|
function getBillingPeriodShortDescription(t: TFunction, billingPeriod: SubscriptionBillingPeriod) {
|
|
switch (billingPeriod) {
|
|
case SubscriptionBillingPeriod.MONTHLY:
|
|
return t("pricing.MONTHLYShort").toString();
|
|
case SubscriptionBillingPeriod.YEARLY:
|
|
return t("pricing.YEARLYShort").toString();
|
|
case SubscriptionBillingPeriod.QUARTERLY:
|
|
return t("pricing.QUARTERLYShort").toString();
|
|
case SubscriptionBillingPeriod.SEMI_ANNUAL:
|
|
return t("pricing.SEMIANNUALShort").toString();
|
|
case SubscriptionBillingPeriod.ONCE:
|
|
return t("pricing.once").toString();
|
|
default:
|
|
return getBillingPeriodDescription(t, billingPeriod);
|
|
}
|
|
}
|
|
|
|
export default {
|
|
getProductTitle,
|
|
getPriceDescription,
|
|
getBillingPeriodDescription,
|
|
getBillingPeriodShortDescription,
|
|
};
|