Files
member-console/internal/entitlements/queries/pool_provision_transitions.sql
T
cgalo5758 074702c8bc Seed ledger demo via shared grant act helpers
Extract ExtendGrantTx, RevokeGrantTx, and ExpireGrantTx into
internal/entitlements so the operator handlers, expiry activity, and
demo seed all run the same conferral path. The seed plays a five-grant
history on Carlos's org; pin-timestamps now uses dense-rank offsets
and transition list ordering ties break by transition_id.
2026-09-07 00:51:49 -05:00

55 lines
2.3 KiB
SQL

-- Writes to core.pool_provision_transitions go through the conferral functions
-- (queries/conferral.sql) alone; core_writer holds no direct DML on this
-- table (migration 00005 enclosure). Only read paths live here.
-- name: ListTransitionsByPool :many
-- Joins to core.pool_provisions (on provision_id) and core.grants (on
-- grant_id) so the Tier changes view can tell a grant-backed row from any
-- other: a row whose provision came from a grant renders that grant's
-- reason and description (tier-changes-ledger design D4) instead of the
-- transition's own reason string, which Issue grant and Extend fill with
-- the operator's free-text note. Both joins are LEFT JOINs -- a
-- subscription- or purchase-sourced provision has no grant, and an
-- expiry/revocation/cancellation's restorative row has no provision_id at
-- all in some paths -- so a row with neither join match still renders via
-- its own reason. The transition id (uuidv7, rising with insertion) ends the
-- ORDER BY so the order is total: the two rows one act writes share
-- effective_at and created_at (one transaction), and the snapshot's clock
-- pin keeps such ties tied; the later-written row (a restoration after an
-- end) lists first.
SELECT
t.*,
p.grant_id,
g.grant_reason AS grant_reason,
g.description AS grant_description
FROM core.pool_provision_transitions t
LEFT JOIN core.pool_provisions p ON p.provision_id = t.provision_id
LEFT JOIN core.grants g ON g.grant_id = p.grant_id
WHERE t.pool_id = $1
ORDER BY t.effective_at DESC, t.created_at DESC, t.transition_id DESC;
-- name: ListTransitionsByProvision :many
SELECT * FROM core.pool_provision_transitions
WHERE provision_id = $1
ORDER BY effective_at ASC, created_at ASC, transition_id ASC;
-- name: ListRecentTransitions :many
-- Recent transitions for the operator landing activity timeline. Joins to
-- core.resource_pools to resolve the org_id (same schema, safe for
-- sqlc). Operator-actor transitions carry actor_id; system/webhook actors
-- don't, so person_id is nullable in the result.
SELECT
t.transition_id,
t.pool_id,
rp.org_id,
t.transition_type,
t.actor_type,
t.actor_id,
t.from_rank,
t.to_rank,
t.effective_at
FROM core.pool_provision_transitions t
JOIN core.resource_pools rp ON rp.pool_id = t.pool_id
ORDER BY t.effective_at DESC
LIMIT $1;