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.

Remove deprecated Spree::Core::Importer (#13691)

Damian Legawiec42ba6829444d
diff --git a/server/Gemfile.lock b/server/Gemfile.lock
index 2b23ea66d2..6a73252c42 100644
--- a/server/Gemfile.lock
+++ b/server/Gemfile.lock
@@ -11,7 +11,7 @@ GIT
PATH
remote: ../spree/admin
specs:
- spree_admin (5.4.0.beta)
+ spree_admin (5.4.0.beta2)
active_link_to
breadcrumbs_on_rails (~> 4.1)
chartkick (~> 5.0)
@@ -23,7 +23,7 @@ PATH
mapkick-rb (~> 0.1)
pagy (~> 43.0)
ruby-oembed (~> 0.18)
- spree (>= 5.4.0.beta)
+ spree (>= 5.4.0.beta2)
stimulus-rails
tailwindcss-rails (>= 4.0)
tailwindcss-ruby (>= 4.0)
@@ -33,22 +33,22 @@ PATH
PATH
remote: ../spree/emails
specs:
- spree_emails (5.4.0.beta)
- spree (>= 5.4.0.beta)
+ spree_emails (5.4.0.beta2)
+ spree (>= 5.4.0.beta2)
PATH
remote: ../spree
specs:
- spree (5.4.0.beta)
- spree_api (= 5.4.0.beta)
- spree_core (= 5.4.0.beta)
- spree_api (5.4.0.beta)
+ spree (5.4.0.beta2)
+ spree_api (= 5.4.0.beta2)
+ spree_core (= 5.4.0.beta2)
+ spree_api (5.4.0.beta2)
alba (~> 3.0)
oj (~> 3.16)
pagy (~> 43.0)
- spree_core (= 5.4.0.beta)
+ spree_core (= 5.4.0.beta2)
typelizer (~> 0.9)
- spree_core (5.4.0.beta)
+ spree_core (5.4.0.beta2)
active_storage_validations (= 1.3.0)
acts-as-taggable-on
acts_as_list (>= 0.8)
@@ -1089,12 +1089,12 @@ CHECKSUMS
solid_cable (3.0.12) sha256=a168a54731a455d5627af48d8441ea3b554b8c1f6e6cd6074109de493e6b0460
solid_cache (1.0.10) sha256=bc05a2fb3ac78a6f43cbb5946679cf9db67dd30d22939ededc385cb93e120d41
solid_queue (1.3.2) sha256=44a53047be4255f616ff13fa5d35980e7b3eee6e31d957eadb88fbe8e0db4509
- spree (5.4.0.beta)
- spree_admin (5.4.0.beta)
- spree_api (5.4.0.beta)
- spree_core (5.4.0.beta)
+ spree (5.4.0.beta2)
+ spree_admin (5.4.0.beta2)
+ spree_api (5.4.0.beta2)
+ spree_core (5.4.0.beta2)
spree_dev_tools (0.5.3) sha256=64ae7b215ac5d5c5f5890f6fc0dbcb12882b9af305a860f36f95f564ec240a75
- spree_emails (5.4.0.beta)
+ spree_emails (5.4.0.beta2)
spree_extension (0.1.0) sha256=b648327f4787295e051be4ea0c0a47baec47c2e245bca9c93ecf73605fc3a051
spree_i18n (5.3.2) sha256=03b805235ca156644951f6bc3d07cbe43d0aa01134c4a27e3f3304011956f79b
spree_stripe (1.5.1)
diff --git a/spree/core/lib/spree/core.rb b/spree/core/lib/spree/core.rb
index eab9e38779..2b9743926a 100644
--- a/spree/core/lib/spree/core.rb
+++ b/spree/core/lib/spree/core.rb
@@ -427,7 +427,6 @@ require 'spree/events'
require 'spree/webhooks'
require 'spree/core/partials'
-require 'spree/core/importer'
require 'spree/core/controller_helpers/auth'
require 'spree/core/controller_helpers/common'
require 'spree/core/controller_helpers/order'
diff --git a/spree/core/lib/spree/core/importer.rb b/spree/core/lib/spree/core/importer.rb
deleted file mode 100644
index e71b9e0d5b..0000000000
--- a/spree/core/lib/spree/core/importer.rb
+++ /dev/null
@@ -1,9 +0,0 @@
-module Spree
- module Core
- module Importer
- end
- end
-end
-
-require 'spree/core/importer/order'
-require 'spree/core/importer/product'
diff --git a/spree/core/lib/spree/core/importer/order.rb b/spree/core/lib/spree/core/importer/order.rb
deleted file mode 100644
index 4f2bd768ef..0000000000
--- a/spree/core/lib/spree/core/importer/order.rb
+++ /dev/null
@@ -1,244 +0,0 @@
-module Spree
- module Core
- module Importer
- class Order
- def self.import(user, params)
- Spree::Deprecation.warn('Spree::Core::Importer::Order is deprecated and will be removed in Spree 5.5. Please use `Spree::Imports::Order` instead.')
-
- ensure_country_id_from_params params[:ship_address_attributes]
- ensure_state_id_from_params params[:ship_address_attributes]
- ensure_country_id_from_params params[:bill_address_attributes]
- ensure_state_id_from_params params[:bill_address_attributes]
-
- create_params = params.slice :currency
- order = Spree::Order.create! create_params
- order.associate_user!(user)
-
- shipments_attrs = params.delete(:shipments_attributes)
-
- create_line_items_from_params(params.delete(:line_items_attributes), order)
- create_shipments_from_params(shipments_attrs, order)
- create_adjustments_from_params(params.delete(:adjustments_attributes), order)
- create_payments_from_params(params.delete(:payments_attributes), order)
-
- if completed_at = params.delete(:completed_at)
- order.completed_at = completed_at
- order.state = 'complete'
- end
-
- params.delete(:user_id) unless user.try(:has_spree_role?, 'admin') && params.key?(:user_id)
-
- order.update!(params)
-
- order.create_proposed_shipments unless shipments_attrs.present?
-
- # Really ensure that the order totals & states are correct
- order.updater.update
- if shipments_attrs.present?
- order.shipments.each_with_index do |shipment, index|
- shipment.update_columns(cost: shipments_attrs[index][:cost].to_f) if shipments_attrs[index][:cost].present?
- end
- end
- order.reload
- rescue StandardError => e
- order.destroy if order&.persisted?
- raise e.message
- end
-
- def self.create_shipments_from_params(shipments_hash, order)
- return [] unless shipments_hash
-
- shipments_hash.each do |s|
- shipment = order.shipments.build
- shipment.tracking = s[:tracking]
- shipment.stock_location = Spree::StockLocation.find_by(admin_name: s[:stock_location]) ||
- Spree::StockLocation.find_by!(name: s[:stock_location])
- inventory_units = create_inventory_units_from_order_and_params(order, s[:inventory_units])
-
- inventory_units.each do |inventory_unit|
- inventory_unit.shipment = shipment
-
- if s[:shipped_at].present?
- inventory_unit.pending = false
- inventory_unit.state = 'shipped'
- end
-
- inventory_unit.save!
- end
-
- if s[:shipped_at].present?
- shipment.shipped_at = s[:shipped_at]
- shipment.state = 'shipped'
- end
-
- shipment.save!
-
- shipping_method = Spree::ShippingMethod.find_by(name: s[:shipping_method]) ||
- Spree::ShippingMethod.find_by!(admin_name: s[:shipping_method])
- rate = shipment.shipping_rates.create!(shipping_method: shipping_method, cost: s[:cost])
-
- shipment.selected_shipping_rate_id = rate.id
- shipment.update_amounts
-
- adjustments = s.delete(:adjustments_attributes)
- create_adjustments_from_params(adjustments, order, shipment)
- rescue StandardError => e
- raise "Order import shipments: #{e.message} #{s}"
- end
- end
-
- def self.create_inventory_units_from_order_and_params(order, inventory_unit_params)
- inventory_unit_params.each_with_object([]) do |inventory_unit_param, inventory_units|
- ensure_variant_id_from_params(inventory_unit_param)
- existing = inventory_units.detect { |unit| unit.variant_id == inventory_unit_param[:variant_id] }
- if existing
- existing.quantity += 1
- else
- line_item = order.line_items.detect { |ln| ln.variant_id == inventory_unit_param[:variant_id] }
- inventory_units << InventoryUnit.new(line_item: line_item, order_id: order.id, variant: line_item.variant, quantity: 1)
- end
- end
- end
-
- def self.create_line_items_from_params(line_items, order)
- return {} unless line_items
-
- line_items.each do |line_item|
- adjustments = line_item.delete(:adjustments_attributes)
- extra_params = line_item.except(:variant_id, :quantity, :sku)
- line_item = ensure_variant_id_from_params(line_item)
- variant = Spree::Variant.find(line_item[:variant_id])
- line_item = Cart::AddItem.call(order: order, variant: variant, quantity: line_item[:quantity]).value
- # Raise any errors with saving to prevent import succeeding with line items
- # failing silently.
- if extra_params.present?
- line_item.update!(extra_params)
- else
- line_item.save!
- end
- create_adjustments_from_params(adjustments, order, line_item)
- rescue StandardError => e
- raise "Order import line items: #{e.message} #{line_item}"
- end
- end
-
- def self.create_adjustments_from_params(adjustments, order, adjustable = nil)
- return [] unless adjustments
-
- adjustments.each do |a|
- adjustment = (adjustable || order).adjustments.build(
- order: order,
- amount: a[:amount].to_f,
- label: a[:label],
- source_type: source_type_from_adjustment(a)
- )
- adjustment.save!
- adjustment.close!
- rescue StandardError => e
- raise "Order import adjustments: #{e.message} #{a}"
- end
- end
-
- def self.create_payments_from_params(payments_hash, order)
- return [] unless payments_hash
-
- payments_hash.each do |p|
- payment = order.payments.build order: order
- payment.amount = p[:amount].to_f
- # Order API should be using state as that's the normal payment field.
- # spree_wombat serializes payment state as status so imported orders should fall back to status field.
- payment.state = p[:state] || p[:status] || 'completed'
- payment.created_at = p[:created_at] if p[:created_at]
- payment.payment_method = Spree::PaymentMethod.find_by!(name: p[:payment_method])
- payment.source = create_source_payment_from_params(p[:source], payment) if p[:source]
- payment.save!
- rescue StandardError => e
- raise "Order import payments: #{e.message} #{p}"
- end
- end
-
- def self.create_source_payment_from_params(source_hash, payment)
- Spree::CreditCard.create(
- month: source_hash[:month],
- year: source_hash[:year],
- cc_type: source_hash[:cc_type],
- last_digits: source_hash[:last_digits],
- name: source_hash[:name],
- payment_method: payment.payment_method,
- gateway_customer_profile_id: source_hash[:gateway_customer_profile_id],
- gateway_payment_profile_id: source_hash[:gateway_payment_profile_id],
- imported: true
- )
- rescue StandardError => e
- raise "Order import source payments: #{e.message} #{source_hash}"
- end
-
- def self.ensure_variant_id_from_params(hash)
- sku = hash.delete(:sku)
- unless hash[:variant_id].present?
- hash[:variant_id] = Spree::Variant.active.find_by!(sku: sku).id
- end
- hash
- rescue ActiveRecord::RecordNotFound => e
- raise "Ensure order import variant: Variant w/SKU #{sku} not found."
- rescue StandardError => e
- raise "Ensure order import variant: #{e.message} #{hash}"
- end
-
- def self.ensure_country_id_from_params(address)
- return if address.nil? || address[:country_id].present? || address[:country].nil?
-
- begin
- search = {}
- if name = address[:country]['name']
- search[:name] = name
- elsif iso_name = address[:country]['iso_name']
- search[:iso_name] = iso_name.upcase
- elsif iso = address[:country]['iso']
- search[:iso] = iso.upcase
- elsif iso3 = address[:country]['iso3']
- search[:iso3] = iso3.upcase
- end
-
- address.delete(:country)
- address[:country_id] = Spree::Country.where(search).first!.id
- rescue StandardError => e
- raise "Ensure order import address country: #{e.message} #{search}"
- end
- end
-
- def self.ensure_state_id_from_params(address)
- return if address.nil? || address[:state_id].present? || address[:state].nil?
-
- begin
- search = {}
- if name = address[:state]['name']
- search[:name] = name
- elsif abbr = address[:state]['abbr']
- search[:abbr] = abbr.upcase
- end
-
- address.delete(:state)
- search[:country_id] = address[:country_id]
-
- if state = Spree::State.where(search).first
- address[:state_id] = state.id
- else
- address[:state_name] = search[:name] || search[:abbr]
- end
- rescue StandardError => e
- raise "Ensure order import address state: #{e.message} #{search}"
- end
- end
-
- def self.source_type_from_adjustment(adjustment)
- if adjustment[:tax]
- 'Spree::TaxRate'
- elsif adjustment[:promotion]
- 'Spree::PromotionAction'
- end
- end
- end
- end
- end
-end
diff --git a/spree/core/lib/spree/core/importer/product.rb b/spree/core/lib/spree/core/importer/product.rb
deleted file mode 100644
index 8003196c51..0000000000
--- a/spree/core/lib/spree/core/importer/product.rb
+++ /dev/null
@@ -1,67 +0,0 @@
-module Spree
- module Core
- module Importer
- class Product
- attr_reader :product, :product_attrs, :variants_attrs, :options_attrs, :store
-
- def initialize(product, product_params, options = {})
- Spree::Deprecation.warn('Spree::Core::Importer::Product is deprecated and will be removed in Spree 5.5. Please use `Spree::Imports::Product` instead.')
-
- @store = options[:store] || Spree::Store.default
- @product = product || Spree::Product.new(product_params)
- @product.stores << @store if @product.stores.exclude?(@store)
-
- @product_attrs = product_params.to_h
- @variants_attrs = (options[:variants_attrs] || []).map(&:to_h)
- @options_attrs = options[:options_attrs] || []
- end
-
- def create
- if product.save
- variants_attrs.each do |variant_attribute|
- # make sure the product is assigned before the options=
- product.variants.create({ product: product }.merge(variant_attribute))
- end
-
- set_up_options
- end
-
- product
- end
-
- def update
- if product.update(product_attrs)
- variants_attrs.each do |variant_attribute|
- # update the variant if the id is present in the payload
- if variant_attribute['id'].present?
- product.variants.find(variant_attribute['id']).update(variant_attribute)
- else
- # make sure the product is assigned before the options=
- product.variants.create({ product: product }.merge(variant_attribute))
- end
- end
-
- set_up_options
- end
-
- product
- end
-
- private
-
- def set_up_options
- options_attrs.each do |name|
- option_type = Spree::OptionType.where(name: name).first_or_initialize do |option_type|
- option_type.presentation = name
- option_type.save!
- end
-
- unless product.option_types.include?(option_type)
- product.option_types << option_type
- end
- end
- end
- end
- end
- end
-end
diff --git a/spree/core/spec/lib/spree/core/importer/order_spec.rb b/spree/core/spec/lib/spree/core/importer/order_spec.rb
deleted file mode 100644
index aab2c15afd..0000000000
--- a/spree/core/spec/lib/spree/core/importer/order_spec.rb
+++ /dev/null
@@ -1,622 +0,0 @@
-require 'spec_helper'
-
-module Spree
- module Core
- describe Importer::Order do
- let(:store) { @default_store }
- let(:country) { store.default_country || create(:country_us) }
- let(:state) { country.states.first.presence || create(:state, country: country, name: 'New York', abbr: 'NY') }
- let!(:stock_location) { create(:stock_location, admin_name: 'Admin Name') }
-
- let(:user) { create(:user) }
- let(:shipping_method) { create(:shipping_method) }
- let(:payment_method) { create(:check_payment_method, stores: [store]) }
-
- let(:product) { create(:product_in_stock, stores: [store], name: 'Test', sku: 'TEST-1', price: 33.22) }
-
- let(:variant) { product.master }
-
- let(:sku) { variant.sku }
- let(:variant_id) { variant.id }
-
- let(:line_items) { [{ variant_id: variant.id, quantity: 5 }] }
- let(:ship_address) do
- {
- address1: '123 Testable Way',
- firstname: 'Fox',
- lastname: 'Mulder',
- city: 'Washington',
- country_id: country.id,
- state_id: state.id,
- zipcode: '66666',
- phone: '666-666-6666'
- }
- end
-
- it 'can import an order number' do
- params = { number: '123-456-789' }
- order = Importer::Order.import(user, params)
- expect(order.number).to eq '123-456-789'
- end
-
- it 'optionally add completed at' do
- params = {
- email: '[email protected]',
- completed_at: Time.current,
- line_items_attributes: line_items
- }
-
- order = Importer::Order.import(user, params)
- expect(order).to be_completed
- expect(order.state).to eq 'complete'
- end
-
- it 'assigns order[email] over user email to order' do
- params = { email: '[email protected]' }
- order = Importer::Order.import(user, params)
- expect(order.email).to eq params[:email]
- end
-
- context 'assigning a user to an order' do
- let(:other_user) { create(:user) }
-
- context 'as an admin' do
- before { allow(user).to receive_messages has_spree_role?: true }
-
- context "a user's id is not provided" do
- # this is a regression spec for an issue we ran into at Bonobos
- it "doesn't unassociate the admin from the order" do
- params = {}
- order = Importer::Order.import(user, params)
- expect(order.user_id).to eq(user.id)
- end
- end
- end
-
- context 'as a user' do
- before { allow(user).to receive_messages has_spree_role?: false }
-
- it 'does not assign the order to the other user' do
- params = { user_id: other_user.id }
- order = Importer::Order.import(user, params)
- expect(order.user_id).to eq(user.id)
- end
- end
- end
-
- it 'can build an order from API with just line items' do
- params = { line_items_attributes: line_items }
-
- expect(Importer::Order).to receive(:ensure_variant_id_from_params).and_return(variant_id: variant.id,
- quantity: 5)
- order = Importer::Order.import(user, params)
- line_item = order.line_items.first
- expect(line_item.quantity).to eq(5)
- expect(line_item.variant_id).to eq(variant_id)
- end
-
- it 'handles line_item building exceptions' do
- line_items.first[:variant_id] = 'XXX'
- params = { line_items_attributes: line_items }
-
- expect { Importer::Order.import(user, params) }.to raise_error(/XXX/)
- end
-
- it 'handles line_item updating exceptions' do
- line_items.first[:currency] = 'GBP'
- params = { line_items_attributes: line_items }
-
- expect { Importer::Order.import(user, params) }.to raise_error(/Validation failed/)
- end
-
- it 'can build an order from API with variant sku' do
- params = { line_items_attributes: [{ sku: sku, quantity: 5 }] }
-
- order = Importer::Order.import(user, params)
-
- line_item = order.line_items.first
- expect(line_item.variant_id).to eq(variant_id)
- expect(line_item.quantity).to eq(5)
- end
-
- it 'handles exceptions when sku is not found' do
- params = { line_items_attributes: [{ sku: 'XXX', quantity: 5 }] }
- expect { Importer::Order.import(user, params) }.to raise_error(/XXX/)
- end
-
- it 'can build an order from API shipping address' do
- params = {
- ship_address_attributes: ship_address,
- line_items_attributes: line_items
- }
-
- order = Importer::Order.import(user, params)
- expect(order.ship_address.address1).to eq '123 Testable Way'
- end
-
- it 'can build an order from API with country attributes' do
- ship_address.delete(:country_id)
- ship_address[:country] = { 'iso' => 'US' }
- params = {
- ship_address_attributes: ship_address,
- line_items_attributes: line_items
- }
-
- order = Importer::Order.import(user, params)
- expect(order.ship_address.country.iso).to eq 'US'
- end
-
- it 'handles country lookup exceptions' do
- ship_address.delete(:country_id)
- ship_address[:country] = { 'iso' => 'XXX' }
- params = {
- ship_address_attributes: ship_address,
- line_items_attributes: line_items
- }
-
- expect { Importer::Order.import(user, params) }.to raise_error(/XXX/)
- end
-
- it 'can build an order from API with state attributes' do
- ship_address.delete(:state_id)
- ship_address[:state] = { 'name' => state.name }
- params = {
- ship_address_attributes: ship_address,
- line_items_attributes: line_items
- }
-
- order = Importer::Order.import(user, params)
- expect(order.ship_address.state.name).to eq state.name
- end
-
- context 'with a different currency' do
- before { variant.price_in('GBP').update_attribute(:price, 18.99) }
-
- it 'sets the order currency' do
- params = { currency: 'GBP' }
- order = Importer::Order.import(user, params)
- expect(order.currency).to eq 'GBP'
- end
-
- it 'can handle it when a line order price is specified' do
- params = {
- currency: 'GBP',
- line_items_attributes: line_items
- }
- line_items.first.merge! currency: 'GBP', price: 1.99
- order = Importer::Order.import(user, params)
- expect(order.currency).to eq 'GBP'
- expect(order.line_items.first.price).to eq 1.99
- expect(order.line_items.first.currency).to eq 'GBP'
- end
- end
-
- xcontext 'state passed is not associated with country' do
- let(:params) do
- {
- ship_address_attributes: ship_address,
- line_items_attributes: line_items
- }
- end
-
- let(:other_state) { create(:state, name: 'Uhuhuh', country: create(:country)) }
-
- before do
- ship_address.delete(:state_id)
- ship_address[:state] = { 'name' => other_state.name }
- end
-
- it 'sets states name instead of state id' do
- order = Importer::Order.import(user, params)
- expect(order.ship_address.state_name).to eq other_state.name
- end
- end
-
- xit 'sets state name if state record not found' do
- ship_address.delete(:state_id)
- ship_address[:state] = { 'name' => 'XXX' }
- params = {
- ship_address_attributes: ship_address,
- line_items_attributes: line_items
- }
-
- order = Importer::Order.import(user, params)
- expect(order.ship_address.state_name).to eq 'XXX'
- end
-
- context 'variant not deleted' do
- it 'ensures variant id from api' do
- hash = { sku: variant.sku }
- Importer::Order.ensure_variant_id_from_params(hash)
- expect(hash[:variant_id]).to eq variant.id
- end
- end
-
- context 'variant was deleted' do
- it "raise error as variant shouldn't be found" do
- variant.product.destroy
- hash = { sku: variant.sku }
- expect { Importer::Order.ensure_variant_id_from_params(hash) }.to raise_error("Ensure order import variant: Variant w/SKU #{hash[:sku]} not found.")
- end
- end
-
- it 'ensures_country_id for country fields' do
- [:name, :iso, :iso_name, :iso3].each do |field|
- address = { country: { field => country.send(field) } }
- Importer::Order.ensure_country_id_from_params(address)
- expect(address[:country_id]).to eq country.id
- end
- end
-
- it "raises with proper message when can't find country" do
- address = { country: { 'name' => 'NoNoCountry' } }
- expect { Importer::Order.ensure_country_id_from_params(address) }.to raise_error(/NoNoCountry/)
- end
-
- it 'ensures_state_id for state fields' do
- [:name, :abbr].each do |field|
- address = { country_id: country.id, state: { field => state.send(field) } }
- Importer::Order.ensure_state_id_from_params(address)
- expect(address[:state_id]).to eq state.id
- end
- end
-
- context 'shipments' do
- let(:params) do
- {
- line_items_attributes: line_items,
- shipments_attributes: [
- {
- tracking: '123456789',
- cost: '14.99',
- shipping_method: shipping_method.name,
- stock_location: stock_location.name,
- inventory_units: Array.new(3) { { sku: sku, variant_id: variant.id } }
- },
- {
- tracking: '123456789',
- cost: '14.99',
- shipping_method: shipping_method.name,
- stock_location: stock_location.name,
- inventory_units: Array.new(2) { { sku: sku, variant_id: variant.id } }
- }
- ]
- }
- end
-
- it 'ensures variant exists and is not deleted' do
- expect(Importer::Order).to receive(:ensure_variant_id_from_params).exactly(6).times { line_items.first }
- Importer::Order.import(user, params)
- end
-
- it 'builds them properly' do
- order = Importer::Order.import(user, params)
- shipment = order.shipments.first
-
- expect(shipment.cost.to_f).to eq 14.99
- expect(shipment.inventory_units.first.variant_id).to eq product.master.id
- expect(shipment.tracking).to eq '123456789'
- expect(shipment.shipping_rates.first.cost).to eq 14.99
- expect(shipment.selected_shipping_rate).to eq(shipment.shipping_rates.first)
- expect(shipment.stock_location).to eq stock_location
- expect(order.shipment_total.to_f).to eq 29.98
- end
-
- it 'allocates inventory units to the correct shipments' do
- order = Importer::Order.import(user, params)
-
- expect(order.inventory_units.count).to eq 2
- expect(order.shipments.first.inventory_units.count).to eq 1
- expect(order.shipments.first.inventory_units.first.quantity).to eq 3
- expect(order.shipments.last.inventory_units.count).to eq 1
- expect(order.shipments.last.inventory_units.first.quantity).to eq 2
- end
-
- it 'accepts admin name for stock location' do
- params[:shipments_attributes][0][:stock_location] = stock_location.admin_name
- order = Importer::Order.import(user, params)
- shipment = order.shipments.first
-
- expect(shipment.stock_location).to eq stock_location
- end
-
- it "raises if can't find stock location" do
- params[:shipments_attributes][0][:stock_location] = 'doesnt exist'
- expect { Importer::Order.import(user, params) }.to raise_error(StandardError)
- end
-
- context 'when a shipping adjustment is present' do
- it 'creates the shipping adjustment' do
- adjustment_attributes = [{ label: 'Shipping Discount', amount: -5.00 }]
- params[:shipments_attributes][0][:adjustments_attributes] = adjustment_attributes
- order = Importer::Order.import(user, params)
- shipment = order.shipments.first
-
- expect(shipment.adjustments.first.label).to eq('Shipping Discount')
- expect(shipment.adjustments.first.amount).to eq(-5.00)
- end
- end
-
- context 'when completed_at and shipped_at present' do
- let(:params) do
- {
- completed_at: 2.days.ago,
- line_items_attributes: line_items,
- shipments_attributes: [
- {
- tracking: '123456789',
- cost: '4.99',
- shipped_at: 1.day.ago,
- shipping_method: shipping_method.name,
- stock_location: stock_location.name,
- inventory_units: [{ sku: sku }]
- }
- ]
- }
- end
-
- it 'builds them properly' do
- order = Importer::Order.import(user, params)
- shipment = order.shipments.first
-
- expect(shipment.cost.to_f).to eq 4.99
- expect(shipment.inventory_units.first.variant_id).to eq product.master.id
- expect(shipment.tracking).to eq '123456789'
- expect(shipment.shipped_at).to be_present
- expect(shipment.shipping_rates.first.cost).to eq 4.99
- expect(shipment.selected_shipping_rate).to eq(shipment.shipping_rates.first)
- expect(shipment.stock_location).to eq stock_location
- expect(shipment.state).to eq('shipped')
- expect(shipment.inventory_units.all?(&:shipped?)).to be true
- expect(order.shipment_state).to eq('shipped')
- expect(order.shipment_total.to_f).to eq 4.99
- end
- end
- end
-
- it 'handles shipment building exceptions' do
- params = {
- shipments_attributes: [
- {
- tracking: '123456789',
- cost: '4.99',
- shipping_method: 'XXX',
- inventory_units: [{ sku: sku }]
- }
- ]
- }
- expect { Importer::Order.import(user, params) }.to raise_error(/XXX/)
- end
-
- it 'adds adjustments' do
- params = {
- adjustments_attributes: [
- {
- label: 'Shipping Discount',
- amount: -4.99
- },
- {
- label: 'Promotion Discount',
- amount: -3.00
- }
- ]
- }
-
- order = Importer::Order.import(user, params)
- expect(order.adjustments.all?(&:closed?)).to be true
- expect(order.adjustments.first.label).to eq 'Shipping Discount'
- expect(order.adjustments.first.amount).to eq(-4.99)
- end
-
- it 'adds line item adjustments from promotion' do
- line_items.first[:adjustments_attributes] = [
- {
- label: 'Line Item Discount',
- amount: -4.99,
- promotion: true
- }
- ]
- params = {
- line_items_attributes: line_items,
- adjustments_attributes: [
- { label: 'Order Discount', amount: -5.99 }
- ]
- }
-
- order = Importer::Order.import(user, params)
- line_item_adjustment = order.line_item_adjustments.first
- expect(line_item_adjustment.closed?).to be true
- expect(line_item_adjustment.label).to eq 'Line Item Discount'
- expect(line_item_adjustment.amount).to eq(-4.99)
- expect(order.line_items.first.adjustment_total).to eq(-4.99)
- end
-
- it 'adds line item adjustments from taxation' do
- line_items.first[:adjustments_attributes] = [
- { label: 'Line Item Tax', amount: -4.99, tax: true }
- ]
- params = {
- line_items_attributes: line_items,
- adjustments_attributes: [
- { label: 'Order Discount', amount: -5.99 }
- ],
- additional_tax_total: 0
- }
-
- order = Importer::Order.import(user, params)
-
- line_item_adjustment = order.line_item_adjustments.first
- expect(line_item_adjustment.closed?).to be true
- expect(line_item_adjustment.label).to eq 'Line Item Tax'
- expect(line_item_adjustment.amount).to eq(-4.99)
- expect(order.line_items.first.adjustment_total).to eq(-4.99)
- end
-
- it 'calculates final order total correctly' do
- params = {
- adjustments_attributes: [
- { label: 'Promotion Discount', amount: -3.00 }
- ],
- line_items_attributes: [
- {
- variant_id: variant.id,
- quantity: 5
- }
- ]
- }
-
- order = Importer::Order.import(user, params)
- expect(order.item_total).to eq(166.1)
- expect(order.total).to eq(163.1) # = item_total (166.1) - adjustment_total (3.00)
- end
-
- it 'handles adjustment building exceptions' do
- params = {
- adjustments_attributes: [
- {
- amount: 'XXX'
- },
- {
- label: 'Promotion Discount',
- amount: '-3.00'
- }
- ]
- }
-
- expect { Importer::Order.import(user, params) }.to raise_error(/XXX/)
- end
-
- it 'builds a payment using state' do
- params = {
- line_items_attributes: [
- {
- variant_id: variant.id,
- quantity: 5
- }
- ],
- payments_attributes: [
- {
- amount: '4.99',
- payment_method: payment_method.name,
- state: 'completed'
- }
- ]
- }
- order = Importer::Order.import(user, params)
- expect(order.payments.first.amount).to eq 4.99
- end
-
- it 'builds a payment using status as fallback' do
- params = {
- line_items_attributes: [
- {
- variant_id: variant.id,
- quantity: 5
- }
- ],
- payments_attributes: [
- {
- amount: '4.99',
- payment_method: payment_method.name,
- status: 'completed'
- }
- ]
- }
- order = Importer::Order.import(user, params)
- expect(order.payments.first.amount).to eq 4.99
- end
-
- it 'handles payment building exceptions' do
- params = {
- payments_attributes: [
- {
- amount: '4.99',
- payment_method: 'XXX'
- }
- ]
- }
- expect { Importer::Order.import(user, params) }.to raise_error(/XXX/)
- end
-
- it 'build a source payment using years and month' do
- params = {
- line_items_attributes: [
- {
- variant_id: variant.id,
- quantity: 5
- }
- ],
- payments_attributes: [
- {
- amount: '4.99',
- payment_method: payment_method.name,
- status: 'completed',
- source: {
- name: 'Fox',
- last_digits: '7424',
- cc_type: 'visa',
- year: '2022',
- month: '5'
- }
- }
- ]
- }
-
- order = Importer::Order.import(user, params)
- expect(order.payments.first.source.last_digits).to eq '7424'
- end
-
- it 'handles source building exceptions when do not have years and month' do
- params = {
- payments_attributes: [
- {
- amount: '4.99',
- payment_method: payment_method.name,
- status: 'completed',
- source: {
- name: 'Fox',
- last_digits: '7424',
- cc_type: 'visa'
- }
- }
- ]
- }
-
- expect { Importer::Order.import(user, params) }.
- to raise_error(/Validation failed: Credit card Month is not a number, Credit card Year is not a number/)
- end
-
- it 'builds a payment with an optional created_at' do
- created_at = 2.days.ago
- params = {
- line_items_attributes: [
- {
- variant_id: variant.id,
- quantity: 5
- }
- ],
- payments_attributes: [
- {
- amount: '4.99',
- payment_method: payment_method.name,
- state: 'completed',
- created_at: created_at
- }
- ]
- }
- order = Importer::Order.import(user, params)
- expect(order.payments.first.created_at).to be_within(1).of created_at
- end
-
- context 'raises error' do
- it 'clears out order from db' do
- params = { payments_attributes: [{ payment_method: 'XXX' }] }
- count = Order.count
-
- expect { Importer::Order.import(user, params) }.to raise_error(StandardError)
- expect(Order.count).to eq count
- end
- end
- end
- end
-end