// Code generated by sqlc. DO NOT EDIT. // versions: // sqlc v1.29.0 // source: subscriptions.sql package billing import ( "context" "database/sql" "time" "github.com/lib/pq" ) const countLiveSubscriptionsByStatus = `-- name: CountLiveSubscriptionsByStatus :one SELECT COUNT(*) FILTER (WHERE status = 'active') AS active_count, COUNT(*) FILTER (WHERE status = 'trialing') AS trialing_count FROM core.subscriptions ` type CountLiveSubscriptionsByStatusRow struct { ActiveCount int64 `json:"active_count"` TrialingCount int64 `json:"trialing_count"` } // Both halves of the operator overview's Monthly recurring caption in one // read: 'active' and 'trialing' are the two statuses that mean the // subscription is presently owed service (the pair operator_billing.go // treats as live), but only active ones contribute money, so the caption // needs them separately. func (q *Queries) CountLiveSubscriptionsByStatus(ctx context.Context) (CountLiveSubscriptionsByStatusRow, error) { row := q.db.QueryRowContext(ctx, countLiveSubscriptionsByStatus) var i CountLiveSubscriptionsByStatusRow err := row.Scan(&i.ActiveCount, &i.TrialingCount) return i, err } const createSubscription = `-- name: CreateSubscription :one INSERT INTO core.subscriptions (billing_account_id, status, current_period_start, current_period_end) VALUES ($1, $2, $3, $4) RETURNING subscription_id, billing_account_id, status, current_period_start, current_period_end, cancel_at_period_end, canceled_at, ended_at, created_at, updated_at, commitment_end, commitment_renewal, early_termination_policy ` type CreateSubscriptionParams struct { BillingAccountID string `json:"billing_account_id"` Status string `json:"status"` CurrentPeriodStart sql.NullTime `json:"current_period_start"` CurrentPeriodEnd sql.NullTime `json:"current_period_end"` } func (q *Queries) CreateSubscription(ctx context.Context, arg CreateSubscriptionParams) (Subscription, error) { row := q.db.QueryRowContext(ctx, createSubscription, arg.BillingAccountID, arg.Status, arg.CurrentPeriodStart, arg.CurrentPeriodEnd, ) var i Subscription err := row.Scan( &i.SubscriptionID, &i.BillingAccountID, &i.Status, &i.CurrentPeriodStart, &i.CurrentPeriodEnd, &i.CancelAtPeriodEnd, &i.CanceledAt, &i.EndedAt, &i.CreatedAt, &i.UpdatedAt, &i.CommitmentEnd, &i.CommitmentRenewal, &i.EarlyTerminationPolicy, ) return i, err } const getSubscriptionByID = `-- name: GetSubscriptionByID :one SELECT subscription_id, billing_account_id, status, current_period_start, current_period_end, cancel_at_period_end, canceled_at, ended_at, created_at, updated_at, commitment_end, commitment_renewal, early_termination_policy FROM core.subscriptions WHERE subscription_id = $1 ` func (q *Queries) GetSubscriptionByID(ctx context.Context, subscriptionID string) (Subscription, error) { row := q.db.QueryRowContext(ctx, getSubscriptionByID, subscriptionID) var i Subscription err := row.Scan( &i.SubscriptionID, &i.BillingAccountID, &i.Status, &i.CurrentPeriodStart, &i.CurrentPeriodEnd, &i.CancelAtPeriodEnd, &i.CanceledAt, &i.EndedAt, &i.CreatedAt, &i.UpdatedAt, &i.CommitmentEnd, &i.CommitmentRenewal, &i.EarlyTerminationPolicy, ) return i, err } const getSubscriptionsByBillingAccountID = `-- name: GetSubscriptionsByBillingAccountID :many SELECT subscription_id, billing_account_id, status, current_period_start, current_period_end, cancel_at_period_end, canceled_at, ended_at, created_at, updated_at, commitment_end, commitment_renewal, early_termination_policy FROM core.subscriptions WHERE billing_account_id = $1 ORDER BY created_at DESC ` func (q *Queries) GetSubscriptionsByBillingAccountID(ctx context.Context, billingAccountID string) ([]Subscription, error) { rows, err := q.db.QueryContext(ctx, getSubscriptionsByBillingAccountID, billingAccountID) if err != nil { return nil, err } defer rows.Close() items := []Subscription{} for rows.Next() { var i Subscription if err := rows.Scan( &i.SubscriptionID, &i.BillingAccountID, &i.Status, &i.CurrentPeriodStart, &i.CurrentPeriodEnd, &i.CancelAtPeriodEnd, &i.CanceledAt, &i.EndedAt, &i.CreatedAt, &i.UpdatedAt, &i.CommitmentEnd, &i.CommitmentRenewal, &i.EarlyTerminationPolicy, ); 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 listSubscriptionsPage = `-- name: ListSubscriptionsPage :many SELECT s.subscription_id, s.billing_account_id, ba.org_id, s.status, s.current_period_start, s.current_period_end, s.cancel_at_period_end, s.canceled_at, s.ended_at, s.created_at, s.updated_at, ba.name as billing_account_name, count(*) OVER() AS total_count FROM core.subscriptions s JOIN core.accounts ba ON s.billing_account_id = ba.billing_account_id WHERE ($1::text IS NULL OR ba.name ILIKE '%' || $1::text || '%' OR ba.org_id = ANY($2::uuid[])) AND ($3::text IS NULL OR s.status = $3::text) ORDER BY s.created_at DESC LIMIT $5::int OFFSET $4::int ` type ListSubscriptionsPageParams struct { Q sql.NullString `json:"q"` OrgIds []string `json:"org_ids"` Status sql.NullString `json:"status"` PageOffset int32 `json:"page_offset"` PageLimit int32 `json:"page_limit"` } type ListSubscriptionsPageRow struct { SubscriptionID string `json:"subscription_id"` BillingAccountID string `json:"billing_account_id"` OrgID string `json:"org_id"` Status string `json:"status"` CurrentPeriodStart sql.NullTime `json:"current_period_start"` CurrentPeriodEnd sql.NullTime `json:"current_period_end"` CancelAtPeriodEnd bool `json:"cancel_at_period_end"` CanceledAt sql.NullTime `json:"canceled_at"` EndedAt sql.NullTime `json:"ended_at"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` BillingAccountName string `json:"billing_account_name"` TotalCount int64 `json:"total_count"` } // Operator subscriptions view, paged/searched/filtered (operator-list-scale // UX-4; operator-billing-views D8). Carries ba.org_id so the handler can // resolve and link the organization without a cross-module join (see // billing_accounts.sql's ListBillingAccountsPage comment for why). // // sqlc.narg(q): NULL matches every row; set, a case-insensitive substring // match against the billing account's name (the view's leading identifier // pre-reorder) or its organization via sqlc.narg(org_ids), pre-resolved in // Go (operator_billing.go: matchingOrgIDs). // sqlc.narg(status): NULL matches every status; set, an exact match // against the subscription's CHECK-constrained status vocabulary // (00010_schema_hardening.sql: chk_subscriptions_status_valid -- // incomplete, incomplete_expired, trialing, active, past_due, canceled, // unpaid, paused). // count(*) OVER() carries the true total for the filtered set (design D2). func (q *Queries) ListSubscriptionsPage(ctx context.Context, arg ListSubscriptionsPageParams) ([]ListSubscriptionsPageRow, error) { rows, err := q.db.QueryContext(ctx, listSubscriptionsPage, arg.Q, pq.Array(arg.OrgIds), arg.Status, arg.PageOffset, arg.PageLimit, ) if err != nil { return nil, err } defer rows.Close() items := []ListSubscriptionsPageRow{} for rows.Next() { var i ListSubscriptionsPageRow if err := rows.Scan( &i.SubscriptionID, &i.BillingAccountID, &i.OrgID, &i.Status, &i.CurrentPeriodStart, &i.CurrentPeriodEnd, &i.CancelAtPeriodEnd, &i.CanceledAt, &i.EndedAt, &i.CreatedAt, &i.UpdatedAt, &i.BillingAccountName, &i.TotalCount, ); 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 sumMonthlyRecurringByCurrency = `-- name: SumMonthlyRecurringByCurrency :many SELECT p.currency, SUM( CASE p.recurring_interval WHEN 'month' THEN p.unit_amount::bigint * si.quantity WHEN 'year' THEN (p.unit_amount::bigint * si.quantity) / 12 ELSE 0 END )::bigint AS monthly_cents FROM core.subscriptions s JOIN core.subscription_items si ON si.subscription_id = s.subscription_id JOIN core.prices p ON p.price_id = si.price_id WHERE s.status = 'active' AND p.recurring_interval IS NOT NULL GROUP BY p.currency ORDER BY monthly_cents DESC ` type SumMonthlyRecurringByCurrencyRow struct { Currency string `json:"currency"` MonthlyCents int64 `json:"monthly_cents"` } // The Monthly recurring headline, per currency — the caller displays the // largest bucket and acknowledges the rest (cross-currency conversion is // explicitly out of scope: issues.md). Monthly-normalized: month prices at // face value, year at one-twelfth, each times item quantity. Active only — // trialing subscriptions pay nothing today and counting their money would // overstate. func (q *Queries) SumMonthlyRecurringByCurrency(ctx context.Context) ([]SumMonthlyRecurringByCurrencyRow, error) { rows, err := q.db.QueryContext(ctx, sumMonthlyRecurringByCurrency) if err != nil { return nil, err } defer rows.Close() items := []SumMonthlyRecurringByCurrencyRow{} for rows.Next() { var i SumMonthlyRecurringByCurrencyRow if err := rows.Scan(&i.Currency, &i.MonthlyCents); 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 updateSubscriptionStatus = `-- name: UpdateSubscriptionStatus :one UPDATE core.subscriptions SET status = $2, current_period_start = $3, current_period_end = $4, cancel_at_period_end = $5, canceled_at = $6, ended_at = $7 WHERE subscription_id = $1 RETURNING subscription_id, billing_account_id, status, current_period_start, current_period_end, cancel_at_period_end, canceled_at, ended_at, created_at, updated_at, commitment_end, commitment_renewal, early_termination_policy ` type UpdateSubscriptionStatusParams struct { SubscriptionID string `json:"subscription_id"` Status string `json:"status"` CurrentPeriodStart sql.NullTime `json:"current_period_start"` CurrentPeriodEnd sql.NullTime `json:"current_period_end"` CancelAtPeriodEnd bool `json:"cancel_at_period_end"` CanceledAt sql.NullTime `json:"canceled_at"` EndedAt sql.NullTime `json:"ended_at"` } func (q *Queries) UpdateSubscriptionStatus(ctx context.Context, arg UpdateSubscriptionStatusParams) (Subscription, error) { row := q.db.QueryRowContext(ctx, updateSubscriptionStatus, arg.SubscriptionID, arg.Status, arg.CurrentPeriodStart, arg.CurrentPeriodEnd, arg.CancelAtPeriodEnd, arg.CanceledAt, arg.EndedAt, ) var i Subscription err := row.Scan( &i.SubscriptionID, &i.BillingAccountID, &i.Status, &i.CurrentPeriodStart, &i.CurrentPeriodEnd, &i.CancelAtPeriodEnd, &i.CanceledAt, &i.EndedAt, &i.CreatedAt, &i.UpdatedAt, &i.CommitmentEnd, &i.CommitmentRenewal, &i.EarlyTerminationPolicy, ) return i, err }