mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-14 23:47:48 +00:00
Remove all dependencies to product distributions including the validation to avoid deleting fees with product distributions
This commit is contained in:
@@ -26,7 +26,6 @@ class EnterpriseFee < ActiveRecord::Base
|
||||
validates_presence_of :name
|
||||
|
||||
before_save :ensure_valid_tax_category_settings
|
||||
before_destroy :ensure_no_product_distributions
|
||||
|
||||
scope :for_enterprise, lambda { |enterprise| where(enterprise_id: enterprise) }
|
||||
scope :for_enterprises, lambda { |enterprises| where(enterprise_id: enterprises) }
|
||||
@@ -46,10 +45,6 @@ class EnterpriseFee < ActiveRecord::Base
|
||||
joins(:calculator).where('spree_calculators.type IN (?)', PER_ORDER_CALCULATORS)
|
||||
}
|
||||
|
||||
def self.clear_all_adjustments_for(line_item)
|
||||
line_item.order.adjustments.where(originator_type: 'EnterpriseFee', source_id: line_item, source_type: 'Spree::LineItem').destroy_all
|
||||
end
|
||||
|
||||
def self.clear_all_adjustments_on_order(order)
|
||||
order.adjustments.where(originator_type: 'EnterpriseFee').destroy_all
|
||||
end
|
||||
@@ -69,15 +64,6 @@ class EnterpriseFee < ActiveRecord::Base
|
||||
return true
|
||||
end
|
||||
|
||||
def ensure_no_product_distributions
|
||||
dependent_distribution = ProductDistribution.where(enterprise_fee_id: self).first
|
||||
return unless dependent_distribution
|
||||
product = dependent_distribution.product
|
||||
error = I18n.t(:enterprise_fees_destroy_error, id: product.id, name: product.name)
|
||||
errors.add(:base, error)
|
||||
false
|
||||
end
|
||||
|
||||
def refresh_products_cache
|
||||
OpenFoodNetwork::ProductsCache.enterprise_fee_changed self
|
||||
end
|
||||
|
||||
@@ -8,7 +8,6 @@ module Api
|
||||
let!(:referenced_fee) { create(:enterprise_fee) }
|
||||
let(:product) { create(:product) }
|
||||
let(:distributor) { create(:distributor_enterprise) }
|
||||
let!(:product_distribution) { create(:product_distribution, product: product, distributor: distributor, enterprise_fee: referenced_fee) }
|
||||
let(:current_user) { create(:admin_user) }
|
||||
|
||||
before do
|
||||
@@ -20,15 +19,6 @@ module Api
|
||||
expect { spree_delete :destroy, id: unreferenced_fee.id, format: :json }
|
||||
.to change { EnterpriseFee.count }.by -1
|
||||
end
|
||||
|
||||
context "when the fee is referenced by a product distribution" do
|
||||
it "does not remove the fee" do
|
||||
spree_delete :destroy, id: referenced_fee.id, format: :json
|
||||
expect(response.status).to eq 403
|
||||
expect(response.body).to match(/That enterprise fee cannot be deleted/)
|
||||
expect(referenced_fee.reload).to eq(referenced_fee)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -106,31 +106,6 @@ feature %q{
|
||||
expect(page).to have_no_selector "input[value='#{fee.name}']"
|
||||
end
|
||||
|
||||
scenario "deleting a shipping method referenced by a product distribution" do
|
||||
# Given an enterprise fee referenced by a product distribution
|
||||
fee = create(:enterprise_fee)
|
||||
p = create(:product)
|
||||
d = create(:distributor_enterprise)
|
||||
create(:product_distribution, product: p, distributor: d, enterprise_fee: fee)
|
||||
|
||||
# When I go to the enterprise fees page
|
||||
quick_login_as_admin
|
||||
visit admin_enterprise_fees_path
|
||||
|
||||
# And I click delete
|
||||
accept_alert do
|
||||
find("a.delete-resource").click
|
||||
end
|
||||
|
||||
# Then I should see an error
|
||||
expect(page).to have_content "That enterprise fee cannot be deleted as it is referenced by a product distribution: #{p.id} - #{p.name}."
|
||||
|
||||
# And my enterprise fee should not have been deleted
|
||||
visit admin_enterprise_fees_path
|
||||
expect(page).to have_selector "input[value='#{fee.name}']"
|
||||
expect(EnterpriseFee.find(fee.id)).not_to be_nil
|
||||
end
|
||||
|
||||
context "as an enterprise manager" do
|
||||
let(:enterprise_user) { create_enterprise_user }
|
||||
let(:distributor1) { create(:distributor_enterprise, name: 'First Distributor') }
|
||||
|
||||
@@ -102,51 +102,17 @@ describe EnterpriseFee do
|
||||
end
|
||||
end
|
||||
|
||||
describe "clearing all enterprise fee adjustments for a line item" do
|
||||
it "clears adjustments originating from many different enterprise fees" do
|
||||
p = create(:simple_product)
|
||||
d1, d2 = create(:distributor_enterprise), create(:distributor_enterprise)
|
||||
pd1 = create(:product_distribution, product: p, distributor: d1)
|
||||
pd2 = create(:product_distribution, product: p, distributor: d2)
|
||||
line_item = create(:line_item, product: p)
|
||||
pd1.enterprise_fee.create_adjustment('foo1', line_item.order, line_item, true)
|
||||
pd2.enterprise_fee.create_adjustment('foo2', line_item.order, line_item, true)
|
||||
|
||||
expect do
|
||||
EnterpriseFee.clear_all_adjustments_for line_item
|
||||
end.to change(line_item.order.adjustments, :count).by(-2)
|
||||
end
|
||||
|
||||
it "does not clear adjustments originating from another source" do
|
||||
p = create(:simple_product)
|
||||
pd = create(:product_distribution)
|
||||
line_item = create(:line_item, product: pd.product)
|
||||
tax_rate = create(:tax_rate, calculator: build(:calculator, preferred_amount: 10))
|
||||
tax_rate.create_adjustment('foo', line_item.order, line_item)
|
||||
|
||||
expect do
|
||||
EnterpriseFee.clear_all_adjustments_for line_item
|
||||
end.to change(line_item.order.adjustments, :count).by(0)
|
||||
end
|
||||
end
|
||||
|
||||
describe "clearing all enterprise fee adjustments on an order" do
|
||||
it "clears adjustments from many fees and on all line items" do
|
||||
order = create(:order)
|
||||
order_cycle = create(:order_cycle)
|
||||
order = create(:order, order_cycle: order_cycle)
|
||||
line_item1 = create(:line_item, order: order, variant: order_cycle.variants.first)
|
||||
line_item2 = create(:line_item, order: order, variant: order_cycle.variants.second)
|
||||
|
||||
p1 = create(:simple_product)
|
||||
p2 = create(:simple_product)
|
||||
d1, d2 = create(:distributor_enterprise), create(:distributor_enterprise)
|
||||
pd1 = create(:product_distribution, product: p1, distributor: d1)
|
||||
pd2 = create(:product_distribution, product: p1, distributor: d2)
|
||||
pd3 = create(:product_distribution, product: p2, distributor: d1)
|
||||
pd4 = create(:product_distribution, product: p2, distributor: d2)
|
||||
line_item1 = create(:line_item, order: order, product: p1)
|
||||
line_item2 = create(:line_item, order: order, product: p2)
|
||||
pd1.enterprise_fee.create_adjustment('foo1', line_item1.order, line_item1, true)
|
||||
pd2.enterprise_fee.create_adjustment('foo2', line_item1.order, line_item1, true)
|
||||
pd3.enterprise_fee.create_adjustment('foo3', line_item2.order, line_item2, true)
|
||||
pd4.enterprise_fee.create_adjustment('foo4', line_item2.order, line_item2, true)
|
||||
order_cycle.coordinator_fees[0].create_adjustment('foo1', line_item1.order, line_item1, true)
|
||||
order_cycle.coordinator_fees[0].create_adjustment('foo2', line_item2.order, line_item2, true)
|
||||
order_cycle.exchanges[0].enterprise_fees[0].create_adjustment('foo3', line_item1.order, line_item1, true)
|
||||
order_cycle.exchanges[0].enterprise_fees[0].create_adjustment('foo4', line_item2.order, line_item2, true)
|
||||
|
||||
expect do
|
||||
EnterpriseFee.clear_all_adjustments_on_order order
|
||||
@@ -155,9 +121,9 @@ describe EnterpriseFee do
|
||||
|
||||
it "clears adjustments from per-order fees" do
|
||||
order = create(:order)
|
||||
ef = create(:enterprise_fee)
|
||||
efa = OpenFoodNetwork::EnterpriseFeeApplicator.new(ef, nil, 'coordinator')
|
||||
efa.create_order_adjustment(order)
|
||||
enterprise_fee = create(:enterprise_fee)
|
||||
enterprise_fee_aplicator = OpenFoodNetwork::EnterpriseFeeApplicator.new(enterprise_fee, nil, 'coordinator')
|
||||
enterprise_fee_aplicator.create_order_adjustment(order)
|
||||
|
||||
expect do
|
||||
EnterpriseFee.clear_all_adjustments_on_order order
|
||||
|
||||
Reference in New Issue
Block a user