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.

Split DataFeed generation into multiple services.

wjwitek1027a07f7eb3
diff --git a/api/app/controllers/spree/api/v2/platform/google_feed_settings_controller.rb b/api/app/controllers/spree/api/v2/platform/google_feed_settings_controller.rb
index d697aa7d32..0460ae59d8 100644
--- a/api/app/controllers/spree/api/v2/platform/google_feed_settings_controller.rb
+++ b/api/app/controllers/spree/api/v2/platform/google_feed_settings_controller.rb
@@ -13,4 +13,3 @@ module Spree
end
end
end
-
diff --git a/api/app/controllers/spree/api/v2/storefront/stores_controller.rb b/api/app/controllers/spree/api/v2/storefront/stores_controller.rb
index 009e68678f..a6a0eac397 100644
--- a/api/app/controllers/spree/api/v2/storefront/stores_controller.rb
+++ b/api/app/controllers/spree/api/v2/storefront/stores_controller.rb
@@ -7,19 +7,18 @@ module Spree
render_serialized_payload { serialize_resource(current_store) }
end
- def export_google_rss_feed
- send_data export_google_rss_service.value[:file], filename: 'products.rss', type: 'text/xml'
+ def data_feeds_google_rss_feed
+ send_data data_feeds_google_rss_service.value[:file], filename: 'products.rss', type: 'text/xml'
end
private
- def update_settings
- @settings = Spree::GoogleFeedSetting.find_by(spree_store_id: current_store)
+ def settings
+ @settings ||= Spree::DataFeedSetting.find_by!(spree_store_id: current_store, uuid: params[:unique_url], enabled: true)
end
- def export_google_rss_service
- update_settings
- Spree::Dependencies.export_google_rss_service.constantize.new.call(@settings)
+ def data_feeds_google_rss_service
+ Spree::Dependencies.data_feeds_google_rss_service.constantize.new.call(settings)
end
def model_class
diff --git a/api/config/routes.rb b/api/config/routes.rb
index 1c8559ec2f..df9a9a0800 100644
--- a/api/config/routes.rb
+++ b/api/config/routes.rb
@@ -64,8 +64,8 @@ Spree::Core::Engine.add_routes do
get '/digitals/:token', to: 'digitals#download', as: 'digital'
- # google export API
- get '/rss/google', to: 'stores#export_google_rss_feed'
+ # export API
+ get '/data_feeds/google/:unique_url', to: 'stores#data_feeds_google_rss_feed'
end
namespace :platform do
diff --git a/core/app/models/spree/data_feed_setting.rb b/core/app/models/spree/data_feed_setting.rb
new file mode 100644
index 0000000000..89d5330d40
--- /dev/null
+++ b/core/app/models/spree/data_feed_setting.rb
@@ -0,0 +1,16 @@
+module Spree
+ class DataFeedSetting < Base
+ belongs_to :store, class_name: 'Spree::Store', foreign_key: 'spree_store_id'
+
+ before_create :generate_uuid
+
+ with_options presence: true do
+ validates :store
+ validates :uuid, uniqueness: true, on: :update
+ end
+
+ def generate_uuid
+ write_attribute(:uuid, SecureRandom.uuid)
+ end
+ end
+end
diff --git a/core/app/models/spree/google_feed_setting.rb b/core/app/models/spree/google_feed_setting.rb
deleted file mode 100644
index ede387c2c5..0000000000
--- a/core/app/models/spree/google_feed_setting.rb
+++ /dev/null
@@ -1,19 +0,0 @@
-module Spree
- class GoogleFeedSetting < Base
- belongs_to :store, class_name: 'Spree::Store', foreign_key: 'spree_store_id'
-
- validates :store, presence: true
-
- def enabled_keys
- keys = []
-
- attributes.each do |key, value|
- if value == true
- keys.append(key.to_sym)
- end
- end
-
- keys
- end
- end
-end
diff --git a/core/app/services/spree/data_feeds/google/optional_attributes.rb b/core/app/services/spree/data_feeds/google/optional_attributes.rb
new file mode 100644
index 0000000000..0959235352
--- /dev/null
+++ b/core/app/services/spree/data_feeds/google/optional_attributes.rb
@@ -0,0 +1,23 @@
+module Spree
+ module DataFeeds
+ module Google
+ class OptionalAttributes
+ prepend Spree::ServiceModule::Base
+
+ def call(input)
+ information = {}
+
+ input[:product].property_ids.each do |key|
+ name = Spree::Property.find(key)&.name
+ value = input[:product].property(name)
+ unless value.nil?
+ information[name] = value
+ end
+ end
+
+ success(information: information)
+ end
+ end
+ end
+ end
+end
diff --git a/core/app/services/spree/data_feeds/google/optional_sub_attributes.rb b/core/app/services/spree/data_feeds/google/optional_sub_attributes.rb
new file mode 100644
index 0000000000..0f767a538c
--- /dev/null
+++ b/core/app/services/spree/data_feeds/google/optional_sub_attributes.rb
@@ -0,0 +1,21 @@
+module Spree
+ module DataFeeds
+ module Google
+ class OptionalSubAttributes
+ prepend Spree::ServiceModule::Base
+
+ def call(input)
+ information = {}
+
+ # This is a place where you can put attributes that have sub-attributes, example for shipping:
+ #
+ # information['shipping'] = {}
+ # information['shipping']['price'] = calculate_shipping(input[:product])
+ # information['shipping']['country'] = input[:store].default_country
+
+ success(information: information)
+ end
+ end
+ end
+ end
+end
diff --git a/core/app/services/spree/data_feeds/google/products_list.rb b/core/app/services/spree/data_feeds/google/products_list.rb
new file mode 100644
index 0000000000..56dd7d1c74
--- /dev/null
+++ b/core/app/services/spree/data_feeds/google/products_list.rb
@@ -0,0 +1,15 @@
+module Spree
+ module DataFeeds
+ module Google
+ class ProductsList
+ prepend Spree::ServiceModule::Base
+
+ def call(store)
+ products = store.products.active
+ return success(products: products)
+ end
+ end
+ end
+ end
+end
+
diff --git a/core/app/services/spree/data_feeds/google/required_attributes.rb b/core/app/services/spree/data_feeds/google/required_attributes.rb
new file mode 100644
index 0000000000..d5cdfb085e
--- /dev/null
+++ b/core/app/services/spree/data_feeds/google/required_attributes.rb
@@ -0,0 +1,67 @@
+module Spree
+ module DataFeeds
+ module Google
+ class RequiredAttributes
+ prepend Spree::ServiceModule::Base
+
+ def call(input)
+ information = {}
+
+ return failure(nil, error: 'No image link') if get_image_link(input[:variant], input[:product]).nil?
+
+ information['id'] = input[:variant].id
+ information['title'] = format_title(input[:product], input[:variant])
+ information['description'] = get_description(input[:product], input[:variant])
+ information['link'] = "#{input[:store].url}/#{input[:product].slug}"
+ information['image_link'] = get_image_link(input[:variant], input[:product])
+ information['price'] = format_price(input[:variant])
+ information['availability'] = get_availability(input[:product])
+ information['availability_date'] = input[:product].available_on&.xmlschema unless input[:product].available_on.nil?
+
+ success(information: information)
+ end
+
+ private
+
+ def format_title(product, variant)
+ # Title of a variant is created by joining title of a product and variant's option_values, as they are
+ # what differentiates it from other variants.
+ title = product.name
+ variant.option_values.find_each do |option_value|
+ title << " - #{option_value.name}"
+ end
+ title
+ end
+
+ def get_description(product, variant)
+ return product.description unless product.description.nil?
+
+ format_title(product, variant)
+ end
+
+ def get_image_link(variant, product)
+ # try getting image from variant
+ img = variant.images.first&.plp_url
+
+ # if no image specified for variant try getting product image
+ if img.nil?
+ img = product.images.first&.plp_url
+ end
+
+ img
+ end
+
+ def format_price(variant)
+ "#{variant.price} #{variant.cost_currency}"
+ end
+
+ def get_availability(product)
+ return 'in stock' if product.available? && product.available_on&.past?
+ return 'backorder' if product.backorderable? && product.backordered? && product.available_on&.future?
+
+ 'out of stock'
+ end
+ end
+ end
+ end
+end
diff --git a/core/app/services/spree/data_feeds/google/rss.rb b/core/app/services/spree/data_feeds/google/rss.rb
new file mode 100644
index 0000000000..059d641545
--- /dev/null
+++ b/core/app/services/spree/data_feeds/google/rss.rb
@@ -0,0 +1,107 @@
+require 'nokogiri'
+
+module Spree
+ module DataFeeds
+ module Google
+ class Rss
+ prepend Spree::ServiceModule::Base
+
+ def call(settings)
+ @settings = settings
+
+ return failure(store, error: "Store with id: #{settings.spree_store_id} does not exist.") if store.nil?
+
+ builder = Nokogiri::XML::Builder.new do |xml|
+ xml.rss('xmlns:g' => 'http://base.google.com/ns/1.0', 'version' => '2.0') do
+ xml.channel do
+ add_store_information_to_xml(xml)
+ result = products_list.call(store)
+ if result.success?
+ result.value[:products].find_each do |product|
+ product.variants.active.find_each do |variant|
+ add_variant_information_to_xml(xml, product, variant)
+ end
+ end
+ end
+ end
+ end
+ end
+
+ success(file: builder.to_xml)
+ end
+
+ private
+
+ def store
+ return @store if defined? @store
+
+ @store ||= Spree::Store.find_by(id: @settings.spree_store_id)
+ end
+
+ def add_store_information_to_xml(xml)
+ xml.title store.name
+ xml.link store.url
+ xml.description store.meta_description
+ end
+
+ def add_variant_information_to_xml(xml, product, variant)
+ input = { product: product, variant: variant, settings: @settings, store: store }
+ result = required_attributes.call(input)
+
+ if result.success
+ xml.item do
+ result.value[:information]&.each do |key, value|
+ xml['g'].send(key, value)
+ end
+
+ add_optional_information(xml, product, variant)
+ add_optional_sub_attributes(xml, product, variant)
+ end
+ end
+ end
+
+ def add_optional_information(xml, product, variant)
+ input = { product: product, variant: variant, settings: @settings, store: store }
+ result = optional_attributes.call(input)
+ if result.success?
+ information = result.value[:information]
+ information.each do |key, value|
+ xml['g'].send(key, value)
+ end
+ end
+ end
+
+ def add_optional_sub_attributes(xml, product, variant)
+ input = { product: product, variant: variant, settings: @settings, store: store }
+ result = optional_sub_attributes.call(input)
+ if result.success?
+ information = result.value[:information]
+ information.each do |key, value|
+ xml['g'].send(key) do
+ value.each do |subkey, subvalue|
+ xml['g'].send(subkey, subvalue)
+ end
+ end
+ end
+ end
+ end
+
+ def optional_attributes
+ Spree::Dependencies.data_feeds_google_optional_attributes_service.constantize.new
+ end
+
+ def required_attributes
+ Spree::Dependencies.data_feeds_google_required_attributes_service.constantize.new
+ end
+
+ def optional_sub_attributes
+ Spree::Dependencies.data_feeds_google_optional_sub_attributes_service.constantize.new
+ end
+
+ def products_list
+ Spree::Dependencies.data_feeds_google_products_list.constantize.new
+ end
+ end
+ end
+ end
+end
diff --git a/core/app/services/spree/export/google_rss.rb b/core/app/services/spree/export/google_rss.rb
deleted file mode 100644
index 6529f48110..0000000000
--- a/core/app/services/spree/export/google_rss.rb
+++ /dev/null
@@ -1,142 +0,0 @@
-require 'nokogiri'
-
-module Spree
- module Export
- class GoogleRss
- prepend Spree::ServiceModule::Base
-
- def call(settings)
- @settings = settings
-
- return failure(store, error: "Store with id: #{settings.spree_store_id} does not exist.") if store.nil?
-
- builder = Nokogiri::XML::Builder.new do |xml|
- xml.rss('xmlns:g' => 'http://base.google.com/ns/1.0', 'version' => '2.0') do
- xml.channel do
- add_store_information_to_xml(xml)
- store.products.active.each do |product|
- product.variants.active.each do |variant|
- add_variant_information_to_xml(xml, product, variant)
- end
- end
- end
- end
- end
-
- success(file: builder.to_xml)
- end
-
- private
-
- def store
- return @store if defined? @store
-
- @store ||= Spree::Store.find_by(id: @settings.spree_store_id)
- end
-
- def add_store_information_to_xml(xml)
- xml.title store.name
- xml.link store.url
- xml.description store.meta_description
- end
-
- def add_variant_information_to_xml(xml, product, variant)
- return if get_image_link(variant, product).nil?
-
- xml.item do
- xml['g'].id variant.id
- xml['g'].title format_title(product, variant)
- xml['g'].description get_description(product, variant)
- xml['g'].link "#{store.url}/#{product.slug}"
- xml['g'].image_link get_image_link(variant, product)
- xml['g'].price format_price(variant)
- xml['g'].availability get_availability(product)
- xml['g'].availability_date product.available_on&.xmlschema unless product.available_on.nil?
-
- add_optional_information(xml, product)
- end
- end
-
- def format_title(product, variant)
- # Title of a variant is created by joining title of a product and variant's option_values, as they are
- # what differentiaties it from other variants.
- title = product.name
- variant.option_values.find_each do |option_value|
- title << " - #{option_value.name}"
- end
- title
- end
-
- def get_description(product, variant)
- return product.description unless product.description.nil?
-
- format_title(product, variant)
- end
-
- def get_image_link(variant, product)
- # try getting image from variant
- img = variant.images.first&.plp_url
-
- # if no image specified for variant try getting product image
- if img.nil?
- img = product.images.first&.plp_url
- end
-
- img
- end
-
- def format_price(variant)
- "#{variant.price} #{variant.cost_currency}"
- end
-
- def get_availability(product)
- return 'in stock' if product.available_on&.past?
- return 'backorder' unless product.available_on.nil?
-
- 'out of stock'
- end
-
- def add_optional_information(xml, product)
- @settings.enabled_keys.each do |key|
- if @settings.send(key) && !product.property(key.to_s).nil?
- xml['g'].send(key, product.property(key.to_s))
- end
- end
- end
-
- # example of modifying optional information
- #
- # By default, this code assumes that any information that is not required by Google
- # (see https://support.google.com/merchants/answer/160589?hl=en) is stored in Spree::Products's properties.
- # If it's in other column you can modify add_optional_information like this:
- # def add_optional_information(xml, product)
- # @options.enabled_keys.each do |key|
- # if @options.send(key) && !product.property(key.to_s).nil?
- # xml['g'].send(key, product.property(key.to_s))
- # end
- # end
- # if !product.column_name.nil?
- # xml['g'].attribute product.column_name
- # end
- # end
- #
- # If the column is part of variant or for example is option value, you will need to add variant to function's
- # arguments and modify code analogically as above.
- #
- # def add_optional_information(xml, product, variant)
- # @options.enabled_keys.each do |key|
- # if @options.send(key) && !product.property(key.to_s).nil?
- # xml['g'].send(key, product.property(key.to_s))
- # end
- # end
- # if !variant.column_name.nil?
- # xml['g'].attribute variant.column_name
- # end
- # size_value = variant.option_value("size")
- # if !size_value.nil?
- # xml['g'].size size_value
- # end
- # end
- end
- end
-end
diff --git a/core/db/migrate/20221229132350_create_spree_data_feed_settings.rb b/core/db/migrate/20221229132350_create_spree_data_feed_settings.rb
new file mode 100644
index 0000000000..d1a4825177
--- /dev/null
+++ b/core/db/migrate/20221229132350_create_spree_data_feed_settings.rb
@@ -0,0 +1,12 @@
+class CreateSpreeDataFeedSettings < ActiveRecord::Migration[6.0]
+ def change
+ create_table :spree_data_feed_settings do |t|
+ t.references :spree_store
+
+ t.string :uuid, unique: true
+ t.boolean :enabled, default: true
+
+ t.timestamps
+ end
+ end
+end
diff --git a/core/db/migrate/20221229132350_create_spree_google_feed_settings.rb b/core/db/migrate/20221229132350_create_spree_google_feed_settings.rb
deleted file mode 100644
index 90709a247d..0000000000
--- a/core/db/migrate/20221229132350_create_spree_google_feed_settings.rb
+++ /dev/null
@@ -1,16 +0,0 @@
-class CreateSpreeGoogleFeedSettings < ActiveRecord::Migration[6.0]
- def change
- create_table :spree_google_feed_settings do |t|
- t.references :spree_store
-
- keys = [:material, :brand, :gender, :condition, :gtin, :mpn, :adult, :multipack, :is_bundle, :color, :pattern,
- :size, :item_group_id]
-
- keys.each do |key|
- t.boolean key, default: false
- end
-
- t.timestamps
- end
- end
-end
diff --git a/core/lib/spree/core/dependencies.rb b/core/lib/spree/core/dependencies.rb
index 13a000d21b..e3b707c5a4 100644
--- a/core/lib/spree/core/dependencies.rb
+++ b/core/lib/spree/core/dependencies.rb
@@ -21,7 +21,8 @@ module Spree
:order_approve_service, :order_cancel_service, :shipment_change_state_service, :shipment_update_service,
:shipment_create_service, :shipment_add_item_service, :shipment_remove_item_service,
:payment_create_service, :address_create_service, :address_update_service,
- :checkout_select_shipping_method_service, :export_google_rss_service
+ :checkout_select_shipping_method_service, :data_feeds_google_rss_service, :data_feeds_google_optional_attributes_service,
+ :data_feeds_google_required_attributes_service, :data_feeds_google_optional_sub_attributes_service, :data_feeds_google_products_list
].freeze
attr_accessor *INJECTION_POINTS
@@ -111,8 +112,12 @@ module Spree
# errors
@error_handler = 'Spree::ErrorReporter'
- # export
- @export_google_rss_service = 'Spree::Export::GoogleRss'
+ # data feeds for Google
+ @data_feeds_google_rss_service = 'Spree::DataFeeds::Google::Rss'
+ @data_feeds_google_optional_attributes_service = 'Spree::DataFeeds::Google::OptionalAttributes'
+ @data_feeds_google_required_attributes_service = 'Spree::DataFeeds::Google::RequiredAttributes'
+ @data_feeds_google_optional_sub_attributes_service = 'Spree::DataFeeds::Google::OptionalSubAttributes'
+ @data_feeds_google_products_list = 'Spree::DataFeeds::Google::ProductsList'
end
def set_default_finders
diff --git a/core/lib/spree/testing_support/factories/data_feed_setting_factory.rb b/core/lib/spree/testing_support/factories/data_feed_setting_factory.rb
new file mode 100644
index 0000000000..a0675d74d3
--- /dev/null
+++ b/core/lib/spree/testing_support/factories/data_feed_setting_factory.rb
@@ -0,0 +1,7 @@
+FactoryBot.define do
+ factory :data_feed_setting, class: Spree::DataFeedSetting do
+ id { 1 }
+ enabled { true }
+ store { create(:store) }
+ end
+end
diff --git a/core/lib/spree/testing_support/factories/google_feed_setting_factory.rb b/core/lib/spree/testing_support/factories/google_feed_setting_factory.rb
deleted file mode 100644
index e727e567d4..0000000000
--- a/core/lib/spree/testing_support/factories/google_feed_setting_factory.rb
+++ /dev/null
@@ -1,6 +0,0 @@
-FactoryBot.define do
- factory :google_feed_setting, class: Spree::GoogleFeedSetting do
- id { 1 }
- brand { true }
- end
-end
diff --git a/core/spec/models/spree/data_feed_setting_spec.rb b/core/spec/models/spree/data_feed_setting_spec.rb
new file mode 100644
index 0000000000..b998555a25
--- /dev/null
+++ b/core/spec/models/spree/data_feed_setting_spec.rb
@@ -0,0 +1,8 @@
+require 'spec_helper'
+
+describe Spree::DataFeedSetting, type: :model do
+ let(:store) { create(:store) }
+ let(:data_feed_setting) { create(:data_feed_setting, store: store) }
+
+ # TODO
+end
diff --git a/core/spec/models/spree/google_feed_setting_spec.rb b/core/spec/models/spree/google_feed_setting_spec.rb
deleted file mode 100644
index 577aad9f55..0000000000
--- a/core/spec/models/spree/google_feed_setting_spec.rb
+++ /dev/null
@@ -1,16 +0,0 @@
-require 'spec_helper'
-
-describe Spree::GoogleFeedSetting, type: :model do
- let(:store) { create(:store) }
- let(:google_feed_setting) { create(:google_feed_setting, store: store) }
-
- describe '#enabled_keys' do
- it 'returns enabled key' do
- expect(google_feed_setting.enabled_keys).to include(:brand)
- end
-
- it 'does not return not enabled key' do
- expect(google_feed_setting.enabled_keys).not_to include(:size)
- end
- end
-end
diff --git a/core/spec/services/spree/export/google_rss_spec.rb b/core/spec/services/spree/data_feeds/google/rss_spec.rb
similarity index 87%
rename from core/spec/services/spree/export/google_rss_spec.rb
rename to core/spec/services/spree/data_feeds/google/rss_spec.rb
index f6502dd7a4..45a172e89d 100644
--- a/core/spec/services/spree/export/google_rss_spec.rb
+++ b/core/spec/services/spree/data_feeds/google/rss_spec.rb
@@ -1,11 +1,11 @@
require 'spec_helper'
module Spree
- describe Export::GoogleRss do
+ describe DataFeeds::Google::Rss do
subject { described_class.new }
let(:store) { create(:store) }
- let(:setting) { create(:google_feed_setting, store: store) }
+ let(:setting) { create(:data_feed_setting, store: store) }
let(:product) { create(:product, stores: [store]) }
let!(:variant) { create(:with_image_variant, product: product) }
let(:result) { subject.call(setting) }
@@ -57,7 +57,7 @@ module Spree
expect(result.value[:file]).to include("<g:price>#{variant.price} #{variant.cost_currency}</g:price>").once
end
- context 'availability date is in the past' do
+ context 'product is set to available' do
it 'shows that product is in stock' do
expect(result.value[:file]).to include('<g:availability>in stock</g:availability>')
end
@@ -67,7 +67,7 @@ module Spree
end
end
- context 'availability date is in the future' do
+ context 'product is set to backorderable' do
let(:product) { create(:product, stores: [store], available_on: 1.year.from_now) }
it 'shows that product is on backorder' do
@@ -98,14 +98,6 @@ module Spree
it 'adds brand to item attributes' do
expect(result.value[:file]).to include("<g:brand>#{product.property('brand')}</g:brand>")
end
-
- context 'brand option is set to false' do
- let(:setting) { create(:google_feed_setting, store: store, brand: false) }
-
- it 'does not add brand to item attributes' do
- expect(result.value[:file]).not_to include('<g:brand>')
- end
- end
end
end
end
diff --git a/sample/db/samples/data_feed_settings.rb b/sample/db/samples/data_feed_settings.rb
new file mode 100644
index 0000000000..6e223ca0c0
--- /dev/null
+++ b/sample/db/samples/data_feed_settings.rb
@@ -0,0 +1,5 @@
+# settings for default store
+Spree::DataFeedSetting.create!(
+ store: Spree::Store.default
+)
+
diff --git a/sample/db/samples/google_feed_settings.rb b/sample/db/samples/google_feed_settings.rb
deleted file mode 100644
index 700788d346..0000000000
--- a/sample/db/samples/google_feed_settings.rb
+++ /dev/null
@@ -1,7 +0,0 @@
-# settings for default store
-Spree::GoogleFeedSetting.create!(
- store: Spree::Store.default,
- brand: true,
- material: true
-)
-
diff --git a/sample/lib/spree_sample.rb b/sample/lib/spree_sample.rb
index f0993697e0..c9f87743c4 100644
--- a/sample/lib/spree_sample.rb
+++ b/sample/lib/spree_sample.rb
@@ -32,7 +32,7 @@ module SpreeSample
Spree::Sample.load_sample('reimbursements')
Spree::Sample.load_sample('return_authorization_reasons')
Spree::Sample.load_sample('stores')
- Spree::Sample.load_sample('google_feed_settings')
+ Spree::Sample.load_sample('data_feed_settings')
Spree::Sample.load_sample('cms_standard_pages')
Spree::Sample.load_sample('cms_feature_pages')