Merge pull request #10514 from jibees/9292-changes-to-incomingoutgoing-products-lists-triggering-failed-to-update-order-cycle-error

Do not update `estimate_price` of a variant that is not available for the shop on OrderCycle update
This commit is contained in:
Konrad
2023-03-08 15:45:11 +01:00
committed by GitHub
2 changed files with 32 additions and 1 deletions

View File

@@ -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

View File

@@ -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 } }