Commit 107474d6 authored by Mahmoud Aglan's avatar Mahmoud Aglan

fix: handle JSONB columns returned as arrays from PostgREST

PostgREST auto-decodes JSONB columns, so json_decode() fails when
the value is already an array. Added is_array() checks before decoding.
Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent a11f4199
......@@ -142,8 +142,10 @@ function getPairings(): void {
if (empty($round) || isset($round['error'])) jsonResponse(['pairings' => []]);
$pairings = json_decode($round[0]['pairings'] ?? '[]', true);
$results = json_decode($round[0]['results'] ?? '[]', true);
$rawP = $round[0]['pairings'] ?? '[]';
$pairings = is_array($rawP) ? $rawP : json_decode($rawP, true);
$rawR = $round[0]['results'] ?? '[]';
$results = is_array($rawR) ? $rawR : json_decode($rawR, true);
jsonResponse(['pairings' => $pairings, 'results' => $results]);
}
......
......@@ -53,7 +53,8 @@ function handleCreateOrJoin($db, string $userId, array $input): void {
if (!$round) jsonError('Round not found', 404);
if ($round['status'] !== 'in_progress') jsonError('Round is not in progress');
$pairings = json_decode($round['pairings'] ?? '[]', true);
$rawPairings = $round['pairings'] ?? '[]';
$pairings = is_array($rawPairings) ? $rawPairings : json_decode($rawPairings, true);
if (!isset($pairings[$pairingIndex])) jsonError('Pairing not found');
$pairing = $pairings[$pairingIndex];
......@@ -164,7 +165,8 @@ function handleReportResult($db, string $userId, array $input): void {
if ($match['tournament_id'] !== $tournamentId) jsonError('Match does not belong to this tournament');
// Check if already reported (idempotent)
$metadata = json_decode($match['metadata'] ?? '{}', true);
$rawMeta = $match['metadata'] ?? '{}';
$metadata = is_array($rawMeta) ? $rawMeta : json_decode($rawMeta, true);
if (!empty($metadata['tournament_reported'])) {
jsonResponse(['ok' => true, 'already_reported' => true]);
}
......@@ -202,7 +204,8 @@ function handleReportResult($db, string $userId, array $input): void {
if (is_array($roundsData) && !empty($roundsData) && !isset($roundsData['error'])) {
$roundRow = $roundsData[0];
$results = json_decode($roundRow['results'] ?? '[]', true);
$rawResults = $roundRow['results'] ?? '[]';
$results = is_array($rawResults) ? $rawResults : json_decode($rawResults, true);
$results[] = [
'match_id' => $matchId,
'result' => $swissResult,
......@@ -278,7 +281,8 @@ function handleMyPending($db, string $userId): void {
if (!is_array($rounds) || empty($rounds) || isset($rounds['error'])) continue;
$round = $rounds[0];
$pairings = json_decode($round['pairings'] ?? '[]', true);
$rawPairings = $round['pairings'] ?? '[]';
$pairings = is_array($rawPairings) ? $rawPairings : json_decode($rawPairings, true);
foreach ($pairings as $idx => $pairing) {
$playerA = $pairing['player_a'] ?? $pairing['white_id'] ?? null;
......
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