mirror of
https://github.com/supabase/supabase.git
synced 2026-10-08 10:55:06 +03:00
refactor(telemetry): extract deriveTier to utils with tests, dedupe previewTier
This commit is contained in:
3 files changed
+36
-14
No files matched your search
@@ -35,6 +35,7 @@ import {
|
||||
type OrgKind,
|
||||
type OrgSize,
|
||||
} from './OrganizationDetailsFields'
|
||||
import { deriveTier, type OrgTier } from './NewOrgForm.utils'
|
||||
import { UpgradeExistingOrganizationCallout } from './UpgradeExistingOrganizationCallout'
|
||||
import { ChargeBreakdown } from '@/components/interfaces/Billing/ChargeBreakdown'
|
||||
import { getStripeElementsAppearanceOptions } from '@/components/interfaces/Billing/Payment/Payment.utils'
|
||||
@@ -84,18 +85,6 @@ const formSchema = organizationDetailsSchema.extend({
|
||||
|
||||
type FormState = z.infer<typeof formSchema>
|
||||
|
||||
type OrgTier = 'tier_free' | 'tier_pro' | 'tier_payg' | 'tier_team'
|
||||
|
||||
/**
|
||||
* Derives the telemetry tier from the (non-null) submitted form values. This is the single source
|
||||
* of truth for translating a plan + spend cap into a tier, so callers can never emit an
|
||||
* organization creation event without a valid tier.
|
||||
*/
|
||||
const deriveTier = (plan: (typeof plans)[number], spendCap: boolean): OrgTier => {
|
||||
const dbTier = plan === 'PRO' && !spendCap ? 'PAYG' : plan
|
||||
return ('tier_' + dbTier.toLowerCase()) as OrgTier
|
||||
}
|
||||
|
||||
const stripePromise = loadStripe(STRIPE_PUBLIC_KEY)
|
||||
|
||||
const FORM_ID = 'new-org-form'
|
||||
@@ -225,8 +214,7 @@ export const NewOrgForm = ({
|
||||
|
||||
const previewTier = useMemo(() => {
|
||||
if (selectedPlan === 'FREE') return undefined
|
||||
const dbTier = selectedPlan === 'PRO' && !selectedSpendCap ? 'PAYG' : selectedPlan
|
||||
return ('tier_' + dbTier.toLowerCase()) as 'tier_pro' | 'tier_payg' | 'tier_team'
|
||||
return deriveTier(selectedPlan, selectedSpendCap)
|
||||
}, [selectedPlan, selectedSpendCap])
|
||||
|
||||
const {
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import { deriveTier } from './NewOrgForm.utils'
|
||||
|
||||
describe('deriveTier', () => {
|
||||
it('maps FREE to tier_free regardless of spend cap', () => {
|
||||
expect(deriveTier('FREE', false)).toBe('tier_free')
|
||||
expect(deriveTier('FREE', true)).toBe('tier_free')
|
||||
})
|
||||
|
||||
it('maps PRO with spend cap enabled to tier_pro', () => {
|
||||
expect(deriveTier('PRO', true)).toBe('tier_pro')
|
||||
})
|
||||
|
||||
it('maps PRO with spend cap disabled to tier_payg', () => {
|
||||
expect(deriveTier('PRO', false)).toBe('tier_payg')
|
||||
})
|
||||
|
||||
it('maps TEAM to tier_team regardless of spend cap', () => {
|
||||
expect(deriveTier('TEAM', true)).toBe('tier_team')
|
||||
expect(deriveTier('TEAM', false)).toBe('tier_team')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,11 @@
|
||||
export type OrgTier = 'tier_free' | 'tier_pro' | 'tier_payg' | 'tier_team'
|
||||
|
||||
/**
|
||||
* Derives the telemetry tier from the (non-null) submitted form values. This is the single source
|
||||
* of truth for translating a plan + spend cap into a tier, so callers can never emit an
|
||||
* organization creation event without a valid tier.
|
||||
*/
|
||||
export const deriveTier = (plan: 'FREE' | 'PRO' | 'TEAM', spendCap: boolean): OrgTier => {
|
||||
const dbTier = plan === 'PRO' && !spendCap ? 'PAYG' : plan
|
||||
return ('tier_' + dbTier.toLowerCase()) as OrgTier
|
||||
}
|
||||
Reference in new issue
Block a user