Class: VendorsService::InventorySync::ReservationService
- Inherits:
-
Object
- Object
- VendorsService::InventorySync::ReservationService
- Includes:
- ElasticAPM::SpanHelpers, SupplierConfig
- Defined in:
- app/services/vendors_service/inventory_sync/reservation_service.rb
Overview
Service for handling reservation inventory sync operations across different suppliers. This service provides a unified interface for syncing inventory when reservations are rejected due to inventory unavailability or when new reservations are created. It uses supplier-specific configurations and implements cooldown logic to prevent excessive API calls.
The service supports two trigger types:
-
:inv_mismatch - Used when inventory mismatches are detected, applies cooldown logic
-
:reservation_created - Used when new reservations are created, bypasses cooldown for immediate sync
The service handles comprehensive APM instrumentation for monitoring sync operations and uses BUSINESS_LOGGER for structured logging with OpenSearch compatibility. All errors are reported through APMErrorHandler for proper error tracking and diagnostics.
Constant Summary
Constants included from SupplierConfig
SupplierConfig::SUPPLIER_CONFIG
Instance Method Summary collapse
-
#initialize(supplier) ⇒ ReservationService
constructor
Initializes the service with supplier-specific configuration.
-
#sync_if_needed(restaurant_id:, date:, trigger_type:) ⇒ Hash
Syncs inventory for a restaurant on a specific date based on the trigger type.
Methods included from SupplierConfig
Constructor Details
#initialize(supplier) ⇒ ReservationService
Initializes the service with supplier-specific configuration
43 44 45 46 |
# File 'app/services/vendors_service/inventory_sync/reservation_service.rb', line 43 def initialize(supplier) @supplier = supplier.to_sym @config = get_supplier_config(supplier) end |
Instance Method Details
#sync_if_needed(restaurant_id:, date:, trigger_type:) ⇒ Hash
Syncs inventory for a restaurant on a specific date based on the trigger type.
This method implements different sync strategies based on the trigger type:
-
:inv_mismatch: Uses cooldown mechanism to prevent excessive API calls, only proceeds if enough time has passed since the last sync or if this is the first sync
-
:reservation_created: Bypasses cooldown and schedules sync immediately to ensure real-time inventory updates for new reservations
The method provides comprehensive APM instrumentation with spans for each major operation and sets relevant labels for filtering and monitoring. All business logic is logged using BUSINESS_LOGGER with structured JSON format for OpenSearch compatibility.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'app/services/vendors_service/inventory_sync/reservation_service.rb', line 69 def sync_if_needed(restaurant_id:, date:, trigger_type:) # Validate trigger type unless [:inv_mismatch, :reservation_created].include?(trigger_type) = "Unknown trigger_type: #{trigger_type}. Supported types: :inv_mismatch, :reservation_created" APMErrorHandler.report( 'Invalid trigger type for reservation inventory sync', context: { restaurant_id: restaurant_id, date: date, supplier: @supplier.to_s, trigger_type: trigger_type, service_class: self.class.name, error_message: , }, ) raise NotImplementedError, end # Set business context set_context_and_labels(restaurant_id, date, trigger_type) BUSINESS_LOGGER.info( "#{self.class}: Checking sync eligibility", { restaurant_id: restaurant_id, date: date, supplier: @supplier.to_s, trigger_type: trigger_type, } ) # Handle different trigger types case trigger_type when :inv_mismatch # Use stale check logic check_and_process_sync(restaurant_id, date, trigger_type) when :reservation_created # Skip stale check and sync directly process_direct_sync(restaurant_id, date, trigger_type) end rescue StandardError => e handle_sync_error(e, restaurant_id, date, trigger_type) raise e end |