Skip to content

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

fix(billing): allow for `invoice_pdf` to be null (#50138)

Julian Domke21077a407102
diff --git a/apps/studio/components/interfaces/Organization/InvoicesSettings/InvoicesSettings.test.tsx b/apps/studio/components/interfaces/Organization/InvoicesSettings/InvoicesSettings.test.tsx
index baffd645a80..05f922830c8 100644
--- a/apps/studio/components/interfaces/Organization/InvoicesSettings/InvoicesSettings.test.tsx
+++ b/apps/studio/components/interfaces/Organization/InvoicesSettings/InvoicesSettings.test.tsx
@@ -1,11 +1,17 @@
import { screen } from '@testing-library/react'
+import userEvent from '@testing-library/user-event'
import { mockIntersectionObserver } from 'jsdom-testing-mocks'
+import { toast } from 'sonner'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { InvoicesSettings } from './InvoicesSettings'
+import { getInvoice } from '@/data/invoices/invoice-query'
+import type { InvoicesData } from '@/data/invoices/invoices-query'
import { MANAGED_BY } from '@/lib/constants/infrastructure'
import { createMockOrganization, render } from '@/tests/helpers'
+type Invoice = NonNullable<InvoicesData>[number]
+
mockIntersectionObserver()
const { mockSelectedOrganization, mockInvoicesQuery, mockInvoicesCountQuery } = vi.hoisted(() => ({
@@ -38,6 +44,35 @@ vi.mock('@/components/ui/PartnerManagedResource', () => ({
default: () => <div data-testid="partner-managed-resource" />,
}))
+vi.mock('sonner', () => ({
+ toast: {
+ error: vi.fn(),
+ success: vi.fn(),
+ },
+}))
+
+const createMockInvoice = (details: Partial<Invoice> = {}): Invoice => ({
+ id: 'in_123',
+ number: 'ABCD-0001',
+ amount_due: 0,
+ invoice_pdf: 'https://example.com/invoice.pdf',
+ payment_attempted: false,
+ payment_is_processing: false,
+ period_end: 1_700_000_000,
+ status: 'draft',
+ subscription: null,
+ subtotal: 0,
+ ...details,
+})
+
+const createMockInvoiceWithoutPdf = (): Invoice => {
+ const invoice = createMockInvoice()
+ // The API can return null for invoice_pdf. Not reflected in the generated schema yet, but
+ // will be soon — drop the cast once the type is nullable.
+ ;(invoice as any).invoice_pdf = null
+ return invoice
+}
+
describe('InvoicesSettings', () => {
beforeEach(() => {
vi.clearAllMocks()
@@ -78,4 +113,36 @@ describe('InvoicesSettings', () => {
expect(screen.getByTestId('partner-managed-resource')).toBeInTheDocument()
})
+
+ it('disables the download button for invoices without a PDF', () => {
+ mockInvoicesCountQuery.mockReturnValue({ data: 1, isError: false })
+ mockInvoicesQuery.mockReturnValue({
+ data: [createMockInvoiceWithoutPdf()],
+ error: null,
+ isPending: false,
+ isError: false,
+ })
+
+ render(<InvoicesSettings />)
+
+ expect(screen.getByRole('button', { name: 'Download invoice' })).toBeDisabled()
+ })
+
+ it('shows an error when the fetched invoice has no PDF', async () => {
+ mockInvoicesCountQuery.mockReturnValue({ data: 1, isError: false })
+ mockInvoicesQuery.mockReturnValue({
+ data: [createMockInvoice()],
+ error: null,
+ isPending: false,
+ isError: false,
+ })
+ vi.mocked(getInvoice).mockResolvedValue(createMockInvoiceWithoutPdf())
+
+ render(<InvoicesSettings />)
+ await userEvent.click(screen.getByRole('button', { name: 'Download invoice' }))
+
+ expect(toast.error).toHaveBeenCalledWith(
+ 'Invoice PDF is not available yet. Please try again later.'
+ )
+ })
})
diff --git a/apps/studio/components/interfaces/Organization/InvoicesSettings/InvoicesSettings.tsx b/apps/studio/components/interfaces/Organization/InvoicesSettings/InvoicesSettings.tsx
index ebbb793c58d..c1a7b8811e6 100644
--- a/apps/studio/components/interfaces/Organization/InvoicesSettings/InvoicesSettings.tsx
+++ b/apps/studio/components/interfaces/Organization/InvoicesSettings/InvoicesSettings.tsx
@@ -90,7 +90,11 @@ export const InvoicesSettings = () => {
const fetchInvoice = async (id: string) => {
try {
const invoice = await getInvoice({ invoiceId: id, slug })
- if (invoice?.invoice_pdf) window.open(invoice.invoice_pdf, '_blank')
+ if (invoice?.invoice_pdf) {
+ window.open(invoice.invoice_pdf, '_blank')
+ } else {
+ toast.error('Invoice PDF is not available yet. Please try again later.')
+ }
} catch (error: any) {
toast.error(`Failed to fetch the selected invoice: ${error.message}`)
}
@@ -171,6 +175,10 @@ export const InvoicesSettings = () => {
) : (
<>
{invoices.map((x) => {
+ // invoice_pdf can be null. Not reflected in the generated schema yet, but will
+ // be soon
+ const hasInvoicePdf = Boolean(x.invoice_pdf)
+
return (
<TableRow key={x.id}>
<TableCell className="w-2">
@@ -208,8 +216,17 @@ export const InvoicesSettings = () => {
variant="outline"
className="w-7"
icon={<ScrollText size={16} strokeWidth={1.5} />}
+ aria-label="Download invoice"
+ disabled={!hasInvoicePdf}
onClick={() => fetchInvoice(x.id)}
- tooltip={{ content: { side: 'bottom', text: 'Download invoice' } }}
+ tooltip={{
+ content: {
+ side: 'bottom',
+ text: hasInvoicePdf
+ ? 'Download invoice'
+ : 'Invoice PDF is not available yet. Please try again later.',
+ },
+ }}
/>
{x.status === InvoiceStatus.PAID && x.amount_due > 0 && (
@@ -217,6 +234,7 @@ export const InvoicesSettings = () => {
variant="outline"
className="w-7"
icon={<Receipt size={16} strokeWidth={1.5} />}
+ aria-label="Download receipt"
onClick={() => fetchReceipt(x.id)}
tooltip={{ content: { side: 'bottom', text: 'Download receipt' } }}
/>