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 the workflows that replace the remaining state machines

Damian Legawiec04b736c3836e
diff --git a/spree/core/app/models/concerns/spree/has_status.rb b/spree/core/app/models/concerns/spree/has_status.rb
index 25a4da5a89..c237935365 100644
--- a/spree/core/app/models/concerns/spree/has_status.rb
+++ b/spree/core/app/models/concerns/spree/has_status.rb
@@ -36,6 +36,11 @@ module Spree
validates :status, inclusion: { in: ->(record) { record.class.statuses } }
values.each { |value| define_status_methods(value) }
+
+ # Multi-value lookup, replacing what state_machines-activerecord
+ # generated. The single-value scopes read better for one status;
+ # this exists for the "any of these" case.
+ scope :with_status, ->(*wanted) { where(status: wanted.flatten.map(&:to_s)) }
end
# Appends a status. Additive by design: core workflows guard on core
diff --git a/spree/core/app/workflows/spree/imports/complete.rb b/spree/core/app/workflows/spree/imports/complete.rb
new file mode 100644
index 0000000000..2ac31a64fd
--- /dev/null
+++ b/spree/core/app/workflows/spree/imports/complete.rb
@@ -0,0 +1,37 @@
+module Spree
+ module Imports
+ # Closes out an import once every row has been processed.
+ class Complete < Spree::Workflow
+ hooks :validate, :after_complete
+
+ # @param import [Spree::Import]
+ # @return [Spree::ServiceModule::Result] value is the import
+ def perform(import:)
+ super
+
+ run_hooks :validate
+
+ ApplicationRecord.transaction do
+ step :mark_completed
+ step :touch_store
+ run_hooks :after_complete
+ end
+
+ import.publish_event('import.completed')
+ success(import)
+ end
+
+ private
+
+ def mark_completed
+ failure(import) unless import.update(status: 'completed')
+ end
+
+ # An import changes the catalog, so the store's cache keys have to move
+ # with it.
+ def touch_store
+ import.store.touch
+ end
+ end
+ end
+end
diff --git a/spree/core/app/workflows/spree/imports/complete_mapping.rb b/spree/core/app/workflows/spree/imports/complete_mapping.rb
new file mode 100644
index 0000000000..50d7f24879
--- /dev/null
+++ b/spree/core/app/workflows/spree/imports/complete_mapping.rb
@@ -0,0 +1,50 @@
+module Spree
+ module Imports
+ # Accepts the column mapping and starts building rows from the file.
+ #
+ # Dispatch happens after the transaction rather than inside it: the job
+ # reads the import back, so it must not start before the status is
+ # visible to another connection.
+ class CompleteMapping < Spree::Workflow
+ hooks :validate, :after_complete_mapping
+
+ # @param import [Spree::Import]
+ # @return [Spree::ServiceModule::Result] value is the import
+ def perform(import:)
+ super
+
+ step :ensure_mapping
+ run_hooks :validate
+
+ ApplicationRecord.transaction do
+ step :mark_completed_mapping
+ run_hooks :after_complete_mapping
+ end
+
+ step :dispatch_row_creation
+ success(import)
+ end
+
+ private
+
+ def ensure_mapping
+ failure(import, :import_not_mapping) unless import.status == 'mapping'
+ end
+
+ def mark_completed_mapping
+ failure(import) unless import.update(status: 'completed_mapping')
+ end
+
+ # Inline runs are for rake tasks, seeds and the console, where the data
+ # has to exist by the time the call returns and there may be no worker.
+ def dispatch_row_creation
+ if import.preferred_inline
+ Spree::Imports::ProcessJob.perform_now(import.id)
+ else
+ # The short delay lets the attachment settle before the job reads it.
+ Spree::Imports::ProcessJob.set(wait: 2.seconds).perform_later(import.id)
+ end
+ end
+ end
+ end
+end
diff --git a/spree/core/app/workflows/spree/imports/retry_failed_rows.rb b/spree/core/app/workflows/spree/imports/retry_failed_rows.rb
new file mode 100644
index 0000000000..3359f859bd
--- /dev/null
+++ b/spree/core/app/workflows/spree/imports/retry_failed_rows.rb
@@ -0,0 +1,46 @@
+module Spree
+ module Imports
+ # Runs the failed rows of a finished import again. The dispatcher already
+ # targets pending and failed rows, so this is a status move plus a
+ # re-dispatch — the successful rows are left alone.
+ class RetryFailedRows < Spree::Workflow
+ hooks :validate, :after_retry
+
+ # @param import [Spree::Import]
+ # @return [Spree::ServiceModule::Result] value is the import
+ def perform(import:)
+ super
+
+ step :ensure_retryable
+ run_hooks :validate
+
+ ApplicationRecord.transaction do
+ step :mark_processing
+ run_hooks :after_retry
+ end
+
+ step :dispatch_rows
+ success(import)
+ end
+
+ private
+
+ def ensure_retryable
+ failure(import, :import_not_completed) unless import.status == 'completed'
+ failure(import, :import_has_no_failed_rows) unless import.rows.failed.exists?
+ end
+
+ def mark_processing
+ failure(import) unless import.update(status: 'processing')
+ end
+
+ def dispatch_rows
+ if import.preferred_inline
+ Spree::Imports::ProcessJob.perform_now(import.id, skip_row_creation: true)
+ else
+ Spree::Imports::ProcessJob.perform_later(import.id, skip_row_creation: true)
+ end
+ end
+ end
+ end
+end
diff --git a/spree/core/app/workflows/spree/imports/start_mapping.rb b/spree/core/app/workflows/spree/imports/start_mapping.rb
new file mode 100644
index 0000000000..526b55c770
--- /dev/null
+++ b/spree/core/app/workflows/spree/imports/start_mapping.rb
@@ -0,0 +1,44 @@
+module Spree
+ module Imports
+ # Opens an uploaded file for mapping: reads its headers, creates a mapping
+ # row per schema field and guesses which column feeds which.
+ #
+ # Reading the file is where a malformed CSV shows up, so the caller sees
+ # the parse error from here rather than from a later step.
+ class StartMapping < Spree::Workflow
+ hooks :validate, :after_start_mapping
+
+ # @param import [Spree::Import]
+ # @return [Spree::ServiceModule::Result] value is the import
+ def perform(import:)
+ super
+
+ run_hooks :validate
+
+ ApplicationRecord.transaction do
+ step :build_mappings
+ step :mark_mapping
+ run_hooks :after_start_mapping
+ end
+
+ success(import)
+ end
+
+ private
+
+ # TODO: seed from the previous import of the same type, so a merchant
+ # doesn't map the same columns twice.
+ def build_mappings
+ import.schema_fields.each do |schema_field|
+ mapping = import.mappings.find_or_create_by!(schema_field: schema_field[:name])
+ mapping.try_to_auto_assign_file_column(import.csv_headers)
+ mapping.save!
+ end
+ end
+
+ def mark_mapping
+ failure(import) unless import.update(status: 'mapping')
+ end
+ end
+ end
+end
diff --git a/spree/core/app/workflows/spree/imports/start_processing.rb b/spree/core/app/workflows/spree/imports/start_processing.rb
new file mode 100644
index 0000000000..ada25972f1
--- /dev/null
+++ b/spree/core/app/workflows/spree/imports/start_processing.rb
@@ -0,0 +1,29 @@
+module Spree
+ module Imports
+ # Marks the import as working through its rows.
+ class StartProcessing < Spree::Workflow
+ hooks :validate, :after_start_processing
+
+ # @param import [Spree::Import]
+ # @return [Spree::ServiceModule::Result] value is the import
+ def perform(import:)
+ super
+
+ run_hooks :validate
+
+ ApplicationRecord.transaction do
+ step :mark_processing
+ run_hooks :after_start_processing
+ end
+
+ success(import)
+ end
+
+ private
+
+ def mark_processing
+ failure(import) unless import.update(status: 'processing')
+ end
+ end
+ end
+end
diff --git a/spree/core/app/workflows/spree/invitations/accept.rb b/spree/core/app/workflows/spree/invitations/accept.rb
new file mode 100644
index 0000000000..fc74dcbc03
--- /dev/null
+++ b/spree/core/app/workflows/spree/invitations/accept.rb
@@ -0,0 +1,50 @@
+module Spree
+ module Invitations
+ # Turns an invitation into access: the invitee gains the role they were
+ # invited to, and the invitation is marked accepted.
+ #
+ # The expiry and invitee checks were state-scoped validations on the old
+ # machine, which meant an expired invitation became a record that refused
+ # to save. As guards they refuse the acceptance itself, so the invitation
+ # is left untouched and still pending.
+ class Accept < Spree::Workflow
+ hooks :validate, :after_accept
+
+ # @param invitation [Spree::Invitation]
+ # @return [Spree::ServiceModule::Result] value is the invitation
+ def perform(invitation:)
+ super
+
+ step :ensure_acceptable
+ run_hooks :validate
+
+ ApplicationRecord.transaction do
+ step :grant_role
+ step :mark_accepted
+ run_hooks :after_accept
+ end
+
+ invitation.publish_event('invitation.accepted')
+ success(invitation)
+ end
+
+ private
+
+ def ensure_acceptable
+ failure(invitation, :invitation_already_accepted) if invitation.accepted?
+ failure(invitation, :invitation_expired) if invitation.expired?
+ failure(invitation, :invitation_invitee_missing) if invitation.invitee.blank?
+ end
+
+ def grant_role
+ invitation.role_user = invitation.resource.add_user(invitation.invitee, invitation.role)
+ end
+
+ # One write where the machine did three: the transition, the role_user
+ # link and the accepted_at stamp were each their own save.
+ def mark_accepted
+ failure(invitation) unless invitation.update(status: 'accepted', accepted_at: Time.current)
+ end
+ end
+ end
+end
diff --git a/spree/core/app/workflows/spree/price_lists/activate.rb b/spree/core/app/workflows/spree/price_lists/activate.rb
new file mode 100644
index 0000000000..f697e7e45b
--- /dev/null
+++ b/spree/core/app/workflows/spree/price_lists/activate.rb
@@ -0,0 +1,35 @@
+module Spree
+ module PriceLists
+ # Puts a price list into effect. A list whose start date is still in the
+ # future is scheduled instead of activated, so activating is always the
+ # single answer to "make this list live" regardless of its dates.
+ class Activate < Spree::Workflow
+ hooks :validate, :after_activate
+
+ # @param price_list [Spree::PriceList]
+ # @return [Spree::ServiceModule::Result] value is the price list
+ def perform(price_list:)
+ super
+
+ run_hooks :validate
+
+ ApplicationRecord.transaction do
+ step :mark_live
+ run_hooks :after_activate
+ end
+
+ success(price_list)
+ end
+
+ private
+
+ def scheduled?
+ price_list.starts_at.present? && price_list.starts_at.future?
+ end
+
+ def mark_live
+ failure(price_list) unless price_list.update(status: scheduled? ? 'scheduled' : 'active')
+ end
+ end
+ end
+end
diff --git a/spree/core/app/workflows/spree/price_lists/deactivate.rb b/spree/core/app/workflows/spree/price_lists/deactivate.rb
new file mode 100644
index 0000000000..3b7a98e046
--- /dev/null
+++ b/spree/core/app/workflows/spree/price_lists/deactivate.rb
@@ -0,0 +1,30 @@
+module Spree
+ module PriceLists
+ # Takes a price list out of effect. Its prices stop applying immediately;
+ # nothing about them is discarded, so it can be activated again later.
+ class Deactivate < Spree::Workflow
+ hooks :validate, :after_deactivate
+
+ # @param price_list [Spree::PriceList]
+ # @return [Spree::ServiceModule::Result] value is the price list
+ def perform(price_list:)
+ super
+
+ run_hooks :validate
+
+ ApplicationRecord.transaction do
+ step :mark_inactive
+ run_hooks :after_deactivate
+ end
+
+ success(price_list)
+ end
+
+ private
+
+ def mark_inactive
+ failure(price_list) unless price_list.update(status: 'inactive')
+ end
+ end
+ end
+end
diff --git a/spree/core/app/workflows/spree/products/activate.rb b/spree/core/app/workflows/spree/products/activate.rb
new file mode 100644
index 0000000000..97b0d5a921
--- /dev/null
+++ b/spree/core/app/workflows/spree/products/activate.rb
@@ -0,0 +1,33 @@
+module Spree
+ module Products
+ # Puts a product on sale. Replaces the `activate` state machine event: the
+ # write and the event it publishes live here rather than in an
+ # after_transition, so a handler can veto the change before anything is
+ # written and see the product it applies to.
+ class Activate < Spree::Workflow
+ hooks :validate, :after_activate
+
+ # @param product [Spree::Product]
+ # @return [Spree::ServiceModule::Result] value is the product
+ def perform(product:)
+ super
+
+ run_hooks :validate
+
+ ApplicationRecord.transaction do
+ step :mark_active
+ run_hooks :after_activate
+ end
+
+ product.publish_event('product.activated')
+ success(product)
+ end
+
+ private
+
+ def mark_active
+ failure(product) unless product.update(status: 'active')
+ end
+ end
+ end
+end
diff --git a/spree/core/app/workflows/spree/products/archive.rb b/spree/core/app/workflows/spree/products/archive.rb
new file mode 100644
index 0000000000..81dd814c6c
--- /dev/null
+++ b/spree/core/app/workflows/spree/products/archive.rb
@@ -0,0 +1,31 @@
+module Spree
+ module Products
+ # Takes a product off sale for good. Replaces the `archive` state machine
+ # event.
+ class Archive < Spree::Workflow
+ hooks :validate, :after_archive
+
+ # @param product [Spree::Product]
+ # @return [Spree::ServiceModule::Result] value is the product
+ def perform(product:)
+ super
+
+ run_hooks :validate
+
+ ApplicationRecord.transaction do
+ step :mark_archived
+ run_hooks :after_archive
+ end
+
+ product.publish_event('product.archived')
+ success(product)
+ end
+
+ private
+
+ def mark_archived
+ failure(product) unless product.update(status: 'archived')
+ end
+ end
+ end
+end
diff --git a/spree/core/app/workflows/spree/products/draft.rb b/spree/core/app/workflows/spree/products/draft.rb
new file mode 100644
index 0000000000..0ce558bbfe
--- /dev/null
+++ b/spree/core/app/workflows/spree/products/draft.rb
@@ -0,0 +1,31 @@
+module Spree
+ module Products
+ # Returns a product to draft, hiding it from the storefront while it is
+ # worked on. Replaces the `draft` state machine event, which published no
+ # event of its own — the `after_draft` hook is the extension point.
+ class Draft < Spree::Workflow
+ hooks :validate, :after_draft
+
+ # @param product [Spree::Product]
+ # @return [Spree::ServiceModule::Result] value is the product
+ def perform(product:)
+ super
+
+ run_hooks :validate
+
+ ApplicationRecord.transaction do
+ step :mark_draft
+ run_hooks :after_draft
+ end
+
+ success(product)
+ end
+
+ private
+
+ def mark_draft
+ failure(product) unless product.update(status: 'draft')
+ end
+ end
+ end
+end
diff --git a/spree/core/lib/spree/core/dependencies.rb b/spree/core/lib/spree/core/dependencies.rb
index 8b757f2e7f..54a563ca0b 100644
--- a/spree/core/lib/spree/core/dependencies.rb
+++ b/spree/core/lib/spree/core/dependencies.rb
@@ -128,6 +128,23 @@ module Spree
product_create_workflow: 'Spree::Products::Create',
product_update_workflow: 'Spree::Products::Update',
product_destroy_workflow: 'Spree::Products::Destroy',
+ product_activate_workflow: 'Spree::Products::Activate',
+ product_archive_workflow: 'Spree::Products::Archive',
+ product_draft_workflow: 'Spree::Products::Draft',
+
+ # price lists
+ price_list_activate_workflow: 'Spree::PriceLists::Activate',
+ price_list_deactivate_workflow: 'Spree::PriceLists::Deactivate',
+
+ # invitations
+ invitation_accept_workflow: 'Spree::Invitations::Accept',
+
+ # imports — the CSV pipeline's steps
+ import_start_mapping_workflow: 'Spree::Imports::StartMapping',
+ import_complete_mapping_workflow: 'Spree::Imports::CompleteMapping',
+ import_start_processing_workflow: 'Spree::Imports::StartProcessing',
+ import_complete_workflow: 'Spree::Imports::Complete',
+ import_retry_failed_rows_workflow: 'Spree::Imports::RetryFailedRows',
# which of a product's variants it leads with when sellers share the
# listing. Ranking is marketplace policy — swap this to weight seller