diff --git a/app/jobs/backorder_job.rb b/app/jobs/backorder_job.rb index 9c885c001e..a4648e930e 100644 --- a/app/jobs/backorder_job.rb +++ b/app/jobs/backorder_job.rb @@ -134,7 +134,6 @@ class BackorderJob < ApplicationJob user, order.distributor, order.order_cycle, placed_order.semanticId ) - exchange = order.order_cycle.exchanges.outgoing.find_by(receiver: order.distributor) - exchange.semantic_links.create!(semantic_id: placed_order.semanticId) + order.exchange.semantic_links.create!(semantic_id: placed_order.semanticId) end end diff --git a/app/models/spree/order.rb b/app/models/spree/order.rb index 1e167f8e92..4c3da7caf9 100644 --- a/app/models/spree/order.rb +++ b/app/models/spree/order.rb @@ -67,8 +67,11 @@ module Spree class_name: 'Spree::Adjustment', dependent: :destroy has_many :invoices, dependent: :restrict_with_exception - belongs_to :order_cycle, optional: true + has_one :exchange, ->(order) { + outgoing.to_enterprise(order.distributor) + }, through: :order_cycle, source: :exchanges + belongs_to :distributor, class_name: 'Enterprise', optional: true belongs_to :customer, optional: true has_one :proxy_order, dependent: :destroy diff --git a/spec/models/spree/order_spec.rb b/spec/models/spree/order_spec.rb index acd2881a36..184d556cf3 100644 --- a/spec/models/spree/order_spec.rb +++ b/spec/models/spree/order_spec.rb @@ -6,6 +6,8 @@ RSpec.describe Spree::Order do let(:user) { build(:user, email: "spree@example.com") } let(:order) { build(:order, user:) } + it { is_expected.to have_one :exchange } + describe "#errors" do it "provides friendly error messages" do order.ship_address = Spree::Address.new