Skip to content

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

fix(studio): name the organization in the overdue invoices banner (#51747)

Drew McLellan1d9808ab8b10
diff --git a/apps/studio/components/interfaces/Organization/restriction.constants.test.tsx b/apps/studio/components/interfaces/Organization/restriction.constants.test.tsx
new file mode 100644
index 00000000000..aa37bb08ced
--- /dev/null
+++ b/apps/studio/components/interfaces/Organization/restriction.constants.test.tsx
@@ -0,0 +1,51 @@
+import { screen } from '@testing-library/react'
+import { describe, expect, test } from 'vitest'
+
+import { RESTRICTION_MESSAGES } from './restriction.constants'
+import { customRender } from '@/tests/lib/custom-render'
+
+const { description } = RESTRICTION_MESSAGES.OVERDUE_INVOICES_FROM_OTHER_ORGS
+
+const stevens = { name: "Steven's Org", slug: 'stevens-org' }
+const acme = { name: 'Acme', slug: 'acme' }
+const globex = { name: 'Globex', slug: 'globex' }
+
+describe('OVERDUE_INVOICES_FROM_OTHER_ORGS description', () => {
+ test('names the organization and links to its invoices', () => {
+ customRender(<p data-testid="message">{description([stevens])}</p>)
+
+ expect(screen.getByRole('link', { name: "Steven's Org" })).toHaveAttribute(
+ 'href',
+ '/org/stevens-org/billing#invoices'
+ )
+ expect(screen.getByTestId('message')).toHaveTextContent(
+ "Pay invoices for Steven's Org to avoid service disruption"
+ )
+ })
+
+ test('names only the first organization and counts the rest', () => {
+ customRender(<p data-testid="message">{description([acme, stevens])}</p>)
+ expect(screen.getByTestId('message')).toHaveTextContent(
+ 'Pay invoices for Acme and 1 other to avoid service disruption'
+ )
+ expect(screen.queryByRole('link', { name: "Steven's Org" })).not.toBeInTheDocument()
+ })
+
+ test('pluralizes the remaining count', () => {
+ customRender(<p data-testid="message">{description([acme, stevens, globex])}</p>)
+ expect(screen.getByTestId('message')).toHaveTextContent(
+ 'Pay invoices for Acme and 2 others to avoid service disruption'
+ )
+ })
+
+ test('falls back to the organizations list when none can be resolved', () => {
+ customRender(<p data-testid="message">{description([])}</p>)
+ expect(screen.getByRole('link', { name: 'Pay outstanding invoices' })).toHaveAttribute(
+ 'href',
+ '/organizations'
+ )
+ expect(screen.getByTestId('message')).toHaveTextContent(
+ 'Pay outstanding invoices to avoid service disruption'
+ )
+ })
+})
diff --git a/apps/studio/components/interfaces/Organization/restriction.constants.tsx b/apps/studio/components/interfaces/Organization/restriction.constants.tsx
index 6f609a823b0..868dc9f1d10 100644
--- a/apps/studio/components/interfaces/Organization/restriction.constants.tsx
+++ b/apps/studio/components/interfaces/Organization/restriction.constants.tsx
@@ -48,12 +48,28 @@ export const RESTRICTION_MESSAGES = {
),
},
OVERDUE_INVOICES_FROM_OTHER_ORGS: {
- title: 'Outstanding invoices in other organization',
- description: (slug: string): ReactNode => (
- <>
- Please <InlineLink href={`/org/${slug}/billing#invoices`}>pay invoices</InlineLink> for
- other organizations to avoid service disruption
- </>
- ),
+ title: 'Outstanding invoices in other organizations',
+ description: (orgs: { name: string; slug: string }[]): ReactNode => {
+ const [firstOrg, ...remainingOrgs] = orgs
+
+ if (!firstOrg) {
+ return (
+ <>
+ <InlineLink href="/organizations">Pay outstanding invoices</InlineLink> to avoid service
+ disruption
+ </>
+ )
+ }
+
+ return (
+ <>
+ Pay invoices for{' '}
+ <InlineLink href={`/org/${firstOrg.slug}/billing#invoices`}>{firstOrg.name}</InlineLink>
+ {remainingOrgs.length > 0 &&
+ ` and ${remainingOrgs.length} other${remainingOrgs.length === 1 ? '' : 's'}`}{' '}
+ to avoid service disruption
+ </>
+ )
+ },
},
}
diff --git a/apps/studio/components/ui/DevToolbar/ResourceWarningsTab.tsx b/apps/studio/components/ui/DevToolbar/ResourceWarningsTab.tsx
index b7a3f0f1ba1..e49a5df72fe 100644
--- a/apps/studio/components/ui/DevToolbar/ResourceWarningsTab.tsx
+++ b/apps/studio/components/ui/DevToolbar/ResourceWarningsTab.tsx
@@ -10,6 +10,7 @@ import { InfoTooltip } from 'ui-patterns/info-tooltip'
import type { OverdueInvoicesResponse } from '@/data/invoices/invoices-overdue-query'
import { invoicesKeys } from '@/data/invoices/keys'
import { organizationKeys } from '@/data/organizations/keys'
+import { useOrganizationsQuery } from '@/data/organizations/organizations-query'
import { usageKeys } from '@/data/usage/keys'
import type { ResourceWarning } from '@/data/usage/resource-warnings-query'
import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization'
@@ -40,6 +41,7 @@ export const ResourceWarningsTab = () => {
const { ref } = useParams()
const queryClient = useQueryClient()
const { data: selectedOrg, isLoading: isOrgLoading } = useSelectedOrganizationQuery()
+ const { data: organizations } = useOrganizationsQuery()
const orgSlug = selectedOrg?.slug
const [isReadOnly, setIsReadOnly] = useState(false)
@@ -150,14 +152,19 @@ export const ResourceWarningsTab = () => {
// distinguished by organization_id rather than a status enum, so they can't
// be folded into the restriction_status patch above.
const selectedOrgId = selectedOrg?.id
- const overdueInvoices: OverdueInvoicesResponse[] =
- selectedOrgId === undefined
- ? []
- : effectiveOrgWarning === 'overdue_invoices'
- ? [{ organization_id: selectedOrgId, overdue_invoice_count: 1 }]
- : effectiveOrgWarning === 'overdue_invoices_from_other_org'
- ? [{ organization_id: selectedOrgId + 1, overdue_invoice_count: 1 }]
- : []
+ const overdueInvoices: OverdueInvoicesResponse[] = []
+ if (selectedOrgId !== undefined && effectiveOrgWarning === 'overdue_invoices') {
+ overdueInvoices.push({ organization_id: selectedOrgId, overdue_invoice_count: 1 })
+ }
+ if (selectedOrgId !== undefined && effectiveOrgWarning === 'overdue_invoices_from_other_org') {
+ // Use real orgs so the banner can resolve and name them; fall back to an unknown ID
+ const otherOrgIds = (organizations ?? [])
+ .filter((org) => org.id !== selectedOrgId)
+ .map((org) => org.id)
+ for (const orgId of otherOrgIds.length > 0 ? otherOrgIds : [selectedOrgId + 1]) {
+ overdueInvoices.push({ organization_id: orgId, overdue_invoice_count: 1 })
+ }
+ }
queryClient.setQueryData(invoicesKeys.overdueInvoices(), overdueInvoices)
hasOverridesRef.current = true
diff --git a/apps/studio/hooks/misc/useOrganizationRestrictions.test.tsx b/apps/studio/hooks/misc/useOrganizationRestrictions.test.tsx
new file mode 100644
index 00000000000..da68d7499b5
--- /dev/null
+++ b/apps/studio/hooks/misc/useOrganizationRestrictions.test.tsx
@@ -0,0 +1,120 @@
+import { waitFor } from '@testing-library/react'
+import { platformComponents as components } from 'api-types'
+import { HttpResponse } from 'msw'
+import { renderToStaticMarkup } from 'react-dom/server'
+import { describe, expect, test, vi } from 'vitest'
+
+import { useOrganizationRestrictions } from './useOrganizationRestrictions'
+import { RESTRICTION_MESSAGES } from '@/components/interfaces/Organization/restriction.constants'
+import { createMockOrganizationResponse } from '@/tests/helpers'
+import { customRenderHook } from '@/tests/lib/custom-render'
+import { addAPIMock } from '@/tests/lib/msw'
+import { createMockProfileContext } from '@/tests/lib/profile-helpers'
+
+type OrganizationResponse = components['schemas']['OrganizationResponse_Output']
+type OverdueInvoiceCount = components['schemas']['OverdueInvoiceCount_Output']
+
+vi.mock(import('common'), async (importOriginal) => {
+ const actual = await importOriginal()
+ return { ...actual, useParams: () => ({ slug: 'mine' }), useIsLoggedIn: () => true }
+})
+
+vi.mock(import('@/lib/constants'), async (importOriginal) => {
+ const actual = await importOriginal()
+ return { ...actual, IS_PLATFORM: true }
+})
+
+const ORGANIZATIONS = [
+ createMockOrganizationResponse({ id: 1, name: 'Mine', slug: 'mine' }),
+ createMockOrganizationResponse({ id: 2, name: 'Zeta', slug: 'zeta' }),
+ createMockOrganizationResponse({ id: 3, name: 'Acme', slug: 'acme' }),
+]
+
+const mockEndpoints = (overdueInvoices: OverdueInvoiceCount[]) => {
+ addAPIMock({
+ method: 'get',
+ path: '/platform/organizations',
+ response: () => HttpResponse.json<OrganizationResponse[]>(ORGANIZATIONS),
+ })
+ addAPIMock({
+ method: 'get',
+ path: '/platform/stripe/invoices/overdue',
+ response: () => HttpResponse.json<OverdueInvoiceCount[]>(overdueInvoices),
+ })
+}
+
+const renderWarnings = async (overdueInvoices: OverdueInvoiceCount[]) => {
+ mockEndpoints(overdueInvoices)
+ const { result } = customRenderHook(() => useOrganizationRestrictions(), {
+ profileContext: createMockProfileContext(),
+ })
+ // Wait for both queries: the selected org and the overdue invoices
+ await waitFor(() => expect(result.current.org?.slug).toBe('mine'))
+ return result
+}
+
+const otherOrgsWarningMarkup = (result: {
+ current: ReturnType<typeof useOrganizationRestrictions>
+}) => {
+ const warning = result.current.warnings.find(
+ (w) => w.title === RESTRICTION_MESSAGES.OVERDUE_INVOICES_FROM_OTHER_ORGS.title
+ )
+ return warning ? renderToStaticMarkup(<>{warning.description}</>) : undefined
+}
+
+describe('useOrganizationRestrictions: overdue invoices in other organizations', () => {
+ test('names the other organization with overdue invoices', async () => {
+ const result = await renderWarnings([{ organization_id: 2, overdue_invoice_count: 1 }])
+
+ await waitFor(() => expect(otherOrgsWarningMarkup(result)).toContain('Zeta'))
+ expect(otherOrgsWarningMarkup(result)).toContain('href="/org/zeta/billing#invoices"')
+ expect(otherOrgsWarningMarkup(result)).not.toContain('other')
+ })
+
+ test('names the first organization alphabetically and counts the rest', async () => {
+ const result = await renderWarnings([
+ { organization_id: 2, overdue_invoice_count: 1 },
+ { organization_id: 3, overdue_invoice_count: 1 },
+ ])
+
+ await waitFor(() => expect(otherOrgsWarningMarkup(result)).toContain('Acme'))
+ expect(otherOrgsWarningMarkup(result)).toContain('and 1 other')
+ expect(otherOrgsWarningMarkup(result)).not.toContain('Zeta')
+ })
+
+ test('counts an organization once however many invoices it has overdue', async () => {
+ const result = await renderWarnings([
+ { organization_id: 3, overdue_invoice_count: 4 },
+ { organization_id: 3, overdue_invoice_count: 1 },
+ ])
+
+ await waitFor(() => expect(otherOrgsWarningMarkup(result)).toContain('Acme'))
+ expect(otherOrgsWarningMarkup(result)).not.toContain('other')
+ })
+
+ test('does not list the current organization as an other organization', async () => {
+ const result = await renderWarnings([
+ { organization_id: 1, overdue_invoice_count: 1 },
+ { organization_id: 2, overdue_invoice_count: 1 },
+ ])
+
+ await waitFor(() => expect(otherOrgsWarningMarkup(result)).toContain('Zeta'))
+ expect(otherOrgsWarningMarkup(result)).not.toContain('Mine')
+ expect(otherOrgsWarningMarkup(result)).not.toContain('other')
+ // The current organization's own overdue invoices get their own banner
+ expect(result.current.warnings.map((w) => w.title)).toContain(
+ RESTRICTION_MESSAGES.OVERDUE_INVOICES.title
+ )
+ })
+
+ test('shows no other-organizations banner when only the current organization is overdue', async () => {
+ const result = await renderWarnings([{ organization_id: 1, overdue_invoice_count: 1 }])
+
+ await waitFor(() =>
+ expect(result.current.warnings.map((w) => w.title)).toContain(
+ RESTRICTION_MESSAGES.OVERDUE_INVOICES.title
+ )
+ )
+ expect(otherOrgsWarningMarkup(result)).toBeUndefined()
+ })
+})
diff --git a/apps/studio/hooks/misc/useOrganizationRestrictions.ts b/apps/studio/hooks/misc/useOrganizationRestrictions.ts
index f18c02ab265..303b8faa559 100644
--- a/apps/studio/hooks/misc/useOrganizationRestrictions.ts
+++ b/apps/studio/hooks/misc/useOrganizationRestrictions.ts
@@ -51,15 +51,14 @@ export function useOrganizationRestrictions() {
}
if (overdueInvoicesFromOtherOrgs?.length) {
- const otherOrgSlug = organizations?.find(
- (o) => o.id === overdueInvoicesFromOtherOrgs[0].organization_id
- )?.slug
+ const overdueOrgIds = new Set(overdueInvoicesFromOtherOrgs.map((i) => i.organization_id))
+ const orgsWithOverdueInvoices = (organizations ?? []).filter((o) => overdueOrgIds.has(o.id))
+
warnings.push({
variant: 'danger',
title: RESTRICTION_MESSAGES.OVERDUE_INVOICES_FROM_OTHER_ORGS.title,
- description: RESTRICTION_MESSAGES.OVERDUE_INVOICES_FROM_OTHER_ORGS.description(
- otherOrgSlug ?? org?.slug ?? 'default'
- ),
+ description:
+ RESTRICTION_MESSAGES.OVERDUE_INVOICES_FROM_OTHER_ORGS.description(orgsWithOverdueInvoices),
})
}