Class: SpreeCmCommissioner::Integrations::BookMeBusV1::Polling::SyncItemAvailability

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

Overview

SyncItemAvailability syncs pricing and reserved blocks from BookMeBus API

BookMeBus trips have an ‘is_selectable` flag that determines sync behavior:

  • is_selectable == true: Sync reserved blocks (booked seats)

  • is_selectable == false: Only update seats_remaining count

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.



14
15
16
17
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/polling/sync_item_availability.rb', line 14

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



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/polling/sync_item_availability.rb', line 19

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: '[BookMeBusV1::SyncItemAvailability] sync failed', data: { error: e.message })
  @sync_result
end

#sync_item(trip, on_date, inventory_item) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/polling/sync_item_availability.rb', line 42

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
    external_trip = fetch_external_trip(trip, on_date)

    return unless external_trip # rubocop:disable Rails/TransactionExitStatement

    sync_price(inventory_item, external_trip)

    # Sync strategy depends on trip type and seat availability
    if should_sync_reserved_blocks?(external_trip)
      sync_reserved_blocks(external_trip, on_date, inventory_item)
    else
      sync_stock_item(inventory_item, external_trip)
    end

    update_last_sync_at(inventory_item_mapping)
  end
rescue StandardError => e
  @sync_result.record_error(e)
  CmAppLogger.error(label: '[BookMeBusV1::SyncItemAvailability] error', data: { inventory_item_id: inventory_item.id, error: e.message })
end