Class: SpreeCmCommissioner::Integrations::StadiumXV1::Polling::SyncZones

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

Instance Method Summary collapse

Constructor Details

#initialize(client:, integration:, sync_result:) ⇒ SyncZones

Returns a new instance of SyncZones.



4
5
6
7
8
# File 'app/services/spree_cm_commissioner/integrations/stadium_x_v1/polling/sync_zones.rb', line 4

def initialize(client:, integration:, sync_result:)
  @client = client
  @integration = integration
  @sync_result = sync_result
end

Instance Method Details

#call(external_match_id:) ⇒ Object

Raises:



10
11
12
13
14
15
16
17
18
19
20
# File 'app/services/spree_cm_commissioner/integrations/stadium_x_v1/polling/sync_zones.rb', line 10

def call(external_match_id:)
  raise SpreeCmCommissioner::Integrations::SyncError, 'external_match_id is required' if external_match_id.nil?

  match_mapping = Spree::Taxon.find_integration_mapping(integration_id: @integration.id, external_id: external_match_id)
  raise SpreeCmCommissioner::Integrations::SyncError, "Match not found: #{external_match_id}" if match_mapping.blank?

  match_section = match_mapping.internal.children.first_or_create! { |s| s.name = 'Zones' }

  synced_zone_mappings = sync_zones!(match_mapping, match_section)
  cleanup_stale_mappings!(match_mapping, synced_zone_mappings, match_section)
end

#sync_zones!(match_mapping, match_section) ⇒ Object

Sync zones for a specific club and match

Raises:



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
# File 'app/services/spree_cm_commissioner/integrations/stadium_x_v1/polling/sync_zones.rb', line 23

def sync_zones!(match_mapping, match_section)
  external_club_id = match_mapping.external_payload&.dig('club_id')
  raise SpreeCmCommissioner::Integrations::SyncError, 'club_id is required' if external_club_id.blank?

  external_zones = begin
    @sync_result.track_api_call('get_zones') { @client.get_zones!(club_id: external_club_id, match_id: match_mapping.external_id) }
  rescue SpreeCmCommissioner::Integrations::ExternalClientError => e
    raise SpreeCmCommissioner::Integrations::SyncError, "Failed to sync zones match #{match_mapping.external_id}: #{e.message}"
  end

  # match ID is not returned by API, but it is important info when creating tickets in the external system
  # We need to set it manually on each zone object so that it can be used when creating tickets.
  # This is a temporary workaround until the API returns the match ID
  external_zones.each do |zone|
    zone.set_match_id(match_mapping.external_id)
  end

  synced_zone_mappings = external_zones.map do |external_zone|
    sync_zone!(external_zone, match_mapping.internal, match_section)
  end

  CmAppLogger.log(
    label: 'SyncZones#sync_zones! Zones synced for match',
    data: { external_match_id: match_mapping.external_id }
  )

  synced_zone_mappings
end