Replace the entity slugs on organizations, workspaces, resource pools, and plan ladders with nullable `key` columns and add keys to products, prices, and entitlement sets. Rename `providers.slug` to `provider` and add partial unique indexes for system and org role names. Assign invoice numbers per billing account from a gapless transactional counter; Stripe's number moves to the invoice mapping as an external reference. Seeds, fixtures, and the operator lookup address rows by key, and the returning-login resync no longer blanks a display name when the IdP sends no `name` claim.
282 lines
8.2 KiB
Go
282 lines
8.2 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.29.0
|
|
// source: prices.sql
|
|
|
|
package billing
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
)
|
|
|
|
const anyActivePrice = `-- name: AnyActivePrice :one
|
|
SELECT EXISTS(SELECT 1 FROM core.prices WHERE is_active = TRUE)
|
|
`
|
|
|
|
// Setup-checklist existence probe.
|
|
func (q *Queries) AnyActivePrice(ctx context.Context) (bool, error) {
|
|
row := q.db.QueryRowContext(ctx, anyActivePrice)
|
|
var exists bool
|
|
err := row.Scan(&exists)
|
|
return exists, err
|
|
}
|
|
|
|
const clearDefaultPrice = `-- name: ClearDefaultPrice :exec
|
|
UPDATE core.prices
|
|
SET is_default = FALSE
|
|
WHERE product_id = $1 AND is_default
|
|
`
|
|
|
|
// 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).
|
|
func (q *Queries) ClearDefaultPrice(ctx context.Context, productID string) error {
|
|
_, err := q.db.ExecContext(ctx, clearDefaultPrice, productID)
|
|
return err
|
|
}
|
|
|
|
const createPrice = `-- name: CreatePrice :one
|
|
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),
|
|
$6)
|
|
RETURNING price_id, product_id, currency, unit_amount, recurring_interval, trial_period_days, is_active, created_at, updated_at, is_default, usage_type, key
|
|
`
|
|
|
|
type CreatePriceParams struct {
|
|
ProductID string `json:"product_id"`
|
|
Currency string `json:"currency"`
|
|
UnitAmount int32 `json:"unit_amount"`
|
|
RecurringInterval sql.NullString `json:"recurring_interval"`
|
|
TrialPeriodDays sql.NullInt32 `json:"trial_period_days"`
|
|
Key sql.NullString `json:"key"`
|
|
}
|
|
|
|
// 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.
|
|
func (q *Queries) CreatePrice(ctx context.Context, arg CreatePriceParams) (Price, error) {
|
|
row := q.db.QueryRowContext(ctx, createPrice,
|
|
arg.ProductID,
|
|
arg.Currency,
|
|
arg.UnitAmount,
|
|
arg.RecurringInterval,
|
|
arg.TrialPeriodDays,
|
|
arg.Key,
|
|
)
|
|
var i Price
|
|
err := row.Scan(
|
|
&i.PriceID,
|
|
&i.ProductID,
|
|
&i.Currency,
|
|
&i.UnitAmount,
|
|
&i.RecurringInterval,
|
|
&i.TrialPeriodDays,
|
|
&i.IsActive,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.IsDefault,
|
|
&i.UsageType,
|
|
&i.Key,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const deactivatePrice = `-- name: DeactivatePrice :one
|
|
UPDATE core.prices
|
|
SET is_active = FALSE
|
|
WHERE price_id = $1 AND is_default = FALSE
|
|
RETURNING price_id, product_id, currency, unit_amount, recurring_interval, trial_period_days, is_active, created_at, updated_at, is_default, usage_type, key
|
|
`
|
|
|
|
// Handlers must refuse to deactivate the default price (members would lose
|
|
// the purchase path); the is_default guard here backstops that rule.
|
|
func (q *Queries) DeactivatePrice(ctx context.Context, priceID string) (Price, error) {
|
|
row := q.db.QueryRowContext(ctx, deactivatePrice, priceID)
|
|
var i Price
|
|
err := row.Scan(
|
|
&i.PriceID,
|
|
&i.ProductID,
|
|
&i.Currency,
|
|
&i.UnitAmount,
|
|
&i.RecurringInterval,
|
|
&i.TrialPeriodDays,
|
|
&i.IsActive,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.IsDefault,
|
|
&i.UsageType,
|
|
&i.Key,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getDefaultPriceByProduct = `-- name: GetDefaultPriceByProduct :one
|
|
SELECT price_id, product_id, currency, unit_amount, recurring_interval, trial_period_days, is_active, created_at, updated_at, is_default, usage_type, key FROM core.prices
|
|
WHERE product_id = $1 AND is_default = TRUE
|
|
`
|
|
|
|
func (q *Queries) GetDefaultPriceByProduct(ctx context.Context, productID string) (Price, error) {
|
|
row := q.db.QueryRowContext(ctx, getDefaultPriceByProduct, productID)
|
|
var i Price
|
|
err := row.Scan(
|
|
&i.PriceID,
|
|
&i.ProductID,
|
|
&i.Currency,
|
|
&i.UnitAmount,
|
|
&i.RecurringInterval,
|
|
&i.TrialPeriodDays,
|
|
&i.IsActive,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.IsDefault,
|
|
&i.UsageType,
|
|
&i.Key,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getPrice = `-- name: GetPrice :one
|
|
SELECT price_id, product_id, currency, unit_amount, recurring_interval, trial_period_days, is_active, created_at, updated_at, is_default, usage_type, key FROM core.prices
|
|
WHERE price_id = $1
|
|
`
|
|
|
|
func (q *Queries) GetPrice(ctx context.Context, priceID string) (Price, error) {
|
|
row := q.db.QueryRowContext(ctx, getPrice, priceID)
|
|
var i Price
|
|
err := row.Scan(
|
|
&i.PriceID,
|
|
&i.ProductID,
|
|
&i.Currency,
|
|
&i.UnitAmount,
|
|
&i.RecurringInterval,
|
|
&i.TrialPeriodDays,
|
|
&i.IsActive,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.IsDefault,
|
|
&i.UsageType,
|
|
&i.Key,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getPriceByProductAndKey = `-- name: GetPriceByProductAndKey :one
|
|
SELECT price_id, product_id, currency, unit_amount, recurring_interval, trial_period_days, is_active, created_at, updated_at, is_default, usage_type, key FROM core.prices
|
|
WHERE product_id = $1 AND key = $2
|
|
`
|
|
|
|
type GetPriceByProductAndKeyParams struct {
|
|
ProductID string `json:"product_id"`
|
|
Key sql.NullString `json:"key"`
|
|
}
|
|
|
|
// 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.
|
|
func (q *Queries) GetPriceByProductAndKey(ctx context.Context, arg GetPriceByProductAndKeyParams) (Price, error) {
|
|
row := q.db.QueryRowContext(ctx, getPriceByProductAndKey, arg.ProductID, arg.Key)
|
|
var i Price
|
|
err := row.Scan(
|
|
&i.PriceID,
|
|
&i.ProductID,
|
|
&i.Currency,
|
|
&i.UnitAmount,
|
|
&i.RecurringInterval,
|
|
&i.TrialPeriodDays,
|
|
&i.IsActive,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.IsDefault,
|
|
&i.UsageType,
|
|
&i.Key,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const listPricesByProduct = `-- name: ListPricesByProduct :many
|
|
SELECT price_id, product_id, currency, unit_amount, recurring_interval, trial_period_days, is_active, created_at, updated_at, is_default, usage_type, key FROM core.prices
|
|
WHERE product_id = $1 AND is_active = TRUE
|
|
ORDER BY created_at ASC, price_id ASC
|
|
`
|
|
|
|
func (q *Queries) ListPricesByProduct(ctx context.Context, productID string) ([]Price, error) {
|
|
rows, err := q.db.QueryContext(ctx, listPricesByProduct, productID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []Price{}
|
|
for rows.Next() {
|
|
var i Price
|
|
if err := rows.Scan(
|
|
&i.PriceID,
|
|
&i.ProductID,
|
|
&i.Currency,
|
|
&i.UnitAmount,
|
|
&i.RecurringInterval,
|
|
&i.TrialPeriodDays,
|
|
&i.IsActive,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.IsDefault,
|
|
&i.UsageType,
|
|
&i.Key,
|
|
); 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 markDefaultPrice = `-- name: MarkDefaultPrice :one
|
|
UPDATE core.prices
|
|
SET is_default = TRUE
|
|
WHERE price_id = $1 AND product_id = $2 AND is_active = TRUE
|
|
RETURNING price_id, product_id, currency, unit_amount, recurring_interval, trial_period_days, is_active, created_at, updated_at, is_default, usage_type, key
|
|
`
|
|
|
|
type MarkDefaultPriceParams struct {
|
|
PriceID string `json:"price_id"`
|
|
ProductID string `json:"product_id"`
|
|
}
|
|
|
|
// 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.
|
|
func (q *Queries) MarkDefaultPrice(ctx context.Context, arg MarkDefaultPriceParams) (Price, error) {
|
|
row := q.db.QueryRowContext(ctx, markDefaultPrice, arg.PriceID, arg.ProductID)
|
|
var i Price
|
|
err := row.Scan(
|
|
&i.PriceID,
|
|
&i.ProductID,
|
|
&i.Currency,
|
|
&i.UnitAmount,
|
|
&i.RecurringInterval,
|
|
&i.TrialPeriodDays,
|
|
&i.IsActive,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.IsDefault,
|
|
&i.UsageType,
|
|
&i.Key,
|
|
)
|
|
return i, err
|
|
}
|