Files
member-console/internal/billing/queries/prices.sql
T
cgalo5758 fa169dca3b Add purchasability status to products list
- Refine tier changes ledger: fold supersession pairs, humanize reasons,
  carry grant notes
- Fix OAuth token expiry to use wall clock
2026-09-06 23:31:12 -05:00

78 lines
3.2 KiB
SQL

-- name: GetPrice :one
SELECT * FROM core.prices
WHERE price_id = $1;
-- name: ListPricesByProduct :many
SELECT * FROM core.prices
WHERE product_id = $1 AND is_active = TRUE
ORDER BY created_at ASC, price_id ASC;
-- name: ListPricesByProductIDs :many
-- Batch reader for the products list's per-page verdict (product-management
-- "The verdict per page, from batches, never cached", design D4): every
-- active price for the page's products, one query instead of one per
-- product (mirrors ListPricesByProduct's is_active filter and order, so
-- priceReadinessFromBatches can pick the same tracked price per product
-- from the grouped rows).
SELECT * FROM core.prices
WHERE product_id = ANY(sqlc.arg(product_ids)::uuid[]) AND is_active = TRUE
ORDER BY product_id ASC, created_at ASC, price_id ASC;
-- name: GetDefaultPriceByProduct :one
SELECT * FROM core.prices
WHERE product_id = $1 AND is_default = TRUE;
-- name: CreatePrice :one
-- The first price a product gets becomes its default automatically (there is
-- nothing else to offer members); subsequent prices are created non-default
-- and promoted explicitly via Clear/MarkDefaultPrice. The NOT EXISTS probe
-- also self-heals a product that somehow lost its default: the next price
-- created becomes it. The partial unique index
-- (idx_prices_one_default_per_product) backstops concurrent creation races.
--
-- `key` is the optional declarative address (entity-keys §3), unique within
-- the product because a price is a child of one. Stripe's `lookup_key` is
-- the same idea on the same entity. The operator create form does not
-- collect one, so a UI-created price leaves it NULL.
INSERT INTO core.prices (product_id, currency, unit_amount, recurring_interval, trial_period_days, is_active, is_default, key)
VALUES ($1, $2, $3, $4, $5, TRUE,
NOT EXISTS (SELECT 1 FROM core.prices p WHERE p.product_id = $1 AND p.is_default),
sqlc.narg(key))
RETURNING *;
-- name: GetPriceByProductAndKey :one
-- Child key resolver (entity-keys §5): the declarative address of a price is
-- its product's key plus its own. uq_prices_product_id_key guarantees at
-- most one row matches.
SELECT * FROM core.prices
WHERE product_id = $1 AND key = $2;
-- name: ClearDefaultPrice :exec
-- First half of the make-default handshake; run in the same transaction as
-- MarkDefaultPrice (clear before set, so the partial unique index never sees
-- two defaults for one product).
UPDATE core.prices
SET is_default = FALSE
WHERE product_id = $1 AND is_default;
-- name: MarkDefaultPrice :one
-- Second half of the make-default handshake. Scoped to (price_id, product_id)
-- so a stale/forged price ID from another product cannot be promoted; the
-- is_active guard keeps a deactivated price from becoming the default.
UPDATE core.prices
SET is_default = TRUE
WHERE price_id = $1 AND product_id = $2 AND is_active = TRUE
RETURNING *;
-- name: DeactivatePrice :one
-- Handlers must refuse to deactivate the default price (members would lose
-- the purchase path); the is_default guard here backstops that rule.
UPDATE core.prices
SET is_active = FALSE
WHERE price_id = $1 AND is_default = FALSE
RETURNING *;
-- name: AnyActivePrice :one
-- Setup-checklist existence probe.
SELECT EXISTS(SELECT 1 FROM core.prices WHERE is_active = TRUE);