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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Open media, variants and memberships to sellers over the API
Damian Legawiec3160612b7e8b
diff --git a/packages/seller-sdk/src/seller-client.ts b/packages/seller-sdk/src/seller-client.tsindex 792fb57c6f..4321ca6a0e 100644--- a/packages/seller-sdk/src/seller-client.ts+++ b/packages/seller-sdk/src/seller-client.ts@@ -2,6 +2,8 @@ import type { ListParams, PaginatedResponse, RequestFn, RequestOptions } from '@import { transformListParams } from '@spree/sdk-core'import type {AuthTokens,+ Category,+ Collection,Fulfillment,Invitation,Order,@@ -256,6 +258,32 @@ export class SellerClient {this.request<ProductType>('GET', `/product_types/${id}`, options),}+ /**+ * What a seller may file a product under. Read only — the marketplace owns+ * its own taxonomy and merchandising.+ */+ readonly categories = {+ list: (+ params?: ListParams & Record<string, unknown>,+ options?: RequestOptions,+ ): Promise<PaginatedResponse<Category>> =>+ this.request<PaginatedResponse<Category>>('GET', '/categories', {+ ...options,+ params: params ? transformListParams(params) : undefined,+ }),+ }++ readonly collections = {+ list: (+ params?: ListParams & Record<string, unknown>,+ options?: RequestOptions,+ ): Promise<PaginatedResponse<Collection>> =>+ this.request<PaginatedResponse<Collection>>('GET', '/collections', {+ ...options,+ params: params ? transformListParams(params) : undefined,+ }),+ }+/*** What this seller has sold. A basket spanning several sellers is split* into one order each, so these are the seller's own orders rather than a@@ -422,7 +450,63 @@ export interface ProductParams {product_type_id?: stringtags?: string[]category_ids?: string[]+ collection_ids?: string[]metadata?: Record<string, unknown>+ custom_fields?: Array<{ id?: string; custom_field_definition_id?: string; value?: unknown }>+ /**+ * The gallery, as a whole. A file absent from the list is removed, so send+ * every image the product should end up with. `signed_id` attaches a fresh+ * upload; `id` patches one already there.+ */+ media?: Array<{+ id?: string+ signed_id?: string+ alt?: string | null+ position?: number+ media_type?: string+ external_video_url?: string | null+ focal_point_x?: number | null+ focal_point_y?: number | null+ variant_ids?: string[]+ }>+ /**+ * The variants, as a whole — one absent from the list is removed. Tax+ * category and delivery profile are deliberately absent: they are+ * marketplace configuration and the API ignores them here.+ */+ variants?: Array<{+ id?: string+ sku?: string+ barcode?: string | null+ cost_price?: string | number | null+ cost_currency?: string | null+ weight?: string | number | null+ height?: string | number | null+ width?: string | number | null+ depth?: string | number | null+ weight_unit?: string | null+ dimensions_unit?: string | null+ hs_code?: string | null+ country_of_origin?: string | null+ customs_description?: string | null+ track_inventory?: boolean+ preorderable?: boolean+ preorder_ships_at?: string | null+ backorder_limit?: number | null+ position?: number+ options?: Array<{ name: string; value: string }>+ prices?: Array<{+ amount: string | number+ compare_at_amount?: string | number+ currency?: string+ }>+ stock_levels?: Array<{+ id?: string+ stock_location_id?: string+ count_on_hand?: number+ backorderable?: boolean+ }>+ }>/*** Omit `currency` and the price is recorded in the store's, which is the* only place that knows it. A seller has no currency of their own to read.diff --git a/packages/seller-sdk/src/types/generated/Collection.ts b/packages/seller-sdk/src/types/generated/Collection.tsnew file mode 100644index 0000000000..2c4e5405fd--- /dev/null+++ b/packages/seller-sdk/src/types/generated/Collection.ts@@ -0,0 +1,23 @@+// Typelizer digest a7e6fd6063a93a58924c567439e30449+//+// DO NOT MODIFY: This file was automatically generated by Typelizer.+import type {CustomField} from '@/types'++type Collection = {+ id: string;+ name: string;+ permalink: string;+ position: number;+ sort_order: string;+ meta_title: string | null;+ meta_description: string | null;+ meta_keywords: string | null;+ products_count: number;+ description: string;+ description_html: string;+ image_url: string | null;+ square_image_url: string | null;+ custom_fields?: Array<CustomField>;+}++export default Collection;diff --git a/packages/seller-sdk/src/types/generated/CustomField.ts b/packages/seller-sdk/src/types/generated/CustomField.tsindex 7359ba30c0..84bccba7f8 100644--- a/packages/seller-sdk/src/types/generated/CustomField.ts+++ b/packages/seller-sdk/src/types/generated/CustomField.ts@@ -1,4 +1,4 @@-// Typelizer digest 56b576d6ef0d9c0fb9565241c1a2e115+// Typelizer digest c42b787a238a8a2d15d53804af4ee312//// DO NOT MODIFY: This file was automatically generated by Typelizer.@@ -10,6 +10,7 @@ type CustomField = {field_type: 'short_text' | 'long_text' | 'rich_text' | 'number' | 'boolean' | 'json';key: string;value: any;+ custom_field_definition_id: string | null;}export default CustomField;diff --git a/packages/seller-sdk/src/types/generated/Media.ts b/packages/seller-sdk/src/types/generated/Media.tsindex 78a8299cba..b56999acd3 100644--- a/packages/seller-sdk/src/types/generated/Media.ts+++ b/packages/seller-sdk/src/types/generated/Media.ts@@ -1,4 +1,4 @@-// Typelizer digest 982fb1c77e99644481ed30fe4261bc1b+// Typelizer digest 28dcef77e525f4e3fad50c1e72524eb6//// DO NOT MODIFY: This file was automatically generated by Typelizer.@@ -23,6 +23,8 @@ type Media = {large_url: string | null;xlarge_url: string | null;og_image_url: string | null;+ created_at: string;+ updated_at: string;}export default Media;diff --git a/packages/seller-sdk/src/types/generated/Product.ts b/packages/seller-sdk/src/types/generated/Product.tsindex 84eb1aea85..9b64b85053 100644--- a/packages/seller-sdk/src/types/generated/Product.ts+++ b/packages/seller-sdk/src/types/generated/Product.ts@@ -1,4 +1,4 @@-// Typelizer digest f006c41b975a1f3ae9b1bcc2fe0c00d6+// Typelizer digest a29fb25825f9c5c551d1cf86b5459bc7//// DO NOT MODIFY: This file was automatically generated by Typelizer.import type {Price, PriceHistory, Seller, Media, Variant, OptionType, OptionValue, Category, CustomField} from '@/types'@@ -43,6 +43,7 @@ type Product = {updated_at: string;product_type_id: string | null;category_ids: Array<string>;+ collection_ids: Array<string>;}export default Product;diff --git a/packages/seller-sdk/src/types/generated/StockLevel.ts b/packages/seller-sdk/src/types/generated/StockLevel.tsnew file mode 100644index 0000000000..ef252d3e18--- /dev/null+++ b/packages/seller-sdk/src/types/generated/StockLevel.ts@@ -0,0 +1,19 @@+// Typelizer digest e3094f78a9c8fcb333b03ceb6b23e424+//+// DO NOT MODIFY: This file was automatically generated by Typelizer.+import type {StockLocation} from '@/types'++type StockLevel = {+ id: string;+ count_on_hand: number;+ backorderable: boolean;+ stock_location_id: string | null;+ variant_id: string | null;+ created_at: string;+ updated_at: string;+ allocated_count: number;+ available_count: number;+ stock_location?: StockLocation;+}++export default StockLevel;diff --git a/packages/seller-sdk/src/types/generated/Variant.ts b/packages/seller-sdk/src/types/generated/Variant.tsindex cd702373f3..7f6b614b76 100644--- a/packages/seller-sdk/src/types/generated/Variant.ts+++ b/packages/seller-sdk/src/types/generated/Variant.ts@@ -1,7 +1,7 @@-// Typelizer digest 2bd581f6f63d82cc74c3672e101d675a+// Typelizer digest 723177d485cf3f14db8cf9b40ad36baf//// DO NOT MODIFY: This file was automatically generated by Typelizer.-import type {Price, PriceHistory, Seller, Media, OptionValue, CustomField} from '@/types'+import type {Price, PriceHistory, Seller, Media, OptionValue, CustomField, StockLevel} from '@/types'type Variant = {id: string;@@ -29,6 +29,24 @@ type Variant = {option_values: Array<OptionValue>;custom_fields?: Array<CustomField>;prior_price?: PriceHistory | null;+ metadata: Record<string, unknown>;+ position: number;+ cost_price: string | null;+ cost_currency: string | null;+ barcode: string | null;+ weight_unit: string | null;+ dimensions_unit: string | null;+ backorder_limit: number | null;+ hs_code: string | null;+ country_of_origin: string | null;+ customs_description: string | null;+ created_at: string;+ updated_at: string;+ preorderable: boolean;+ available_stock: number | null;+ total_on_hand: number | null;+ prices?: Array<Price>;+ stock_levels?: Array<StockLevel>;}export default Variant;diff --git a/packages/seller-sdk/src/types/generated/index.ts b/packages/seller-sdk/src/types/generated/index.tsindex f132873640..a50094e113 100644--- a/packages/seller-sdk/src/types/generated/index.ts+++ b/packages/seller-sdk/src/types/generated/index.ts@@ -1,17 +1,18 @@-// Typelizer digest 6cc301582be6ae792f11fc45c408196a+// Typelizer digest 52261024024d062f92f5e55ba3fd4ab8//// DO NOT MODIFY: This file was automatically generated by Typelizer.export type { default as Address } from './Address'-export type { default as Category } from './Category'-export type { default as CustomField } from './CustomField'-export type { default as Media } from './Media'export type { default as OptionType } from './OptionType'export type { default as OptionValue } from './OptionValue'export type { default as PriceHistory } from './PriceHistory'export type { default as Price } from './Price'+export type { default as Category } from './Category'+export type { default as Collection } from './Collection'+export type { default as CustomField } from './CustomField'export type { default as FulfillmentItem } from './FulfillmentItem'export type { default as Fulfillment } from './Fulfillment'export type { default as Invitation } from './Invitation'+export type { default as Media } from './Media'export type { default as OrderLineItem } from './OrderLineItem'export type { default as Order } from './Order'export type { default as Product } from './Product'@@ -21,8 +22,9 @@ export type { default as Profile } from './Profile'export type { default as RequirementCustomField } from './RequirementCustomField'export type { default as RequirementStatus } from './RequirementStatus'export type { default as RequirementSubmission } from './RequirementSubmission'+export type { default as StockLevel } from './StockLevel'export type { default as StockLocation } from './StockLocation'export type { default as TaxIdentifier } from './TaxIdentifier'export type { default as TeamMember } from './TeamMember'-export type { default as Seller } from './Seller'export type { default as Variant } from './Variant'+export type { default as Seller } from './Seller'diff --git a/spree/api/app/controllers/spree/api/v3/seller/categories_controller.rb b/spree/api/app/controllers/spree/api/v3/seller/categories_controller.rbnew file mode 100644index 0000000000..90dc7f539d--- /dev/null+++ b/spree/api/app/controllers/spree/api/v3/seller/categories_controller.rb@@ -0,0 +1,30 @@+module Spree+ module Api+ module V3+ module Seller+ # The categories a seller may file a product under.+ #+ # Read only: a seller chooses from what the marketplace offers, they do+ # not extend it (docs/plans/6.0-seller-product-submission.md). Scoped to+ # the store, which is derived from the seller.+ class CategoriesController < Seller::ResourceController+ scoped_resource :products++ protected++ def model_class+ Spree::Category+ end++ def serializer_class+ Spree.api.seller_category_serializer+ end++ def scope+ current_store.categories+ end+ end+ end+ end+ end+enddiff --git a/spree/api/app/controllers/spree/api/v3/seller/collections_controller.rb b/spree/api/app/controllers/spree/api/v3/seller/collections_controller.rbnew file mode 100644index 0000000000..3dff38510c--- /dev/null+++ b/spree/api/app/controllers/spree/api/v3/seller/collections_controller.rb@@ -0,0 +1,30 @@+module Spree+ module Api+ module V3+ module Seller+ # The collections a seller may file a product under.+ #+ # Read only: a seller chooses from what the marketplace offers, they do+ # not extend it (docs/plans/6.0-seller-product-submission.md). Scoped to+ # the store, which is derived from the seller.+ class CollectionsController < Seller::ResourceController+ scoped_resource :products++ protected++ def model_class+ Spree::Collection+ end++ def serializer_class+ Spree.api.seller_collection_serializer+ end++ def scope+ current_store.collections+ end+ end+ end+ end+ end+enddiff --git a/spree/api/app/controllers/spree/api/v3/seller/products_controller.rb b/spree/api/app/controllers/spree/api/v3/seller/products_controller.rbindex 86d2e33e3b..5ca58e7f26 100644--- a/spree/api/app/controllers/spree/api/v3/seller/products_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/seller/products_controller.rb@@ -53,6 +53,24 @@ module SpreeSpree.api.seller_product_serializerend+ # The same workflows the operator's endpoint runs, so a seller's write+ # gets the nested variant and media handling and the validate hooks+ # rather than a bare assign_attributes.+ def create_workflow+ Spree.product_create_workflow+ end++ def update_workflow+ Spree.product_update_workflow+ end++ # The workflow builds its own product, which would leave a seller's+ # listing unowned. Handing it one built through `current_seller.products`+ # keeps the seller on the record, whatever the payload claims.+ def create_workflow_arguments+ super.merge(record: build_resource)+ end+def permitted_paramsattrs = params.permit(:name, :description, :slug,@@ -60,8 +78,28 @@ module Spree:product_type_id,tags: [],category_ids: [],+ collection_ids: [],metadata: {},- prices: [:amount, :compare_at_amount, :currency]+ prices: [:amount, :compare_at_amount, :currency],+ custom_fields: [:id, :custom_field_definition_id, :value, { value: [] }, { value: {} }],+ # Inline media, the same shape the operator sends. `external_url`+ # stays out for the same reason it does there — it would make the+ # server fetch a URL the caller chose.+ media: [*Spree::Media::WRITABLE_ATTRIBUTES, :id, :signed_id, { variant_ids: [] }],+ # A seller's variants. Narrower than the operator's set:+ # `seller_id` would let a payload hand a variant to somebody else,+ # and `tax_category_id`/`delivery_profile_id` are marketplace+ # configuration.+ variants: [+ :id, :sku, :barcode,+ :cost_price, :cost_currency,+ :weight, :height, :width, :depth, :weight_unit, :dimensions_unit,+ :hs_code, :country_of_origin, :customs_description,+ :track_inventory, :preorderable, :preorder_ships_at, :backorder_limit, :position,+ options: [:name, :value],+ prices: [:amount, :compare_at_amount, :currency],+ stock_levels: [:id, :stock_location_id, :count_on_hand, :backorderable]+ ])# A price the client did not name a currency for is priced in thediff --git a/spree/api/app/serializers/spree/api/v3/seller/category_serializer.rb b/spree/api/app/serializers/spree/api/v3/seller/category_serializer.rbnew file mode 100644index 0000000000..617dc644d5--- /dev/null+++ b/spree/api/app/serializers/spree/api/v3/seller/category_serializer.rb@@ -0,0 +1,12 @@+module Spree+ module Api+ module V3+ module Seller+ # A category a seller may file a product under. Read only — the+ # marketplace owns its taxonomy.+ class CategorySerializer < V3::CategorySerializer+ end+ end+ end+ end+enddiff --git a/spree/api/app/serializers/spree/api/v3/seller/collection_serializer.rb b/spree/api/app/serializers/spree/api/v3/seller/collection_serializer.rbnew file mode 100644index 0000000000..3325daadd7--- /dev/null+++ b/spree/api/app/serializers/spree/api/v3/seller/collection_serializer.rb@@ -0,0 +1,12 @@+module Spree+ module Api+ module V3+ module Seller+ # A collection a seller's product can belong to. Read only — creating+ # or configuring one is the operator's.+ class CollectionSerializer < V3::CollectionSerializer+ end+ end+ end+ end+enddiff --git a/spree/api/app/serializers/spree/api/v3/seller/custom_field_serializer.rb b/spree/api/app/serializers/spree/api/v3/seller/custom_field_serializer.rbnew file mode 100644index 0000000000..50f945f526--- /dev/null+++ b/spree/api/app/serializers/spree/api/v3/seller/custom_field_serializer.rb@@ -0,0 +1,16 @@+module Spree+ module Api+ module V3+ module Seller+ # One custom field value a seller filled in.+ class CustomFieldSerializer < V3::CustomFieldSerializer+ typelize custom_field_definition_id: [:string, nullable: true]++ attribute :custom_field_definition_id do |custom_field|+ custom_field.custom_field_definition&.prefixed_id+ end+ end+ end+ end+ end+enddiff --git a/spree/api/app/serializers/spree/api/v3/seller/media_serializer.rb b/spree/api/app/serializers/spree/api/v3/seller/media_serializer.rbnew file mode 100644index 0000000000..2d2afbda03--- /dev/null+++ b/spree/api/app/serializers/spree/api/v3/seller/media_serializer.rb@@ -0,0 +1,26 @@+module Spree+ module Api+ module V3+ module Seller+ # A file on a seller's own product.+ #+ # Declared from the store base rather than the admin serializer, like+ # every serializer on this branch. What a seller needs is the file, how+ # it is cropped, and which variants it belongs to.+ class MediaSerializer < V3::MediaSerializer+ typelize position: :number,+ variant_ids: [:string, multi: true],+ focal_point_x: [:number, nullable: true],+ focal_point_y: [:number, nullable: true]++ attributes :position, :focal_point_x, :focal_point_y,+ created_at: :iso8601, updated_at: :iso8601++ attribute :variant_ids do |media|+ media.variants.map(&:prefixed_id)+ end+ end+ end+ end+ end+enddiff --git a/spree/api/app/serializers/spree/api/v3/seller/product_serializer.rb b/spree/api/app/serializers/spree/api/v3/seller/product_serializer.rbindex b0040ceec6..20f216a3ac 100644--- a/spree/api/app/serializers/spree/api/v3/seller/product_serializer.rb+++ b/spree/api/app/serializers/spree/api/v3/seller/product_serializer.rb@@ -15,6 +15,7 @@ module Spreetypelize status: :string,product_type_id: [:string, nullable: true],category_ids: [:string, multi: true],+ collection_ids: [:string, multi: true],metadata: 'Record<string, unknown>'attributes :status, :metadata, created_at: :iso8601, updated_at: :iso8601@@ -29,6 +30,31 @@ module Spreecategory.prefixed_id if store_id.nil? || category.store_id == store_idendend++ attribute :collection_ids do |product|+ product.collections.map(&:prefixed_id)+ end++ many :variants,+ resource: proc { Spree.api.seller_variant_serializer },+ if: proc { expand?('variants') }++ one :default_variant,+ resource: proc { Spree.api.seller_variant_serializer },+ if: proc { expand?('default_variant') }++ many :gallery_media,+ key: :media,+ resource: proc { Spree.api.seller_media_serializer },+ if: proc { expand?('media') }++ many :option_types,+ resource: proc { Spree.api.option_type_serializer },+ if: proc { expand?('option_types') }++ many :custom_fields,+ resource: proc { Spree.api.seller_custom_field_serializer },+ if: proc { expand?('custom_fields') }endendenddiff --git a/spree/api/app/serializers/spree/api/v3/seller/stock_level_serializer.rb b/spree/api/app/serializers/spree/api/v3/seller/stock_level_serializer.rbnew file mode 100644index 0000000000..86bc765f99--- /dev/null+++ b/spree/api/app/serializers/spree/api/v3/seller/stock_level_serializer.rb@@ -0,0 +1,27 @@+module Spree+ module Api+ module V3+ module Seller+ # What a seller holds of one variant at one of their locations.+ class StockLevelSerializer < V3::StockLevelSerializer+ typelize allocated_count: :number, available_count: :number++ attributes created_at: :iso8601, updated_at: :iso8601++ # Units promised to placed orders but not yet dispatched.+ attribute :allocated_count do |stock_level|+ stock_level.allocated_count.to_i+ end++ attribute :available_count do |stock_level|+ stock_level.available_count.to_i+ end++ one :stock_location,+ resource: proc { Spree.api.seller_stock_location_serializer },+ if: proc { expand?('stock_location') }+ end+ end+ end+ end+enddiff --git a/spree/api/app/serializers/spree/api/v3/seller/variant_serializer.rb b/spree/api/app/serializers/spree/api/v3/seller/variant_serializer.rbnew file mode 100644index 0000000000..6e15e60491--- /dev/null+++ b/spree/api/app/serializers/spree/api/v3/seller/variant_serializer.rb@@ -0,0 +1,69 @@+module Spree+ module Api+ module V3+ module Seller+ # One variant of a seller's own product.+ #+ # Carries what a seller manages: the SKU and barcode, what it cost+ # them, its weight and dimensions, its customs classification and what+ # is on the shelf. No `tax_category_id` or `delivery_profile_id` —+ # those are marketplace configuration and the seller cannot write them,+ # so showing them would invite an edit that silently does nothing.+ class VariantSerializer < V3::VariantSerializer+ typelize position: :number,+ cost_price: [:string, nullable: true],+ cost_currency: [:string, nullable: true],+ barcode: [:string, nullable: true],+ weight_unit: [:string, nullable: true],+ dimensions_unit: [:string, nullable: true],+ available_stock: [:number, nullable: true],+ total_on_hand: [:number, nullable: true],+ preorderable: :boolean,+ preorder_ships_at: [:string, nullable: true],+ backorder_limit: [:number, nullable: true],+ hs_code: [:string, nullable: true],+ country_of_origin: [:string, nullable: true],+ customs_description: [:string, nullable: true],+ metadata: 'Record<string, unknown>'++ attributes :metadata, :position, :cost_price, :cost_currency,+ :barcode, :weight_unit, :dimensions_unit, :backorder_limit,+ :hs_code, :country_of_origin, :customs_description,+ preorder_ships_at: :iso8601,+ created_at: :iso8601, updated_at: :iso8601++ attribute :preorderable, &:preorderable?++ attribute :available_stock do |variant|+ variant.available_stock.to_i if variant.should_track_inventory?+ end++ attribute :total_on_hand do |variant|+ variant.total_on_hand.to_i if variant.should_track_inventory?+ end++ one :primary_media,+ resource: proc { Spree.api.seller_media_serializer },+ if: proc { expand?('primary_media') }++ many :gallery_media,+ key: :media,+ resource: proc { Spree.api.seller_media_serializer },+ if: proc { expand?('media') }++ many :prices,+ resource: proc { Spree.api.price_serializer },+ if: proc { expand?('prices') }++ many :custom_fields,+ resource: proc { Spree.api.seller_custom_field_serializer },+ if: proc { expand?('custom_fields') }++ many :stock_levels,+ resource: proc { Spree.api.seller_stock_level_serializer },+ if: proc { expand?('stock_levels') }+ end+ end+ end+ end+enddiff --git a/spree/api/config/initializers/typelizer.rb b/spree/api/config/initializers/typelizer.rbindex a17dee4d1a..8b831823c3 100644--- a/spree/api/config/initializers/typelizer.rb+++ b/spree/api/config/initializers/typelizer.rb@@ -68,9 +68,13 @@ Rails.application.config.after_initialize do# Store-level names are emitted only when a seller serializer reaches# them — the `store_nested_for_seller` set — so this package does not# grow into a second copy of the whole store SDK.+ #+ # A name the seller branch declares itself must NOT be listed here: both+ # map to the same bare type name, so listing it emits the export twice and+ # the package stops compiling. Category, Media, Variant and CustomField+ # left this set when the seller branch grew its own.store_nested_for_seller = %w[- Address Category CustomField Media OptionType OptionValue- Price PriceHistory Seller Variant+ Address OptionType OptionValue Price PriceHistory Seller].to_setconfig.writer(:seller) do |c|diff --git a/spree/api/config/routes.rb b/spree/api/config/routes.rbindex 02aa1b722b..15242ebea2 100644--- a/spree/api/config/routes.rb+++ b/spree/api/config/routes.rb@@ -746,6 +746,11 @@ Spree::Core::Engine.add_routes do# only — defining either is the operator's.resources :product_types, only: [:index, :show]+ # What a seller may file a product under. Read only for the same+ # reason: the marketplace owns its own taxonomy and merchandising.+ resources :categories, only: [:index]+ resources :collections, only: [:index]+# What this seller has sold. Cancelling is a member action because it# is a workflow with its own arguments, and fulfilling is nested: a# parcel means nothing outside the order it belongs to.diff --git a/spree/api/lib/spree/api/dependencies.rb b/spree/api/lib/spree/api/dependencies.rbindex fbc2da5a66..eb9171ef3d 100644--- a/spree/api/lib/spree/api/dependencies.rb+++ b/spree/api/lib/spree/api/dependencies.rb@@ -166,6 +166,12 @@ module Spreeseller_invitation_serializer: 'Spree::Api::V3::Seller::InvitationSerializer',seller_product_serializer: 'Spree::Api::V3::Seller::ProductSerializer',seller_product_type_serializer: 'Spree::Api::V3::Seller::ProductTypeSerializer',+ seller_variant_serializer: 'Spree::Api::V3::Seller::VariantSerializer',+ seller_media_serializer: 'Spree::Api::V3::Seller::MediaSerializer',+ seller_custom_field_serializer: 'Spree::Api::V3::Seller::CustomFieldSerializer',+ seller_category_serializer: 'Spree::Api::V3::Seller::CategorySerializer',+ seller_collection_serializer: 'Spree::Api::V3::Seller::CollectionSerializer',+ seller_stock_level_serializer: 'Spree::Api::V3::Seller::StockLevelSerializer',seller_product_type_custom_field_definition_serializer: 'Spree::Api::V3::Seller::ProductTypeCustomFieldDefinitionSerializer',seller_order_serializer: 'Spree::Api::V3::Seller::OrderSerializer',seller_order_line_item_serializer: 'Spree::Api::V3::Seller::OrderLineItemSerializer',diff --git a/spree/api/spec/controllers/spree/api/v3/seller/categories_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/seller/categories_controller_spec.rbnew file mode 100644index 0000000000..c50b48f34f--- /dev/null+++ b/spree/api/spec/controllers/spree/api/v3/seller/categories_controller_spec.rb@@ -0,0 +1,46 @@+require 'spec_helper'++# What a seller may file a product under. Read only by design: the marketplace+# owns its taxonomy (docs/plans/6.0-seller-product-submission.md).+RSpec.describe Spree::Api::V3::Seller::CategoriesController, type: :controller do+ render_views++ include_context 'API v3 Seller'++ let(:seller_user) do+ create(:admin_user, :without_admin_role).tap { |user| seller.add_user(user) }+ end+ let(:token) do+ Spree::Api::V3::TestingSupport.generate_jwt(+ seller_user, audience: Spree::Api::V3::JwtAuthentication::JWT_AUDIENCE_SELLER+ )+ end++ let!(:category) { create(:category, store: store, name: 'Lighting') }++ before do+ request.headers['Authorization'] = "Bearer #{token}"+ request.headers['X-Spree-Seller-Id'] = seller.prefixed_id+ end++ it "lists the store's categories" do+ get :index, as: :json++ expect(response).to have_http_status(:ok)+ expect(json_response['data'].pluck('name')).to include('Lighting')+ end++ it "leaves out another store's categories" do+ elsewhere = create(:category, store: create(:store), name: 'Elsewhere')++ get :index, as: :json++ expect(json_response['data'].pluck('id')).not_to include(elsewhere.prefixed_id)+ end++ it 'offers no way to create, edit or delete one' do+ expect(post: '/api/v3/seller/categories').not_to be_routable+ expect(patch: '/api/v3/seller/categories/cat_x').not_to be_routable+ expect(delete: '/api/v3/seller/categories/cat_x').not_to be_routable+ end+enddiff --git a/spree/api/spec/controllers/spree/api/v3/seller/collections_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/seller/collections_controller_spec.rbnew file mode 100644index 0000000000..2ff60469c4--- /dev/null+++ b/spree/api/spec/controllers/spree/api/v3/seller/collections_controller_spec.rb@@ -0,0 +1,46 @@+require 'spec_helper'++# What a seller may file a product under. Read only by design: the marketplace+# owns its taxonomy (docs/plans/6.0-seller-product-submission.md).+RSpec.describe Spree::Api::V3::Seller::CollectionsController, type: :controller do+ render_views++ include_context 'API v3 Seller'++ let(:seller_user) do+ create(:admin_user, :without_admin_role).tap { |user| seller.add_user(user) }+ end+ let(:token) do+ Spree::Api::V3::TestingSupport.generate_jwt(+ seller_user, audience: Spree::Api::V3::JwtAuthentication::JWT_AUDIENCE_SELLER+ )+ end++ let!(:collection) { create(:collection, store: store, name: 'Summer Picks') }++ before do+ request.headers['Authorization'] = "Bearer #{token}"+ request.headers['X-Spree-Seller-Id'] = seller.prefixed_id+ end++ it "lists the store's collections" do+ get :index, as: :json++ expect(response).to have_http_status(:ok)+ expect(json_response['data'].pluck('name')).to include('Summer Picks')+ end++ it "leaves out another store's collections" do+ elsewhere = create(:collection, store: create(:store), name: 'Elsewhere')++ get :index, as: :json++ expect(json_response['data'].pluck('id')).not_to include(elsewhere.prefixed_id)+ end++ it 'offers no way to create, edit or delete one' do+ expect(post: '/api/v3/seller/collections').not_to be_routable+ expect(patch: '/api/v3/seller/collections/col_x').not_to be_routable+ expect(delete: '/api/v3/seller/collections/col_x').not_to be_routable+ end+enddiff --git a/spree/api/spec/controllers/spree/api/v3/seller/products_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/seller/products_controller_spec.rbindex 130d30aeb3..e03f77a229 100644--- a/spree/api/spec/controllers/spree/api/v3/seller/products_controller_spec.rb+++ b/spree/api/spec/controllers/spree/api/v3/seller/products_controller_spec.rb@@ -179,6 +179,73 @@ RSpec.describe Spree::Api::V3::Seller::ProductsController, type: :controller doexpect(json_response['metadata']).to eq('care' => 'wash cold')end+ # The form loads a product with everything it edits in one request, so each+ # of these has to resolve — a missing seller-side serializer raises rather+ # than quietly omitting the key.+ it 'expands the collections the form edits' do+ get :show,+ params: { id: mine.prefixed_id, expand: 'variants,media,custom_fields,default_variant' },+ as: :json++ expect(response).to have_http_status(:ok)+ expect(json_response).to include('variants', 'media', 'custom_fields', 'default_variant', 'collection_ids')+ end++ describe 'writing variants, media and memberships' do+ let(:category) { create(:category, store: store) }++ it 'saves a variant the payload carries' do+ patch :update,+ params: {+ id: mine.prefixed_id,+ variants: [{ sku: 'LAMP-1', barcode: '5060', options: [] }]+ },+ as: :json++ expect(response).to have_http_status(:ok)+ expect(mine.reload.variants.map(&:sku)).to include('LAMP-1')+ end++ it 'attaches an uploaded image' do+ blob = ActiveStorage::Blob.create_and_upload!(+ io: File.open(Spree::Core::Engine.root.join('spec', 'fixtures', 'thinking-cat.jpg')),+ filename: 'lamp.jpg',+ content_type: 'image/jpeg'+ )++ patch :update,+ params: { id: mine.prefixed_id, media: [{ signed_id: blob.signed_id, alt: 'A lamp' }] },+ as: :json++ expect(response).to have_http_status(:ok)+ expect(mine.reload.media.map(&:alt)).to include('A lamp')+ end++ it 'files the product under a category' do+ patch :update, params: { id: mine.prefixed_id, category_ids: [category.prefixed_id] }, as: :json++ expect(response).to have_http_status(:ok)+ expect(mine.reload.categories).to include(category)+ end++ # Marketplace configuration, not a seller's: silently ignored rather than+ # refused, the same way tax and delivery already are on this endpoint.+ # Asserted on the column, since the reader falls back to the store default.+ it 'ignores a tax category on a variant' do+ tax_category = create(:tax_category, store: store)++ patch :update,+ params: {+ id: mine.prefixed_id,+ variants: [{ sku: 'LAMP-2', tax_category_id: tax_category.prefixed_id, options: [] }]+ },+ as: :json++ expect(response).to have_http_status(:ok)+ expect(mine.reload.variants.find_by(sku: 'LAMP-2')[:tax_category_id]).to be_nil+ end+ end+describe 'PATCH #update' doit 'edits their own product' dopatch :update, params: { id: mine.prefixed_id, name: 'Renamed Lamp' }, as: :json