Files
supabase__supabase/apps/studio/data/organizations/organization-customer-profile-update-mutation.ts
Kanishk Dudeja b917b0e1bf feat(billing): adds non-dismissable modal for indirect tax declaration (#49643)
### Summary

This PR adds a blocking dashboard modal for affected Australian
customers to confirm their GST registration and business use of
Supabase.

KPMG requires us to collect this declaration from certain existing
Australian customers. The backend now identifies organizations that
still need to respond using `requires_indirect_tax_declaration` and
stores their `yes` or `no`
response in Orb customer metadata.

It also supports email links with `submit_indirect_tax_declaration=true`
and shows a dismissible confirmation when the organization has already
responded.

### Testing

#### Manual testing

- Confirmed the modal appears for an affected organization without an
existing response and cannot be dismissed.
- Submitted both `yes` and `no` and confirmed the modal remains closed
after a refresh.
- Confirmed the declaration is stored without changing the customer's
Tax ID.
- Confirmed the modal does not appear for non admins/owners or
organizations that do not require a declaration.
- Confirmed the email-link parameter shows the already-submitted
confirmation only for organizations that have responded, and is removed
when dismissed.

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* Added an indirect tax declaration dialog for eligible Australian
organizations.
* Users with billing permissions can select “Yes” or “No” and submit
their declaration.
* Added a dismissible confirmation for declarations submitted through a
linked prompt.
* The dialog requires an explicit response and provides guidance when no
option is selected.

* **Bug Fixes**
* Declaration prompts remain visible through submission confirmation and
close when dismissed.
  * Users without billing permissions do not see the dialog.
* Success notifications no longer overlap with the confirmation dialog.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Julian Domke <68325451+juleswritescode@users.noreply.github.com>
2026-09-01 18:04:25 +05:30

157 lines
4.7 KiB
TypeScript

import { useMutation, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import { organizationKeys } from './keys'
import type { OrganizationsData } from './organizations-query'
import type { CustomerAddress, CustomerTaxId } from './types'
import { handleError, put } from '@/data/fetchers'
import type { ResponseError, UseCustomMutationOptions } from '@/types'
export type OrganizationCustomerProfileUpdateVariables = {
slug?: string
address?: CustomerAddress
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[]
indirect_tax_registration_declaration?: 'yes' | 'no'
/** When true, validates the request without persisting changes */
dry_run?: boolean
}
export async function updateOrganizationCustomerProfile({
slug,
address,
billing_name,
tax_id,
email,
additional_emails,
indirect_tax_registration_declaration,
dry_run,
}: OrganizationCustomerProfileUpdateVariables) {
if (!slug) return console.error('Slug is required')
const { data, error } = await put(`/platform/organizations/{slug}/customer`, {
params: {
path: {
slug,
},
},
body: {
address: address != null ? address : undefined,
billing_name,
...(tax_id === null
? { clear_tax_id: true as const }
: tax_id !== undefined
? { tax_id }
: {}),
email,
additional_emails,
indirect_tax_registration_declaration,
...(dry_run ? { dry_run } : {}),
},
})
if (error) throw handleError(error)
return data
}
type OrganizationCustomerProfileUpdateData = Awaited<
ReturnType<typeof updateOrganizationCustomerProfile>
>
export const useOrganizationCustomerProfileUpdateMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseCustomMutationOptions<
OrganizationCustomerProfileUpdateData,
ResponseError,
OrganizationCustomerProfileUpdateVariables
>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<
OrganizationCustomerProfileUpdateData,
ResponseError,
OrganizationCustomerProfileUpdateVariables
>({
mutationFn: (vars) => updateOrganizationCustomerProfile(vars),
async onSuccess(data, variables, context) {
const {
address,
slug,
billing_name,
tax_id,
email,
additional_emails,
indirect_tax_registration_declaration,
dry_run,
} = variables
if (dry_run) {
await onSuccess?.(data, variables, context)
return
}
// 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 !== undefined ? { billing_name } : {}),
...(address !== undefined ? { address } : {}),
...(email !== undefined ? { email } : {}),
...(additional_emails !== undefined ? { additional_emails: additional_emails } : {}),
}
}
)
if (tax_id !== undefined) {
queryClient.setQueryData(organizationKeys.taxId(slug), tax_id)
}
if (indirect_tax_registration_declaration !== undefined) {
queryClient.setQueryData<OrganizationsData>(organizationKeys.list(), (previous) =>
previous?.map((organization) =>
organization.slug === slug
? { ...organization, requires_indirect_tax_declaration: false }
: organization
)
)
}
// Refetch after a delay to pick up server-canonical values (e.g. normalized tax IDs).
// The GET endpoint can be stale for 1-2 seconds after an update.
setTimeout(() => {
queryClient.invalidateQueries({
queryKey: organizationKeys.customerProfile(slug),
})
if (tax_id !== undefined) {
queryClient.invalidateQueries({
queryKey: organizationKeys.taxId(slug),
})
}
}, 3000)
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to update customer profile: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
})
}