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 endpoint and event models.

Sebastian Palma6e7b6e32d63a
diff --git a/api/app/models/spree/webhooks/endpoint.rb b/api/app/models/spree/webhooks/endpoint.rb
new file mode 100644
index 0000000000..97b1f830d4
--- /dev/null
+++ b/api/app/models/spree/webhooks/endpoint.rb
@@ -0,0 +1,16 @@
+module Spree
+ module Webhooks
+ class Endpoint < Spree::Base
+ self.table_name = 'spree_webhooks_endpoints'
+
+ before_save :assign_default_subscriptions,
+ if: proc { ActiveRecord::Base.connection.adapter_name == 'Mysql2' }
+
+ private
+
+ def assign_default_subscriptions
+ self.subscriptions = ['*'] if subscriptions.blank?
+ end
+ end
+ end
+end
diff --git a/api/app/models/spree/webhooks/event.rb b/api/app/models/spree/webhooks/event.rb
new file mode 100644
index 0000000000..586d0ffa6a
--- /dev/null
+++ b/api/app/models/spree/webhooks/event.rb
@@ -0,0 +1,7 @@
+module Spree
+ module Webhooks
+ class Event < Spree::Base
+ self.table_name = 'spree_webhooks_events'
+ end
+ end
+end
diff --git a/api/spec/models/spree/webhooks/endpoint_spec.rb b/api/spec/models/spree/webhooks/endpoint_spec.rb
new file mode 100644
index 0000000000..0fc1ea81a6
--- /dev/null
+++ b/api/spec/models/spree/webhooks/endpoint_spec.rb
@@ -0,0 +1,48 @@
+require 'spec_helper'
+
+module Spree
+ describe Webhooks::Endpoint, type: :model do
+ context 'after-save sets default subscriptions' do
+ subject { build(:endpoint, subscriptions: subscriptions) }
+
+ let(:default_all_subscriptions) { ['*'] }
+
+ before do
+ subject.save
+ subject.reload
+ end
+
+ context 'mysql', if: ActiveRecord::Base.connection.adapter_name == 'Mysql2' do
+
+ context 'without subscriptions when initializing an endpoint' do
+ let(:subscriptions) { nil }
+
+ it 'sets the default subscriptions' do
+ expect(subject.subscriptions).to eq(default_all_subscriptions)
+ end
+ end
+
+ context 'with subscriptions when initializing an endpoint' do
+ subject { build(:endpoint, subscriptions: subscriptions) }
+
+ let(:subscriptions) { ['model.event'] }
+
+ it 'after-save does not set default subscriptions' do
+ expect(subject.subscriptions).to eq(subscriptions)
+ end
+ end
+ end
+
+ context 'pg', if: ActiveRecord::Base.connection.adapter_name == 'PostgreSQL' do
+ let(:subscriptions) { '' }
+
+ it 'does not execute the callback as Postgres implements default JSONB values' do
+ # Although an empty string isn't a working subscription
+ # what's being tested is setting default susbcriptions,
+ # since MySQL does not support default values for JSON.
+ expect(subject.subscriptions).to eq(subscriptions)
+ end
+ end
+ end
+ end
+end
diff --git a/core/lib/spree/testing_support/factories/endpoint.rb b/core/lib/spree/testing_support/factories/endpoint.rb
new file mode 100644
index 0000000000..7b9fc161b9
--- /dev/null
+++ b/core/lib/spree/testing_support/factories/endpoint.rb
@@ -0,0 +1,7 @@
+FactoryBot.define do
+ factory :endpoint, class: Spree::Webhooks::Endpoint do
+ enabled { true }
+ subscriptions { ['*'] }
+ url { 'https://google.com:81' }
+ end
+end
diff --git a/core/lib/spree/testing_support/factories/event.rb b/core/lib/spree/testing_support/factories/event.rb
new file mode 100644
index 0000000000..47ae59c53c
--- /dev/null
+++ b/core/lib/spree/testing_support/factories/event.rb
@@ -0,0 +1,6 @@
+FactoryBot.define do
+ factory :event, class: Spree::Webhooks::Event do
+ enabled { true }
+ name { 'resource.create' }
+ end
+end