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
Added Classifications Platform API endpoints
Damian Legawiecb26ecbbb4631
diff --git a/api/app/controllers/spree/api/v2/platform/classifications_controller.rb b/api/app/controllers/spree/api/v2/platform/classifications_controller.rbnew file mode 100644index 0000000000..b92b5a23b2--- /dev/null+++ b/api/app/controllers/spree/api/v2/platform/classifications_controller.rb@@ -0,0 +1,43 @@+module Spree+ module Api+ module V2+ module Platform+ class ClassificationsController < ResourceController+ before_action -> { doorkeeper_authorize! :write, :admin }, only: WRITE_ACTIONS << :reposition++ def reposition+ spree_authorize! :update, resource if spree_current_user.present?++ result = classification_reposition_service.call(+ classification: resource,+ position: permitted_resource_params[:position]+ )++ if result.success?+ render_serialized_payload { serialize_resource(result.value) }+ else+ render_error_payload(result.error)+ end+ end++ private++ def model_class+ Spree::Classification+ end++ def scope_includes+ [+ :product,+ :taxon+ ]+ end++ def classification_reposition_service+ Spree::Dependencies.classification_reposition_service.constantize+ end+ end+ end+ end+ end+enddiff --git a/api/app/controllers/spree/api/v2/platform/resource_controller.rb b/api/app/controllers/spree/api/v2/platform/resource_controller.rbindex 46baa4317e..70368eec0b 100644--- a/api/app/controllers/spree/api/v2/platform/resource_controller.rb+++ b/api/app/controllers/spree/api/v2/platform/resource_controller.rb@@ -90,10 +90,20 @@ module Spreeendend+ def model_param_name+ model_class.to_s.demodulize.underscore+ end++ def spree_permitted_attributes+ Spree::PermittedAttributes.try("#{model_param_name}_attributes") || {}+ end+def permitted_resource_params- model_param_name = model_class.to_s.demodulize.underscore+ params.require(model_param_name).permit(spree_permitted_attributes)+ end- params.require(model_param_name).permit(Spree::PermittedAttributes.send("#{model_param_name}_attributes"))+ def allowed_sort_attributes+ super << spree_permitted_attributesendendenddiff --git a/api/app/controllers/spree/api/v2/resource_controller.rb b/api/app/controllers/spree/api/v2/resource_controller.rbindex 93e65a0edf..3276f54dbc 100644--- a/api/app/controllers/spree/api/v2/resource_controller.rb+++ b/api/app/controllers/spree/api/v2/resource_controller.rb@@ -23,7 +23,7 @@ module Spreeenddef default_sort_atributes- [:id, :name, :number, :updated_at, :created_at]+ [:id, :name, :number, :position, :updated_at, :created_at]enddef scope(skip_cancancan: false)diff --git a/api/app/serializers/spree/api/v2/platform/classification_serializer.rb b/api/app/serializers/spree/api/v2/platform/classification_serializer.rbnew file mode 100644index 0000000000..ec94c6f75e--- /dev/null+++ b/api/app/serializers/spree/api/v2/platform/classification_serializer.rb@@ -0,0 +1,14 @@+module Spree+ module Api+ module V2+ module Platform+ class ClassificationSerializer < BaseSerializer+ include ::Spree::Api::V2::ResourceSerializerConcern++ belongs_to :product+ belongs_to :taxon+ end+ end+ end+ end+enddiff --git a/api/config/routes.rb b/api/config/routes.rbindex c2ab4bb68d..20fd4cd345 100644--- a/api/config/routes.rb+++ b/api/config/routes.rb@@ -185,6 +185,11 @@ Spree::Core::Engine.add_routes do# Product Catalog APIresources :products+ resources :classifications do+ member do+ put :reposition+ end+ endresources :imagesresources :variantsresources :propertiesdiff --git a/api/spec/integration/api/v2/platform/classifications_spec.rb b/api/spec/integration/api/v2/platform/classifications_spec.rbnew file mode 100644index 0000000000..5b0dd461aa--- /dev/null+++ b/api/spec/integration/api/v2/platform/classifications_spec.rb@@ -0,0 +1,50 @@+require 'swagger_helper'++describe 'Classifications API', swagger: true do+ include_context 'Platform API v2'++ resource_name = 'Classification'+ include_example = 'product,taxon'+ filter_example = 'taxon_id_eq=1'++ let(:id) { create(:classification).id }+ let(:records_list) { create_list(:classification, 2) }+ let(:product) { create(:product) }+ let(:taxon) { create(:taxon) }+ let(:valid_create_param_value) { { position: 1, product_id: product.id, taxon_id: taxon.id } }+ let(:valid_update_param_value) do+ {+ position: 1+ }+ end+ let(:invalid_param_value) do+ {+ product_id: nil+ }+ end++ include_examples 'CRUD examples', resource_name, include_example, filter_example++ path '/api/v2/platform/classifications/{id}/reposition' do+ put 'Reposition a Classification' do+ tags resource_name.pluralize+ security [ bearer_auth: [] ]+ consumes 'application/json'+ parameter name: :id, in: :path, type: :string+ parameter name: :classification, in: :body, schema: { '$ref' => '#/components/schemas/classification_params' }+ json_api_include_parameter(include_example)++ let(:classification) { valid_update_param_value }+ let(:invalid_param_value) do+ {+ position: 'invalid'+ }+ end++ it_behaves_like 'record updated'+ it_behaves_like 'invalid request', :classification+ it_behaves_like 'record not found'+ it_behaves_like 'authentication failed'+ end+ end+enddiff --git a/api/spec/swagger_helper.rb b/api/spec/swagger_helper.rbindex dea5542d68..50872e9b86 100644--- a/api/spec/swagger_helper.rb+++ b/api/spec/swagger_helper.rb@@ -60,6 +60,14 @@ RSpec.configure do |config|user_id: { type: :integer }}},+ classification_params: {+ type: :object,+ properties: {+ product_id: { type: :integer },+ taxon_id: { type: :integer },+ position: { type: :integer }+ }+ },resources_list: {type: :object,properties: {diff --git a/core/app/models/spree/classification.rb b/core/app/models/spree/classification.rbindex f4574bca85..3588c9705c 100644--- a/core/app/models/spree/classification.rb+++ b/core/app/models/spree/classification.rb@@ -9,7 +9,10 @@ module Spreeendvalidates :taxon, :product, presence: true+ validates :position, numericality: { only_integer: true, allow_blank: true, allow_nil: true }# For #3494validates :taxon_id, uniqueness: { scope: :product_id, message: :already_linked, allow_blank: true }++ self.whitelisted_ransackable_attributes = ['taxon_id', 'product_id']endenddiff --git a/core/lib/spree/permitted_attributes.rb b/core/lib/spree/permitted_attributes.rbindex aba81b279b..b34e424367 100644--- a/core/lib/spree/permitted_attributes.rb+++ b/core/lib/spree/permitted_attributes.rb@@ -2,6 +2,7 @@ module Spreemodule PermittedAttributesATTRIBUTES = [:address_attributes,+ :classification_attributes,:checkout_attributes,:customer_return_attributes,:image_attributes,@@ -45,6 +46,10 @@ module Spree:user_id, :bill_address_id, :ship_address_id]+ @@classification_attributes = [+ :position, :taxon_id, :product_id+ ]+@@cms_page_attributes = [:title, :meta_title, :content, :meta_description, :visible, :slug, :locale]@@cms_section_attributes = [:name, :content, :settings, :fit, :destination]