|
| 1 | +import { Controller, Logger } from "@nestjs/common"; |
| 2 | +import { HasuraAction } from "../hasura/hasura.controller"; |
| 3 | +import { HasuraService } from "../hasura/hasura.service"; |
| 4 | +import { S3Service } from "../s3/s3.service"; |
| 5 | +import { User } from "../auth/types/User"; |
| 6 | + |
| 7 | +@Controller("tournaments") |
| 8 | +export class TournamentsController { |
| 9 | + constructor( |
| 10 | + private readonly logger: Logger, |
| 11 | + private readonly hasura: HasuraService, |
| 12 | + private readonly s3: S3Service, |
| 13 | + ) {} |
| 14 | + |
| 15 | + @HasuraAction() |
| 16 | + public async deleteTournament(data: { |
| 17 | + user: User; |
| 18 | + tournament_id: string; |
| 19 | + }) { |
| 20 | + const { tournament_id } = data; |
| 21 | + this.logger.log(`[${tournament_id}] deleting tournament`); |
| 22 | + |
| 23 | + // Query with user context for authorization checks |
| 24 | + const { tournaments_by_pk } = await this.hasura.query( |
| 25 | + { |
| 26 | + tournaments_by_pk: { |
| 27 | + __args: { |
| 28 | + id: tournament_id, |
| 29 | + }, |
| 30 | + id: true, |
| 31 | + status: true, |
| 32 | + is_organizer: true, |
| 33 | + }, |
| 34 | + }, |
| 35 | + data.user.steam_id, |
| 36 | + ); |
| 37 | + |
| 38 | + if (!tournaments_by_pk) { |
| 39 | + throw Error("tournament not found"); |
| 40 | + } |
| 41 | + |
| 42 | + if (!tournaments_by_pk.is_organizer) { |
| 43 | + throw Error("not the tournament organizer"); |
| 44 | + } |
| 45 | + |
| 46 | + if (tournaments_by_pk.status === "Live") { |
| 47 | + throw Error("cannot delete a live tournament"); |
| 48 | + } |
| 49 | + |
| 50 | + // Query as admin to access demo file paths |
| 51 | + const { tournaments_by_pk: tournament } = await this.hasura.query({ |
| 52 | + tournaments_by_pk: { |
| 53 | + __args: { |
| 54 | + id: tournament_id, |
| 55 | + }, |
| 56 | + stages: { |
| 57 | + brackets: { |
| 58 | + match: { |
| 59 | + id: true, |
| 60 | + match_maps: { |
| 61 | + demos: { |
| 62 | + id: true, |
| 63 | + file: true, |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + }); |
| 71 | + |
| 72 | + const demos: Array<{ id: string; file: string }> = []; |
| 73 | + const matchIds: string[] = []; |
| 74 | + |
| 75 | + for (const stage of tournament.stages || []) { |
| 76 | + for (const bracket of stage.brackets || []) { |
| 77 | + if (!bracket.match) { |
| 78 | + continue; |
| 79 | + } |
| 80 | + matchIds.push(bracket.match.id); |
| 81 | + for (const matchMap of bracket.match.match_maps || []) { |
| 82 | + for (const demo of matchMap.demos || []) { |
| 83 | + demos.push(demo); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + for (const demo of demos) { |
| 90 | + try { |
| 91 | + await this.s3.remove(demo.file); |
| 92 | + await this.hasura.mutation({ |
| 93 | + delete_match_map_demos_by_pk: { |
| 94 | + __args: { |
| 95 | + id: demo.id, |
| 96 | + }, |
| 97 | + __typename: true, |
| 98 | + }, |
| 99 | + }); |
| 100 | + } catch (error) { |
| 101 | + this.logger.error( |
| 102 | + `[${tournament_id}] failed to clean up demo ${demo.id}`, |
| 103 | + error, |
| 104 | + ); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + for (const matchId of matchIds) { |
| 109 | + try { |
| 110 | + await this.hasura.mutation({ |
| 111 | + delete_matches_by_pk: { |
| 112 | + __args: { |
| 113 | + id: matchId, |
| 114 | + }, |
| 115 | + __typename: true, |
| 116 | + }, |
| 117 | + }); |
| 118 | + } catch (error) { |
| 119 | + this.logger.error( |
| 120 | + `[${tournament_id}] failed to delete match ${matchId}`, |
| 121 | + error, |
| 122 | + ); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + await this.hasura.mutation({ |
| 127 | + delete_tournaments_by_pk: { |
| 128 | + __args: { |
| 129 | + id: tournament_id, |
| 130 | + }, |
| 131 | + __typename: true, |
| 132 | + }, |
| 133 | + }); |
| 134 | + |
| 135 | + this.logger.log( |
| 136 | + `[${tournament_id}] tournament deleted, cleaned up ${demos.length} demo files across ${matchIds.length} matches`, |
| 137 | + ); |
| 138 | + |
| 139 | + return { |
| 140 | + success: true, |
| 141 | + }; |
| 142 | + } |
| 143 | +} |
0 commit comments