Files
member-console/internal/entitlements/entitlement_set_rules.sql.go
T
cgalo5758 f8a3478f2a Rebuild the entitlement set Rules surface as a staged batch
The Rules section is one record table grouped by kind, Limit then
Boolean, on fixed columns, edited in place: Edit opens a row's controls
in their columns, Add rule opens a dense row above the table, and every
change is staged into a tray that lists the deltas with Undo and applies
them as one rule-change act. The reduction policy is a column of the
rule beside its limit. History shows counts only. Group rows are a quiet
heading rather than a divider, the maintainer's pick from four rounds of
outside-model ideation.

Dense rows align to the top and render each error under its control in
every form family (design D16), replacing the below-row error block; the
forms library gains the batch form (rows plus one tray) and the RowField
dense and label-hidden options. Migration 00019 records the governing
reduction policy on effect rows.

Archive staged-rule-changes with its spec updates (entitlement-set-
management, entitlement-set-history, entitlements, form-library,
form-conventions, ui-quality-gate). Screens accepted 2026-09-19.
2026-09-19 19:46:09 -05:00

178 lines
5.7 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
// SPDX-FileCopyrightText: 2025-2026 Christian Galo
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.29.0
// source: entitlement_set_rules.sql
package entitlements
import (
"context"
"database/sql"
"github.com/lib/pq"
)
const getActiveRulesBySetID = `-- name: GetActiveRulesBySetID :many
SELECT rule_id, set_id, rule_type, resource_key, resource_value, resource_per_unit, stacking_policy, reset_period, credit_amount, credit_currency, description, is_active, created_at, updated_at, tier_reduction_policy FROM core.entitlement_set_rules
WHERE set_id = $1 AND is_active = TRUE
ORDER BY resource_key ASC
`
func (q *Queries) GetActiveRulesBySetID(ctx context.Context, setID string) ([]EntitlementSetRule, error) {
rows, err := q.db.QueryContext(ctx, getActiveRulesBySetID, setID)
if err != nil {
return nil, err
}
defer rows.Close()
items := []EntitlementSetRule{}
for rows.Next() {
var i EntitlementSetRule
if err := rows.Scan(
&i.RuleID,
&i.SetID,
&i.RuleType,
&i.ResourceKey,
&i.ResourceValue,
&i.ResourcePerUnit,
&i.StackingPolicy,
&i.ResetPeriod,
&i.CreditAmount,
&i.CreditCurrency,
&i.Description,
&i.IsActive,
&i.CreatedAt,
&i.UpdatedAt,
&i.TierReductionPolicy,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getEntitlementSetRule = `-- name: GetEntitlementSetRule :one
SELECT rule_id, set_id, rule_type, resource_key, resource_value, resource_per_unit, stacking_policy, reset_period, credit_amount, credit_currency, description, is_active, created_at, updated_at, tier_reduction_policy FROM core.entitlement_set_rules
WHERE rule_id = $1
`
// One rule in any state, so a preview and a commit can tell a rule that was
// deactivated a moment ago from a rule id that is not on this set. Writes go
// through core.commit_rule_change alone (migration 00018 revokes the DML).
func (q *Queries) GetEntitlementSetRule(ctx context.Context, ruleID string) (EntitlementSetRule, error) {
row := q.db.QueryRowContext(ctx, getEntitlementSetRule, ruleID)
var i EntitlementSetRule
err := row.Scan(
&i.RuleID,
&i.SetID,
&i.RuleType,
&i.ResourceKey,
&i.ResourceValue,
&i.ResourcePerUnit,
&i.StackingPolicy,
&i.ResetPeriod,
&i.CreditAmount,
&i.CreditCurrency,
&i.Description,
&i.IsActive,
&i.CreatedAt,
&i.UpdatedAt,
&i.TierReductionPolicy,
)
return i, err
}
const getGoverningReductionPolicy = `-- name: GetGoverningReductionPolicy :one
SELECT CASE
WHEN BOOL_OR(r.tier_reduction_policy = 'force_reduce') THEN 'force_reduce'
WHEN BOOL_OR(r.tier_reduction_policy = 'clamp') THEN 'clamp'
WHEN BOOL_OR(r.tier_reduction_policy = 'block') THEN 'block'
WHEN BOOL_OR(r.tier_reduction_policy = 'defer') THEN 'defer'
END::VARCHAR AS tier_reduction_policy
FROM core.pool_provisions pp
JOIN core.entitlement_set_rules r ON r.set_id = pp.entitlement_set_id
WHERE pp.pool_id = $1
AND pp.status = 'active'
AND r.is_active
AND r.rule_type = 'limit'
AND r.resource_key = $2
HAVING COUNT(*) > 0
`
type GetGoverningReductionPolicyParams struct {
PoolID string `json:"pool_id"`
ResourceKey sql.NullString `json:"resource_key"`
}
// The reduction policy governing one pool and one resource key: the strongest
// among the active limit rules that fund the pool through its active
// provisions, in the order force_reduce, clamp, block, defer. The order is
// stated here as an explicit CASE and once more as a list in the Go fold
// (entitlements, "The reduction policy governing a pool and key is the
// strongest across the rules funding it"); neither compares the values as
// text, which is what MIN() did until this query was rewritten.
func (q *Queries) GetGoverningReductionPolicy(ctx context.Context, arg GetGoverningReductionPolicyParams) (string, error) {
row := q.db.QueryRowContext(ctx, getGoverningReductionPolicy, arg.PoolID, arg.ResourceKey)
var tier_reduction_policy string
err := row.Scan(&tier_reduction_policy)
return tier_reduction_policy, err
}
const listEntitlementSetRulesBySetIDs = `-- name: ListEntitlementSetRulesBySetIDs :many
SELECT rule_id, set_id, rule_type, resource_key, resource_value, resource_per_unit, stacking_policy, reset_period, credit_amount, credit_currency, description, is_active, created_at, updated_at, tier_reduction_policy FROM core.entitlement_set_rules
WHERE set_id = ANY($1::uuid[]) AND is_active = TRUE
ORDER BY set_id ASC, resource_key ASC
`
// Batch reader for the products list's per-page verdict (product-management
// "The verdict per page, from batches, never cached", design D4): active
// rules across every entitlement set the page's products reference, one
// query instead of one per product (mirrors GetActiveRulesBySetID).
func (q *Queries) ListEntitlementSetRulesBySetIDs(ctx context.Context, setIds []string) ([]EntitlementSetRule, error) {
rows, err := q.db.QueryContext(ctx, listEntitlementSetRulesBySetIDs, pq.Array(setIds))
if err != nil {
return nil, err
}
defer rows.Close()
items := []EntitlementSetRule{}
for rows.Next() {
var i EntitlementSetRule
if err := rows.Scan(
&i.RuleID,
&i.SetID,
&i.RuleType,
&i.ResourceKey,
&i.ResourceValue,
&i.ResourcePerUnit,
&i.StackingPolicy,
&i.ResetPeriod,
&i.CreditAmount,
&i.CreditCurrency,
&i.Description,
&i.IsActive,
&i.CreatedAt,
&i.UpdatedAt,
&i.TierReductionPolicy,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}