The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
fix(admin-panel): stop calling a metered line item's quantity "seats" (#25698)
Félix Malfait9c787ab5dbe5
diff --git a/packages/twenty-front/src/modules/settings/admin-panel/components/SettingsAdminWorkspaceBillingContent.tsx b/packages/twenty-front/src/modules/settings/admin-panel/components/SettingsAdminWorkspaceBillingContent.tsxindex 22ce9ffb9a6..365199765c7 100644--- a/packages/twenty-front/src/modules/settings/admin-panel/components/SettingsAdminWorkspaceBillingContent.tsx+++ b/packages/twenty-front/src/modules/settings/admin-panel/components/SettingsAdminWorkspaceBillingContent.tsx@@ -25,6 +25,7 @@ import { themeCssVariables } from 'twenty-ui/theme-constants';import { useApolloAdminClient } from '@/settings/admin-panel/apollo/hooks/useApolloAdminClient';import { SettingsAdminWorkspaceCreditGrantModal } from '@/settings/admin-panel/components/SettingsAdminWorkspaceCreditGrantModal';import { SettingsAdminWorkspaceCreditGrantsTable } from '@/settings/admin-panel/components/SettingsAdminWorkspaceCreditGrantsTable';+import { formatSubscriptionItemValue } from '@/settings/admin-panel/utils/formatSubscriptionItemValue';import { useModal } from '@/ui/layout/modal/hooks/useModal';import { GET_WORKSPACE_BILLING_ADMIN_PANEL } from '@/settings/admin-panel/graphql/queries/getWorkspaceBillingAdminPanel';import { SettingsTableCard } from '@/settings/components/SettingsTableCard';@@ -32,7 +33,10 @@ import { PlansTags } from '@/settings/billing/components/internal/PlansTags';import { SettingsSectionSkeletonLoader } from '@/settings/components/SettingsSectionSkeletonLoader';import { useNumberFormat } from '@/localization/hooks/useNumberFormat';import { beautifyExactDate } from '~/utils/date-utils';-import { BillingPlanKey } from '~/generated-metadata/graphql';+import {+ BillingPlanKey,+ BillingProductKey,+} from '~/generated-metadata/graphql';import {SubscriptionInterval,SubscriptionStatus,@@ -40,8 +44,6 @@ import {} from '~/generated-admin/graphql';const STRIPE_DASHBOARD_BASE_URL = 'https://dashboard.stripe.com';-const BASE_PRODUCT_KEY = 'BASE_PRODUCT';-const RESOURCE_CREDIT_KEY = 'RESOURCE_CREDIT';const EM_DASH = '\u2014';const GRANT_CREDITS_MODAL_ID = 'settings-admin-grant-workspace-credits';@@ -100,19 +102,6 @@ const STATUS_LABELS: Record<SubscriptionStatus, string> = {[SubscriptionStatus.IncompleteExpired]: 'Incomplete Expired',};-const formatCurrency = (amountMinor: number, currency: string): string => {- const normalizedCurrency = currency.toUpperCase();-- try {- return new Intl.NumberFormat('en-US', {- style: 'currency',- currency: normalizedCurrency,- }).format(amountMinor / 100);- } catch {- return `${(amountMinor / 100).toFixed(2)} ${normalizedCurrency}`;- }-};-const toBillingPlanKey = (planKey: string): BillingPlanKey | null =>planKey === BillingPlanKey.PRO? BillingPlanKey.PRO@@ -252,23 +241,12 @@ export const SettingsAdminWorkspaceBillingContent = ({const formatItemValue = (item: NonNullable<typeof subscription>['items'][number],- ): string => {- const parts: string[] = [];-- if (isDefined(item.quantity)) {- parts.push(`${formatNumber(item.quantity)} ${t`seats`}`);- }- if (isDefined(item.includedCredits)) {- parts.push(- `${formatNumber(item.includedCredits, { abbreviate: true, decimals: 2 })} ${t`credits/period`}`,- );- }- if (isDefined(item.unitAmount) && isDefined(subscription)) {- parts.push(formatCurrency(item.unitAmount, subscription.currency));- }-- return parts.length > 0 ? parts.join(' · ') : EM_DASH;- };+ ): string =>+ formatSubscriptionItemValue({+ item,+ currency: subscription?.currency ?? '',+ formatNumber,+ });const subscriptionItems = subscription? [@@ -360,9 +338,9 @@ export const SettingsAdminWorkspaceBillingContent = ({: []),...subscription.items.map((item) => ({Icon:- item.productKey === BASE_PRODUCT_KEY+ item.productKey === BillingProductKey.BASE_PRODUCT? IconUsers- : item.productKey === RESOURCE_CREDIT_KEY+ : item.productKey === BillingProductKey.RESOURCE_CREDIT? IconCoins: IconBox,label: item.productName || t`Unnamed product`,diff --git a/packages/twenty-front/src/modules/settings/admin-panel/utils/__tests__/formatSubscriptionItemValue.test.ts b/packages/twenty-front/src/modules/settings/admin-panel/utils/__tests__/formatSubscriptionItemValue.test.tsnew file mode 100644index 00000000000..3082c11eead--- /dev/null+++ b/packages/twenty-front/src/modules/settings/admin-panel/utils/__tests__/formatSubscriptionItemValue.test.ts@@ -0,0 +1,88 @@+import { formatSubscriptionItemValue } from '@/settings/admin-panel/utils/formatSubscriptionItemValue';+import { BillingProductKey } from '~/generated-metadata/graphql';++const formatNumber = (value: number, options?: { decimals?: number }) =>+ value.toFixed(options?.decimals ?? 0);++const format = (+ item: Parameters<typeof formatSubscriptionItemValue>[0]['item'],+) => formatSubscriptionItemValue({ item, currency: 'USD', formatNumber });++describe('formatSubscriptionItemValue', () => {+ it('calls a base-product quantity seats', () => {+ expect(+ format({ productKey: BillingProductKey.BASE_PRODUCT, quantity: 3 }),+ ).toBe('3 seats');+ });++ it('does not call a metered quantity seats', () => {+ expect(+ format({ productKey: BillingProductKey.RESOURCE_CREDIT, quantity: 1 }),+ ).toBe('1');+ });++ it('does not call an unknown product quantity seats', () => {+ expect(format({ productKey: null, quantity: 2 })).toBe('2');+ });++ it('agrees in number with a single seat', () => {+ expect(+ format({ productKey: BillingProductKey.BASE_PRODUCT, quantity: 1 }),+ ).toBe('1 seat');+ });++ it('agrees in number with a single included credit', () => {+ expect(+ format({+ productKey: BillingProductKey.RESOURCE_CREDIT,+ includedCredits: 1,+ }),+ ).toBe('1.00 credit/period');+ });++ it('joins quantity, included credits and unit amount', () => {+ expect(+ format({+ productKey: BillingProductKey.BASE_PRODUCT,+ quantity: 3,+ includedCredits: 5,+ unitAmount: 2500,+ }),+ ).toBe('3 seats · 5.00 credits/period · $25.00');+ });++ it('renders an em dash when the item carries nothing to show', () => {+ expect(format({ productKey: BillingProductKey.RESOURCE_CREDIT })).toBe('—');+ });++ it('keeps a zero quantity rather than reading it as absent', () => {+ expect(+ format({ productKey: BillingProductKey.BASE_PRODUCT, quantity: 0 }),+ ).toBe('0 seats');+ });++ it('formats the amount through the caller formatter, not its own', () => {+ // The digits have to honour the workspace member's number format, so the+ // injected formatter is what decides how an amount reads.+ expect(+ formatSubscriptionItemValue({+ item: {+ productKey: BillingProductKey.BASE_PRODUCT,+ unitAmount: 123456,+ },+ currency: 'EUR',+ formatNumber: (value) => `<${value}>`,+ }),+ ).toBe('\u20ac<1234.56>');+ });++ it('falls back to the code when the currency has no resolvable symbol', () => {+ expect(+ formatSubscriptionItemValue({+ item: { productKey: BillingProductKey.BASE_PRODUCT, unitAmount: 1500 },+ currency: 'US',+ formatNumber,+ }),+ ).toBe('US15.00');+ });+});diff --git a/packages/twenty-front/src/modules/settings/admin-panel/utils/formatSubscriptionItemValue.ts b/packages/twenty-front/src/modules/settings/admin-panel/utils/formatSubscriptionItemValue.tsnew file mode 100644index 00000000000..a479b16557b--- /dev/null+++ b/packages/twenty-front/src/modules/settings/admin-panel/utils/formatSubscriptionItemValue.ts@@ -0,0 +1,87 @@+import { plural } from '@lingui/core/macro';+import { isDefined } from 'twenty-shared/utils';++import { type FormatNumberOptions } from '~/utils/format/formatNumber';+import { BillingProductKey } from '~/generated-metadata/graphql';++const EM_DASH = '—';++type SubscriptionItem = {+ productKey?: string | null;+ quantity?: number | null;+ includedCredits?: number | null;+ unitAmount?: number | null;+};++type FormatSubscriptionItemValueArgs = {+ item: SubscriptionItem;+ currency: string;+ formatNumber: (+ value: number,+ options?: Omit<FormatNumberOptions, 'format'>,+ ) => string;+};++// Only the symbol comes from Intl. The digits go through formatNumber like+// every other number on the page, so an amount does not ignore the workspace+// member's number format while the counts beside it honour it.+const getCurrencySymbol = (currency: string): string => {+ const normalizedCurrency = currency.toUpperCase();++ try {+ return (+ new Intl.NumberFormat('en-US', {+ style: 'currency',+ currency: normalizedCurrency,+ })+ .formatToParts(0)+ .find((part) => part.type === 'currency')?.value ?? normalizedCurrency+ );+ } catch {+ return normalizedCurrency;+ }+};++export const formatSubscriptionItemValue = ({+ item,+ currency,+ formatNumber,+}: FormatSubscriptionItemValueArgs): string => {+ const parts: string[] = [];++ if (isDefined(item.quantity)) {+ // Only a licensed base-product item sells seats. A metered item carries a+ // Stripe bookkeeping quantity that bills nothing, so naming it seats states+ // something untrue about what the workspace is paying for.+ parts.push(+ item.productKey === BillingProductKey.BASE_PRODUCT+ ? `${formatNumber(item.quantity)} ${plural(item.quantity, {+ one: 'seat',+ other: 'seats',+ })}`+ : formatNumber(item.quantity),+ );+ }++ if (isDefined(item.includedCredits)) {+ parts.push(+ `${formatNumber(item.includedCredits, {+ abbreviate: true,+ decimals: 2,+ })} ${plural(item.includedCredits, {+ one: 'credit/period',+ other: 'credits/period',+ })}`,+ );+ }++ if (isDefined(item.unitAmount)) {+ parts.push(+ `${getCurrencySymbol(currency)}${formatNumber(item.unitAmount / 100, {+ decimals: 2,+ })}`,+ );+ }++ return parts.length > 0 ? parts.join(' · ') : EM_DASH;+};