Add bugsnag notifications to detect potential dead code scenarios in variants_stock_levels and cart_service

This commit is contained in:
luisramos0
2018-12-21 13:02:41 +00:00
parent d926f7c776
commit ea61e415e1
2 changed files with 24 additions and 4 deletions

View File

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

View File

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