Skip to content

Commit 34a0a78

Browse files
committed
feat: add pause/resume validation and catch-up scheduling to tournament triggers
1 parent e03de3b commit 34a0a78

1 file changed

Lines changed: 35 additions & 2 deletions

File tree

hasura/triggers/tournaments.sql

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ CREATE OR REPLACE FUNCTION public.tau_tournaments() RETURNS TRIGGER
33
AS $$
44
DECLARE
55
first_stage_id uuid;
6+
bracket_row tournament_brackets%ROWTYPE;
67
BEGIN
78
IF (
89
NEW.status IS DISTINCT FROM OLD.status AND
@@ -30,6 +31,28 @@ BEGIN
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;
3457
END;
3558
$$;
@@ -71,8 +94,18 @@ BEGIN
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

Comments
 (0)