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.

17.3 KB
Raw Blame

Seller-owned package types

Status: Implemented — design finalized 2026-09-08; all five phases implemented 2026-09-08 (core ownership and quoting, the seller API and permission resource, the onboarding requirement, both panels, docs) Target: Spree 6.0 Depends on: 6.0-b2b-wholesale-shipping.md (Spree::PackageType, the carton chain on variants, the tare and dimensions a parcel quote is built on), 6.0-multi-vendor-marketplace.md (Decision 13: the seller owns "who ships it and how"; seller-owned delivery methods and stock locations are the precedent), 6.0-seller-onboarding-requirements.md (the requirement kinds registry the new kind joins), 6.0-admin-rbac.md (permission catalog; a capability a new audience holds gets its own resource) Author: Damian Legawiec Last updated: 2026-09-08

Summary

Spree::PackageType is the store's packaging vocabulary: the box parcels ship in, the cartons products are packed into, the pallets and containers wholesale orders leave on. It was built store-scoped, and on a marketplace that is the wrong owner for most of it. A seller packs their own goods into their own cartons and posts them in their own boxes, yet today every seller's parcel is quoted with the marketplace's default box, the seller API already accepts a carton reference on a variant that no seller can discover, and the seller panel has no way to record either. This plan gives package types an optional seller owner, following the split the marketplace plan settled for delivery methods: the operator's rows stay a shared vocabulary every seller can read and pack into, a seller's rows are theirs alone, and a package from a seller's warehouse is quoted with that seller's box. A new computed onboarding requirement asks each seller to record the size and weight of the box they ship in, so the fallback to the operator's box is a safety net rather than the norm.

Key Decisions (do not deviate without discussion)

  • Package types are seller-ownable, like delivery methods and stock locations; they are not marketplace vocabulary like profiles and zones (2026-09-08). Decision 13 of the marketplace plan draws the line at the question being answered: "what kind of goods is this" belongs to the marketplace, "who ships it, how, and in what" belongs to the seller. A carton size is a fact about the seller's product and a box is what the seller physically posts, so spree_package_types gains a nullable seller_id. A nil owner is the marketplace's row, exactly as it is for delivery methods and stock locations.
  • The operator's rows are readable by every seller; a seller's rows are readable by nobody else (2026-09-08). A package type is only geometry and a tare, so letting sellers pack into the marketplace's standard cartons costs nothing and gives a new seller a vocabulary from day one. No opt-in flag like available_to_sellers: that flag exists because a delivery method carries a price, and a carton carries none. Writes root in the seller's own rows, so a marketplace row's id is a 404 on every seller action but show.
  • A package's box is its seller's default, falling back to the store's (2026-09-08). Stock::Package reads the box through its stock location's seller, which is how a package's seller is derived everywhere else. A seller with no default box of their own is quoted with the marketplace's, so checkout never loses its tare because a seller skipped a settings page; the onboarding requirement below is what makes that fallback rare.
  • One default per owner, not one per store (2026-09-08). The operator has a default box and each seller has their own. The partial unique index that enforces "one default per store" today is split into two, one for marketplace rows and one for seller rows, because a nullable column inside a unique index constrains nothing for the rows where it is null. The name uniqueness index is split the same way. MySQL cannot express partial indexes and already relies on the model validations for the default flag; it now relies on them for the name as well. Accepted trade-off, recorded under Constraints.
  • Package types leave the settings permission resource for their own, seller-grantable package_types resource (2026-09-08). settings is never seller-grantable, and per the RBAC plan a capability a new audience holds gets its own entry rather than a widened existing one. This is the same move delivery_methods made on 2026-09-01. Unreleased edge, so no bridge: operator roles are data and the seeded admin role is a super-role.
  • A seller must be asked for their box, not blocked without one (2026-09-08). Spree::SellerRequirements::PackageType is a computed kind, satisfied when the seller has a default package type of their own with all three dimensions and a weight recorded. A default row with blanks is exactly the silent under-quoting the model's own guards exist to prevent, so the kind checks the numbers, not the row. It does not count the operator's box: a seller using the marketplace's shared rates still packs and posts their own parcel, and a marketplace where the operator physically ships everything switches the requirement off. Provisioned by default, active and required, for parity with the DeliveryMethod kind; both close the same gap, a wrong quote at checkout.
  • The seller's settings page lists only their own rows (2026-09-08, found in manual QA). The endpoint still answers both owners by default, because a carton picker needs the marketplace's shared vocabulary. But a settings page showing three marketplace boxes beside an outstanding "shipping box" requirement reads as a bug in the requirement rather than as work the seller has not done — which is exactly how it was reported. owner=mine narrows it, sent as a plain query param rather than a Ransack predicate because available_to_seller composes with .or(), so a seller_id filter widens back out to the whole condition instead of narrowing it.
  • A departed seller's rows are not released to the marketplace (2026-09-08). Spree::Seller#package_types is dependent: nil, for the reason delivery methods are: a nil seller_id is the marketplace's row, so nullifying a seller's default box would hand the operator a second default and start quoting the seller's tare on first-party parcels. The seller is paranoid, so the rows keep pointing at a record that still resolves.

Design Details

Schema

One migration, add_seller_to_spree_package_types:

add_reference :spree_package_types, :seller, null: true

remove_index :spree_package_types, name: 'index_spree_package_types_on_store_and_name'
remove_index :spree_package_types, name: 'index_spree_package_types_default_per_store', if_exists: true

# Two of each because a null seller_id inside a unique index constrains
# nothing for the rows where it is null (the marketplace's own).
unless mysql?
  add_index :spree_package_types, [:store_id, :name], unique: true,
            where: 'seller_id IS NULL', name: 'index_spree_package_types_marketplace_name'
  add_index :spree_package_types, [:store_id, :seller_id, :name], unique: true,
            where: 'seller_id IS NOT NULL', name: 'index_spree_package_types_seller_name'
  add_index :spree_package_types, :store_id, unique: true,
            where: '"default" = TRUE AND seller_id IS NULL', name: 'index_spree_package_types_marketplace_default'
  add_index :spree_package_types, [:store_id, :seller_id], unique: true,
            where: '"default" = TRUE AND seller_id IS NOT NULL', name: 'index_spree_package_types_seller_default'
else
  add_index :spree_package_types, [:store_id, :seller_id, :name], unique: true,
            name: 'index_spree_package_types_seller_name'
end

On MySQL the seller-row index still holds (no nulls in it) and the marketplace rows fall back to the validation, which is already the arrangement for the default flag there. No data migration: every existing row has a nil seller_id and is, correctly, the marketplace's.

Spree::PackageType

belongs_to :seller, -> { with_deleted }, class_name: 'Spree::Seller', optional: true,
           inverse_of: :package_types

scope :first_party, -> { where(seller_id: nil) }
scope :for_seller, ->(seller) { where(seller_id: seller.respond_to?(:id) ? seller.id : seller) }
# Everything a seller may pack into: their own rows plus the marketplace's.
scope :available_to_seller, ->(seller) { seller.nil? ? first_party : for_seller(seller).or(first_party) }

validates :name, presence: true,
                 uniqueness: { scope: spree_base_uniqueness_scope + [:store_id, :seller_id] }

The default-flag machinery (demote_other_defaults, default_cannot_be_given_up, ensure_not_default) changes its unit from "same store" to "same store and same seller". Spree::Store#default_package_type becomes where(default: true, seller_id: nil) so the store's answer is the marketplace's box, and Spree::Seller gains:

has_many :package_types, class_name: 'Spree::PackageType', dependent: nil, inverse_of: :seller
has_one :default_package_type, -> { where(default: true) }, class_name: 'Spree::PackageType',
        dependent: nil, inverse_of: :seller

whitelisted_ransackable_attributes gains seller_id so the operator's list can filter to one seller's rows or (blank) the marketplace's, as delivery methods do.

Which box quotes a package

Stock::Package#default_package_type reads, in order: the stock location's seller's default box, then the store's. #tare and #dimensions already read through that one method, so the EasyPost parcel and the internal calculators change nothing.

def default_package_type
  return @default_package_type if defined?(@default_package_type)

  @default_package_type = stock_location&.seller&.default_package_type || owner&.store&.default_package_type
end

Which cartons a variant may reference

Variant#carton_package_type_must_belong_to_store widens to ownership: the row must belong to the product's store and be either the marketplace's or the variant's own seller's (resolved_seller_id). A seller's variant pointing at another seller's carton is :invalid, the same error a foreign store's row gets today.

The products import's prepare_carton resolves a seller's row through store.package_types.available_to_seller(seller), preferring the seller's own row when both owners named a carton the same way. An operator's import is unchanged.

Permission

register_scope(:package_types, group: :settings, audiences: %i[store seller], resources: -> {
  [Spree::PackageType]
})

Spree::PackageType leaves the settings subjects list. The admin controller moves to scoped_resource :package_types; a secret key that held write_settings for this endpoint needs write_package_types instead, which is the unreleased-edge cost accepted above.

API surface

  • Admin: /api/v3/admin/package_types is unchanged in shape. The admin serializer gains seller_id and seller_name (as the delivery method and stock location serializers carry), and the list accepts q[seller_id_eq]. The operator reads and writes every row in the store, marketplace or seller's, and a row the operator creates is the marketplace's; seller_id is not writable there.
  • Seller: /api/v3/seller/package_types CRUD rooted in current_seller.package_types for writes and available_to_seller for the listing, mirroring Seller::DeliveryMethodsController#scope / #resource_scope / #find_resource. The seller serializer carries seller_id so the panel can label a row "Mine" or "Marketplace". The permitted attributes are the admin controller's list; the store and seller come from the request, never the payload.
  • Seller onboarding: the kind appears in GET /seller/onboarding like every other; nothing new on the wire.

Spree::SellerRequirements::PackageType

class PackageType < Spree::SellerRequirement
  def met_by_seller?(seller)
    box = seller.default_package_type
    box.present? && [box.length, box.width, box.height, box.weight].all? { |value| value.to_d.positive? }
  end
end

Registered in the engine's kind list and in DEFAULT_KINDS after DeliveryMethod, so a store provisioned before it existed picks it up on the next idempotent run. i18n under seller_requirement_types.package_type ("Shipping box" / "Tell us the size and weight of the box you ship orders in"). A package_type_requirement factory beside the others. action_url stays nil: the work happens in the panel, and the panel's onboarding page maps the kind to its settings route itself.

Seller panel

  • Settings → Fulfillment → Package types, beside Stock locations and Delivery methods. The operator's page (routes/.../settings/package-types.tsx) is the reference; its table, sheet form and schema move into shared pieces (@spree/dashboard-core for the page and form, @spree/dashboard-ui for anything presentational) that both panels mount, per the seller-panel rule in CLAUDE.md. The seller's table adds an owner column ("Mine" / "Marketplace") and hides the row actions on marketplace rows.
  • Carton picker for free. The shared product form and the bulk variants dialog already read cartons through client.packageTypes?.list(...) and render nothing when the panel's client has no such entry. Registering packageTypes: { list } in packages/seller-dashboard/src/api-client.ts lights the existing picker up for sellers with no product-form change.
  • Onboarding page: panelRoute('package_type') points at the new settings route.
  • @spree/seller-sdk: a packageTypes resource (list, get, create, update, delete) and the generated PackageType type.

Operator dashboard

The package types settings page gains an owner column and a seller filter, matching the delivery methods page. Nothing else changes.

Migration Path

  1. Core: migration, Spree::PackageType ownership (association, scopes, uniqueness, per-owner default), Spree::Seller associations, Spree::Store#default_package_type, Stock::Package#default_package_type, the variant validation, the import's carton lookup. Model and service specs.
  2. API: the package_types permission resource; admin serializer owner fields and Ransack filter; Seller::PackageTypesController and serializer; routes; type generation for both SDKs; the seller SDK resource; controller specs and one integration spec for the OpenAPI examples.
  3. Requirement: the kind, registration, DEFAULT_KINDS, factory, i18n, examples in seller_requirements_spec.rb.
  4. Panels: extract the operator's page into shared pieces; mount it in the seller panel with the owner column; wire packageTypes into the seller panel's client; map the kind on the onboarding page; operator owner column and filter. Translations in every locale of both panels. One seller-panel e2e spec: create a box, mark it default, see the requirement turn complete.
  5. Docs: developer docs for seller package types and the requirement kind; cross-references from the three depending plans; OpenAPI regeneration.

No upgrade task. Existing rows are the marketplace's and stay so.

Constraints on Current Work

  • Anything that needs "the box for this package" reads Stock::Package#default_package_type, never store.default_package_type directly. The store's answer is the marketplace's box; only the package knows whose warehouse it left.
  • A seller-facing lookup of package types goes through available_to_seller(seller); a seller write goes through seller.package_types. store.package_types is the operator's view and hands a seller every other seller's rows.
  • MySQL enforces marketplace-row name uniqueness and the default flag through the model validations only. Do not remove or weaken those validations on the grounds that "the index covers it"; on MySQL it does not.
  • A new seller-facing requirement kind is mapped in the panel's onboarding page (panelRoute) when its work happens on a panel page; action_url is for flows the server knows about.
  • Secret keys reaching /admin/package_types need read_package_types / write_package_types, not settings.

Open Questions

None left. The four the draft raised — whether a seller may pack into the marketplace's cartons, whether the requirement is provisioned by default, what satisfies it, and what quotes a seller's package when they have no box — were settled on 2026-09-08 and are recorded above under Key Decisions.

References

  • docs/plans/6.0-b2b-wholesale-shipping.md — Spree::PackageType, the carton chain, tare and dimensions on Stock::Package.
  • docs/plans/6.0-multi-vendor-marketplace.md — Decision 13 (seller-owned delivery methods; who owns which delivery question), per-seller stock locations.
  • docs/plans/6.0-seller-onboarding-requirements.md — kinds registry, DEFAULT_KINDS, the DeliveryMethod computed kind.
  • docs/plans/6.0-admin-rbac.md — permission catalog, per-audience resources.
  • docs/plans/decisions.md — 2026-09-05, "A CSV names its carton rather than referencing one".
  • Precedents in code: Spree::DeliveryMethod (seller_id, first_party, for_seller, available_to_seller), Spree::StockLocation (owned_by), Spree::Api::V3::Seller::DeliveryMethodsController (shared rows read-only, writes rooted in the seller), Spree::SellerRequirements::DeliveryMethod.