From ea61e415e15f8816d1d71add99ca4ce95c6e4229 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Fri, 21 Dec 2018 13:02:41 +0000 Subject: [PATCH] Add bugsnag notifications to detect potential dead code scenarios in variants_stock_levels and cart_service --- app/services/cart_service.rb | 8 ++++++++ app/services/variants_stock_levels.rb | 20 ++++++++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/app/services/cart_service.rb b/app/services/cart_service.rb index a5e812c0d3..c239c938cd 100644 --- a/app/services/cart_service.rb +++ b/app/services/cart_service.rb @@ -80,11 +80,19 @@ class CartService end def read_products_hash(data) + # This is most probably dead code, this bugsnag notification will confirm it + notify_bugsnag(data) if data[:products].present? + (data[:products] || []).map do |_product_id, variant_id| { variant_id: variant_id, quantity: data[:quantity] } end end + def notify_bugsnag(data) + Bugsnag.notify(RuntimeError.new("CartService.populate called with products hash"), + data: data.as_json) + end + def read_variants_hash(data) (data[:variants] || []).map do |variant_id, quantity| if quantity.is_a?(Hash) diff --git a/app/services/variants_stock_levels.rb b/app/services/variants_stock_levels.rb index 751edfe67e..43d9190800 100644 --- a/app/services/variants_stock_levels.rb +++ b/app/services/variants_stock_levels.rb @@ -5,20 +5,32 @@ class VariantsStockLevels def call(order, requested_variant_ids) variant_stock_levels = variant_stock_levels(order.line_items) - # Potentially, the following lines are dead code, they are never reached - # Additionally, variants are not scoped here and so the stock levels reported would be incorrect + # Variants are not scoped here and so the stock levels reported are incorrect # See cart_controller_spec for more details and #3222 - li_variant_ids = variant_stock_levels.keys - (requested_variant_ids - li_variant_ids).each do |variant_id| + order_variant_ids = variant_stock_levels.keys + missing_variant_ids = requested_variant_ids - order_variant_ids + missing_variant_ids.each do |variant_id| variant_on_hand = Spree::Variant.find(variant_id).on_hand variant_stock_levels[variant_id] = { quantity: 0, max_quantity: 0, on_hand: variant_on_hand } end + # The code above is most probably dead code, this bugsnag notification will confirm it + notify_bugsnag(order, requested_variant_ids, order_variant_ids) if missing_variant_ids.present? + variant_stock_levels end private + def notify_bugsnag(order, requested_variant_ids, order_variant_ids) + error_msg = "VariantsStockLevels.call with variants in the request that are not in the order" + Bugsnag.notify(RuntimeError.new(error_msg), + requested_variant_ids: requested_variant_ids.as_json, + order_variant_ids: order_variant_ids.as_json, + order: order.as_json, + line_items: order.line_items.as_json) + end + def variant_stock_levels(line_items) Hash[ line_items.map do |line_item|