Files
member-console/internal/billing/queries/payments.sql
T
cgalo5758 dd3962990b Adopt entity keys and add invoice numbers
Replace the entity slugs on organizations, workspaces, resource pools,
and
plan ladders with nullable `key` columns and add keys to products,
prices,
and entitlement sets. Rename `providers.slug` to `provider` and add
partial
unique indexes for system and org role names.

Assign invoice numbers per billing account from a gapless transactional
counter; Stripe's number moves to the invoice mapping as an external
reference.

Seeds, fixtures, and the operator lookup address rows by key, and the
returning-login resync no longer blanks a display name when the IdP
sends
no `name` claim.
2026-08-29 20:12:04 -05:00

80 lines
2.7 KiB
SQL

-- name: CreatePayment :one
INSERT INTO core.payments (
invoice_id, billing_account_id, payment_method_id,
amount, currency, status, failed_at
)
VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING *;
-- name: GetPaymentByID :one
SELECT * FROM core.payments
WHERE payment_id = $1;
-- name: GetPaymentsByInvoiceID :many
SELECT * FROM core.payments
WHERE invoice_id = $1
ORDER BY created_at DESC;
-- name: ListPaymentsPage :many
-- Operator payments view, paged and searched (operator-list-scale UX-4;
-- operator-billing-views D8). No status facet: the operator-list-scale
-- spec's "Status filters exist where a status vocabulary exists"
-- requirement enumerates grants/invoices/subscriptions/organizations, not
-- payments, so this query offers search + pagination only. Carries
-- ba.org_id so the handler can resolve and link the organization without a
-- cross-module join (see billing_accounts.sql's ListBillingAccountsPage
-- comment for why).
--
-- sqlc.narg(q): NULL matches every row; set, a case-insensitive substring
-- match against the billing account's name (the view's leading identifier
-- pre-reorder) or its organization via sqlc.narg(org_ids), pre-resolved in
-- Go (operator_billing.go: matchingOrgIDs).
-- Joins to core.invoices (not left -- invoice_id is NOT NULL on
-- core.payments) to carry invoice_number: invoice-numbers D3 puts the
-- number in the title of this view's "View invoice" cross-reference.
-- count(*) OVER() carries the true total for the filtered set (design D2).
SELECT
p.payment_id,
p.invoice_id,
p.billing_account_id,
ba.org_id,
p.payment_method_id,
p.amount,
p.currency,
p.status,
p.failed_at,
p.created_at,
p.updated_at,
ba.name as billing_account_name,
pm.pm_type as payment_method_type,
pm.card_brand,
pm.card_last4,
inv.invoice_number,
count(*) OVER() AS total_count
FROM core.payments p
JOIN core.accounts ba ON p.billing_account_id = ba.billing_account_id
LEFT JOIN core.payment_methods pm ON p.payment_method_id = pm.payment_method_id
JOIN core.invoices inv ON p.invoice_id = inv.invoice_id
WHERE (sqlc.narg(q)::text IS NULL
OR ba.name ILIKE '%' || sqlc.narg(q)::text || '%'
OR ba.org_id = ANY(sqlc.narg(org_ids)::uuid[]))
ORDER BY p.created_at DESC
LIMIT sqlc.arg(page_limit)::int OFFSET sqlc.arg(page_offset)::int;
-- name: ListRecentPayments :many
-- Recent payments for the operator landing activity timeline. Joins to
-- core.accounts to resolve the org_id (same schema, safe for sqlc).
SELECT
p.payment_id,
p.invoice_id,
p.billing_account_id,
ba.org_id,
p.amount,
p.currency,
p.status,
p.created_at
FROM core.payments p
JOIN core.accounts ba ON ba.billing_account_id = p.billing_account_id
ORDER BY p.created_at DESC
LIMIT $1;