Commit 4fb214bb authored by Mahmoud Aglan's avatar Mahmoud Aglan

fix: ensureSwissApiLink handles nested data response + better error messages

Swiss API wraps list responses in {data:[...]}. Also added null checks
at each step and detailed error messages so failures are diagnosable.
Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent cbe84447
...@@ -30,17 +30,22 @@ class BotSimulationService ...@@ -30,17 +30,22 @@ class BotSimulationService
// Get or create a Swiss API organization // Get or create a Swiss API organization
$orgResponse = SwissApiService::listOrganizations(); $orgResponse = SwissApiService::listOrganizations();
$orgs = SwissApiService::isSuccess($orgResponse) ? (SwissApiService::getBody($orgResponse) ?? []) : []; $orgsBody = SwissApiService::isSuccess($orgResponse) ? SwissApiService::getBody($orgResponse) : null;
$swissOrgId = $orgs[0]['id'] ?? null; $orgs = $orgsBody['data'] ?? $orgsBody ?? [];
$swissOrgId = is_array($orgs) && !empty($orgs) ? ($orgs[0]['id'] ?? null) : null;
if (!$swissOrgId) { if (!$swissOrgId) {
$createOrgResp = SwissApiService::createOrganization('El3ab Bot Testing', SWISS_API_EMAIL); $createOrgResp = SwissApiService::createOrganization('El3ab Bot Testing', SWISS_API_EMAIL);
if (!SwissApiService::isSuccess($createOrgResp)) { if (!SwissApiService::isSuccess($createOrgResp)) {
return ['success' => false, 'error' => 'فشل في إنشاء منظمة Swiss API']; return ['success' => false, 'error' => 'فشل في إنشاء منظمة Swiss API: ' . SwissApiService::getError($createOrgResp)];
} }
$swissOrgId = SwissApiService::getBody($createOrgResp)['id'] ?? null; $swissOrgId = SwissApiService::getBody($createOrgResp)['id'] ?? null;
} }
if (!$swissOrgId) {
return ['success' => false, 'error' => 'فشل في الحصول على معرف المنظمة'];
}
// Create event // Create event
$eventResp = SwissApiService::createEvent( $eventResp = SwissApiService::createEvent(
$swissOrgId, $swissOrgId,
...@@ -49,22 +54,30 @@ class BotSimulationService ...@@ -49,22 +54,30 @@ class BotSimulationService
date('Y-m-d', strtotime('+1 day')) date('Y-m-d', strtotime('+1 day'))
); );
if (!SwissApiService::isSuccess($eventResp)) { if (!SwissApiService::isSuccess($eventResp)) {
return ['success' => false, 'error' => 'فشل في إنشاء حدث Swiss API']; return ['success' => false, 'error' => 'فشل في إنشاء حدث Swiss API: ' . SwissApiService::getError($eventResp)];
} }
$swissEventId = SwissApiService::getBody($eventResp)['id'] ?? null; $swissEventId = SwissApiService::getBody($eventResp)['id'] ?? null;
if (!$swissEventId) {
return ['success' => false, 'error' => 'فشل في الحصول على معرف الحدث'];
}
// Create tournament // Create tournament
$tournResp = SwissApiService::createTournament($swissEventId, [ $tournResp = SwissApiService::createTournament($swissEventId, [
'name' => $tournament['name'] ?? 'Bot Test', 'name' => $tournament['name'] ?? 'Bot Test',
'tournamentType' => 'swiss', 'tournamentType' => 'swiss',
'roundsNumber' => $tournament['rounds_total'] ?? $tournament['swiss_rounds'] ?? 7, 'roundsNumber' => (int)($tournament['rounds_total'] ?? $tournament['swiss_rounds'] ?? 7),
'maxPlayers' => $tournament['max_players'] ?? 200, 'maxPlayers' => (int)($tournament['max_players'] ?? 200),
]); ]);
if (!SwissApiService::isSuccess($tournResp)) { if (!SwissApiService::isSuccess($tournResp)) {
return ['success' => false, 'error' => 'فشل في إنشاء بطولة Swiss API']; return ['success' => false, 'error' => 'فشل في إنشاء بطولة Swiss API: ' . SwissApiService::getError($tournResp)];
} }
$swissTournamentId = SwissApiService::getBody($tournResp)['id'] ?? null; $swissTournamentId = SwissApiService::getBody($tournResp)['id'] ?? null;
if (!$swissTournamentId) {
return ['success' => false, 'error' => 'فشل في الحصول على معرف البطولة'];
}
// Update local tournament with Swiss API link // Update local tournament with Swiss API link
$db->update('el3ab_tournaments', ['id' => "eq.{$tournamentId}"], [ $db->update('el3ab_tournaments', ['id' => "eq.{$tournamentId}"], [
'swiss_api_tournament_id' => $swissTournamentId, 'swiss_api_tournament_id' => $swissTournamentId,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment