Class: SpreeCmCommissioner::Integrations::BookMeBusV1::Inventory::UnstockInventory
- Inherits:
-
Object
- Object
- SpreeCmCommissioner::Integrations::BookMeBusV1::Inventory::UnstockInventory
- Includes:
- Spree::ServiceModule::Base
- Defined in:
- app/services/spree_cm_commissioner/integrations/book_me_bus_v1/inventory/unstock_inventory.rb
Instance Method Summary collapse
-
#build_reserved_seats(line_item, from_stop_id) ⇒ Object
Build reserved seats hash for BookMeBus API.
- #call(integration:, order:, line_items:) ⇒ Object
-
#confirm_payment!(integration, reservation_cart, order) ⇒ Object
Create and confirm wallet payment for the reservation.
-
#find_trip_stop_mapping(integration, trip_stop_id) ⇒ Object
Find active trip stop mapping for integration.
-
#sync_guests!(guests, reservation_cart, integration) ⇒ Object
Creates guest mappings and stores QR codes for check-in.
-
#sync_line_item!(integration, order, line_item) ⇒ Object
Sync a single line item with BookMeBus.
Instance Method Details
#build_reserved_seats(line_item, from_stop_id) ⇒ Object
Build reserved seats hash for BookMeBus API
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/inventory/unstock_inventory.rb', line 119 def build_reserved_seats(line_item, from_stop_id) # rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity seats = line_item.guests.map do |guest| seat_label = guest.block&.label next if seat_label.blank? name = [guest.first_name, guest.last_name].compact.join(' ') name = "Guest #{guest.id}" if name.blank? block_type = guest.block&.block_type&.to_s { name: name, label: seat_label, seat_type: block_type == 'seat' || block_type.blank? ? 'normal' : block_type, layer: guest.block&.seat_section&.name || 'BUS', nationality: (guest.nationality&.code || 'other').downcase } end.compact seats.empty? ? {} : { from_stop_id.to_s => seats } end |
#call(integration:, order:, line_items:) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/inventory/unstock_inventory.rb', line 4 def call(integration:, order:, line_items:) ApplicationRecord.transaction do line_items.each do |line_item| raise SpreeCmCommissioner::Integrations::SyncError, 'Invalid guests' if line_item.guests.size != line_item.quantity sync_line_item!(integration, order, line_item) end success(order: order, line_items: line_items) end rescue SpreeCmCommissioner::Integrations::SyncError, SpreeCmCommissioner::Integrations::ExternalClientError => e failure(nil, e.) end |
#confirm_payment!(integration, reservation_cart, order) ⇒ Object
Create and confirm wallet payment for the reservation
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/inventory/unstock_inventory.rb', line 92 def confirm_payment!(integration, reservation_cart, order) wallet_payment = integration.client.create_wallet_payment!( cart_id: reservation_cart.id, internal_order: order ) confirmed_payment = integration.client.confirm_wallet_payment!( payment_id: wallet_payment.id, internal_order: order ) raise SpreeCmCommissioner::Integrations::SyncError, 'Payment confirmation failed' unless confirmed_payment.paid? end |
#find_trip_stop_mapping(integration, trip_stop_id) ⇒ Object
Find active trip stop mapping for integration
107 108 109 110 111 112 113 114 115 116 |
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/inventory/unstock_inventory.rb', line 107 def find_trip_stop_mapping(integration, trip_stop_id) return nil if trip_stop_id.blank? SpreeCmCommissioner::IntegrationMapping.find_by( integration_id: integration.id, internal_type: 'SpreeCmCommissioner::TripStop', internal_id: trip_stop_id, status: :active ) end |
#sync_guests!(guests, reservation_cart, integration) ⇒ Object
Creates guest mappings and stores QR codes for check-in
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/inventory/unstock_inventory.rb', line 76 def sync_guests!(guests, reservation_cart, integration) reservation = reservation_cart.first_reservation return unless reservation guests.each do |guest| guest_mapping = guest.integration_mappings.find_or_initialize_by( integration: integration, external_id: reservation.id ) guest_mapping.external_qr_data = reservation.reservation_qr_code_url guest_mapping.mark_as_active!(external_payload: reservation.to_h) end end |
#sync_line_item!(integration, order, line_item) ⇒ Object
Sync a single line item with BookMeBus
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 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 67 68 69 70 71 72 73 |
# File 'app/services/spree_cm_commissioner/integrations/book_me_bus_v1/inventory/unstock_inventory.rb', line 20 def sync_line_item!(integration, order, line_item) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity return if line_item.integration_mappings.exists?(integration_id: integration.id, status: :active) internal_trip_id = line_item.trip_id raise SpreeCmCommissioner::Integrations::SyncError, 'trip_id missing from line item metadata' if internal_trip_id.blank? trip_mapping = SpreeCmCommissioner::IntegrationMapping.find_by( integration_id: integration.id, internal_type: 'SpreeCmCommissioner::Trip', internal_id: internal_trip_id, status: :active ) raise SpreeCmCommissioner::Integrations::SyncError, "Integration mapping not found for trip ##{internal_trip_id}" if trip_mapping.nil? external_trip_id = trip_mapping.external_id raise SpreeCmCommissioner::Integrations::SyncError, 'External trip_id missing from mapping' if external_trip_id.blank? boarding_mapping = find_trip_stop_mapping(integration, line_item.boarding_trip_stop_id) drop_off_mapping = find_trip_stop_mapping(integration, line_item.drop_off_trip_stop_id) from_stop_id = boarding_mapping&.external_payload&.dig('stop', 'id') || trip_mapping.external_payload&.dig('from_stop_id') to_stop_id = drop_off_mapping&.external_payload&.dig('stop', 'id') || trip_mapping.external_payload&.dig('to_stop_id') from_stop_time_id = boarding_mapping&.external_payload&.dig('id') to_stop_time_id = drop_off_mapping&.external_payload&.dig('id') address = order.bill_address || order.ship_address reservation_cart = integration.client.create_reservation!( { trip_id: external_trip_id.to_i, from_stop_id: from_stop_id.to_i, to_stop_id: to_stop_id.to_i, from_stop_time_id: from_stop_time_id, to_stop_time_id: to_stop_time_id, departure_date: line_item.from_date.to_date.strftime('%Y-%m-%d'), reserved_seats: build_reserved_seats(line_item, from_stop_id), contact_name: address&.full_name.presence || order.email, contact_email: order.email, contact_phone: address&.phone.presence || order.user&.phone, internal_order: order } ) sync_guests!(line_item.guests, reservation_cart, integration) line_item_mapping = line_item.integration_mappings.find_or_initialize_by( integration: integration, external_id: reservation_cart.id ) line_item_mapping.mark_as_active!(external_payload: reservation_cart.to_h) confirm_payment!(integration, reservation_cart, order) end |