Files
member-console/internal/billing/queries/payments.sql
T
cgalo5758 257955c9d3 Add operator list-scale contract and People directory
Governed operator lists (organizations, grants, people, billing×4) gain
server-side search, status filters, and 50-row pages with true totals
from count(*) OVER(); state is URL-addressable, out-of-range pages
clamp,
and no-match is distinct from true-empty.

People is the eighth flat sidebar entry: /operator/persons lists persons
newest-joined first (excluding the reserved system person), rows linking
to the existing detail.

Billing gains an operator invoice detail at
/operator/billing/invoices/{invoiceID} reusing the member projection;
open invoices past due present as Overdue (derived, filterable, stored
status untouched); all four views lead with the linked organization and
mute object IDs.

Grants filter over the derived Live/Superseded/Inactive state, the SQL
HAVING predicate pinned to the Go derivation by test. Embedded lists
(org composite ledger, Tier changes) adopt the shared controls under
namespaced params with sibling-state-preserving URLs and scoped htmx
swaps that hold the viewport.

Review corrections: blocked ladder Delete renders disabled with tooltip
and mutations fire toasts; collapse triggers paint their open state;
sections use outside headings; plan topology drops the orphan-product
check; domains policy collapses behind a disclosure.
2026-08-24 03:58:18 -05:00

75 lines
2.5 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).
-- 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,
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
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;