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.

Add Markets (read-only) Admin API

Damian Legawiec697d9cb0cbda
diff --git a/spree/api/app/controllers/spree/api/v3/admin/markets_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/markets_controller.rb
new file mode 100644
index 0000000000..76c85fd211
--- /dev/null
+++ b/spree/api/app/controllers/spree/api/v3/admin/markets_controller.rb
@@ -0,0 +1,39 @@
+module Spree
+ module Api
+ module V3
+ module Admin
+ # Read-only Markets surface for the admin SPA. Markets are
+ # store-scoped (`store_id` column + `acts_as_list scope: :store_id`),
+ # so the base ResourceController's `scope` chain narrows
+ # appropriately when we restrict to `current_store.markets`.
+ #
+ # Why read-only: the admin SPA only needs Markets in order to
+ # render labels for `MarketRule` rows and resolve the picker
+ # dropdown. Full write surface (create/update/delete) lives in
+ # the legacy Rails admin pending the broader Channel/Catalog
+ # rework — see `docs/plans/6.0-channels-catalogs-b2b.md`.
+ class MarketsController < ResourceController
+ scoped_resource :settings
+
+ protected
+
+ def model_class
+ Spree::Market
+ end
+
+ def serializer_class
+ Spree.api.admin_market_serializer
+ end
+
+ def scope
+ current_store.markets.accessible_by(current_ability, :show)
+ end
+
+ def collection_includes
+ [:countries]
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/spree/api/config/routes.rb b/spree/api/config/routes.rb
index b6819c3c6d..2cd2684232 100644
--- a/spree/api/config/routes.rb
+++ b/spree/api/config/routes.rb
@@ -181,21 +181,16 @@ Spree::Core::Engine.add_routes do
# Tax Categories
resources :tax_categories
+ # Markets
+ resources :markets, only: [:index, :show]
+
# Store Credit Categories (read-only — for store credit dropdowns)
resources :store_credit_categories, only: [:index, :show]
- # Stock Locations
+ # Inventory
resources :stock_locations
-
- # Stock Reservations
resources :stock_reservations, only: [:index, :show]
-
- # Stock Items (write surface — list/show/update/destroy; creation
- # happens implicitly when variants meet stock locations).
resources :stock_items, only: [:index, :show, :update, :destroy]
-
- # Stock Transfers (move inventory between locations, or receive
- # from external vendor when source_location_id is omitted).
resources :stock_transfers, only: [:index, :show, :create, :destroy]
# Payment Methods
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/markets_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/markets_controller_spec.rb
new file mode 100644
index 0000000000..cb2fe7a75d
--- /dev/null
+++ b/spree/api/spec/controllers/spree/api/v3/admin/markets_controller_spec.rb
@@ -0,0 +1,39 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Api::V3::Admin::MarketsController, type: :controller do
+ render_views
+
+ include_context 'API v3 Admin authenticated'
+
+ let!(:market) { create(:market, store: store) }
+ let!(:other_store_market) { create(:market, store: create(:store)) }
+
+ before { request.headers.merge!(headers) }
+
+ describe 'GET #index' do
+ it 'returns markets in the current store only' do
+ get :index, as: :json
+
+ expect(response).to have_http_status(:ok)
+ ids = json_response['data'].map { |m| m['id'] }
+ expect(ids).to include(market.prefixed_id)
+ expect(ids).not_to include(other_store_market.prefixed_id)
+ end
+ end
+
+ describe 'GET #show' do
+ it 'returns the market' do
+ get :show, params: { id: market.prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['id']).to eq(market.prefixed_id)
+ expect(json_response['name']).to eq(market.name)
+ end
+
+ it '404s on a market from another store' do
+ get :show, params: { id: other_store_market.prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:not_found)
+ end
+ end
+end
diff --git a/spree/api/spec/integration/spree/api/v3/admin/markets_spec.rb b/spree/api/spec/integration/spree/api/v3/admin/markets_spec.rb
new file mode 100644
index 0000000000..8d720877e7
--- /dev/null
+++ b/spree/api/spec/integration/spree/api/v3/admin/markets_spec.rb
@@ -0,0 +1,84 @@
+# frozen_string_literal: true
+
+require 'swagger_helper'
+
+RSpec.describe 'Admin Markets API', type: :request, swagger_doc: 'api-reference/admin.yaml' do
+ include_context 'API v3 Admin'
+
+ let(:Authorization) { "Bearer #{admin_jwt_token}" }
+ let!(:market) { create(:market, store: store, name: 'EU') }
+
+ path '/api/v3/admin/markets' do
+ get 'List markets' do
+ tags 'Pricing'
+ produces 'application/json'
+ security [api_key: [], bearer_auth: []]
+ description <<~DESC
+ Returns the markets configured for the current store. Read-only:
+ write surface lives in the legacy Rails admin pending the
+ Channel / Catalog rework (see `docs/plans/6.0-channels-catalogs-b2b.md`).
+ DESC
+ admin_scope :read, :settings
+
+ admin_sdk_example 'markets/list'
+
+ parameter name: 'x-spree-api-key', in: :header, type: :string, required: true
+ parameter name: :Authorization, in: :header, type: :string, required: true
+ parameter name: :page, in: :query, type: :integer, required: false
+ parameter name: :limit, in: :query, type: :integer, required: false
+ parameter name: :'q[name_cont]', in: :query, type: :string, required: false
+ parameter name: :sort, in: :query, type: :string, required: false
+ parameter name: :expand, in: :query, type: :string, required: false,
+ description: 'Comma-separated associations to embed. Supported: `countries`.'
+
+ response '200', 'markets found' do
+ let(:'x-spree-api-key') { secret_api_key.plaintext_token }
+
+ schema SwaggerSchemaHelpers.paginated('Market')
+
+ run_test! do |response|
+ data = JSON.parse(response.body)
+ expect(data['data'].pluck('id')).to include(market.prefixed_id)
+ end
+ end
+ end
+ end
+
+ path '/api/v3/admin/markets/{id}' do
+ parameter name: :id, in: :path, type: :string, required: true
+
+ get 'Get a market' do
+ tags 'Pricing'
+ produces 'application/json'
+ security [api_key: [], bearer_auth: []]
+ admin_scope :read, :settings
+
+ admin_sdk_example 'markets/get'
+
+ parameter name: 'x-spree-api-key', in: :header, type: :string, required: true
+ parameter name: :Authorization, in: :header, type: :string, required: true
+ parameter name: :expand, in: :query, type: :string, required: false
+
+ response '200', 'market found' do
+ let(:'x-spree-api-key') { secret_api_key.plaintext_token }
+ let(:id) { market.prefixed_id }
+
+ schema '$ref' => '#/components/schemas/Market'
+
+ run_test! do |response|
+ data = JSON.parse(response.body)
+ expect(data['id']).to eq(market.prefixed_id)
+ end
+ end
+
+ response '404', 'market not found' do
+ let(:'x-spree-api-key') { secret_api_key.plaintext_token }
+ let(:id) { 'market_nonexistent' }
+
+ schema '$ref' => '#/components/schemas/ErrorResponse'
+
+ run_test!
+ end
+ end
+ end
+end
diff --git a/spree/core/app/models/spree/permission_sets/configuration_management.rb b/spree/core/app/models/spree/permission_sets/configuration_management.rb
index e3411fce5a..67a7998462 100644
--- a/spree/core/app/models/spree/permission_sets/configuration_management.rb
+++ b/spree/core/app/models/spree/permission_sets/configuration_management.rb
@@ -23,6 +23,11 @@ module Spree
can :manage, Spree::Zone
can :manage, Spree::ZoneMember
+ # Markets — Channel / Market is the long-term replacement for
+ # Zone (see docs/plans/6.0-tax-provider.md), but both coexist
+ # during the migration and need admin read/write either way.
+ can :manage, Spree::Market
+
# Tax configuration
can :manage, Spree::TaxCategory
can :manage, Spree::TaxRate