@@ -3,6 +3,7 @@ CREATE OR REPLACE FUNCTION public.tau_tournaments() RETURNS TRIGGER
33 AS $$
44DECLARE
55 first_stage_id uuid;
6+ bracket_row tournament_brackets%ROWTYPE;
67BEGIN
78 IF (
89 NEW .status IS DISTINCT FROM OLD .status AND
3031 END IF;
3132 END IF;
3233
34+ -- When tournament resumes from Paused, schedule all ready brackets
35+ IF (
36+ NEW .status IS DISTINCT FROM OLD .status AND
37+ OLD .status = ' Paused' AND NEW .status = ' Live'
38+ ) THEN
39+ FOR bracket_row IN
40+ SELECT tb.*
41+ FROM tournament_brackets tb
42+ INNER JOIN tournament_stages ts ON ts .id = tb .tournament_stage_id
43+ WHERE ts .tournament_id = NEW .id
44+ AND tb .match_id IS NULL
45+ AND tb .finished = false
46+ AND tb .tournament_team_id_1 IS NOT NULL
47+ AND tb .tournament_team_id_2 IS NOT NULL
48+ ORDER BY tb .round , tb .match_number
49+ LOOP
50+ PERFORM schedule_tournament_match(bracket_row);
51+ END LOOP;
52+
53+ PERFORM calculate_tournament_bracket_start_times(NEW .id );
54+ END IF;
55+
3356 RETURN NEW;
3457END;
3558$$;
7194 RAISE EXCEPTION USING ERRCODE = ' 22000' , MESSAGE = ' Cannot close tournament registration' ;
7295 END IF;
7396 WHEN ' Live' THEN
74- IF NOT tournament_has_min_teams(NEW) THEN
75- NEW .status = ' CancelledMinTeams' ;
97+ IF OLD .status = ' Paused' THEN
98+ IF NOT can_resume_tournament(OLD, current_setting(' hasura.user' , true)::json) THEN
99+ RAISE EXCEPTION USING ERRCODE = ' 22000' , MESSAGE = ' Cannot resume tournament' ;
100+ END IF;
101+ ELSE
102+ IF NOT tournament_has_min_teams(NEW) THEN
103+ NEW .status = ' CancelledMinTeams' ;
104+ END IF;
105+ END IF;
106+ WHEN ' Paused' THEN
107+ IF NOT can_pause_tournament(OLD, current_setting(' hasura.user' , true)::json) THEN
108+ RAISE EXCEPTION USING ERRCODE = ' 22000' , MESSAGE = ' Cannot pause tournament' ;
76109 END IF;
77110 ELSE
78111 -- No action needed for other status changes
0 commit comments