Class: SpreeCmCommissioner::Integrations::Larryta::Polling::SyncItemAvailability

Inherits:
Object
  • Object
show all
Includes:
InventorySyncCachable
Defined in:
app/services/spree_cm_commissioner/integrations/larryta/polling/sync_item_availability.rb

Overview

SyncItemAvailability syncs pricing and reserved blocks from Larryta API

Usage:

SyncItemAvailability.new(integration: integration, sync_result: sync_result)
  .call(trip_id: 1, on_date: '2026-04-06')

Instance Method Summary collapse

Methods included from InventorySyncCachable

#sync_cache_ttl

Constructor Details

#initialize(integration:, sync_result: nil) ⇒ SyncItemAvailability

Returns a new instance of SyncItemAvailability.



10
11
12
13
# File 'app/services/spree_cm_commissioner/integrations/larryta/polling/sync_item_availability.rb', line 10

def initialize(integration:, sync_result: nil)
  @integration = integration
  @sync_result = sync_result || SpreeCmCommissioner::Integrations::Base::SyncResult.new
end

Instance Method Details

#call(trip_id:, on_date:) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/services/spree_cm_commissioner/integrations/larryta/polling/sync_item_availability.rb', line 15

def call(trip_id:, on_date:)
  trip = find_trip(trip_id)
  return @sync_result unless trip

  inventory_items = find_inventory_items_for_trip(trip, on_date)
  return @sync_result if inventory_items.empty?

  inventory_items.each do |inventory_item|
    sync_item(trip, on_date, inventory_item)
  end

  # return the most recent last_synced_at from the inventory item mappings for this trip and date
  @sync_result.last_synced_at =
    inventory_items.last
                   &.integration_mappings
                   &.maximum(:last_synced_at)

  @sync_result
rescue StandardError => e
  @sync_result.record_error(e)
  CmAppLogger.error(label: '[Larryta::SyncItemAvailability] sync failed', data: { error: e.message })
  @sync_result
end

#sync_item(trip, on_date, inventory_item) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/services/spree_cm_commissioner/integrations/larryta/polling/sync_item_availability.rb', line 39

def sync_item(trip, on_date, inventory_item)
  inventory_item_mapping = find_or_initialize_item_mapping(inventory_item)

  return unless should_sync_item?(inventory_item_mapping)

  ActiveRecord::Base.transaction do
    sync_price(trip, on_date, inventory_item, inventory_item_mapping)
    sync_reserved_blocks(trip, on_date, inventory_item) if trip.allow_seat_selection?
    update_last_sync_at(inventory_item_mapping)
  end
rescue StandardError => e
  @sync_result.record_error(e)
  CmAppLogger.error(label: '[Larryta::SyncItemAvailability] error syncing item', data: { inventory_item_id: inventory_item.id, error: e.message })
end