Class: VendorsService::InventorySync::ReservationService

Inherits:
Object
  • Object
show all
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.

Examples:

Basic usage with inventory mismatch trigger

service = VendorsService::InventorySync::ReservationService.new(:tablecheck)
result = service.sync_if_needed(restaurant_id: 123, date: '2024-01-15', trigger_type: :inv_mismatch)
puts result[:action] # => 'sync_scheduled' or 'sync_skipped'

With reservation created trigger (bypasses cooldown)

service = VendorsService::InventorySync::ReservationService.new(:sevenrooms)
result = service.sync_if_needed(restaurant_id: 456, date: '2024-01-16', trigger_type: :reservation_created)
puts result[:action] # => 'sync_scheduled' (always schedules sync)

With error handling

begin
  service = VendorsService::InventorySync::ReservationService.new(:sevenrooms)
  result = service.sync_if_needed(restaurant_id: 456, date: '2024-01-16', trigger_type: :inv_mismatch)
rescue StandardError => e
  # Error is automatically reported to APM and logged via BUSINESS_LOGGER
  puts "Sync failed: #{e.message}"
end

Constant Summary

Constants included from SupplierConfig

SupplierConfig::SUPPLIER_CONFIG

Instance Method Summary collapse

Methods included from SupplierConfig

#get_supplier_config

Constructor Details

#initialize(supplier) ⇒ ReservationService

Initializes the service with supplier-specific configuration

Parameters:

  • supplier (Symbol)

    The supplier type (:tablecheck, :sevenrooms)

Raises:

  • (NotImplementedError)

    if supplier is not supported



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.

Parameters:

  • restaurant_id (Integer)

    ID of the restaurant to sync

  • date (String)

    Date to sync in 'YYYY-MM-DD' format

  • trigger_type (Symbol)

    Type of sync trigger (:inv_mismatch or :reservation_created)

Returns:

  • (Hash)

    Result hash with sync status and metadata

    • :success [Boolean] Whether the operation completed successfully

    • :action [String] 'sync_scheduled' if sync was triggered, 'sync_skipped' if within cooldown

    • :last_sync_record [ActiveRecord::Base, nil] The sync tracking record (nil for :reservation_created)

Raises:

  • (NotImplementedError)

    if trigger_type is not supported

  • (StandardError)

    Re-raises any errors after logging and APM reporting



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)
    error_message = "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: error_message,
      },
    )

    raise NotImplementedError, error_message
  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