503 lines
15 KiB
Go
503 lines
15 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.31.1
|
|
// source: support_cases.sql
|
|
|
|
package sqlc
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const countSupportCaseRequestEvents = `-- name: CountSupportCaseRequestEvents :one
|
|
SELECT count(*)::bigint AS event_count, min(requested_at) AS first_in_window
|
|
FROM support_case_request_events
|
|
WHERE owner_id = $1
|
|
AND requested_at > $2::timestamptz
|
|
AND requested_at <= $3::timestamptz
|
|
`
|
|
|
|
type CountSupportCaseRequestEventsParams struct {
|
|
OwnerID pgtype.UUID
|
|
WindowStart pgtype.Timestamptz
|
|
At pgtype.Timestamptz
|
|
}
|
|
|
|
type CountSupportCaseRequestEventsRow struct {
|
|
EventCount int64
|
|
FirstInWindow interface{}
|
|
}
|
|
|
|
// The strict lower boundary means an event exactly one window old no longer
|
|
// counts. The use case derives Retry-After from first_in_window.
|
|
func (q *Queries) CountSupportCaseRequestEvents(ctx context.Context, arg CountSupportCaseRequestEventsParams) (CountSupportCaseRequestEventsRow, error) {
|
|
row := q.db.QueryRow(ctx, countSupportCaseRequestEvents, arg.OwnerID, arg.WindowStart, arg.At)
|
|
var i CountSupportCaseRequestEventsRow
|
|
err := row.Scan(&i.EventCount, &i.FirstInWindow)
|
|
return i, err
|
|
}
|
|
|
|
const deleteSupportCaseCreateOutcome = `-- name: DeleteSupportCaseCreateOutcome :exec
|
|
DELETE FROM support_case_create_outcomes
|
|
WHERE scope_hash = $1
|
|
`
|
|
|
|
// No raw Idempotency-Key or request body reaches these statements. Failure
|
|
// responses pass {} as case_snapshot and the CASE stores SQL NULL instead.
|
|
func (q *Queries) DeleteSupportCaseCreateOutcome(ctx context.Context, scopeHash []byte) error {
|
|
_, err := q.db.Exec(ctx, deleteSupportCaseCreateOutcome, scopeHash)
|
|
return err
|
|
}
|
|
|
|
const insertSupportCase = `-- name: InsertSupportCase :execrows
|
|
INSERT INTO support_cases (
|
|
id, case_number, owner_id, case_type, status, description,
|
|
object_type, object_id, assessment_specialization, assessment_grade,
|
|
complaint_reason, rubric_version, rubric_opening_id, rubric_policy_version_id,
|
|
target_business_days, target_due_at,
|
|
target_answered_at, target_policy_version, target_calendar_version,
|
|
decision_outcome, decision_reason, decision_at, created_at, updated_at
|
|
) VALUES (
|
|
$1, $2, $3, $4,
|
|
$5, $6, $7, $8,
|
|
$9, $10,
|
|
$11, $12, $13,
|
|
$14, $15,
|
|
$16, $17, $18,
|
|
$19, $20, $21,
|
|
$22, $23, $24
|
|
)
|
|
ON CONFLICT DO NOTHING
|
|
`
|
|
|
|
type InsertSupportCaseParams struct {
|
|
ID pgtype.UUID
|
|
CaseNumber string
|
|
OwnerID pgtype.UUID
|
|
CaseType string
|
|
Status string
|
|
Description string
|
|
ObjectType pgtype.Text
|
|
ObjectID pgtype.UUID
|
|
AssessmentSpecialization pgtype.Text
|
|
AssessmentGrade pgtype.Text
|
|
ComplaintReason pgtype.Text
|
|
RubricVersion pgtype.Int4
|
|
RubricOpeningID pgtype.UUID
|
|
RubricPolicyVersionID pgtype.UUID
|
|
TargetBusinessDays int32
|
|
TargetDueAt pgtype.Timestamptz
|
|
TargetAnsweredAt pgtype.Timestamptz
|
|
TargetPolicyVersion int32
|
|
TargetCalendarVersion int32
|
|
DecisionOutcome pgtype.Text
|
|
DecisionReason pgtype.Text
|
|
DecisionAt pgtype.Timestamptz
|
|
CreatedAt pgtype.Timestamptz
|
|
UpdatedAt pgtype.Timestamptz
|
|
}
|
|
|
|
// The initial public history is inserted in the caller's case transaction.
|
|
// Any unique business conflict leaves the transaction usable so the caller can
|
|
// store the immutable 409 outcome under the request's idempotency key.
|
|
func (q *Queries) InsertSupportCase(ctx context.Context, arg InsertSupportCaseParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, insertSupportCase,
|
|
arg.ID,
|
|
arg.CaseNumber,
|
|
arg.OwnerID,
|
|
arg.CaseType,
|
|
arg.Status,
|
|
arg.Description,
|
|
arg.ObjectType,
|
|
arg.ObjectID,
|
|
arg.AssessmentSpecialization,
|
|
arg.AssessmentGrade,
|
|
arg.ComplaintReason,
|
|
arg.RubricVersion,
|
|
arg.RubricOpeningID,
|
|
arg.RubricPolicyVersionID,
|
|
arg.TargetBusinessDays,
|
|
arg.TargetDueAt,
|
|
arg.TargetAnsweredAt,
|
|
arg.TargetPolicyVersion,
|
|
arg.TargetCalendarVersion,
|
|
arg.DecisionOutcome,
|
|
arg.DecisionReason,
|
|
arg.DecisionAt,
|
|
arg.CreatedAt,
|
|
arg.UpdatedAt,
|
|
)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const insertSupportCaseCreateOutcome = `-- name: InsertSupportCaseCreateOutcome :exec
|
|
INSERT INTO support_case_create_outcomes (
|
|
scope_hash, outcome_kind, case_snapshot, target_was_late, created_at
|
|
) VALUES (
|
|
$1, $2,
|
|
CASE WHEN $2::text = 'created' THEN $3::jsonb END,
|
|
CASE WHEN $2::text = 'created' THEN $4::boolean END,
|
|
$5
|
|
)
|
|
`
|
|
|
|
type InsertSupportCaseCreateOutcomeParams struct {
|
|
ScopeHash []byte
|
|
OutcomeKind string
|
|
CaseSnapshot []byte
|
|
TargetWasLate bool
|
|
CreatedAt pgtype.Timestamptz
|
|
}
|
|
|
|
func (q *Queries) InsertSupportCaseCreateOutcome(ctx context.Context, arg InsertSupportCaseCreateOutcomeParams) error {
|
|
_, err := q.db.Exec(ctx, insertSupportCaseCreateOutcome,
|
|
arg.ScopeHash,
|
|
arg.OutcomeKind,
|
|
arg.CaseSnapshot,
|
|
arg.TargetWasLate,
|
|
arg.CreatedAt,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const insertSupportCaseEvent = `-- name: InsertSupportCaseEvent :exec
|
|
INSERT INTO support_case_events (
|
|
case_id, sequence, event_kind, actor_kind, occurred_at, status, message
|
|
) VALUES (
|
|
$1, $2, $3, $4,
|
|
$5, $6, $7
|
|
)
|
|
`
|
|
|
|
type InsertSupportCaseEventParams struct {
|
|
CaseID pgtype.UUID
|
|
Sequence int32
|
|
EventKind string
|
|
ActorKind string
|
|
OccurredAt pgtype.Timestamptz
|
|
Status pgtype.Text
|
|
Message pgtype.Text
|
|
}
|
|
|
|
func (q *Queries) InsertSupportCaseEvent(ctx context.Context, arg InsertSupportCaseEventParams) error {
|
|
_, err := q.db.Exec(ctx, insertSupportCaseEvent,
|
|
arg.CaseID,
|
|
arg.Sequence,
|
|
arg.EventKind,
|
|
arg.ActorKind,
|
|
arg.OccurredAt,
|
|
arg.Status,
|
|
arg.Message,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const insertSupportCaseRequestEvent = `-- name: InsertSupportCaseRequestEvent :exec
|
|
INSERT INTO support_case_request_events (owner_id, requested_at)
|
|
VALUES ($1, $2)
|
|
`
|
|
|
|
type InsertSupportCaseRequestEventParams struct {
|
|
OwnerID pgtype.UUID
|
|
RequestedAt pgtype.Timestamptz
|
|
}
|
|
|
|
func (q *Queries) InsertSupportCaseRequestEvent(ctx context.Context, arg InsertSupportCaseRequestEventParams) error {
|
|
_, err := q.db.Exec(ctx, insertSupportCaseRequestEvent, arg.OwnerID, arg.RequestedAt)
|
|
return err
|
|
}
|
|
|
|
const latestSupportCaseRequestEvent = `-- name: LatestSupportCaseRequestEvent :one
|
|
SELECT requested_at
|
|
FROM support_case_request_events
|
|
WHERE owner_id = $1
|
|
ORDER BY requested_at DESC, id DESC
|
|
LIMIT 1
|
|
`
|
|
|
|
// Read the latest serialized event so a temporarily regressing application
|
|
// clock cannot make the next request fall before an event already counted.
|
|
func (q *Queries) LatestSupportCaseRequestEvent(ctx context.Context, ownerID pgtype.UUID) (pgtype.Timestamptz, error) {
|
|
row := q.db.QueryRow(ctx, latestSupportCaseRequestEvent, ownerID)
|
|
var requested_at pgtype.Timestamptz
|
|
err := row.Scan(&requested_at)
|
|
return requested_at, err
|
|
}
|
|
|
|
const listOwnedSupportCases = `-- name: ListOwnedSupportCases :many
|
|
WITH cursor_input AS MATERIALIZED (
|
|
SELECT
|
|
$3::timestamptz AS after_created_at,
|
|
$4::uuid AS after_id
|
|
)
|
|
SELECT
|
|
id, case_number, case_type, status, object_type, object_id,
|
|
assessment_specialization, assessment_grade, target_business_days,
|
|
target_due_at, target_answered_at, target_policy_version,
|
|
target_calendar_version, decision_outcome, created_at, updated_at
|
|
FROM support_cases
|
|
CROSS JOIN cursor_input
|
|
WHERE owner_id = $1
|
|
AND (
|
|
cursor_input.after_created_at IS NULL
|
|
OR (created_at, id) < (
|
|
cursor_input.after_created_at,
|
|
cursor_input.after_id
|
|
)
|
|
)
|
|
ORDER BY created_at DESC, id DESC
|
|
LIMIT $2
|
|
`
|
|
|
|
type ListOwnedSupportCasesParams struct {
|
|
OwnerID pgtype.UUID
|
|
RowLimit int32
|
|
AfterCreatedAt pgtype.Timestamptz
|
|
AfterID pgtype.UUID
|
|
}
|
|
|
|
type ListOwnedSupportCasesRow struct {
|
|
ID pgtype.UUID
|
|
CaseNumber string
|
|
CaseType string
|
|
Status string
|
|
ObjectType pgtype.Text
|
|
ObjectID pgtype.UUID
|
|
AssessmentSpecialization pgtype.Text
|
|
AssessmentGrade pgtype.Text
|
|
TargetBusinessDays int32
|
|
TargetDueAt pgtype.Timestamptz
|
|
TargetAnsweredAt pgtype.Timestamptz
|
|
TargetPolicyVersion int32
|
|
TargetCalendarVersion int32
|
|
DecisionOutcome pgtype.Text
|
|
CreatedAt pgtype.Timestamptz
|
|
UpdatedAt pgtype.Timestamptz
|
|
}
|
|
|
|
// Owner-scoped newest-first list. It omits descriptions, complaint reason,
|
|
// and event history. The tuple cursor matches the composite sort key.
|
|
func (q *Queries) ListOwnedSupportCases(ctx context.Context, arg ListOwnedSupportCasesParams) ([]ListOwnedSupportCasesRow, error) {
|
|
rows, err := q.db.Query(ctx, listOwnedSupportCases,
|
|
arg.OwnerID,
|
|
arg.RowLimit,
|
|
arg.AfterCreatedAt,
|
|
arg.AfterID,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ListOwnedSupportCasesRow
|
|
for rows.Next() {
|
|
var i ListOwnedSupportCasesRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.CaseNumber,
|
|
&i.CaseType,
|
|
&i.Status,
|
|
&i.ObjectType,
|
|
&i.ObjectID,
|
|
&i.AssessmentSpecialization,
|
|
&i.AssessmentGrade,
|
|
&i.TargetBusinessDays,
|
|
&i.TargetDueAt,
|
|
&i.TargetAnsweredAt,
|
|
&i.TargetPolicyVersion,
|
|
&i.TargetCalendarVersion,
|
|
&i.DecisionOutcome,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const lockSupportCaseRequestBudget = `-- name: LockSupportCaseRequestBudget :exec
|
|
SELECT pg_advisory_xact_lock(
|
|
hashtextextended('lincodex.support.case.requests:' || $1::uuid::text, 0)
|
|
)
|
|
`
|
|
|
|
// Serialize per-owner budget checks even before the first event exists.
|
|
func (q *Queries) LockSupportCaseRequestBudget(ctx context.Context, ownerID pgtype.UUID) error {
|
|
_, err := q.db.Exec(ctx, lockSupportCaseRequestBudget, ownerID)
|
|
return err
|
|
}
|
|
|
|
const nextSupportCaseNumber = `-- name: NextSupportCaseNumber :one
|
|
WITH next_value AS MATERIALIZED (
|
|
SELECT nextval('support_case_number_seq') AS value
|
|
)
|
|
SELECT
|
|
'SC-' || $1::text || '-' ||
|
|
CASE
|
|
WHEN next_value.value < 1000000 THEN lpad(next_value.value::text, 6, '0')
|
|
ELSE next_value.value::text
|
|
END::text AS case_number
|
|
FROM next_value
|
|
`
|
|
|
|
// Case numbers are monotonic and never reused. Sequence gaps on a rolled-back
|
|
// request are expected and do not expose request contents.
|
|
func (q *Queries) NextSupportCaseNumber(ctx context.Context, utcYear string) (interface{}, error) {
|
|
row := q.db.QueryRow(ctx, nextSupportCaseNumber, utcYear)
|
|
var case_number interface{}
|
|
err := row.Scan(&case_number)
|
|
return case_number, err
|
|
}
|
|
|
|
const pruneSupportCaseRequestEvents = `-- name: PruneSupportCaseRequestEvents :execrows
|
|
WITH expired AS MATERIALIZED (
|
|
SELECT source.id
|
|
FROM support_case_request_events AS source
|
|
WHERE source.owner_id = $1
|
|
AND source.requested_at <= $2::timestamptz - interval '24 hours'
|
|
ORDER BY source.requested_at, source.id
|
|
LIMIT 100
|
|
FOR UPDATE SKIP LOCKED
|
|
)
|
|
DELETE FROM support_case_request_events AS event
|
|
USING expired
|
|
WHERE event.id = expired.id
|
|
`
|
|
|
|
type PruneSupportCaseRequestEventsParams struct {
|
|
OwnerID pgtype.UUID
|
|
At pgtype.Timestamptz
|
|
}
|
|
|
|
// Keep up to the maximum supported window, regardless of the current policy.
|
|
// The bounded delete prevents one create from doing unbounded cleanup work.
|
|
func (q *Queries) PruneSupportCaseRequestEvents(ctx context.Context, arg PruneSupportCaseRequestEventsParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, pruneSupportCaseRequestEvents, arg.OwnerID, arg.At)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const readOwnedSupportCase = `-- name: ReadOwnedSupportCase :one
|
|
SELECT
|
|
id, case_number, owner_id, case_type, status, description,
|
|
object_type, object_id, assessment_specialization, assessment_grade,
|
|
complaint_reason, rubric_version, rubric_opening_id, rubric_policy_version_id,
|
|
target_business_days, target_due_at,
|
|
target_answered_at, target_policy_version, target_calendar_version,
|
|
decision_outcome, decision_reason, decision_at, created_at, updated_at
|
|
FROM support_cases
|
|
WHERE id = $1 AND owner_id = $2
|
|
`
|
|
|
|
type ReadOwnedSupportCaseParams struct {
|
|
CaseID pgtype.UUID
|
|
OwnerID pgtype.UUID
|
|
}
|
|
|
|
// Reads a full owner-scoped record. Object values are identifiers only.
|
|
func (q *Queries) ReadOwnedSupportCase(ctx context.Context, arg ReadOwnedSupportCaseParams) (SupportCase, error) {
|
|
row := q.db.QueryRow(ctx, readOwnedSupportCase, arg.CaseID, arg.OwnerID)
|
|
var i SupportCase
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.CaseNumber,
|
|
&i.OwnerID,
|
|
&i.CaseType,
|
|
&i.Status,
|
|
&i.Description,
|
|
&i.ObjectType,
|
|
&i.ObjectID,
|
|
&i.AssessmentSpecialization,
|
|
&i.AssessmentGrade,
|
|
&i.ComplaintReason,
|
|
&i.RubricVersion,
|
|
&i.RubricOpeningID,
|
|
&i.RubricPolicyVersionID,
|
|
&i.TargetBusinessDays,
|
|
&i.TargetDueAt,
|
|
&i.TargetAnsweredAt,
|
|
&i.TargetPolicyVersion,
|
|
&i.TargetCalendarVersion,
|
|
&i.DecisionOutcome,
|
|
&i.DecisionReason,
|
|
&i.DecisionAt,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const readSupportCaseCreateOutcome = `-- name: ReadSupportCaseCreateOutcome :one
|
|
SELECT outcome_kind, coalesce(case_snapshot::text, '') AS case_snapshot, target_was_late
|
|
FROM support_case_create_outcomes
|
|
WHERE scope_hash = $1
|
|
`
|
|
|
|
type ReadSupportCaseCreateOutcomeRow struct {
|
|
OutcomeKind string
|
|
CaseSnapshot interface{}
|
|
TargetWasLate pgtype.Bool
|
|
}
|
|
|
|
func (q *Queries) ReadSupportCaseCreateOutcome(ctx context.Context, scopeHash []byte) (ReadSupportCaseCreateOutcomeRow, error) {
|
|
row := q.db.QueryRow(ctx, readSupportCaseCreateOutcome, scopeHash)
|
|
var i ReadSupportCaseCreateOutcomeRow
|
|
err := row.Scan(&i.OutcomeKind, &i.CaseSnapshot, &i.TargetWasLate)
|
|
return i, err
|
|
}
|
|
|
|
const readSupportCaseEvents = `-- name: ReadSupportCaseEvents :many
|
|
SELECT sequence, event_kind, actor_kind, occurred_at, status, message
|
|
FROM support_case_events
|
|
WHERE case_id = $1
|
|
ORDER BY sequence
|
|
`
|
|
|
|
type ReadSupportCaseEventsRow struct {
|
|
Sequence int32
|
|
EventKind string
|
|
ActorKind string
|
|
OccurredAt pgtype.Timestamptz
|
|
Status pgtype.Text
|
|
Message pgtype.Text
|
|
}
|
|
|
|
// Reads the immutable public history only after the case owner has matched.
|
|
func (q *Queries) ReadSupportCaseEvents(ctx context.Context, caseID pgtype.UUID) ([]ReadSupportCaseEventsRow, error) {
|
|
rows, err := q.db.Query(ctx, readSupportCaseEvents, caseID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ReadSupportCaseEventsRow
|
|
for rows.Next() {
|
|
var i ReadSupportCaseEventsRow
|
|
if err := rows.Scan(
|
|
&i.Sequence,
|
|
&i.EventKind,
|
|
&i.ActorKind,
|
|
&i.OccurredAt,
|
|
&i.Status,
|
|
&i.Message,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|