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.

VEN-341 Product State Machine with Webhooks (#11616)

Aleksandar Petrushev5ff8f37b2a59
diff --git a/api/app/models/concerns/spree/webhooks/has_webhooks.rb b/api/app/models/concerns/spree/webhooks/has_webhooks.rb
index 3b2e2c4796..dc6088bd7f 100644
--- a/api/app/models/concerns/spree/webhooks/has_webhooks.rb
+++ b/api/app/models/concerns/spree/webhooks/has_webhooks.rb
@@ -11,7 +11,7 @@ module Spree
def queue_webhooks_requests!(event_name)
return if disable_spree_webhooks?
return if Spree::Webhooks::Subscriber.active.with_urls_for(event_name).none?
- return if update_event?(event_name) && updating_only_timestamps?
+ return if update_event?(event_name) && updating_only_ignored_attributes?
return if webhook_payload_body.blank?
Spree::Webhooks::Subscribers::QueueRequests.call(event_name: event_name, webhook_payload_body: webhook_payload_body)
@@ -55,8 +55,15 @@ module Spree
end
end
- def updating_only_timestamps?
- (saved_changes.keys - %w[created_at updated_at deleted_at]).empty?
+ def updating_only_ignored_attributes?
+ (saved_changes.keys - ignored_attributes).empty?
+ end
+
+ def ignored_attributes
+ timestamps = %w[created_at updated_at deleted_at]
+ return timestamps unless self.class.respond_to?(:ignored_attributes_for_update_webhook_event)
+
+ timestamps + self.class.ignored_attributes_for_update_webhook_event
end
def update_event?(event_name)
diff --git a/api/app/models/spree/api/webhooks/product_decorator.rb b/api/app/models/spree/api/webhooks/product_decorator.rb
index 6a07c66935..1485d01ded 100644
--- a/api/app/models/spree/api/webhooks/product_decorator.rb
+++ b/api/app/models/spree/api/webhooks/product_decorator.rb
@@ -4,12 +4,32 @@ module Spree
module ProductDecorator
def self.prepended(base)
def base.custom_webhook_events
- %w[product.back_in_stock product.backorderable product.discontinued product.out_of_stock]
+ %w[product.back_in_stock product.backorderable product.discontinued
+ product.out_of_stock product.activated product.archived product.drafted]
+ end
+
+ def base.ignored_attributes_for_update_webhook_event
+ %w[status]
end
base.after_update_commit :queue_webhooks_requests_for_product_discontinued!
end
+ def after_activate
+ super
+ queue_webhooks_requests!('product.activated')
+ end
+
+ def after_archive
+ super
+ queue_webhooks_requests!('product.archived')
+ end
+
+ def after_draft
+ super
+ queue_webhooks_requests!('product.drafted')
+ end
+
private
def queue_webhooks_requests_for_product_discontinued!
@@ -24,4 +44,3 @@ module Spree
end
Spree::Product.prepend(Spree::Api::Webhooks::ProductDecorator)
-
diff --git a/api/spec/models/spree/api/webhooks/product_decorator_spec.rb b/api/spec/models/spree/api/webhooks/product_decorator_spec.rb
index 73fdc62ba8..a3591d2052 100644
--- a/api/spec/models/spree/api/webhooks/product_decorator_spec.rb
+++ b/api/spec/models/spree/api/webhooks/product_decorator_spec.rb
@@ -2,15 +2,15 @@ require 'spec_helper'
describe Spree::Api::Webhooks::ProductDecorator do
let(:product) { create(:product) }
+ let(:webhook_payload_body) do
+ Spree::Api::V2::Platform::ProductSerializer.new(
+ product,
+ include: Spree::Api::V2::Platform::ProductSerializer.relationships_to_serialize.keys
+ ).serializable_hash
+ end
+ let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
context 'emitting product.discontinued' do
- let(:webhook_payload_body) do
- Spree::Api::V2::Platform::ProductSerializer.new(
- product,
- include: Spree::Api::V2::Platform::ProductSerializer.relationships_to_serialize.keys
- ).serializable_hash
- end
- let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
let(:event_name) { 'product.discontinued' }
context 'when product discontinued_on changes' do
@@ -41,4 +41,32 @@ describe Spree::Api::Webhooks::ProductDecorator do
end
end
end
+
+ context 'when changing status' do
+ context 'to active' do
+ let(:event_name) { 'product.activated' }
+
+ before { product.update_column(:status, :draft) }
+
+ it do
+ expect { product.activate }.to emit_webhook_event(event_name)
+ end
+ end
+
+ context 'to draft' do
+ let(:event_name) { 'product.drafted' }
+
+ it do
+ expect { product.draft }.to emit_webhook_event(event_name)
+ end
+ end
+
+ context 'to archived' do
+ let(:event_name) { 'product.archived' }
+
+ it do
+ expect { product.archive }.to emit_webhook_event(event_name)
+ end
+ end
+ end
end
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb
index 6e9ec8fc42..656b81ac1a 100644
--- a/core/app/models/spree/product.rb
+++ b/core/app/models/spree/product.rb
@@ -150,6 +150,23 @@ module Spree
alias master_images images
+ state_machine :status, initial: :draft do
+ event :activate do
+ transition to: :active
+ end
+ after_transition to: :active, do: :after_activate
+
+ event :archive do
+ transition to: :archived
+ end
+ after_transition to: :archived, do: :after_archive
+
+ event :draft do
+ transition to: :draft
+ end
+ after_transition to: :draft, do: :after_draft
+ end
+
# Can't use short form block syntax due to https://github.com/Netflix/fast_jsonapi/issues/259
def purchasable?
default_variant.purchasable? || variants.any?(&:purchasable?)
@@ -234,7 +251,7 @@ module Spree
# deleted products and products with status different than active
# are not available
def available?
- status == 'active' && !deleted?
+ active? && !deleted?
end
def discontinue!
@@ -514,5 +531,17 @@ module Spree
def downcase_slug
slug&.downcase!
end
+
+ def after_activate
+ # this method is prepended in api/ to queue Webhooks requests
+ end
+
+ def after_archive
+ # this method is prepended in api/ to queue Webhooks requests
+ end
+
+ def after_draft
+ # this method is prepended in api/ to queue Webhooks requests
+ end
end
end