Class: KitWeightsDimensionsHandler

Inherits:
ApplicationJob show all
Includes:
RailsEventStore::AsyncHandler
Defined in:
app/subscribers/kit_weights_dimensions_handler.rb

Overview

Async subscriber for Events::KitComponentChanged.

Runs the combinatorial half of Item::KitConsolidator#consolidate_all_fields
consolidate_weights_and_dimensions, which calls
Item::ShippingBoxCalculator#fitting_rotation (Array#permutation.uniq
over the kit's component shipping-box dimensions). For cable-system kits
with many components, that step took 100s–400s per kit when wired to
ItemRelation#after_commit inline (incident: UDG4-4999-WY cutover, PR
#918/#921). Moving it async unblocks the request cycle and lets the
handler short-circuit when no shipping change is actually possible.

Subscribes to: Events::KitComponentChanged

Skip semantics (each short-circuit avoids the ShippingBoxCalculator call):

  • kit row gone or no longer a kit (last component removed)
  • kit has unique_kit_dimensions = true (manual override — explicit
    opt-out from auto-rolled dimensions)
  • reason == :swapped AND prior_target and new target are physically
    identical (same gross/base/shipping weights, same shipping L×W×H).
    This is the branded → unbranded substitution case (e.g.
    UDG4-4999-WY → UDG4-4999) where the recompute would produce the
    exact same packing as it already has.

Constant Summary collapse

PHYSICAL_ATTRIBUTES =
%i[
  gross_weight base_weight shipping_weight
  shipping_length shipping_width shipping_height
].freeze

Instance Method Summary collapse

Instance Method Details

#perform(event) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/subscribers/kit_weights_dimensions_handler.rb', line 40

def perform(event)
  kit = Item.find_by(id: event.data[:kit_id])
  return unless kit&.is_kit?
  return if kit.unique_kit_dimensions

  return if physically_identical_swap?(event)

  Item::KitConsolidator.new(kit).consolidate_weights_and_dimensions.commit
rescue StandardError => e
  ErrorReporting.error(e)
  raise
end