From 440e776e3aef17dda79a04e3dc1fced40b5e107f Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Fri, 3 Mar 2023 15:05:07 +0100 Subject: [PATCH 1/3] `fee_calculator` can be retrieved outside subscription_line_items iteration --- app/controllers/admin/order_cycles_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/admin/order_cycles_controller.rb b/app/controllers/admin/order_cycles_controller.rb index 5812d2395c..17c394efaa 100644 --- a/app/controllers/admin/order_cycles_controller.rb +++ b/app/controllers/admin/order_cycles_controller.rb @@ -100,10 +100,10 @@ module Admin order_cycle.schedules.each do |schedule| Subscription.where(schedule_id: schedule.id).each do |subscription| shop = Enterprise.managed_by(spree_current_user).find_by(id: subscription.shop_id) + fee_calculator = OpenFoodNetwork::EnterpriseFeeCalculator.new(shop, order_cycle) subscription.subscription_line_items.nil_price_estimate.each do |line_item| variant = OrderManagement::Subscriptions:: VariantsList.eligible_variants(shop).find_by(id: line_item.variant_id) - fee_calculator = OpenFoodNetwork::EnterpriseFeeCalculator.new(shop, order_cycle) price = variant.price + fee_calculator.indexed_fees_for(variant) line_item.update_column(:price_estimate, price) end From 131772f7b2f3b370dc47aedcedc3c20868e40222 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Fri, 3 Mar 2023 15:07:10 +0100 Subject: [PATCH 2/3] Variant can be "not available": next and don't update its estimate price --- app/controllers/admin/order_cycles_controller.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/controllers/admin/order_cycles_controller.rb b/app/controllers/admin/order_cycles_controller.rb index 17c394efaa..1158ec9259 100644 --- a/app/controllers/admin/order_cycles_controller.rb +++ b/app/controllers/admin/order_cycles_controller.rb @@ -104,6 +104,9 @@ module Admin subscription.subscription_line_items.nil_price_estimate.each do |line_item| variant = OrderManagement::Subscriptions:: VariantsList.eligible_variants(shop).find_by(id: line_item.variant_id) + # If the variant is not available in the shop, the price estimate will be nil + next if variant.nil? + price = variant.price + fee_calculator.indexed_fees_for(variant) line_item.update_column(:price_estimate, price) end From a7644f8e8bae08a8a965afc136df0f05bb74a5c1 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Mon, 6 Mar 2023 17:07:25 +0100 Subject: [PATCH 3/3] Update spec: can edit OC even if variant in subscription has been deleted --- .../admin/order_cycles_controller_spec.rb | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/spec/controllers/admin/order_cycles_controller_spec.rb b/spec/controllers/admin/order_cycles_controller_spec.rb index cacb8e1d4a..88730b02d6 100644 --- a/spec/controllers/admin/order_cycles_controller_spec.rb +++ b/spec/controllers/admin/order_cycles_controller_spec.rb @@ -212,6 +212,34 @@ module Admin before { controller_login_as_enterprise_user([coordinator]) } let(:params) { { format: :json, id: order_cycle.id, order_cycle: {} } } + context "when order cycle has subscriptions" do + let(:coordinator) { order_cycle.coordinator } + let(:producer) { create(:supplier_enterprise) } + let!(:schedule) { create(:schedule, order_cycles: [order_cycle]) } + let!(:p) { create(:product) } + let!(:v) { p.variants.first } + let!(:incoming_exchange) { + create(:exchange, order_cycle: order_cycle, sender: producer, receiver: coordinator, + incoming: true, variants: [v]) + } + let!(:outgoing_exchange) { + create(:exchange, order_cycle: order_cycle, sender: coordinator, receiver: coordinator, + incoming: false, variants: [v]) + } + let!(:subscription) { create(:subscription, shop: coordinator, schedule: schedule) } + let!(:subscription_line_item) { create(:subscription_line_item, subscription: subscription, variant: v) } + + before do + allow(form_mock).to receive(:save) { true } + v.destroy + end + + it "can update order cycle even if the variant has been deleted" do + spree_put :update, { format: :json, id: order_cycle.id, order_cycle: {} } + expect(response.status).to eq 200 + end + end + context "when updating succeeds" do before { allow(form_mock).to receive(:save) { true } }