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
Prepare Spree::Import for the Admin API surface
Damian Legawiec2cabc66fc91f
diff --git a/spree/core/app/models/spree/import.rb b/spree/core/app/models/spree/import.rbindex 9933b6fc8e..4ddf4a0d29 100644--- a/spree/core/app/models/spree/import.rb+++ b/spree/core/app/models/spree/import.rb@@ -34,7 +34,12 @@ module Spree## Ransack configuration#- self.whitelisted_ransackable_attributes = %w[number type]+ self.whitelisted_ransackable_attributes = %w[number type status]++ #+ # Scopes+ #+ scope :for_store, ->(store) { where(owner: store) }## Attachments@@ -70,6 +75,13 @@ module Spreeevent :fail dotransition to: :failedend++ # Re-processes rows that failed: the dispatcher targets pending_and_failed+ # rows, so retry is just a transition + re-dispatch.+ event :retry_failed_rows do+ transition from: :completed, to: :processing, if: ->(import) { import.rows.failed.exists? }+ end+ after_transition on: :retry_failed_rows, do: :process_rows_asyncend#@@ -110,6 +122,32 @@ module Spreestatus == 'completed'end+ # Imports are deletable only while no processing jobs can be in flight —+ # CreateRowsJob/ProcessGroupJob re-load the record mid-run.+ # @return [Boolean]+ def can_be_deleted?+ %w[pending mapping completed failed].include?(status)+ end++ # Per-status row counts for API status payloads, memoized so one request+ # issues a single grouped COUNT instead of one per status.+ # @return [Hash{String => Integer}]+ def rows_status_counts+ @rows_status_counts ||= rows.group(:status).count+ end++ # First data row as { header => value }, reading a single line of the+ # attached CSV — used by the mapping UI to show example values.+ # @return [Hash]+ def sample_row+ return {} if attachment_file_content.blank?++ row = ::CSV.foreach(StringIO.new(attachment_file_content), headers: true, col_sep: preferred_delimiter).first+ row&.to_h || {}+ rescue ::CSV::MalformedCSVError, EncodingError+ {}+ end+# Returns the model class for the import# @return [Class]def model_class@@ -277,6 +315,20 @@ module Spreeavailable_types.find { |type| type.model_class.to_s == model.to_s }end+ # Admin API scope family gating this import type — an import is a bulk+ # write, so an API key needs `write_<required_scope>` to create and+ # manage it. Derived from the class name (Spree::Imports::Customers =>+ # :customers); override in subclasses gated by a different scope.+ # Returns nil on the base class, so unmapped types are only accessible+ # to write_all keys.+ #+ # @return [Symbol, nil]+ def required_scope+ return nil if self == Spree::Import++ to_s.demodulize.underscore.to_sym+ end+# eg. Spree::Imports::Orders => Spree::Orderdef model_classreturn Spree.user_class if to_s == 'Spree::Imports::Customers'diff --git a/spree/core/app/models/spree/import_row.rb b/spree/core/app/models/spree/import_row.rbindex 10a93e5fef..1fc1da86f0 100644--- a/spree/core/app/models/spree/import_row.rb+++ b/spree/core/app/models/spree/import_row.rb@@ -18,6 +18,11 @@ module Spreevalidates :import, :data, presence: truevalidates :row_number, uniqueness: { scope: :import_id }, numericality: { only_integer: true, greater_than: 0 }, presence: true+ #+ # Ransack configuration+ #+ self.whitelisted_ransackable_attributes = %w[status row_number]+## State machine#@@ -92,6 +97,7 @@ module Spreedef process!(mappings: nil, schema_fields: nil)start_processing!self.item = import.row_processor_class.new(self, mappings: mappings, schema_fields: schema_fields).process!+ self.validation_errors = nil # clear a stale error when a retried row succeedscomplete!rescue StandardError => eRails.error.report(e, handled: true, context: { import_row_id: id }, source: 'spree.core')@@ -105,7 +111,7 @@ module Spreeupdate_columns(status: 'processing', updated_at: Time.current)processor = import.row_processor_class.new(self, mappings: mappings, schema_fields: schema_fields)self.item = processor.process!- update_columns(status: 'completed', item_type: item.class.name, item_id: item.id, updated_at: Time.current)+ update_columns(status: 'completed', item_type: item.class.name, item_id: item.id, validation_errors: nil, updated_at: Time.current)rescue StandardError => eRails.error.report(e, handled: true, context: { import_row_id: id }, source: 'spree.core')update_columns(status: 'failed', validation_errors: e.message, updated_at: Time.current)diff --git a/spree/core/app/models/spree/imports/product_translations.rb b/spree/core/app/models/spree/imports/product_translations.rbindex 1845e2a091..41c446f736 100644--- a/spree/core/app/models/spree/imports/product_translations.rb+++ b/spree/core/app/models/spree/imports/product_translations.rb@@ -16,6 +16,12 @@ module Spreedef self.model_classSpree::Productend++ # Translation imports write products, so they share the products scope+ # (mirrors Spree::Exports::ProductTranslations.required_scope).+ def self.required_scope+ :products+ endendendenddiff --git a/spree/core/db/migrate/20260710000001_add_import_id_status_index_to_spree_import_rows.rb b/spree/core/db/migrate/20260710000001_add_import_id_status_index_to_spree_import_rows.rbnew file mode 100644index 0000000000..4121779a73--- /dev/null+++ b/spree/core/db/migrate/20260710000001_add_import_id_status_index_to_spree_import_rows.rb@@ -0,0 +1,7 @@+class AddImportIdStatusIndexToSpreeImportRows < ActiveRecord::Migration[7.2]+ def change+ # Powers the Admin API's per-poll grouped status counts and the+ # failed-rows listing without a full scan on large imports.+ add_index :spree_import_rows, [:import_id, :status]+ end+end