pgrepo

package
v0.34.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Repo

type Repo struct {
	// contains filtered or unexported fields
}

func New

func New(pool *pgxpool.Pool) *Repo

func (*Repo) CancelOrder added in v0.13.0

func (r *Repo) CancelOrder(ctx context.Context, id string, from domain.OrderStatus, canceledBy string, now time.Time) error

CancelOrder is the cancel-flavored UpdateOrderStatus that also stamps canceled_by (migration 051): 'user' / 'admin' for the human cancel paths, 'expiry' for the abandoned-checkout sweep. canceled_by drives the revival rule — only 'expiry' rows may be revived by a late paid event (ReviveOrderAfterExpiry); NULL (pre-051 rows), 'user' and 'admin' are terminal. ErrStatusConflict when the from-status does not match.

func (*Repo) ChangeWorkspacePlan

func (r *Repo) ChangeWorkspacePlan(ctx context.Context, workspaceID, toPlan, actor, reason string, now time.Time, inTx func(pgx.Tx, string) error) error

func (*Repo) ClearWorkspaceCancelRequested added in v0.17.0

func (r *Repo) ClearWorkspaceCancelRequested(ctx context.Context, workspaceID string) error

func (*Repo) CreateOrder

func (r *Repo) CreateOrder(ctx context.Context, o domain.Order) (domain.Order, error)

func (*Repo) ExpireDueWorkspaces

func (r *Repo) ExpireDueWorkspaces(ctx context.Context, now time.Time, limit int) ([]app.ExpiredWorkspace, error)

func (*Repo) ExpireStaleOrders added in v0.13.0

func (r *Repo) ExpireStaleOrders(ctx context.Context, now time.Time, limit int) ([]app.ExpiredOrder, error)

ExpireStaleOrders flips pending orders whose checkout session expired to canceled (F15 abandoned-checkout sweep), stamping canceled_by = 'expiry' so a late paid event can revive them (migration 051). The status literals 'pending'/'canceled' are same-source with domain.OrderPending / domain.OrderCanceled (an OrderStatus value cannot be interpolated into a SET clause without losing the natural-expression scan style used below). Single UPDATE with a locking subquery — atomic without an explicit tx; SKIP LOCKED keeps concurrent sweepers from fighting over rows. The caller chooses the cutoff (repo semantics stay `expires_at <= $1`); the worker passes now-orderSweepGrace so in-flight webhook retries get a grace window.

func (*Repo) FailOrder

func (r *Repo) FailOrder(ctx context.Context, o domain.Order, e domain.PaymentEvent, emit func(pgx.Tx) error, now time.Time) error

func (*Repo) FindOrderByChannelRef

func (r *Repo) FindOrderByChannelRef(ctx context.Context, channel, channelRef string) (domain.Order, bool, error)

func (*Repo) FindOrderByPaymentIntent

func (r *Repo) FindOrderByPaymentIntent(ctx context.Context, paymentIntentRef string) (domain.Order, bool, error)

func (*Repo) FindWorkspaceBySubscription

func (r *Repo) FindWorkspaceBySubscription(ctx context.Context, subscriptionRef string) (string, string, bool, error)

func (*Repo) GetChannelProduct added in v0.13.0

func (r *Repo) GetChannelProduct(ctx context.Context, channel, planCode string, interval domain.BillingInterval) (domain.ChannelProduct, bool, error)

func (*Repo) GetMeter

func (r *Repo) GetMeter(ctx context.Context, slug string) (app.Meter, bool, error)

func (*Repo) GetOrder

func (r *Repo) GetOrder(ctx context.Context, id string) (domain.Order, bool, error)

func (*Repo) GetPlan

func (r *Repo) GetPlan(ctx context.Context, code string) (domain.Plan, bool, error)

func (*Repo) GetWorkspaceCancelRequested added in v0.17.0

func (r *Repo) GetWorkspaceCancelRequested(ctx context.Context, workspaceID string) (time.Time, bool, error)

func (*Repo) GetWorkspacePlan

func (r *Repo) GetWorkspacePlan(ctx context.Context, workspaceID string) (string, error)

func (*Repo) GetWorkspacePlanExpiry added in v0.17.0

func (r *Repo) GetWorkspacePlanExpiry(ctx context.Context, workspaceID string) (time.Time, bool, error)

GetWorkspacePlanExpiry reads workspace.plan_expires_at (R5): the end of the currently paid period — the renews_at field of the subscription view. NULL (perpetual/manual plan) answers ok=false.

func (*Repo) GetWorkspaceSubscription

func (r *Repo) GetWorkspaceSubscription(ctx context.Context, workspaceID string) (string, bool, error)

func (*Repo) InsertOverageOrder

func (r *Repo) InsertOverageOrder(ctx context.Context, o domain.Order) (domain.Order, bool, error)

func (*Repo) InsertPaymentEvent

func (r *Repo) InsertPaymentEvent(ctx context.Context, e domain.PaymentEvent) (bool, error)

func (*Repo) LatestPaidOrderInterval

func (r *Repo) LatestPaidOrderInterval(ctx context.Context, workspaceID string) (string, bool, error)

func (*Repo) ListAllOrders

func (r *Repo) ListAllOrders(ctx context.Context, f app.AdminOrderFilter, limit, offset int) ([]app.AdminOrderView, int, error)

func (*Repo) ListCancelRequestedWorkspaces added in v0.17.0

func (r *Repo) ListCancelRequestedWorkspaces(ctx context.Context, limit int) ([]app.CancelRequestView, error)

func (*Repo) ListChannelProducts added in v0.13.0

func (r *Repo) ListChannelProducts(ctx context.Context) ([]domain.ChannelProduct, error)

func (*Repo) ListMeteredWorkspaces

func (r *Repo) ListMeteredWorkspaces(ctx context.Context, limit int) ([]app.MeteredWorkspace, error)

func (*Repo) ListMeters

func (r *Repo) ListMeters(ctx context.Context) ([]app.Meter, error)

func (*Repo) ListOrdersByWorkspace

func (r *Repo) ListOrdersByWorkspace(ctx context.Context, workspaceID string, limit int) ([]domain.Order, error)

func (*Repo) ListPaymentEvents

func (r *Repo) ListPaymentEvents(ctx context.Context, f app.PaymentEventFilter, limit, offset int) ([]app.PaymentEventView, int, error)

func (*Repo) ListPlanCodes

func (r *Repo) ListPlanCodes(ctx context.Context) ([]string, error)

func (*Repo) ListPlans

func (r *Repo) ListPlans(ctx context.Context) ([]domain.Plan, error)

func (*Repo) MarkEventError

func (r *Repo) MarkEventError(ctx context.Context, channel, channelEventID, processErr string) error

func (*Repo) MarkEventProcessed

func (r *Repo) MarkEventProcessed(ctx context.Context, channel, channelEventID string) error

func (*Repo) PayOrder

func (r *Repo) PayOrder(ctx context.Context, id string, emit func(pgx.Tx) error, now time.Time) error

func (*Repo) RegisterChannelProductAtomic added in v0.13.0

func (r *Repo) RegisterChannelProductAtomic(ctx context.Context, channel, planCode string, interval domain.BillingInterval,
	register func(ctx context.Context) (string, error), syncedPriceCents int, syncedAt time.Time) (string, bool, error)

RegisterChannelProductAtomic closes the register double-click race (billing fix #12, 2026-10-24): check-then-create over the channel REST is not a DB-only sequence, so the mapping check and upsert are wrapped in one transaction holding pg_advisory_xact_lock keyed on (channel, plan, interval). The channel REST itself cannot join the transaction — the chosen trade-off is to run the register callback INSIDE the lock: concurrent double-clicks serialize (second caller blocks until the first commits, then sees the mapping and returns existed=true instead of creating a duplicate channel product). Residual risk, accepted and documented: the lock is held for the duration of one channel REST call, so a hung channel endpoint stalls other registrations of the SAME mapping only (different mappings hash to different lock keys and proceed in parallel).

func (*Repo) RegisterMeter

func (r *Repo) RegisterMeter(ctx context.Context, m app.Meter) error

func (*Repo) ReviveOrderAfterExpiry added in v0.13.0

func (r *Repo) ReviveOrderAfterExpiry(ctx context.Context, id string, now time.Time) (bool, error)

ReviveOrderAfterExpiry performs the single sanctioned exit from canceled: canceled → paid, and only when canceled_by = 'expiry' (the abandoned-checkout sweep). A payment that actually arrived must beat the sweeper's guess — the money is real even if the webhook was late. canceled_by in ('user','admin') or NULL (pre-051 rows: provenance unknown) leaves the row untouched; the caller then keeps the "ignore paid event on canceled order" behavior. Returns false when no row was revived.

func (*Repo) SetOrderChannelRefAndExpiry added in v0.13.0

func (r *Repo) SetOrderChannelRefAndExpiry(ctx context.Context, id, channelRef string, expiresAt time.Time) error

func (*Repo) SetOrderPaymentIntentRef

func (r *Repo) SetOrderPaymentIntentRef(ctx context.Context, orderID, paymentIntentRef string) error

func (*Repo) SetWorkspaceCancelRequested added in v0.17.0

func (r *Repo) SetWorkspaceCancelRequested(ctx context.Context, workspaceID string, now time.Time) error

func (*Repo) SetWorkspaceSubscription

func (r *Repo) SetWorkspaceSubscription(ctx context.Context, workspaceID, subscriptionRef string, expiresAt, now time.Time) error

func (*Repo) UpdateOrderStatus

func (r *Repo) UpdateOrderStatus(ctx context.Context, id string, from, to domain.OrderStatus, now time.Time) error

func (*Repo) UpdatePlan added in v0.8.0

func (r *Repo) UpdatePlan(ctx context.Context, code, name, limitsJSON string, sortNo int, currency string) (found bool, err error)

UpdatePlan full-replaces the editable columns of a plan (the code in the WHERE clause is the immutable identity). found reports whether a row matched; false means the caller turns it into a not-found error.

func (*Repo) UpsertChannelProduct added in v0.13.0

func (r *Repo) UpsertChannelProduct(ctx context.Context, p domain.ChannelProduct) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL