Class: Item::Materials::Check
- Inherits:
-
BaseService
- Object
- BaseService
- Item::Materials::Check
- Defined in:
- app/services/item/materials/check.rb
Overview
Service object: check.
Defined Under Namespace
Classes: Result
Constant Summary collapse
- CHECKS =
Checks.
%i[ engineering_alert control_capacity wood_shake_roof floor_heating snow_melting roof_deicing infrared_heating_panel towel_warmer recommended membrane moq tz_cable_tape_roll ].freeze
- COMMON_SKUS =
Common SKUs used across material checks - preload these once to avoid N+1 Item.find_by calls
[ ItemConstants::POWER_MODULE_SKU, ItemConstants::TZ_CABLE_TAPE_SKU ].freeze
Class Method Summary collapse
-
.find_item_by_sku(sku) ⇒ Object
Class method to get a preloaded item by SKU (called by individual check classes).
Instance Method Summary collapse
- #applied_stock_threshold_and_customer_filters_pass?(container, material_alert) ⇒ Boolean protected
- #apply_stock_threshold_and_customer_filters(container, material_alerts = []) ⇒ Object protected
-
#perform_checks_raw(container:, options: {}) ⇒ Array<Hash>
Returns raw check results as an array of hashes for database persistence.
-
#process(container:, for_www: false, for_cart: false, _cache: true) ⇒ Object
deprecated
Deprecated.
Use Pickable#get_material_alerts which uses database persistence
Class Method Details
.find_item_by_sku(sku) ⇒ Object
Class method to get a preloaded item by SKU (called by individual check classes)
73 74 75 76 |
# File 'app/services/item/materials/check.rb', line 73 def self.find_item_by_sku(sku) preloaded = Thread.current[:material_check_preloaded_items] preloaded&.[](sku) || Item.find_by(sku: sku) end |
Instance Method Details
#applied_stock_threshold_and_customer_filters_pass?(container, material_alert) ⇒ Boolean (protected)
111 112 113 114 115 116 117 118 119 120 121 |
# File 'app/services/item/materials/check.rb', line 111 def applied_stock_threshold_and_customer_filters_pass?(container, material_alert) return true if material_alert.unmaskable return true if material_alert.items.blank? # If no item are in the material alert, do not run this filter catalog = container.customer.catalog material_alert.items.delete_if do |item| ci = catalog.catalog_items.by_skus(item.sku).first !(ci&.item&.suggested_item_applies_to(container.customer) and ci.qty_available > item.suggested_item_stock_threshold) end material_alert.items.present? end |
#apply_stock_threshold_and_customer_filters(container, material_alerts = []) ⇒ Object (protected)
107 108 109 |
# File 'app/services/item/materials/check.rb', line 107 def apply_stock_threshold_and_customer_filters(container, material_alerts = []) material_alerts.select { |ma| applied_stock_threshold_and_customer_filters_pass?(container, ma) } end |
#perform_checks_raw(container:, options: {}) ⇒ Array<Hash>
Returns raw check results as an array of hashes for database persistence.
This is called by Pickable#get_material_alerts to generate fresh alerts.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'app/services/item/materials/check.rb', line 45 def perform_checks_raw(container:, options: {}) # Preload commonly used items to avoid N+1 queries in individual checks preload_common_items material_alerts = [] CHECKS.each do |check| check_class = "Item::Materials::Checks::#{check.to_s.classify}".constantize check_instance = check_class.new result = check_instance.process(container: container, options: ) material_alerts += result.alerts if result.status == :ok end material_alerts = apply_stock_threshold_and_customer_filters(container, material_alerts) # Convert Virtus Alert objects to plain hashes for database storage material_alerts.map do |alert| { name: alert.name, items: alert.items.to_a.compact, recommended_qty: alert.recommended_qty, actual_qty: alert.actual_qty, group_name: alert.group_name, group_type: alert.group_type, unmaskable: alert.unmaskable || false } end end |
#process(container:, for_www: false, for_cart: false, _cache: true) ⇒ Object
Deprecated.
Use Pickable#get_material_alerts which uses database persistence
Legacy method - kept for backward compatibility during transition
33 34 35 36 |
# File 'app/services/item/materials/check.rb', line 33 def process(container:, for_www: false, for_cart: false, _cache: true) = { for_www: for_www, for_cart: for_cart } perform_checks(container, ) end |