Handle scenario where the enterprise fee has been deleted

This commit is contained in:
Gaetan Craig-Riou
2025-02-12 16:26:57 +11:00
parent 12a54dd8f0
commit 67ad532908
2 changed files with 35 additions and 1 deletions

View File

@@ -92,8 +92,13 @@ module Orders
def update_removed_fees!(line_item)
order_cycle_fees = fee_applicators(line_item.variant).map(&:enterprise_fee)
removed_fees = line_item.enterprise_fee_adjustments.where.not(originator: order_cycle_fees)
removed_fees.each do |removed_fee|
removed_fee.update_adjustment!(line_item, force: true)
if removed_fee.originator.nil? || removed_fee.originator.deleted?
removed_fee.destroy
else
removed_fee.update_adjustment!(line_item, force: true)
end
end
end

View File

@@ -107,6 +107,35 @@ RSpec.describe Orders::HandleFeesService do
end
end
context "when an enterprise fee is deleted" do
before do
fee.create_adjustment('foo', line_item, true)
allow(calculator).to receive(
:order_cycle_per_item_enterprise_fee_applicators_for
).and_return([])
end
context "soft delete" do
it "deletes the line item fee" do
fee.destroy
expect do
service.create_or_update_line_item_fees!
end.to change { line_item.adjustments.enterprise_fee.count }.by(-1)
end
end
context "hard delete" do
it "deletes the line item fee" do
fee.really_destroy!
expect do
service.create_or_update_line_item_fees!
end.to change { line_item.adjustments.enterprise_fee.count }.by(-1)
end
end
end
context "with a new enterprise fee added to the order cylce" do
let(:new_fee) { create(:enterprise_fee, enterprise: fee.enterprise) }
let(:fee_applicator2) {