Class: SpreeCmCommissioner::Integrations::BookMeBusV1::Polling::SyncSeatLayout

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

Overview

SyncSeatLayout handles synchronization of seat layouts from BookMeBus API

This service is responsible for:

  • Fetching seat layout data from BookMeBus API for a specific trip

  • Creating or updating SeatLayout records for vehicle types

  • Creating Block records representing individual seats

  • Supporting multi-layer layouts (e.g., double-decker buses)

  • Managing integration mappings for seat layouts

  • Tracking sync results and metrics

BookMeBus API Payload Structure:

{
  "data": {
    "id": "2adea8ac4cf6a75db037f698a9126914",
    "type": "transit_seat_layouts",
    "attributes": {
      "from_stop_id": 233,
      "to_stop_id": 237,
      "on_date": "2025-12-30",
      "layouts": {
        "233": {
          "BUS": [
            [{"label": "1", "seat_type": 0, "status": 0}, {"label": "2"}, null, {"label": "3"}],
            [{"label": "5"}, {"label": "6"}, null, {"label": "7"}]
          ]
        }
      }
    }
  }
}

Usage:

sync_service = SyncSeatLayout.new(integration: integration, sync_result: sync_result)
seat_layout = sync_service.call(vehicle_type: vehicle_type, external_trip: external_trip)

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of SyncSeatLayout.



38
39
40
41
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/polling/sync_seat_layout.rb', line 38

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

Instance Method Details

#call(vehicle_type:, external_trip:, variant: nil, inventory_item: nil) ⇒ Object



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

def call(vehicle_type:, external_trip:, variant: nil, inventory_item: nil)
  return nil if vehicle_type.blank? || external_trip.blank?

  external_seat_layout = fetch_external_seat_layout(external_trip)
  return nil if external_seat_layout.blank?

  # Sync vehicle type template
  vehicle_seat_layout = sync_vehicle_type_template!(vehicle_type: vehicle_type, external_seat_layout: external_seat_layout)

  # If variant provided, duplicate template to trip and sync trip-specific layout
  if variant.present?
    duplicate_to_trip_if_needed!(vehicle_seat_layout: vehicle_seat_layout, variant: variant)
    sync_trip_seat_layout!(external_seat_layout: external_seat_layout, variant: variant, inventory_item: inventory_item)
  end

  vehicle_seat_layout
rescue SpreeCmCommissioner::Integrations::ExternalClientError => e
  @sync_result.record_error(SpreeCmCommissioner::Integrations::SyncError.new("Failed to sync seat layout: #{e.message}"))
  nil
rescue StandardError => e
  @sync_result.record_error(StandardError.new("[SyncSeatLayout] #{e.message}"))
  nil
end