catalog

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: MIT Imports: 7 Imported by: 2 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package catalog is the official commerce catalog module: native pricing, variant matrices, stock, and SKUs for a configured product post type. Products are content items; this module owns the commerce structure keyed to them. Prices are integer minor units — never floats.

Index

Constants

View Source
const (
	MaxVariants = 250

	MaxSKUTrackRunes = 100

	CapabilityManage = capability.Capability("catalog.manage")
)

MaxVariants caps the variant matrix size: the product of the option-axis sizes must not exceed this, so a runaway cross-product cannot flood the table or the admin editor. MaxSKUTrackRunes bounds SKUs (product and variant). CapabilityManage gates catalog admin mutations; it is declared module-local (a plain kernel capability string) so the kernel vocabulary stays frozen and the module contributes it through RoleProvider.

Variables

View Source
var (
	// ErrProductNotFound is returned when a content item carries no catalog
	// product data.
	ErrProductNotFound = errCatalog("product not found")
	// ErrInvalidPrice is returned when a price is negative.
	ErrInvalidPrice = errCatalog("price must be zero or greater")
	// ErrInvalidSalePrice is returned when a sale price is negative or
	// exceeds the regular price.
	ErrInvalidSalePrice = errCatalog("sale price must be zero or greater and must not exceed the price")
	// ErrInvalidSKU is returned when a SKU exceeds the length bound.
	ErrInvalidSKU = errCatalog("SKU must be at most 100 characters")
	// ErrInvalidOptions is returned when the variant axes are malformed:
	// blank axis names, no axes, or axes without values.
	ErrInvalidOptions = errCatalog("variant options must define at least one axis with non-blank values")
	// ErrInvalidVariant is returned when a variant's options do not match the
	// product's variant axes (unknown axis or value not in the axis list).
	ErrInvalidVariant = errCatalog("variant options must match the product's variant axes")
	// ErrVariantSKUDuplicate is returned when two variants of one product
	// share a non-empty SKU.
	ErrVariantSKUDuplicate = errCatalog("variant SKUs must be unique within a product")
	// ErrInvalidStock is returned when a stock value is negative.
	ErrInvalidStock = errCatalog("stock must be zero or greater")
	// ErrInsufficientStock is returned when an adjustment would drive a
	// tracked stock bucket below zero.
	ErrInsufficientStock = errCatalog("insufficient stock")
	// ErrTooManyVariants is returned when the variant matrix exceeds
	// MaxVariants.
	ErrTooManyVariants = errCatalog("too many variants")
	// ErrInvalidImageURL is returned when a variant image URL is neither an
	// http(s) URL nor a root-relative media path. Empty stays valid (no
	// image), mirroring the gallery rule.
	ErrInvalidImageURL = errCatalog("variant imageUrl must be an http(s) URL or a root-relative path")
)

Sentinels surfaced by the service and mapped to HTTP responses by the handlers.

Functions

func ValidateProductInput

func ValidateProductInput(input UpsertProductInput) error

ValidateProductInput checks one upsert: prices, SKU bounds, stock, the variant-axes shape, and the whole variant matrix (axis match, duplicate SKUs, size cap).

Types

type Mailer

type Mailer interface {
	// SendLowStockAlert reports one tracked bucket crossing its
	// threshold downward, carrying the product, the moved bucket, and
	// the new stock for the notice.
	SendLowStockAlert(ctx context.Context, product *Product, variantID *int64, stock int) error
}

Mailer delivers the low-stock notice. Delivery is best-effort by contract: the stock write is authoritative, a failed send never fails an adjustment, and the adapter logs its own failures.

type Product

type Product struct {
	ContentID         int
	Price             int64 // minor units
	SalePrice         *int64
	SKU               string
	TrackInventory    bool
	Stock             int
	WeightGrams       *int
	LowStockThreshold *int
	Options           map[string][]string // variant axes: name → allowed values
	Variants          []Variant
	UpdatedAt         time.Time
}

Product is the commerce data for one content item of the configured product post type.

func (*Product) EffectivePrice

func (p *Product) EffectivePrice(v *Variant) int64

EffectivePrice resolves the sellable price for a variant: the variant's price override, else the product's sale price, else the product price. A nil variant resolves the product-level effective price.

func (*Product) InStock

func (p *Product) InStock() bool

InStock reports whether the product can be sold: inventory-untracked products are always in stock; variant products need any variant with positive stock; variant-less tracked products need positive stock.

func (*Product) LowStock

func (p *Product) LowStock() bool

LowStock reports whether tracked inventory has reached the configured threshold. Untracked products and products without a threshold are never low-stock. Variant products sum the variant buckets (and must be in stock); variant-less products use the primary stock.

func (*Product) VariantInStock

func (p *Product) VariantInStock(v *Variant) bool

VariantInStock reports sellability for one variant: the variant's own stock when the product tracks inventory, otherwise always in stock.

type ProductStore

type ProductStore interface {
	// GetProduct returns the product with its variant matrix, or
	// ErrProductNotFound when the content item carries no catalog data.
	GetProduct(ctx context.Context, contentID int) (*Product, error)
	// UpsertProduct writes the product row (insert or update) and replaces
	// the variant matrix wholesale, atomically.
	UpsertProduct(ctx context.Context, contentID int, product *Product) error
	// AdjustStock applies the signed delta to one tracked stock bucket —
	// the product when variantID is nil, else that variant — and reports
	// whether the row changed. Untracked and vanished buckets report
	// false: the caller classifies from its own read.
	AdjustStock(ctx context.Context, contentID int, variantID *int64, delta int) (bool, error)
}

ProductStore is the persistence seam the service is built over. The single-source store satisfies it; tests use mocks.

type Service

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

Service is the catalog domain service: commerce data for products of the configured post type.

func NewService

func NewService(store ProductStore, mail Mailer) *Service

NewService builds the service over the store and the mail seam.

func (*Service) AdjustStock

func (s *Service) AdjustStock(ctx context.Context, contentID int, variantID *int64, delta int) error

AdjustStock moves one tracked stock bucket by the signed delta — the payment-time inventory bridge: fulfillment decrements here, refunds restock here. Untracked products never move (and never fail), a zero delta is a no-op, and a move below zero is refused with ErrInsufficientStock. Unknown products and unknown variants read ErrProductNotFound.

func (*Service) GetProduct

func (s *Service) GetProduct(ctx context.Context, contentID int) (*Product, error)

GetProduct returns the commerce data for one content item, including the variant matrix. ErrProductNotFound when the item has none.

func (*Service) UpsertProduct

func (s *Service) UpsertProduct(ctx context.Context, contentID int, input UpsertProductInput) (*Product, error)

UpsertProduct validates and writes the commerce data for one content item, replacing the variant matrix. It returns the stored product (re-read, so variant IDs and timestamps are populated).

type UpsertProductInput

type UpsertProductInput struct {
	Price             int64
	SalePrice         *int64
	SKU               string
	TrackInventory    bool
	Stock             int
	WeightGrams       *int
	LowStockThreshold *int
	Options           map[string][]string
	Variants          []VariantInput
}

UpsertProductInput carries the admin-editable commerce data for one product. The variant matrix is replaced wholesale on every upsert.

type Variant

type Variant struct {
	ID               int64
	ProductContentID int
	Options          map[string]string
	SKU              string
	Price            *int64 // nil inherits the product price
	Stock            int
	WeightGrams      *int
	ImageURL         string
}

Variant is one member of a product's variant matrix: a concrete option combination with its own SKU, optional price override, stock, weight, and image.

type VariantInput

type VariantInput struct {
	Options     map[string]string
	SKU         string
	Price       *int64
	Stock       int
	WeightGrams *int
	ImageURL    string
}

VariantInput is one matrix entry in an upsert.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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