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
Open source new export system (#12210)
Damian Legawiece4cb433f1c7c
diff --git a/emails/app/helpers/spree/mail_helper.rb b/core/app/helpers/spree/mail_helper.rbsimilarity index 100%rename from emails/app/helpers/spree/mail_helper.rbrename to core/app/helpers/spree/mail_helper.rbdiff --git a/core/app/jobs/spree/exports/generate_job.rb b/core/app/jobs/spree/exports/generate_job.rbnew file mode 100644index 0000000000..f50022f683--- /dev/null+++ b/core/app/jobs/spree/exports/generate_job.rb@@ -0,0 +1,10 @@+module Spree+ module Exports+ class GenerateJob < Spree::BaseJob+ def perform(export_id)+ export = Spree::Export.find(export_id)+ export.generate+ end+ end+ end+enddiff --git a/emails/app/mailers/spree/base_mailer.rb b/core/app/mailers/spree/base_mailer.rbsimilarity index 100%rename from emails/app/mailers/spree/base_mailer.rbrename to core/app/mailers/spree/base_mailer.rbdiff --git a/core/app/mailers/spree/export_mailer.rb b/core/app/mailers/spree/export_mailer.rbnew file mode 100644index 0000000000..3c76b73c9b--- /dev/null+++ b/core/app/mailers/spree/export_mailer.rb@@ -0,0 +1,19 @@+module Spree+ class ExportMailer < Spree::BaseMailer+ def export_done(export)+ @export = export++ mail(+ to: @export.user.email,+ subject: Spree.t('export_mailer.export_done.subject', export_number: @export.number).to_s,+ from: from_address,+ reply_to: reply_to_address,+ store_url: current_store.url+ )+ end++ def current_store+ @current_store ||= @export.store+ end+ end+enddiff --git a/emails/app/mailers/spree/test_mailer.rb b/core/app/mailers/spree/test_mailer.rbsimilarity index 100%rename from emails/app/mailers/spree/test_mailer.rbrename to core/app/mailers/spree/test_mailer.rbdiff --git a/core/app/models/spree/export.rb b/core/app/models/spree/export.rbnew file mode 100644index 0000000000..e04531a027--- /dev/null+++ b/core/app/models/spree/export.rb@@ -0,0 +1,176 @@+require 'csv'++module Spree+ class Export < Base+ SUPPORTED_FILE_FORMATS = %i[csv].freeze++ include Spree::SingleStoreResource+ include Spree::NumberIdentifier+ include Spree::NumberAsParam+ include Spree::VendorConcern if defined?(Spree::VendorConcern)++ include Spree::Core::NumberGenerator.new(prefix: 'EF')++ #+ # Associations+ #+ belongs_to :store, class_name: 'Spree::Store'+ belongs_to :user, class_name: Spree.admin_user_class.to_s+ belongs_to :vendor, -> { with_deleted }, class_name: 'Spree::Vendor', optional: true++ #+ # Validations+ #+ validates :format, :store, :user, :type, presence: true++ #+ # Enums+ #+ if Rails.version >= '7.0'+ enum :format, SUPPORTED_FILE_FORMATS.each_with_index.to_h+ else+ enum format: SUPPORTED_FILE_FORMATS.each_with_index.to_h+ end++ #+ # Ransack configuration+ #+ self.whitelisted_ransackable_attributes = %w[number type format vendor_id]++ #+ # Attachments+ #+ has_one_attached :attachment, service: Spree.private_storage_service_name++ #+ # Callbacks+ #+ before_validation :set_default_format, on: :create+ before_create :clear_search_params, if: -> { record_selection == 'all' }+ after_commit :generate_async, on: :create++ #+ # Virtual attributes+ #+ attribute :record_selection, :string, default: 'filtered'++ def done?+ attachment.present? && attachment.attached?+ end++ def generate_async+ Spree::Exports::GenerateJob.perform_later(id)+ end++ def generate+ send(:"generate_#{format}")+ handle_attachment+ send_export_done_email+ end++ def generate_csv+ ::CSV.open(export_tmp_file_path, 'wb', encoding: 'UTF-8', col_sep: ',', row_sep: "\r\n") do |csv|+ csv << csv_headers+ records_to_export.includes(scope_includes).find_in_batches do |batch|+ batch.each do |record|+ if multi_line_csv?+ record.to_csv(store).each do |line|+ csv << line+ end+ else+ csv << record.to_csv(store)+ end+ end+ end+ end+ end++ def multi_line_csv?+ false+ end++ def csv_headers+ raise NotImplementedError, 'csv_headers must be implemented'+ end++ def build_csv_line(_record)+ raise NotImplementedError, 'build_csv_line must be implemented'+ end++ def handle_attachment+ file = ::File.open(export_tmp_file_path)+ attachment.attach(io: file, filename: export_file_name)+ ::File.delete(export_tmp_file_path) if ::File.exist?(export_tmp_file_path)+ end++ def scope+ scope = model_class+ scope = scope.for_store(store) if model_class.respond_to?(:for_store)+ scope = scope.for_vendor(vendor) if model_class.respond_to?(:for_vendor) && vendor.present?+ scope.accessible_by(current_ability)+ end++ def records_to_export+ if search_params.present?+ scope.ransack(JSON.parse(search_params).to_h)+ else+ scope.ransack+ end.result+ end++ def scope_includes+ []+ end++ # eg. Spree::Exports::Products => Spree::Product+ def model_class+ "Spree::#{type.demodulize.singularize}".constantize+ end++ def current_ability+ @current_ability ||= Spree::Dependencies.ability_class.constantize.new(user)+ end++ # eg. Spree::Exports::Products => products-store-my-store-code-20241030133348.csv+ def export_file_name+ "#{type.demodulize.underscore}-#{store.code}-#{created_at.strftime('%Y%m%d%H%M%S')}.#{format}"+ end++ def export_tmp_file_path+ Rails.root.join('tmp', export_file_name)+ end++ def send_export_done_email+ Spree::ExportMailer.export_done(self).deliver_later+ end++ class << self+ def available_types+ Rails.application.config.spree.export_types+ end++ def available_models+ available_types.map(&:model_class)+ end++ def type_for_model(model)+ available_types.find { |type| type.model_class.to_s == model.to_s }+ end++ # eg. Spree::Exports::Products => Spree::Product+ def model_class+ "Spree::#{to_s.demodulize.singularize}".constantize+ end+ end++ private++ def set_default_format+ self.format = SUPPORTED_FILE_FORMATS.first if format.blank?+ end++ def clear_search_params+ self.search_params = nil+ end+ end+enddiff --git a/core/app/models/spree/exports/orders.rb b/core/app/models/spree/exports/orders.rbnew file mode 100644index 0000000000..8236032fa9--- /dev/null+++ b/core/app/models/spree/exports/orders.rb@@ -0,0 +1,23 @@+module Spree+ module Exports+ class Orders < Spree::Export+ def scope_includes+ [+ :payments,+ :shipments,+ { bill_address: :state },+ { ship_address: :state },+ { line_items: { variant: { product: [:taxons] } } }+ ]+ end++ def multi_line_csv?+ true+ end++ def csv_headers+ Spree::CSV::OrderLineItemPresenter::HEADERS+ end+ end+ end+enddiff --git a/core/app/models/spree/exports/products.rb b/core/app/models/spree/exports/products.rbnew file mode 100644index 0000000000..caf6d8e315--- /dev/null+++ b/core/app/models/spree/exports/products.rb@@ -0,0 +1,35 @@+module Spree+ module Exports+ class Products < Spree::Export+ # to avoid N+1 queries+ def scope_includes+ [:tax_category, :master, { taxons: :taxonomy }, { product_properties: [:property] }, { variants_including_master: variant_includes }]+ end++ def variant_includes+ [:images, :prices, :stock_items, { option_values: [:option_type] }]+ end++ def multi_line_csv?+ true+ end++ # when doing full product export, we want to exclude archived products+ def scope+ if search_params.nil?+ super.where.not(status: 'archived')+ else+ super+ end+ end++ def csv_headers+ @csv_headers ||= Spree::CSV::ProductVariantPresenter::CSV_HEADERS + properties_headers+ end++ def properties_headers+ @properties_headers ||= Spree::Property.order(:position).count.times.flat_map { |n| ["property#{n + 1}_name", "property#{n + 1}_value"] }+ end+ end+ end+enddiff --git a/core/app/models/spree/legacy_user.rb b/core/app/models/spree/legacy_user.rbindex 5030b9bb9d..54f08a9500 100644--- a/core/app/models/spree/legacy_user.rb+++ b/core/app/models/spree/legacy_user.rb@@ -8,6 +8,6 @@ module Spreeself.table_name = 'spree_users'- attr_accessor :password, :password_confirmation+ attr_accessor :password, :password_confirmation, :firstname, :lastnameendenddiff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rbindex 02187d7ed8..19074d3433 100644--- a/core/app/models/spree/order.rb+++ b/core/app/models/spree/order.rb@@ -703,6 +703,14 @@ module Spreespree_promotion_actions: { type: 'Spree::Promotion::Actions::FreeShipping' }).exists?end+ def to_csv(_store = nil)+ csv_lines = []+ all_line_items.each_with_index do |line_item, index|+ csv_lines << Spree::CSV::OrderLineItemPresenter.new(self, line_item, index).call+ end+ csv_lines+ end+privatedef link_by_emaildiff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rbindex 6f938141de..9f4bfc0492 100644--- a/core/app/models/spree/product.rb+++ b/core/app/models/spree/product.rb@@ -459,6 +459,24 @@ module Spreeshipping_category&.shipping_methods&.any? { |method| method.calculator.is_a?(Spree::Calculator::Shipping::DigitalDelivery) }end+ def to_csv(store = nil)+ store ||= stores.default || stores.first+ properties_for_csv ||= Spree::Property.order(:position).flat_map do |property|+ [+ property.name,+ product_properties.find { |pp| pp.property_id == property.id }&.value+ ]+ end+ taxons_for_csv ||= taxons.reorder(depth: :desc).first(3).pluck(:pretty_name)+ csv_lines = []+ variants_including_master.each_with_index do |variant, index|+ next if variant.is_master? && has_variants?++ csv_lines << Spree::CSV::ProductVariantPresenter.new(self, variant, index, properties_for_csv, taxons_for_csv, store).call+ end+ csv_lines+ end+privatedef add_associations_from_prototypediff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rbindex 0d246b4810..bdec90ca3d 100644--- a/core/app/models/spree/variant.rb+++ b/core/app/models/spree/variant.rb@@ -390,6 +390,12 @@ module Spree(width || 0) + (height || 0) + (depth || 0)end+ # Returns the weight unit for the variant+ # @return [String]+ def weight_unit+ attributes['weight_unit'] || Spree::Store.default.weight_unit+ end+def discontinue!update_attribute(:discontinue_on, Time.current)enddiff --git a/core/app/presenters/spree/csv/order_line_item_presenter.rb b/core/app/presenters/spree/csv/order_line_item_presenter.rbnew file mode 100644index 0000000000..4361339e55--- /dev/null+++ b/core/app/presenters/spree/csv/order_line_item_presenter.rb@@ -0,0 +1,147 @@+module Spree+ module CSV+ class OrderLineItemPresenter+ include Spree::Admin::PaymentsHelper++ HEADERS = [+ 'Number',+ 'Email',+ 'Status',+ 'Currency',+ 'Subtotal',+ 'Shipping',+ 'Taxes',+ 'Taxes included',+ 'Discount Used',+ 'Free Shipping',+ 'Discount',+ 'Discount Code',+ 'Store Credit amount',+ 'Total',+ 'Shipping method',+ 'Total weight',+ 'Payment Type Used',+ 'Product ID',+ 'Item Quantity',+ 'Item SKU',+ 'Item Name',+ 'Item Price',+ 'Item Total Discount',+ 'Item Total Price',+ 'Item requires shipping',+ 'Item taxbale',+ 'Item Vendor',+ 'Category lvl0',+ 'Category lvl1',+ 'Category lvl2',+ 'Category lvl3',+ 'Category lvl4',+ 'Billing Name',+ 'Billing Address 1',+ 'Billing Address 2',+ 'Billing Company',+ 'Billing City',+ 'Billing Zip',+ 'Billing State',+ 'Billing Country',+ 'Billing Phone',+ 'Shipping Name',+ 'Shipping Address 1',+ 'Shipping Address 2',+ 'Shipping Company',+ 'Shipping City',+ 'Shipping Zip',+ 'Shipping State',+ 'Shipping Country',+ 'Shipping Phone',+ 'Placed at',+ 'Shipped at',+ 'Cancelled at',+ 'Cancelled by',+ 'Notes'+ ].freeze++ def initialize(order, line_item, index)+ @order = order+ @line_item = line_item+ @index = index+ end++ attr_accessor :order, :line_item, :index++ def call+ [+ order.number,+ index.zero? ? order.email : nil,+ index.zero? ? order.state : nil,+ index.zero? ? order.currency : nil,+ index.zero? ? order.item_total.to_f : nil,+ index.zero? ? order.shipment_total.to_f : nil,+ index.zero? ? order.tax_total.to_f : nil,+ index.zero? ? order.included_tax_total.positive? : nil,+ index.zero? ? (order.promo_total.negative? || line_item.promo_total.negative?) : nil,+ index.zero? ? order.has_free_shipping? : nil,+ index.zero? ? order.promo_total.abs : nil,+ index.zero? ? order.promo_code : nil,+ index.zero? ? order.payments.store_credits.sum(:amount).abs : nil,+ index.zero? ? order.total.to_f : nil,+ index.zero? ? order.shipping_method&.name : nil,+ index.zero? ? order.total_weight.to_f : nil,+ index.zero? && order.payments.valid.any? ? payment_source_name(order.payments.valid.first) : nil,+ line_item.product_id,+ line_item.quantity,+ line_item.sku,+ line_item.name,+ line_item.price,+ line_item.promo_total.abs,+ line_item.total,+ !line_item.product.digital?,+ line_item.product.tax_category.present?,+ line_item.product.try(:vendor_name),+ taxon_dict(line_item.product.taxons.first)[0],+ taxon_dict(line_item.product.taxons.first)[1],+ taxon_dict(line_item.product.taxons.first)[2],+ taxon_dict(line_item.product.taxons.first)[3],+ taxon_dict(line_item.product.taxons.first)[4],+ index.zero? ? order.bill_address&.full_name : nil,+ index.zero? ? order.bill_address&.address1 : nil,+ index.zero? ? order.bill_address&.address2 : nil,+ index.zero? ? order.bill_address&.company : nil,+ index.zero? ? order.bill_address&.city : nil,+ index.zero? ? order.bill_address&.zipcode : nil,+ index.zero? ? order.bill_address&.state_name : nil,+ index.zero? ? order.bill_address&.country&.name : nil,+ index.zero? ? order.bill_address&.phone : nil,+ index.zero? ? order.ship_address&.full_name : nil,+ index.zero? ? order.ship_address&.address1 : nil,+ index.zero? ? order.ship_address&.address2 : nil,+ index.zero? ? order.ship_address&.company : nil,+ index.zero? ? order.ship_address&.city : nil,+ index.zero? ? order.ship_address&.zipcode : nil,+ index.zero? ? order.ship_address&.state_name : nil,+ index.zero? ? order.ship_address&.country&.name : nil,+ index.zero? ? order.ship_address&.phone : nil,+ index.zero? ? format_date(order.completed_at) : nil,+ format_date(line_item.order.shipments.first&.shipped_at),+ index.zero? ? format_date(order.canceled_at) : nil,+ index.zero? ? order.canceler&.email : nil,+ index.zero? ? order.special_instructions : nil+ ]+ end++ private++ def taxon_dict(taxon)+ return [] if taxon.blank?++ taxon.pretty_name.to_s.split('->').map(&:strip)+ end++ def format_date(date)+ return nil if date.blank?++ date.in_time_zone(order.store.timezone).strftime('%Y-%m-%d %H:%M:%S')+ end+ end+ end+enddiff --git a/core/app/presenters/spree/csv/product_variant_presenter.rb b/core/app/presenters/spree/csv/product_variant_presenter.rbnew file mode 100644index 0000000000..356b523d8f--- /dev/null+++ b/core/app/presenters/spree/csv/product_variant_presenter.rb@@ -0,0 +1,115 @@+module Spree+ module CSV+ class ProductVariantPresenter+ CSV_HEADERS = [+ 'product_id',+ 'name',+ 'slug',+ 'status',+ 'vendor_name',+ 'brand_name',+ 'description',+ 'meta_title',+ 'meta_description',+ 'meta_keywords',+ 'tags',+ 'labels',+ 'variant_id',+ 'variant_sku',+ 'price',+ 'compare_at_price',+ 'currency',+ 'width',+ 'height',+ 'depth',+ 'weight',+ 'available_on',+ 'discontinue_on',+ 'inventory_count',+ 'inventory_backorderable',+ 'tax_category',+ 'digital',+ 'image1_src',+ 'image2_src',+ 'image3_src',+ 'option1_name',+ 'option1_value',+ 'option2_name',+ 'option2_value',+ 'option3_name',+ 'option3_value',+ 'category1',+ 'category2',+ 'category3',+ ].freeze++ def initialize(product, variant, index = 0, properties = [], taxons = [], store = nil)+ @product = product+ @variant = variant+ @index = index+ @properties = properties+ @taxons = taxons+ @store = store || product.stores.first+ @currency = @store.default_currency+ end++ attr_accessor :product, :variant, :index, :properties, :taxons, :store, :currency++ def call+ csv = [+ product.id,+ index.zero? ? product.name : nil,+ index.zero? ? product.slug : nil,+ index.zero? ? product.status : nil,+ index.zero? ? product.try(:vendor_name) : nil,+ index.zero? ? product.try(:brand_name) : nil,+ index.zero? ? product.description&.html_safe : nil,+ index.zero? ? product.meta_title : nil,+ index.zero? ? product.meta_description : nil,+ index.zero? ? product.meta_keywords : nil,+ index.zero? ? product.tag_list.to_s : nil,+ index.zero? ? product.label_list.to_s : nil,+ variant.id,+ variant.sku,+ variant.amount_in(currency).to_f,+ variant.compare_at_price&.to_f,+ currency,+ variant.width,+ variant.height,+ variant.depth,+ variant.weight,+ variant.available_on&.strftime('%Y-%m-%d %H:%M:%S'),+ variant.discontinue_on&.strftime('%Y-%m-%d %H:%M:%S'),+ variant.total_on_hand == BigDecimal::INFINITY ? '∞' : variant.total_on_hand,+ variant.backorderable?,+ variant.tax_category&.name,+ variant.digital?,+ variant.images[0]&.attachment&.url,+ variant.images[1]&.attachment&.url,+ variant.images[2]&.attachment&.url,+ option_type(0)&.name,+ option_value(option_type(0)),+ option_type(1)&.name,+ option_value(option_type(1)),+ option_type(2)&.name,+ option_value(option_type(2))+ ]++ if index.zero?+ csv += taxons+ csv += properties+ end++ csv+ end++ def option_type(index)+ product.option_types[index]+ end++ def option_value(option_type)+ variant.option_values.find { |ov| ov.option_type == option_type }&.name+ end+ end+ end+enddiff --git a/emails/app/views/layouts/spree/base_mailer.html.erb b/core/app/views/layouts/spree/base_mailer.html.erbsimilarity index 100%rename from emails/app/views/layouts/spree/base_mailer.html.erbrename to core/app/views/layouts/spree/base_mailer.html.erbdiff --git a/core/app/views/spree/export_mailer/export_done.html.erb b/core/app/views/spree/export_mailer/export_done.html.erbnew file mode 100644index 0000000000..8aa6bea4bd--- /dev/null+++ b/core/app/views/spree/export_mailer/export_done.html.erb@@ -0,0 +1,15 @@+<h1>+ <%= Spree.t('export_mailer.greeting', user_name: @export.user&.firstname) %>+</h1>+<p>+ <%= Spree.t('export_mailer.export_done.message') %>+</p>+<% if spree.respond_to?(:admin_export_url) %>+ <p>+ <%= link_to @export.attachment.filename, spree.admin_export_url(@export) %>+ </p>+<% end %>+<p>+ <%= Spree.t('export_mailer.thanks') %><br />+ <%= "#{current_store.name}" %>+</p>diff --git a/emails/app/views/spree/shared/_base_mailer_footer.html.erb b/core/app/views/spree/shared/_base_mailer_footer.html.erbsimilarity index 100%rename from emails/app/views/spree/shared/_base_mailer_footer.html.erbrename to core/app/views/spree/shared/_base_mailer_footer.html.erbdiff --git a/emails/app/views/spree/shared/_base_mailer_header.html.erb b/core/app/views/spree/shared/_base_mailer_header.html.erbsimilarity index 100%rename from emails/app/views/spree/shared/_base_mailer_header.html.erbrename to core/app/views/spree/shared/_base_mailer_header.html.erbdiff --git a/emails/app/views/spree/shared/_base_mailer_stylesheets.html.erb b/core/app/views/spree/shared/_base_mailer_stylesheets.html.erbsimilarity index 100%rename from emails/app/views/spree/shared/_base_mailer_stylesheets.html.erbrename to core/app/views/spree/shared/_base_mailer_stylesheets.html.erbdiff --git a/emails/app/views/spree/shared/_mailer_line_item.html.erb b/core/app/views/spree/shared/_mailer_line_item.html.erbsimilarity index 100%rename from emails/app/views/spree/shared/_mailer_line_item.html.erbrename to core/app/views/spree/shared/_mailer_line_item.html.erbdiff --git a/emails/app/views/spree/test_mailer/test_email.html.erb b/core/app/views/spree/test_mailer/test_email.html.erbsimilarity index 100%rename from emails/app/views/spree/test_mailer/test_email.html.erbrename to core/app/views/spree/test_mailer/test_email.html.erbdiff --git a/emails/app/views/spree/test_mailer/test_email.text.erb b/core/app/views/spree/test_mailer/test_email.text.erbsimilarity index 100%rename from emails/app/views/spree/test_mailer/test_email.text.erbrename to core/app/views/spree/test_mailer/test_email.text.erbdiff --git a/core/config/locales/en.yml b/core/config/locales/en.ymlindex b29b399712..6f04623c31 100644--- a/core/config/locales/en.yml+++ b/core/config/locales/en.yml@@ -855,6 +855,13 @@ en:excl: excl.expiration: Expirationexpiration_date: Expiration Date+ export: Export+ export_mailer:+ greeting: Hi %{user_name},+ thanks: Thank you,+ export_done:+ subject: 'Your export %{export_number} was successfully processed!'+ message: Good work, your export is ready! Please use the link below to download it.extension: Extensionextensions_directory: Extensions Directoryexisting_shipments: Existing shipmentsdiff --git a/core/db/migrate/20241030134309_create_spree_exports.rb b/core/db/migrate/20241030134309_create_spree_exports.rbnew file mode 100644index 0000000000..f60f4bb696--- /dev/null+++ b/core/db/migrate/20241030134309_create_spree_exports.rb@@ -0,0 +1,23 @@+class CreateSpreeExports < ActiveRecord::Migration[7.2]+ def change+ return if table_exists?(:spree_exports)++ create_table :spree_exports do |t|+ t.references :user+ t.references :store, null: false++ t.string :number, limit: 32, null: false, index: { unique: true }+ t.string :type, null: false++ if t.respond_to? :jsonb+ t.jsonb :search_params+ else+ t.json :search_params+ end++ t.integer :format, index: true, null: false++ t.timestamps+ end+ end+enddiff --git a/core/lib/spree/core.rb b/core/lib/spree/core.rbindex 59ef33a828..b28817c62b 100644--- a/core/lib/spree/core.rb+++ b/core/lib/spree/core.rb@@ -6,6 +6,10 @@ require 'active_record/railtie'require 'active_storage/engine'require 'action_text/engine'+require 'mail'+require 'action_mailer/railtie'+require 'sprockets/railtie'+require 'active_merchant'require 'acts_as_list'require 'acts-as-taggable-on'diff --git a/core/lib/spree/core/engine.rb b/core/lib/spree/core/engine.rbindex 531d2ba40b..fdac635ae7 100644--- a/core/lib/spree/core/engine.rb+++ b/core/lib/spree/core/engine.rb@@ -132,7 +132,10 @@ module SpreeSpree::DataFeed::Google]- Rails.application.config.spree.export_types = []+ Rails.application.config.spree.export_types = [+ Spree::Exports::Products,+ Spree::Exports::Orders+ ]endinitializer 'spree.promo.register.promotions.actions' do |app|diff --git a/core/lib/spree/testing_support/factories/export_factory.rb b/core/lib/spree/testing_support/factories/export_factory.rbnew file mode 100644index 0000000000..2932410c05--- /dev/null+++ b/core/lib/spree/testing_support/factories/export_factory.rb@@ -0,0 +1,32 @@+# == Schema Information+#+# Table name: spree_exports+#+# id :uuid not null, primary key+# format :integer not null+# number :string(32) not null+# search_params :jsonb+# type :string not null+# created_at :datetime not null+# updated_at :datetime not null+# project_id :uuid not null+# store_id :uuid not null+# user_id :uuid+# vendor_id :uuid+#+FactoryBot.define do+ factory :export, class: 'Spree::Export' do+ store { create(:store) }+ user { create(:admin_user) }+ type { 'Spree::Export::Products' }+ format { 'csv' }++ factory :product_export, class: 'Spree::Exports::Products', parent: :export do+ type { 'Spree::Exports::Products' }+ end++ factory :order_export, class: 'Spree::Exports::Orders', parent: :export do+ type { 'Spree::Exports::Orders' }+ end+ end+enddiff --git a/core/spec/jobs/spree/exports/generate_job_spec.rb b/core/spec/jobs/spree/exports/generate_job_spec.rbnew file mode 100644index 0000000000..b9070ed5e5--- /dev/null+++ b/core/spec/jobs/spree/exports/generate_job_spec.rb@@ -0,0 +1,16 @@+require 'spec_helper'++RSpec.describe Spree::Exports::GenerateJob, type: :job do+ let(:store) { create(:store) }+ let(:user) { create(:admin_user) }+ let!(:export) { create(:product_export, store: store, user: user, format: 'csv') }++ describe '#perform' do+ subject(:perform_job) { described_class.perform_now(export.id) }++ it 'calls generate on the export' do+ expect_any_instance_of(Spree::Export).to receive(:generate)+ perform_job+ end+ end+enddiff --git a/core/spec/mailers/spree/export_mailer_spec.rb b/core/spec/mailers/spree/export_mailer_spec.rbnew file mode 100644index 0000000000..5b336d046e--- /dev/null+++ b/core/spec/mailers/spree/export_mailer_spec.rb@@ -0,0 +1,36 @@+require 'spec_helper'++RSpec.describe Spree::ExportMailer, type: :mailer do+ let(:store) { create(:store) }+ let(:user) { create(:admin_user) }+ let(:export) { create(:product_export, store: store, user: user) }+ let(:spree) { Spree::Core::Engine.routes.url_helpers }++ before do+ export.generate+ export.reload+ allow(spree).to receive(:admin_export_url).and_return('http://test.com/admin/exports/1')+ end++ describe '#export_done' do+ subject(:mail) { described_class.export_done(export) }++ it 'renders the subject' do+ expect(mail.subject).to eq(+ Spree.t('export_mailer.export_done.subject', export_number: export.number)+ )+ end++ it 'sends to the correct recipient' do+ expect(mail.to).to eq([user.email])+ end++ it 'includes export attachment filename' do+ expect(mail.body.encoded).to include(export.attachment.filename.to_s)+ end++ it 'includes download link in the body' do+ expect(mail.body.encoded).to include("/admin/exports/#{export.id}")+ end+ end+enddiff --git a/core/spec/models/spree/export_spec.rb b/core/spec/models/spree/export_spec.rbnew file mode 100644index 0000000000..79a5805e73--- /dev/null+++ b/core/spec/models/spree/export_spec.rb@@ -0,0 +1,73 @@+require 'spec_helper'++RSpec.describe Spree::Export, type: :model, job: true do+ let(:store) { create(:store, code: 'my-store') }+ let(:user) { create(:admin_user) }++ let(:search_params) { nil }+ let(:export) { build(:product_export, store: store, user: user, format: 'csv', search_params: search_params) }++ context 'Callbacks' do+ describe 'after_create' do+ it 'generates the export' do+ expect { export.save! }.to have_enqueued_job(Spree::Exports::GenerateJob).with(export.id)+ end+ end+ end++ describe '#model_class' do+ it 'returns the correct record class' do+ expect(export.model_class).to eq(Spree::Product)+ end+ end++ describe '#export_file_name' do+ before { export.save! }++ it 'returns the correct file name' do+ expect(export.export_file_name).to match(/products-my-store-\d{8}\d{6}\.csv/)+ end+ end++ describe '#generate' do+ before { export.save! }++ it 'generates the export' do+ expect { export.generate }.to change(export.attachment, :attached?).from(false).to(true)++ expect(export.attachment.content_type).to eq('text/csv')+ end++ it 'sends the export done email' do+ expect { export.generate }.to have_enqueued_job(ActionMailer::MailDeliveryJob)+ end+ end++ describe '#records_to_export' do+ let!(:matching_products) { create_list(:product, 3, name: 'test', stores: [store]) }+ let!(:non_matching_products) { create_list(:product, 3, name: 'something else', stores: [store]) }++ context 'without search params' do+ it 'returns all products' do+ expect(export.records_to_export).to match_array(store.products)+ end+ end++ context 'with search params' do+ let(:search_params) { { name_cont: 'test' }.to_json }++ it 'returns matching products' do+ expect(export.records_to_export).to match_array(matching_products)+ expect(export.records_to_export.count).to eq(matching_products.count)+ end+ end+ end++ describe '#send_export_done_email' do+ before { export.save! }++ it 'queues the export done email' do+ expect { export.send_export_done_email }.to have_enqueued_job(ActionMailer::MailDeliveryJob)+ end+ end+enddiff --git a/core/spec/presenters/spree/csv/order_line_items_presenter_spec.rb b/core/spec/presenters/spree/csv/order_line_items_presenter_spec.rbnew file mode 100644index 0000000000..efd955e532--- /dev/null+++ b/core/spec/presenters/spree/csv/order_line_items_presenter_spec.rb@@ -0,0 +1,80 @@+require 'spec_helper'++RSpec.describe Spree::CSV::OrderLineItemPresenter do+ let(:store) { create(:store) }+ let(:order) { create(:completed_order_with_totals, store: store) }+ let(:line_item) { order.line_items.first }+ let(:index) { 0 }+ let(:presenter) { described_class.new(order, line_item, index) }++ describe '#call' do+ subject { presenter.call }++ it 'returns array with correct values' do+ expect(subject[0]).to eq order.number+ expect(subject[1]).to eq order.email+ expect(subject[2]).to eq order.state+ expect(subject[3]).to eq order.currency+ expect(subject[4]).to eq order.item_total.to_f+ expect(subject[5]).to eq order.shipment_total.to_f+ expect(subject[6]).to eq order.tax_total.to_f+ expect(subject[7]).to eq order.included_tax_total.positive?+ expect(subject[8]).to eq(order.promo_total.negative? || line_item.promo_total.negative?)+ expect(subject[9]).to eq order.has_free_shipping?+ expect(subject[10]).to eq order.promo_total.abs+ expect(subject[11]).to eq order.promo_code+ expect(subject[12]).to eq order.payments.store_credits.sum(:amount).abs+ expect(subject[13]).to eq order.total.to_f+ expect(subject[17]).to eq line_item.product_id+ expect(subject[18]).to eq line_item.quantity+ expect(subject[19]).to eq line_item.sku+ expect(subject[20]).to eq line_item.name+ expect(subject[21]).to eq line_item.price+ expect(subject[22]).to eq line_item.promo_total.abs+ expect(subject[23]).to eq line_item.total+ end++ context 'when index is not zero' do+ let(:index) { 1 }++ it 'returns nil for order-level fields' do+ expect(subject[1]).to be_nil # email+ expect(subject[2]).to be_nil # state+ expect(subject[3]).to be_nil # currency+ expect(subject[4]).to be_nil # item_total+ end++ it 'returns line item specific fields' do+ expect(subject[17]).to eq line_item.product_id+ expect(subject[18]).to eq line_item.quantity+ expect(subject[19]).to eq line_item.sku+ end+ end+ end++ describe '#taxon_dict' do+ let(:taxon) { build(:taxon, pretty_name: 'Category -> Subcategory -> Product') }++ it 'splits taxon pretty name into array' do+ expect(presenter.send(:taxon_dict, taxon)).to eq(['Category', 'Subcategory', 'Product'])+ end++ it 'returns empty array for nil taxon' do+ expect(presenter.send(:taxon_dict, nil)).to eq([])+ end+ end++ describe '#format_date' do+ let(:date) { Time.current }++ it 'formats date according to store timezone' do+ expect(presenter.send(:format_date, date)).to eq(+ date.in_time_zone(order.store.timezone).strftime('%Y-%m-%d %H:%M:%S')+ )+ end++ it 'returns nil for blank date' do+ expect(presenter.send(:format_date, nil)).to be_nil+ end+ end+enddiff --git a/core/spec/presenters/spree/csv/product_presenter_spec.rb b/core/spec/presenters/spree/csv/product_presenter_spec.rbdeleted file mode 100644index 6239792872..0000000000--- a/core/spec/presenters/spree/csv/product_presenter_spec.rb+++ /dev/null@@ -1,102 +0,0 @@-require 'spec_helper'--describe Spree::CSV::ProductPresenter do- describe '#call' do- subject(:presentation) { described_class.new(product, properties).call }-- let(:store) { Spree::Store.default }- let(:brand_taxonomy) { store.taxonomies.find_by(name: Spree.t(:taxonomy_brands_name)) || create(:taxonomy, name: Spree.t(:taxonomy_brands_name), store: store) }- let(:brand) { create(:taxon, taxonomy: brand_taxonomy, name: 'Super seller') }- let(:property) { create(:property, :material) }- let(:product) do- create(:product,- product_properties: [create(:product_property, property: property, value: 'Cotton')],- taxons: [brand])- end-- let(:properties) { [property] }-- before(:each) do- product.update(available_on: Time.current, weight: 77, width: 777)- product.label_list.add('label1', 'label2')- product.tag_list.add('tag1', 'tag2')- product.save!- end-- it 'presents product with properties' do- product.reload-- expect(presentation.length).to eq(24)-- expect(presentation[0]).to eq(product.id)- expect(presentation[2]).to eq(product.brand.name)- expect(presentation[3]).to eq(product.name)- expect(presentation[4]).to eq(product.description)- expect(presentation[5]).to eq(product.price.to_f)- expect(presentation[6]).to eq(product.meta_title)- expect(presentation[7]).to eq(product.meta_description)- expect(presentation[8]).to eq(product.meta_keywords)- expect(presentation[9].to_s).to eq(product.tag_list.to_s)- expect(presentation[10].to_s).to eq(product.label_list.to_s)- expect(presentation[11]).to eq(product.width)- expect(presentation[12]).to eq(product.height)- expect(presentation[13]).to eq(product.depth)- expect(presentation[14]).to eq(product.weight)- expect(presentation[15]).to eq(product.available_on.strftime('%Y-%m-%d %H:%M:%S'))- expect(presentation[16]).to eq(product.discontinue_on)- expect(presentation[17]).to eq(product.status)- expect(presentation[18]).to eq('Brands -> Super seller')- expect(presentation[19]).to eq(nil)- expect(presentation[20]).to eq(nil)- expect(presentation[21]).to eq(0)- expect(presentation[22]).to eq(property.name)- expect(presentation[23]).to eq(product.property(property.name))- end-- context 'when providing more properties' do- let(:property_2) { create(:property, :manufacturer) }- let(:property_3) { create(:property, :brand) }-- let(:properties) { [property, property_2, property_3] }-- before do- product.product_properties << create(:product_property, property: property_2, value: 'Global manufacturer')- product.product_properties << create(:product_property, property: property_3, value: 'Global brand')- end-- it 'presents product with properties' do- product.reload-- expect(presentation.length).to eq(28)-- expect(presentation[0]).to eq(product.id)- expect(presentation[2]).to eq(product.brand.name)- expect(presentation[3]).to eq(product.name)- expect(presentation[4]).to eq(product.description)- expect(presentation[5]).to eq(product.price.to_f)- expect(presentation[6]).to eq(product.meta_title)- expect(presentation[7]).to eq(product.meta_description)- expect(presentation[8]).to eq(product.meta_keywords)- expect(presentation[9].to_s).to eq(product.tag_list.to_s)- expect(presentation[10].to_s).to eq(product.label_list.to_s)- expect(presentation[11]).to eq(product.width)- expect(presentation[12]).to eq(product.height)- expect(presentation[13]).to eq(product.depth)- expect(presentation[14]).to eq(product.weight)- expect(presentation[15]).to eq(product.available_on.strftime('%Y-%m-%d %H:%M:%S'))- expect(presentation[16]).to eq(product.discontinue_on)- expect(presentation[17]).to eq(product.status)- expect(presentation[18]).to eq('Brands -> Super seller')- expect(presentation[19]).to eq(nil)- expect(presentation[20]).to eq(nil)- expect(presentation[21]).to eq(0)- expect(presentation[22]).to eq('material')- expect(presentation[23]).to eq('Cotton')- expect(presentation[24]).to eq('manufacturer')- expect(presentation[25]).to eq('Global manufacturer')- expect(presentation[26]).to eq('brand')- expect(presentation[27]).to eq('Global brand')- end- end- end-enddiff --git a/core/spec/presenters/spree/csv/product_variant_presenter_spec.rb b/core/spec/presenters/spree/csv/product_variant_presenter_spec.rbnew file mode 100644index 0000000000..4ac5acb59e--- /dev/null+++ b/core/spec/presenters/spree/csv/product_variant_presenter_spec.rb@@ -0,0 +1,96 @@+require 'spec_helper'++RSpec.describe Spree::CSV::ProductVariantPresenter do+ let(:store) { create(:store) }+ let(:product) { create(:product, stores: [store]) }+ let(:variant) { product.master }+ let(:properties) { [] }+ let(:taxons) { [] }+ let(:presenter) { described_class.new(product, variant, 0, properties, taxons, store) }++ describe '#call' do+ subject { presenter.call }++ it 'returns array with correct values' do+ expect(subject[0]).to eq product.id+ expect(subject[1]).to eq product.name+ expect(subject[2]).to eq product.slug+ expect(subject[3]).to eq product.status+ expect(subject[4]).to eq product.try(:vendor_name)+ expect(subject[5]).to eq product.brand_name+ expect(subject[6]).to eq product.description&.html_safe+ expect(subject[7]).to eq product.meta_title+ expect(subject[8]).to eq product.meta_description+ expect(subject[9]).to eq product.meta_keywords+ expect(subject[10]).to eq product.tag_list.to_s+ expect(subject[11]).to eq product.label_list.to_s+ expect(subject[12]).to eq variant.id+ expect(subject[13]).to eq variant.sku+ expect(subject[14]).to eq variant.amount_in(store.default_currency).to_f+ expect(subject[15]).to eq variant.compare_at_price&.to_f+ expect(subject[16]).to eq store.default_currency+ expect(subject[17]).to eq variant.width+ expect(subject[18]).to eq variant.height+ expect(subject[19]).to eq variant.depth+ expect(subject[20]).to eq variant.weight+ expect(subject[21]).to eq variant.available_on&.strftime('%Y-%m-%d %H:%M:%S')+ expect(subject[22]).to eq variant.discontinue_on&.strftime('%Y-%m-%d %H:%M:%S')+ expect(subject[23]).to eq(variant.total_on_hand == BigDecimal::INFINITY ? '∞' : variant.total_on_hand)+ expect(subject[24]).to eq variant.backorderable?+ expect(subject[25]).to eq variant.tax_category&.name+ expect(subject[26]).to eq variant.digital?+ expect(subject[27]).to eq variant.images[0]&.attachment&.url+ expect(subject[28]).to eq variant.images[1]&.attachment&.url+ expect(subject[29]).to eq variant.images[2]&.attachment&.url+ end++ context 'when index is not zero' do+ let(:variant) { create(:variant, product: product) }+ let(:presenter) { described_class.new(product, variant, 1, properties, taxons, store) }++ it 'returns nil for product-level fields' do+ expect(subject[1]).to be_nil # name+ expect(subject[2]).to be_nil # slug+ expect(subject[3]).to be_nil # status+ expect(subject[4]).to be_nil # vendor_name+ end++ it 'returns variant specific fields' do+ expect(subject[12]).to eq variant.id+ expect(subject[13]).to eq variant.sku+ expect(subject[14]).to eq variant.amount_in(store.default_currency).to_f+ end+ end+ end++ describe '#option_type' do+ let(:option_type) { create(:option_type) }+ before { product.option_types << option_type }++ it 'returns option type at given index' do+ expect(presenter.option_type(0)).to eq option_type+ end++ it 'returns nil for non-existent index' do+ expect(presenter.option_type(99)).to be_nil+ end+ end++ describe '#option_value' do+ let(:option_type) { create(:option_type) }+ let(:option_value) { create(:option_value, option_type: option_type) }++ before do+ product.option_types << option_type+ variant.option_values << option_value+ end++ it 'returns option value name for given option type' do+ expect(presenter.option_value(option_type)).to eq option_value.name+ end++ it 'returns nil for option type without value' do+ expect(presenter.option_value(create(:option_type))).to be_nil+ end+ end+enddiff --git a/emails/lib/spree/emails.rb b/emails/lib/spree/emails.rbindex aba94c5952..d3a4ab75b6 100644--- a/emails/lib/spree/emails.rb+++ b/emails/lib/spree/emails.rb@@ -1,7 +1,3 @@-require 'mail'-require 'action_mailer/railtie'-require 'action_view/railtie'-require 'sprockets/railtie'require 'spree/core'module Spree