Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/lib/ui/app/SubscriptionPlan/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export type TProductsWithPlans = readonly {
amount: number
isDeprecated: boolean
product: { id: string }
apiCallLimits: {
month: number
} | null
}[]
}[]

Expand All @@ -28,6 +31,9 @@ export const queryProductsWithPlans = ApiQuery(
interval
amount
isDeprecated
apiCallLimits {
month
}
}
}
}`,
Expand Down
2 changes: 2 additions & 0 deletions src/lib/ui/app/SubscriptionPlan/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export {
CONSUMER_PLANS,
getSubscriptionPlanKey,
convertSubscriptionPlan,
getNextUpgradePlan,
} from './plans.js'
export {
checkIsSanbaseProduct,
Expand All @@ -23,6 +24,7 @@ export {
checkIsCurrentPlan,
getPlanName,
getFormattedPlan,
getPlansApiLimitsMap,
} from './utils.js'
export {
type TPublicSubscription,
Expand Down
22 changes: 17 additions & 5 deletions src/lib/ui/app/SubscriptionPlan/plans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,30 @@ export const SubscriptionPlanDetails: Record<
},
}

export const CONSUMER_PLANS = new Set<string>([
const CONSUMER_PLANS_ORDER: readonly TPlan[] = [
SubscriptionPlan.FREE.key,
SubscriptionPlan.PRO.key,
SubscriptionPlan.MAX.key,
])
]

export const BUSINESS_PLANS = new Set<string>([
SubscriptionPlan.BUSINESS_MAX.key,
const BUSINESS_PLANS_ORDER: readonly TPlan[] = [
SubscriptionPlan.BUSINESS_PRO.key,
SubscriptionPlan.BUSINESS_MAX.key,
SubscriptionPlan.CUSTOM.key,
])
]

const PLANS_UPGRADE_ORDER = [...CONSUMER_PLANS_ORDER, ...BUSINESS_PLANS_ORDER]

export const CONSUMER_PLANS = new Set<string>(CONSUMER_PLANS_ORDER)
export const BUSINESS_PLANS = new Set<string>(BUSINESS_PLANS_ORDER)

export function checkIsTrialEligiblePlan(planKey?: TSubscriptionPlan['name']) {
return planKey === SubscriptionPlan.PRO.key
}

export function getNextUpgradePlan(plan: TPlan): TPlan | undefined {
const index = PLANS_UPGRADE_ORDER.indexOf(plan)
if (index === -1) return undefined

return PLANS_UPGRADE_ORDER.at(index + 1)
}
28 changes: 28 additions & 0 deletions src/lib/ui/app/SubscriptionPlan/utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import type { TSubscriptionPlan } from './types.js'
import type { TProductsWithPlans } from './api.js'

import {
BUSINESS_PLANS,
CONSUMER_PLANS,
checkIsTrialEligiblePlan,
convertSubscriptionPlan,
getSubscriptionPlanKey,
Product,
SubscriptionPlan,
SubscriptionPlanDetails,
type TPlan,
} from './plans.js'

export type TLooseProduct = {
Expand Down Expand Up @@ -92,3 +96,27 @@ export const checkIsAlternativeBillingPlan = (
targetPlan: TSubscriptionPlan,
) =>
userPlan ? userPlan.name === targetPlan.name && userPlan.interval !== targetPlan.interval : false

export function getPlansApiLimitsMap(products: TProductsWithPlans) {
const limits: Partial<Record<TPlan, number>> = {}

products.forEach(({ id, plans }) => {
const isSanbase = checkIsSanbaseProduct({ id })
const isSanApi = checkIsSanApiProduct({ id })
if (!isSanbase && !isSanApi) return

plans.forEach(({ name, apiCallLimits }) => {
const isBusinessBasicPlan = BUSINESS_PLANS.has(name)
const isConsumerBasicPlan = CONSUMER_PLANS.has(name)
if (!isConsumerBasicPlan && !isBusinessBasicPlan) return
if (isBusinessBasicPlan !== isSanApi || isConsumerBasicPlan !== isSanbase) return

const plan = convertSubscriptionPlan(name)
if (!plan) return

limits[plan] = apiCallLimits?.month
})
})

return limits
}