Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

16.0 KB
Raw Blame

Catalog Agreement Rework (Binding Inversion + Audience-Rule Retirement)

Status: Implemented — phases 1–3 2026-08-30 (binding inversion, resolver + leak fix, audience-rule supersession); phase 4 completed 2026-08-31 (inline price_list payload, agreement editor, products-with-prices view, create wizard, removal warnings). Design finalized 2026-08-30 (extracted from the amendment block in 6.0-b2b-companies-and-catalogs.md, whose Implemented status was hiding this unbuilt work) Target: Spree 6.0 — sequence Wave 1: everything catalog-shaped builds on the inverted binding, and the leak fix touches unreleased code Depends on: B2B Companies & Catalogs (6.0-b2b-companies-and-catalogs.md — the shipped catalogs this reworks), PriceList system (released) Author: Damian + Claude Last updated: 2026-08-31

Summary

The implementation half of the one-question-per-entity doctrine (decisions.md 2026-08-30): the Catalog becomes the commercial agreement — the one object answering "what does this buyer see, pay, and buy on" — and the Catalog ↔ PriceList relationship is reshaped accordingly. The binding inverts (spree_price_lists.catalog_id replaces spree_catalogs.price_list_id), which closes a real leak in the shipped unreleased design; the audience-shaped price rules (PriceRules::CustomerGroupRule, UserRule) are retired in favor of catalog assignment; and the dashboard flips to catalog-first, one-go setup. The related term/pricing features build on this: the quantity plan's catalog terms (6.0-b2b-quantity-rules.md) and the automatic ±% pricing card (6.0-price-list-automatic-pricing.md) both land on the catalog page this rework establishes.

Key Decisions (do not deviate without discussion)

  • The binding inverts: a price list is standalone or owned by exactly one catalog (2026-08-30, interactive review). spree_price_lists.catalog_id (nullable) replaces spree_catalogs.price_list_id; generic rule matching simply skips owned lists (WHERE catalog_id IS NULL). This closes the leak: the shipped derived exclusion set (Catalog.bound_price_list_ids) reads active catalogs only, so deactivating a catalog dropped its — typically rule-less — list back into generic matching, pricing the whole store. With the FK, a deactivated catalog's list goes dormant. Sharing one list across catalogs is lost — one catalog serving many audiences through assignments is the replacement.
  • An owned list is never released — removing a catalog's pricing deletes it (revised 2026-08-30 in review, superseding the detach-with-a-warning design above). Releasing was to be an explicit act behind a dashboard warning, but the released state is the dangerous one: an owned list carries no rules, so back in generic matching it prices every shopper — the same leak this plan exists to close, reached deliberately instead of by accident. No warning makes that a reasonable thing to offer, so price_list: null and destroying the catalog both destroy the list (has_one ... dependent: :destroy; the model is paranoid, so the prices stay recoverable). Consequences: an owned list never appears in the price-lists index (it has no rules or audience of its own and is edited on its catalog), and switching from fixed prices to a percentage clears the hand-entered amounts, since an explicit row would otherwise keep winning over the adjustment the card claims is in effect.
  • Audience price rules are grandfathered, not deprecated (revised 2026-08-30 after review — supersedes the same-day "deprecated + data task" decision). PriceRules::CustomerGroupRule and PriceRules::UserRule duplicated catalog assignment with different, silent precedence, so they are hidden from the rule picker and nothing new builds on them — but they keep matching indefinitely, with no removal date, no creation warning, and no data task. Rationale: the conversion only had to exist because the classes were being removed; once they simply keep working, migrating is churn a merchant gains nothing from, and an automatic remap of who-gets-which-price is exactly the class of change that is dangerous to get wrong. This puts them on the same footing as PriceRules::ChannelRule, already grandfathered for the same reason. MarketRule (carries VAT restatement) and VolumeRule are context-shaped and stay first-class and offered.
  • The resolved price and its source ride the catalog's own products endpoint, behind expand (2026-08-31, phase 4 review). The nested membership listing answers ?expand=catalog_price with the amount a buyer on this agreement pays and where it came from — an explicit row on the owned list, the list's ±% adjustment, or the variant's base price. Resolution stays server-side: the dashboard cannot see a derivation that is computed on read and never stored, so inferring the source in React would mean re-implementing the resolver against a shape it cannot observe. expand rather than a bespoke flag, because every other optional payload on this API is asked for the same way.
  • The catalog's price columns sit on the assortment card, not beside it (2026-08-31). Assortment↔pricing divergence is the thing the view exists to expose, so the price and its source are columns on the rows the merchant already curates — a second list of the same products, or a tab, makes the divergence something to go looking for.
  • The transitional price_list_id accessors are removed; the inline price_list payload is the only binding (2026-08-31). They existed to keep the admin API's pre-inversion shape working until phase 4 reshaped the payload, which it now has: the catalog serializer drops price_list_id, and the model drops the pending-assignment machinery that write path needed. Catalogs are unreleased, so no bridge is owed.
  • New catalog is a linear four-step wizard, written once at the end (2026-08-31). Details → Audience → Pricing → Review, Back and Next only, with a single create request on Finish carrying the whole agreement. Nothing is written until then, so abandoning midway leaves nothing behind — the same staged-until-Save model the agreement editor uses. The assortment is deliberately not a step: product membership stages against a saved parent through the nested endpoints, and the merchant lands on the agreement editor to curate it.
  • Because catalogs are unreleased (6.0 edge), the catalogs side rewrites in place — the create-catalogs migration drops its price_list_id reference, no bridge, per the companies-plan precedent. The spree_price_lists side is a released table and gets an ordinary additive migration for catalog_id.
  • The dashboard flips to catalog-first, and setup is one go. The catalog page becomes the agreement editor — audience assignments, assortment, an inline pricing card for its owned list, terms — plus a products-with-prices view showing, per product, assortment membership and the resolved price with its source (explicit / automatic / base), so assortment↔pricing divergence is visible instead of discoverable-by-accident. The New catalog flow takes name, audience, assortment, and a pricing choice (base prices / ±% adjustment / fixed prices); the admin catalog create/update endpoints accept an inline price_list payload for the owned list, so standing up an agreement never requires visiting the price-lists page.

Design Details

  • Associations flip: PriceList belongs_to :catalog (optional) / Catalog has_one :price_list; Catalog.bound_price_list_ids and its Spree::Current memoization are deleted — the resolver's applicable_price_lists filters catalog_id: nil in SQL, and catalog_price_lists reads the association. Only active catalogs still claim their list in the resolution walk (unchanged semantics, now leak-free on the exclusion side).
  • Supersession is a discovery-level flag, not a bridge: superseded? on the rule class surfaces as superseded: true in types discovery (present only when true, so rule families without the concept keep their wire shape), and the dashboard picker filters those kinds out. Existing rules render, edit and match unchanged.
  • No upgrade step: nothing in the 6.0 manifest touches price rules. A store upgrades with its audience rules matching exactly as before, and adopts catalogs for new agreements at whatever pace suits it.
  • Admin API: catalogs create/update accept price_list: {...} (create or update the owned list inline); the price-lists endpoints keep working for standalone lists; price_list: null removes the owned list.
  • Dashboard: catalog page (agreement editor + products-with-prices view + removal warning), New catalog page. No price-list-page banner for owned lists — they are excluded from that index entirely, since a page offering rules and a schedule describes nothing an owned list has.

Migration Path

  1. Schema — rewrite the catalogs migration in place (drop price_list_id); additive catalog_id on spree_price_lists; edge worktrees rebuild the template DB. Done 2026-08-30 (one live list per catalog is enforced on every adapter — a partial unique index on PostgreSQL/SQLite, a unique index over a stored generated column on MySQL/MariaDB per the spree_commission_rates precedent, so the rule never rests on the model validation alone; soft-deleted lists release the slot. The admin API keeps its price_list_id read/write shape through model-level accessors over the inverted association until phase 4 reshapes the payload).
  2. Model + resolver — association flip, SQL-level owned-list exclusion, delete the derived set + memoization; specs pin the leak (a deactivated catalog's rule-less list prices nobody). Done 2026-08-30 (the exclusion lives in PriceList.for_context — the one feed for both Spree::Current.price_lists and the resolver's direct fetch. Detach and catalog destroy destroy the owned list rather than releasing it — dependent: :destroy, superseding the :nullify this step originally shipped with, per the revised Key Decision above: an owned list carries no rules, so released it would price every shopper.)
  3. Rule supersession — types-discovery marking only. Done 2026-08-30 (superseded: true in subclasses_with_preference_schema, present only when true so other type-discovery endpoints keep their wire shape; the dashboard rule picker filters those kinds while existing rules still render, edit and match). Deliberately no warnings, no bridge and no data task — see the revised Key Decision above.
  4. API + dashboard — inline price_list payload, agreement editor, products-with-prices view, wizard, removal warning. Partly done 2026-08-30: the inline payload (Spree::Catalogs::Create/Update delegating to SetPriceList — create, update in place, or remove on an explicit null; a list a catalog owns is named after the catalog and born active, since the catalog's own active flag already gates the agreement) and the agreement editor, where Pricing is its own card (normal prices / ±% / fixed) between Catalog and Audiences. Fixed mode opens the bulk price spreadsheet inline — it is only half-configured until amounts exist. Catalogs gained a description like price lists, shown in the index; the nav puts Catalogs above Price Lists; and the price-lists index lists standalone lists only. Two warnings state consequences before Save: removing the pricing deletes the list, and switching from fixed to ±% discards the entered amounts. The ±% controls landed with 6.0-price-list-automatic-pricing.md. Completed 2026-08-31: the products-with-prices view — the catalog's nested products listing answers ?expand=catalog_price with the amount a buyer on the agreement pays and its source (explicit / automatic / base), resolved server-side by Spree::Catalogs::ResolvePrices and rendered as price and source columns on the assortment rows, so a product the agreement lists but does not price reads as base beside the ones it does; and the create flow, now a linear four-step wizard (Details → Audience → Pricing → Review) writing once on Finish, with the audience editor extracted so the wizard and the agreement editor share one surface. The transitional Catalog#price_list_id accessors were removed with it — the inline payload is the only binding. The Terms card arrived with 6.0-b2b-quantity-rules.md.

Constraints on Current Work

  • No new code reads spree_catalogs.price_list_id or Catalog.bound_price_list_ids — the FK on the list is the one binding.
  • No new features build on PriceRules::CustomerGroupRule/UserRule or ChannelRule (all grandfathered — kept working, hidden from pickers) — audience targeting is catalog assignment; channel pricing is the default catalog.
  • Catalog-page features (terms, pricing card) mount on the agreement editor this plan establishes — no parallel catalog editing surfaces.
  • An owned list's product membership follows the catalog's assortment — Catalog#add_products/#remove_products carry it, and creating an owned list seeds it from what the catalog already holds. Never populate an owned list by another path: the two memberships are separate tables, and a catalog whose list missed the sync shows an empty price spreadsheet while plainly having products.
  • One list, one formula: PriceList#derived_price_from is where an adjustment list's arithmetic lives — the pricing resolver and the catalog's own reading both call it, so the number a merchant is shown and the number a buyer is charged cannot be computed two different ways. Currency rounding goes through Spree::Money::Rounding, never a local exponent lookup.
  • Going live is its own act, never a field on a form (2026-09-01). A catalog is born inactive and reaches buyers through Spree::Catalogs::Activate — mirroring PriceLists::Activate — so what has to happen alongside (sweeping a buyer's cached catalog set, telling the companies it covers) hangs off after_activate instead of being scattered across every caller that flips the flag. active is absent from the catalog write payload the dashboard sends, so a plain Save can never change whether an agreement applies. Activation is refused for a catalog nobody is assigned to, since it would reach no buyer — the one exception being a channel's default catalog, which is reached through the channel rather than an assignment.
  • A catalog's resolved price is read through Spree::Catalogs::ResolvePrices, never re-derived by a client — an adjusted amount is computed on read and never stored, so anything inferring the source from price rows is wrong the moment base prices move. A new surface asking "what does this agreement charge" goes through that service, or the catalog_price expansion over it.
  • The price spreadsheet reflects staged membership, not just saved membership — a products card stages its edits until Save, so a grid opened beside one excludes products staged for removal (variant_product_id_not_in) rather than inviting prices that Save discards. Staged additions have no rows to show until Save, so the surface says so instead.

Open Questions

None — the design questions were resolved in the 2026-08-30 entity review (decisions.md). The mixed-rule-list conversion policy (report, don't guess) is deliberate, not open.

References

  • decisions.md 2026-08-30 — "One question per entity" (doctrine + rejected alternatives).
  • docs/plans/6.0-b2b-companies-and-catalogs.md — the shipped catalogs + the amendment block this plan was extracted from.
  • docs/plans/6.0-b2b-quantity-rules.md (terms on this surface), docs/plans/6.0-price-list-automatic-pricing.md (±% card on this surface).
  • Leak mechanics: spree/core/app/models/spree/catalog.rb (bound_price_list_ids), spree/core/app/models/spree/pricing_provider/internal.rb (applicable_price_lists).