Skip to content

Commit 5938c6b

Browse files
committed
fix: solve mismatch into sync matchs
1 parent e14a8d3 commit 5938c6b

2 files changed

Lines changed: 74 additions & 13 deletions

File tree

app/modules/matches/controllers/matches_controller.rb

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,9 @@ def stats
139139
end
140140

141141
def import
142-
player_id = validate_required_param!(:player_id)
143-
count = integer_param(:count, default: 20, min: 1, max: 100)
142+
player_id = validate_required_param!(:player_id)
143+
count = integer_param(:count, default: 20, min: 1, max: 100)
144+
force_update = params[:force_update].in?([true, 'true', '1'])
144145

145146
player = organization_scoped(Player).find(player_id)
146147

@@ -152,17 +153,20 @@ def import
152153
)
153154
end
154155

155-
job_id = ImportPlayerMatchesJob.perform_later(
156-
player.id,
157-
current_organization.id,
158-
count
159-
).job_id
160-
161-
render_success({
162-
job_id: job_id,
163-
player_id: player.id.to_s,
164-
count: count
165-
}, message: 'Match import queued successfully')
156+
result = ImportMatchesService.new(
157+
player: player,
158+
organization: current_organization,
159+
count: count,
160+
force_update: force_update
161+
).call
162+
163+
render_success(result, message: 'Matches import started successfully')
164+
rescue RiotApiService::RiotApiError => e
165+
render_error(
166+
message: "Riot API error: #{e.message}",
167+
code: 'RIOT_API_ERROR',
168+
status: :service_unavailable
169+
)
166170
end
167171

168172
private
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# frozen_string_literal: true
2+
3+
module Matches
4+
# Fetches match IDs from Riot API for a player and enqueues SyncMatchJob
5+
# for each new match. Returns counts synchronously so the caller can
6+
# respond with meaningful feedback without waiting for individual syncs.
7+
class ImportMatchesService
8+
def initialize(player:, organization:, count: 20, force_update: false)
9+
@player = player
10+
@organization = organization
11+
@count = count
12+
@force_update = force_update
13+
end
14+
15+
# @return [Hash] counts: total_matches_found, imported, already_imported, updated
16+
def call
17+
match_ids = fetch_match_ids
18+
tally = { imported: 0, already_imported: 0, updated: 0 }
19+
20+
match_ids.each { |id| process_match(id, tally) }
21+
22+
tally.merge(total_matches_found: match_ids.size)
23+
end
24+
25+
private
26+
27+
def fetch_match_ids
28+
RiotApiService.new.get_match_history(
29+
puuid: @player.riot_puuid,
30+
region: region,
31+
count: @count
32+
)
33+
end
34+
35+
def process_match(match_id, tally)
36+
if Match.exists?(riot_match_id: match_id)
37+
handle_existing_match(match_id, tally)
38+
else
39+
SyncMatchJob.perform_later(match_id, @organization.id, region)
40+
tally[:imported] += 1
41+
end
42+
end
43+
44+
def handle_existing_match(match_id, tally)
45+
if @force_update
46+
SyncMatchJob.perform_later(match_id, @organization.id, region, force_update: true)
47+
tally[:updated] += 1
48+
else
49+
tally[:already_imported] += 1
50+
end
51+
end
52+
53+
def region
54+
@player.region || 'BR'
55+
end
56+
end
57+
end

0 commit comments

Comments
 (0)