diff --git a/app/controllers/admin/order_cycles_controller.rb b/app/controllers/admin/order_cycles_controller.rb index 5812d2395c..1158ec9259 100644 --- a/app/controllers/admin/order_cycles_controller.rb +++ b/app/controllers/admin/order_cycles_controller.rb @@ -100,10 +100,13 @@ 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) + # 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 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 } }