Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

feat(billing): Use the customer data endpoint to update billing emails (#49160)

Daniel Guerrab2a216b617f1
diff --git a/apps/studio/components/interfaces/Organization/BillingSettings/BillingCustomerData/BillingCustomerData.tsx b/apps/studio/components/interfaces/Organization/BillingSettings/BillingCustomerData/BillingCustomerData.tsx
index 42f0369936f..8484dccb449 100644
--- a/apps/studio/components/interfaces/Organization/BillingSettings/BillingCustomerData/BillingCustomerData.tsx
+++ b/apps/studio/components/interfaces/Organization/BillingSettings/BillingCustomerData/BillingCustomerData.tsx
@@ -57,7 +57,10 @@ export const BillingCustomerData = () => {
isSuccess,
} = useOrganizationCustomerProfileQuery(
{ slug },
- { enabled: canReadBillingCustomerData && inView }
+ {
+ enabled: canReadBillingCustomerData && inView,
+ select: (data) => (data ? { address: data.address, billing_name: data.billing_name } : data),
+ }
)
const {
diff --git a/apps/studio/components/interfaces/Organization/BillingSettings/BillingEmail.test.tsx b/apps/studio/components/interfaces/Organization/BillingSettings/BillingEmail.test.tsx
new file mode 100644
index 00000000000..83f8d59289d
--- /dev/null
+++ b/apps/studio/components/interfaces/Organization/BillingSettings/BillingEmail.test.tsx
@@ -0,0 +1,252 @@
+import { PermissionAction } from '@supabase/shared-types/out/constants'
+import { QueryClient } from '@tanstack/react-query'
+import { fireEvent, screen, waitFor } from '@testing-library/react'
+import userEvent from '@testing-library/user-event'
+import { platformComponents as components } from 'api-types'
+import { mockAnimationsApi } from 'jsdom-testing-mocks'
+import { HttpResponse } from 'msw'
+import { beforeEach, describe, expect, test, vi } from 'vitest'
+
+import BillingEmail from './BillingEmail'
+import { organizationKeys } from '@/data/organizations/keys'
+import { customRender } from '@/tests/lib/custom-render'
+import { addAPIMock } from '@/tests/lib/msw'
+
+type CustomerResponse = components['schemas']['CustomerResponse']
+
+// The additional-emails control renders a Radix Popover when adding a recipient.
+mockAnimationsApi()
+
+const SLUG = 'acme-org'
+
+const mockCheckPermissions = vi.hoisted(() => vi.fn())
+
+vi.mock('common', async (importOriginal) => {
+ const original = (await importOriginal()) as typeof import('common')
+ return {
+ ...original,
+ // The customer-profile query is platform-only; IS_PLATFORM is false by default in tests.
+ IS_PLATFORM: true,
+ useParams: () => ({ slug: SLUG }),
+ }
+})
+
+vi.mock('@/hooks/misc/useCheckPermissions', () => ({
+ useAsyncCheckPermissions: (action: string) => mockCheckPermissions(action),
+}))
+
+vi.mock('react-intersection-observer', () => ({
+ useInView: () => ({ ref: vi.fn(), inView: true }),
+}))
+
+const createCustomerProfileResponse = (
+ overrides: Partial<CustomerResponse> = {}
+): CustomerResponse => ({
+ additional_emails: [],
+ balance: 0,
+ billing_via_partner: false,
+ email: '[email protected]',
+ tax_id: null,
+ ...overrides,
+})
+
+const mockCustomerProfile = (overrides: Partial<CustomerResponse> = {}) => {
+ addAPIMock({
+ method: 'get',
+ path: '/platform/organizations/:slug/customer',
+ response: () => HttpResponse.json<CustomerResponse>(createCustomerProfileResponse(overrides)),
+ })
+}
+
+const mockUpdateCustomerProfile = () => {
+ const requests: Array<{ slug: string | undefined; body: unknown }> = []
+ addAPIMock({
+ method: 'put',
+ path: '/platform/organizations/:slug/customer',
+ response: async ({ request, params }) => {
+ requests.push({ slug: params.slug as string | undefined, body: await request.json() })
+ return HttpResponse.json({}, { status: 204 })
+ },
+ })
+ return requests
+}
+
+const addRecipient = async (email: string) => {
+ const input = screen.getByPlaceholderText('Add additional recipients')
+ await userEvent.click(input)
+ await waitFor(() => expect(input).toHaveAttribute('aria-expanded', 'true'))
+ // fireEvent.change (rather than userEvent.type) avoids racing the popover's open-state
+ // transition character-by-character, which otherwise drops the first keystroke(s).
+ fireEvent.change(input, { target: { value: email } })
+ fireEvent.click(await screen.findByRole('option', { name: new RegExp(`Create "${email}"`) }))
+}
+
+const removeRecipient = (email: string) => {
+ const badge = screen.getByText(email)
+ fireEvent.click(badge.querySelector('svg')!.parentElement!)
+}
+
+describe('BillingEmail', () => {
+ beforeEach(() => {
+ vi.clearAllMocks()
+ mockCheckPermissions.mockImplementation(() => ({ can: true, isSuccess: true }))
+ })
+
+ test('renders the current billing email and additional recipients', async () => {
+ mockCustomerProfile({
+ email: '[email protected]',
+ additional_emails: ['[email protected]'],
+ })
+
+ customRender(<BillingEmail />)
+
+ expect(await screen.findByPlaceholderText('Email')).toHaveValue('[email protected]')
+ expect(screen.getByText('[email protected]')).toBeInTheDocument()
+ })
+
+ test('shows a permission notice when the user cannot read billing data', async () => {
+ mockCheckPermissions.mockImplementation((action: string) => ({
+ can: action !== PermissionAction.BILLING_READ,
+ isSuccess: true,
+ }))
+
+ customRender(<BillingEmail />)
+
+ expect(await screen.findByText(/view this organization's email recipients/)).toBeInTheDocument()
+ expect(screen.queryByPlaceholderText('Email')).not.toBeInTheDocument()
+ })
+
+ test('disables the email controls when the user cannot update billing data', async () => {
+ mockCheckPermissions.mockImplementation((action: string) => ({
+ can: action !== PermissionAction.BILLING_WRITE,
+ isSuccess: true,
+ }))
+ mockCustomerProfile({ email: '[email protected]', additional_emails: [] })
+
+ customRender(<BillingEmail />)
+
+ expect(await screen.findByPlaceholderText('Email')).toBeDisabled()
+ expect(await screen.findByPlaceholderText('Add additional recipients')).toBeDisabled()
+ expect(
+ screen.getByText('You need additional permissions to update billing emails')
+ ).toBeInTheDocument()
+ })
+
+ test('saves the updated email while keeping the existing additional recipients', async () => {
+ mockCustomerProfile({
+ email: '[email protected]',
+ additional_emails: ['[email protected]'],
+ })
+ const requests = mockUpdateCustomerProfile()
+
+ customRender(<BillingEmail />)
+
+ const emailInput = await screen.findByPlaceholderText('Email')
+ await userEvent.clear(emailInput)
+ await userEvent.type(emailInput, '[email protected]')
+
+ fireEvent.click(await screen.findByRole('button', { name: 'Save' }))
+
+ await waitFor(() => expect(requests).toHaveLength(1))
+ expect(requests[0]).toEqual({
+ slug: SLUG,
+ body: { email: '[email protected]', additional_emails: ['[email protected]'] },
+ })
+ })
+
+ test('adds a recipient when there are none', async () => {
+ mockCustomerProfile({ email: '[email protected]', additional_emails: [] })
+ const requests = mockUpdateCustomerProfile()
+
+ customRender(<BillingEmail />)
+ await screen.findByPlaceholderText('Email')
+
+ await addRecipient('[email protected]')
+ fireEvent.click(await screen.findByRole('button', { name: 'Save' }))
+
+ await waitFor(() => expect(requests).toHaveLength(1))
+ expect(requests[0]).toEqual({
+ slug: SLUG,
+ body: { email: '[email protected]', additional_emails: ['[email protected]'] },
+ })
+ })
+
+ test('removes a recipient when there is one', async () => {
+ mockCustomerProfile({ email: '[email protected]', additional_emails: ['[email protected]'] })
+ const requests = mockUpdateCustomerProfile()
+
+ customRender(<BillingEmail />)
+ await screen.findByText('[email protected]')
+
+ removeRecipient('[email protected]')
+ fireEvent.click(await screen.findByRole('button', { name: 'Save' }))
+
+ await waitFor(() => expect(requests).toHaveLength(1))
+ expect(requests[0]).toEqual({
+ slug: SLUG,
+ body: { email: '[email protected]', additional_emails: [] },
+ })
+ })
+
+ test('appends a recipient when there is already one', async () => {
+ mockCustomerProfile({
+ email: '[email protected]',
+ additional_emails: ['[email protected]'],
+ })
+ const requests = mockUpdateCustomerProfile()
+
+ customRender(<BillingEmail />)
+ await screen.findByText('[email protected]')
+
+ await addRecipient('[email protected]')
+ fireEvent.click(await screen.findByRole('button', { name: 'Save' }))
+
+ await waitFor(() => expect(requests).toHaveLength(1))
+ expect(requests[0]).toEqual({
+ slug: SLUG,
+ body: {
+ email: '[email protected]',
+ additional_emails: ['[email protected]', '[email protected]'],
+ },
+ })
+ })
+
+ test('keeps an in-progress edit when the profile data refetches', async () => {
+ const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } })
+ let getCallCount = 0
+ let customerProfile = createCustomerProfileResponse({
+ email: '[email protected]',
+ additional_emails: ['[email protected]'],
+ })
+ addAPIMock({
+ method: 'get',
+ path: '/platform/organizations/:slug/customer',
+ response: () => {
+ getCallCount++
+ return HttpResponse.json<CustomerResponse>(customerProfile)
+ },
+ })
+
+ customRender(<BillingEmail />, { queryClient })
+
+ const emailInput = await screen.findByPlaceholderText('Email')
+ await waitFor(() => expect(emailInput).toHaveValue('[email protected]'))
+
+ await userEvent.clear(emailInput)
+ await userEvent.type(emailInput, '[email protected]')
+
+ // Simulate an unrelated refetch of the same shared customer-profile query (e.g. triggered
+ // by another section, or a background refetch) returning newer server data.
+ customerProfile = createCustomerProfileResponse({
+ email: '[email protected]',
+ additional_emails: ['[email protected]'],
+ })
+ await queryClient.invalidateQueries({ queryKey: organizationKeys.customerProfile(SLUG) })
+ await waitFor(() => expect(getCallCount).toBeGreaterThanOrEqual(2))
+
+ // The dirty form is left untouched - not overwritten with the newly fetched data.
+ expect(emailInput).toHaveValue('[email protected]')
+ expect(screen.getByText('[email protected]')).toBeInTheDocument()
+ expect(screen.queryByText('[email protected]')).not.toBeInTheDocument()
+ })
+})
diff --git a/apps/studio/components/interfaces/Organization/BillingSettings/BillingEmail.tsx b/apps/studio/components/interfaces/Organization/BillingSettings/BillingEmail.tsx
index f2b7e943f76..a035f9a4650 100644
--- a/apps/studio/components/interfaces/Organization/BillingSettings/BillingEmail.tsx
+++ b/apps/studio/components/interfaces/Organization/BillingSettings/BillingEmail.tsx
@@ -27,9 +27,8 @@ import { FormPanel } from '@/components/ui/Forms/FormPanel'
import { FormSection, FormSectionContent } from '@/components/ui/Forms/FormSection'
import { NoPermission } from '@/components/ui/NoPermission'
import { useOrganizationCustomerProfileQuery } from '@/data/organizations/organization-customer-profile-query'
-import { useOrganizationUpdateMutation } from '@/data/organizations/organization-update-mutation'
+import { useOrganizationCustomerProfileUpdateMutation } from '@/data/organizations/organization-customer-profile-update-mutation'
import { useAsyncCheckPermissions } from '@/hooks/misc/useCheckPermissions'
-import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization'
const FORM_ID = 'org-billing-email'
const formSchema = z.object({
@@ -39,53 +38,56 @@ const formSchema = z.object({
const BillingEmail = () => {
const { slug } = useParams()
- const { data: selectedOrganization } = useSelectedOrganizationQuery()
-
- const { name, billing_email } = selectedOrganization ?? {}
const { can: canReadBillingEmail, isSuccess: isPermissionsLoaded } = useAsyncCheckPermissions(
PermissionAction.BILLING_READ,
- 'stripe.subscriptions'
+ 'stripe.customer'
)
- const { can: canUpdateOrganization } = useAsyncCheckPermissions(
- PermissionAction.UPDATE,
- 'organizations'
+ const { can: canUpdateBillingData } = useAsyncCheckPermissions(
+ PermissionAction.BILLING_WRITE,
+ 'stripe.customer'
)
const { ref, inView } = useInView({ triggerOnce: true })
- const { data: billingCustomer, isPending: loadingBillingCustomer } =
- useOrganizationCustomerProfileQuery({ slug }, { enabled: canReadBillingEmail && inView })
+ const { data: customerProfile, isPending: loadingBillingCustomer } =
+ useOrganizationCustomerProfileQuery(
+ { slug },
+ {
+ enabled: canReadBillingEmail && inView,
+ select: (data) =>
+ data ? { email: data.email, additional_emails: data.additional_emails } : data,
+ }
+ )
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
- billingEmail: billing_email ?? '',
- additionalBillingEmails: billingCustomer?.additional_emails ?? [],
+ billingEmail: customerProfile?.email ?? '',
+ additionalBillingEmails: customerProfile?.additional_emails ?? [],
},
})
const additionalBillingEmails = useWatch({
control: form.control,
name: 'additionalBillingEmails',
})
- const { errors } = form.formState
+ const { errors, isDirty } = form.formState
const additionalEmailsError = errors.additionalBillingEmails ?? []
- const { mutate: updateOrganization, isPending: isUpdating } = useOrganizationUpdateMutation()
+ const { mutate: updateCustomerProfile, isPending: isUpdating } =
+ useOrganizationCustomerProfileUpdateMutation()
const onUpdateOrganizationEmail = async (values: z.infer<typeof formSchema>) => {
- if (!canUpdateOrganization) {
+ if (!canUpdateBillingData) {
return toast.error('You do not have the required permissions to update this organization')
}
if (!slug) return console.error('Slug is required')
- if (!name) return console.error('Organization name is required')
- updateOrganization(
+ updateCustomerProfile(
{
slug,
- name,
- billing_email: values.billingEmail,
- additional_billing_emails: values.additionalBillingEmails,
+ email: values.billingEmail,
+ additional_emails: values.additionalBillingEmails,
},
{
onSuccess: () => {
@@ -97,13 +99,13 @@ const BillingEmail = () => {
}
useEffect(() => {
- if (billingCustomer) {
+ if (customerProfile && !isDirty) {
form.reset({
- billingEmail: billing_email ?? '',
- additionalBillingEmails: billingCustomer.additional_emails ?? [],
+ billingEmail: customerProfile.email ?? '',
+ additionalBillingEmails: customerProfile.additional_emails ?? [],
})
}
- }, [billingCustomer])
+ }, [form, customerProfile, isDirty])
return (
<ScaffoldSection ref={ref}>
@@ -127,11 +129,11 @@ const BillingEmail = () => {
<FormActions
form={FORM_ID}
isSubmitting={isUpdating}
- hasChanges={form.formState.isDirty}
+ hasChanges={isDirty}
handleReset={form.reset}
- disabled={!canUpdateOrganization}
+ disabled={!canUpdateBillingData}
helper={
- !canUpdateOrganization
+ !canUpdateBillingData
? 'You need additional permissions to update billing emails'
: undefined
}
@@ -151,7 +153,7 @@ const BillingEmail = () => {
type="email"
{...field}
placeholder="Email"
- disabled={!canUpdateOrganization}
+ disabled={!canUpdateBillingData}
/>
</FormControl>
<FormMessage />
@@ -177,7 +179,11 @@ const BillingEmail = () => {
}
>
<FormControl>
- <MultiSelector values={field.value} onValuesChange={field.onChange}>
+ <MultiSelector
+ values={field.value}
+ onValuesChange={field.onChange}
+ disabled={!canUpdateBillingData}
+ >
<MultiSelectorTrigger
deletableBadge
showIcon={false}
diff --git a/apps/studio/data/organizations/organization-customer-profile-update-mutation.ts b/apps/studio/data/organizations/organization-customer-profile-update-mutation.ts
index e1d347977dc..d5344c308b3 100644
--- a/apps/studio/data/organizations/organization-customer-profile-update-mutation.ts
+++ b/apps/studio/data/organizations/organization-customer-profile-update-mutation.ts
@@ -9,9 +9,11 @@ import type { ResponseError, UseCustomMutationOptions } from '@/types'
export type OrganizationCustomerProfileUpdateVariables = {
slug?: string
address?: CustomerAddress
- billing_name: string
+ billing_name?: string
/** Pass a tax ID object to set/update, `null` to clear, or `undefined` to leave unchanged */
tax_id?: CustomerTaxId | null
+ email?: string
+ additional_emails?: string[]
/** When true, validates the request without persisting changes */
dry_run?: boolean
}
@@ -21,6 +23,8 @@ export async function updateOrganizationCustomerProfile({
address,
billing_name,
tax_id,
+ email,
+ additional_emails,
dry_run,
}: OrganizationCustomerProfileUpdateVariables) {
if (!slug) return console.error('Slug is required')
@@ -39,6 +43,8 @@ export async function updateOrganizationCustomerProfile({
: tax_id !== undefined
? { tax_id }
: {}),
+ email,
+ additional_emails,
...(dry_run ? { dry_run } : {}),
},
})
@@ -71,22 +77,27 @@ export const useOrganizationCustomerProfileUpdateMutation = ({
>({
mutationFn: (vars) => updateOrganizationCustomerProfile(vars),
async onSuccess(data, variables, context) {
- const { address, slug, billing_name, tax_id, dry_run } = variables
+ const { address, slug, billing_name, tax_id, email, additional_emails, dry_run } = variables
if (dry_run) {
await onSuccess?.(data, variables, context)
return
}
- // Optimistically update the cache for immediate UI consistency
+ // Optimistically update the cache for immediate UI consistency. Only patch the fields
+ // that were actually part of this mutation's variables - each caller (e.g. BillingEmail,
+ // BillingCustomerData) only sends the subset it owns, so an unconditional overwrite here
+ // would wipe out the other fields in the shared cache entry.
queryClient.setQueriesData(
{ queryKey: organizationKeys.customerProfile(slug) },
(prev: any) => {
if (!prev) return prev
return {
...prev,
- billing_name,
+ ...(billing_name !== undefined ? { billing_name } : {}),
...(address !== undefined ? { address } : {}),
+ ...(email !== undefined ? { email } : {}),
+ ...(additional_emails !== undefined ? { additional_emails: additional_emails } : {}),
}
}
)