diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 22a18473a9..5340aecddd 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -43,7 +43,9 @@ class Enterprise < ApplicationRecord foreign_key: 'supplier_id', dependent: :destroy has_many :supplied_variants, through: :supplied_products, source: :variants - has_many :distributed_orders, class_name: 'Spree::Order', foreign_key: 'distributor_id' + has_many :distributed_orders, class_name: 'Spree::Order', + foreign_key: 'distributor_id', + dependent: :destroy belongs_to :address, class_name: 'Spree::Address' belongs_to :business_address, optional: true, class_name: 'Spree::Address', dependent: :destroy has_many :enterprise_fees @@ -52,9 +54,9 @@ class Enterprise < ApplicationRecord belongs_to :owner, class_name: 'Spree::User', inverse_of: :owned_enterprises has_many :distributor_payment_methods, - inverse_of: :distributor, foreign_key: :distributor_id + inverse_of: :distributor, foreign_key: :distributor_id, dependent: :destroy has_many :distributor_shipping_methods, - inverse_of: :distributor, foreign_key: :distributor_id + inverse_of: :distributor, foreign_key: :distributor_id, dependent: :destroy has_many :payment_methods, through: :distributor_payment_methods has_many :shipping_methods, through: :distributor_shipping_methods has_many :customers, dependent: :destroy diff --git a/spec/factories/payment_method_factory.rb b/spec/factories/payment_method_factory.rb index 98e8d36a35..c93657ef21 100644 --- a/spec/factories/payment_method_factory.rb +++ b/spec/factories/payment_method_factory.rb @@ -24,4 +24,9 @@ FactoryBot.define do distributors { [FactoryBot.create(:stripe_account).enterprise] } preferred_enterprise_id { distributors.first.id } end + + factory :distributor_payment_method, class: DistributorPaymentMethod do + distributor { FactoryBot.create(:distributor_enterprise) } + payment_method { FactoryBot.create(:payment_method) } + end end diff --git a/spec/factories/shipping_method_factory.rb b/spec/factories/shipping_method_factory.rb index d6957e78bd..68b41106c4 100644 --- a/spec/factories/shipping_method_factory.rb +++ b/spec/factories/shipping_method_factory.rb @@ -68,4 +68,9 @@ FactoryBot.define do distributors { [create(:distributor_enterprise_with_tax)] } end end + + factory :distributor_shipping_method, class: DistributorShippingMethod do + shipping_method { FactoryBot.create(:shipping_method) } + distributor { FactoryBot.create(:distributor_enterprise) } + end end diff --git a/spec/models/enterprise_spec.rb b/spec/models/enterprise_spec.rb index e764c9da73..06eb7cfbb0 100644 --- a/spec/models/enterprise_spec.rb +++ b/spec/models/enterprise_spec.rb @@ -57,6 +57,35 @@ describe Enterprise do expect(EnterpriseRelationship.where(id: [er1, er2])).to be_empty end + it "destroys all distributed_orders upon destroy" do + enterprise = create(:distributor_enterprise) + order_ids = create_list(:order, 2, distributor: enterprise).map(&:id) + + expect(Spree::Order.where(id: order_ids)).to exist + enterprise.destroy + expect(Spree::Order.where(id: order_ids)).not_to exist + end + + it "destroys all distributor_payment_methods upon destroy" do + enterprise = create(:distributor_enterprise) + payment_method_ids = create_list(:distributor_payment_method, 2, + distributor: enterprise).map(&:id) + + expect(DistributorPaymentMethod.where(id: payment_method_ids)).to exist + enterprise.destroy + expect(DistributorPaymentMethod.where(id: payment_method_ids)).not_to exist + end + + it "destroys all distributor_shipping_methods upon destroy" do + enterprise = create(:enterprise) + shipping_method_ids = create_list(:distributor_shipping_method, 2, + distributor: enterprise).map(&:id) + + expect(DistributorShippingMethod.where(id: shipping_method_ids)).to exist + enterprise.destroy + expect(DistributorShippingMethod.where(id: shipping_method_ids)).not_to exist + end + describe "relationships to other enterprises" do let(:e) { create(:distributor_enterprise) } let(:p) { create(:supplier_enterprise) }