0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-23 20:33:16 -05:00
logto/packages/console/src/utils/subscription.ts

54 lines
1.8 KiB
TypeScript

import dayjs from 'dayjs';
import { type SubscriptionPlanResponse } from '@/cloud/types/router';
import {
communitySupportEnabledMap,
reservedPlanIdOrder,
ticketSupportResponseTimeMap,
} from '@/consts/subscriptions';
import { type Invoice } from '@/types/subscriptions';
export const addSupportQuotaToPlan = (subscriptionPlanResponse: SubscriptionPlanResponse) => {
const { name, quota } = subscriptionPlanResponse;
return {
...subscriptionPlanResponse,
quota: {
...quota,
communitySupportEnabled: communitySupportEnabledMap[name] ?? false, // Fallback to not supported
ticketSupportResponseTime: ticketSupportResponseTimeMap[name] ?? 0, // Fallback to not supported
},
};
};
const getSubscriptionPlanOrderById = (id: string) => {
const index = reservedPlanIdOrder.indexOf(id);
// Note: if the plan id is not in the reservedPlanIdOrder, it will be treated as the highest priority
return index === -1 ? Number.POSITIVE_INFINITY : index;
};
export const isDowngradePlan = (fromPlanId: string, toPlanId: string) =>
getSubscriptionPlanOrderById(fromPlanId) > getSubscriptionPlanOrderById(toPlanId);
type FormatPeriodOptions = {
periodStart: Date;
periodEnd: Date;
displayYear?: boolean;
};
export const formatPeriod = ({ periodStart, periodEnd, displayYear }: FormatPeriodOptions) => {
const format = displayYear ? 'MMM D, YYYY' : 'MMM D';
const formattedStart = dayjs(periodStart).format(format);
const formattedEnd = dayjs(periodEnd).format(format);
return `${formattedStart} - ${formattedEnd}`;
};
export const getLatestUnpaidInvoice = (invoices: Invoice[]) =>
invoices
.slice()
.sort(
(invoiceA, invoiceB) =>
new Date(invoiceB.createdAt).getTime() - new Date(invoiceA.createdAt).getTime()
)
.find(({ status }) => status === 'uncollectible');