Persist pool-scoped grants using OR semantics across active provisions, retaining lapsed rows for transition visibility. Add consumer queries, DB-backed coverage, and plans for the dependent Discourse integration.
30 lines
1.1 KiB
SQL
30 lines
1.1 KiB
SQL
-- name: UpsertBooleanEntitlementGranted :one
|
|
INSERT INTO core.boolean_entitlements (pool_id, resource_key, granted)
|
|
VALUES ($1, $2, TRUE)
|
|
ON CONFLICT (pool_id, resource_key) DO UPDATE SET granted = TRUE
|
|
RETURNING *;
|
|
|
|
-- name: LapseBooleanEntitlement :execrows
|
|
-- Update-only by design: a lapse never creates a row. Absence of a row means
|
|
-- the key was never conferred; granted = FALSE means conferred, then lapsed.
|
|
UPDATE core.boolean_entitlements
|
|
SET granted = FALSE
|
|
WHERE pool_id = $1 AND resource_key = $2;
|
|
|
|
-- name: GetBooleanEntitlementByPoolAndResource :one
|
|
SELECT * FROM core.boolean_entitlements
|
|
WHERE pool_id = $1 AND resource_key = $2;
|
|
|
|
-- name: ListBooleanEntitlementsByPoolID :many
|
|
SELECT * FROM core.boolean_entitlements
|
|
WHERE pool_id = $1
|
|
ORDER BY resource_key ASC;
|
|
|
|
-- name: ListPoolsGrantedBooleanKey :many
|
|
SELECT be.pool_id, rp.org_id, o.name AS org_name
|
|
FROM core.boolean_entitlements be
|
|
JOIN core.resource_pools rp ON rp.pool_id = be.pool_id
|
|
JOIN core.organizations o ON o.org_id = rp.org_id
|
|
WHERE be.resource_key = $1 AND be.granted = TRUE
|
|
ORDER BY o.name ASC, be.pool_id ASC;
|