From b9881fcb6f2b57ea67fed5e5b3be2946dc62d6b3 Mon Sep 17 00:00:00 2001 From: dahyeon Date: Fri, 19 Jun 2026 03:54:40 -0700 Subject: [PATCH] fix: allow public tournament code replacement --- ...nament-code-replacement.controller.spec.ts | 47 +++++++++++++++++++ .../tournament-code-replacement.controller.ts | 30 ++++++++++++ .../tournament-code.controller.ts | 16 ------- .../tournament-code/tournament-code.module.ts | 3 +- .../tournament-code.service.spec.ts | 40 ++++++++++++---- .../tournament-code.service.ts | 6 ++- apps/web/src/pages/MatchDetailPage.tsx | 2 +- 7 files changed, 117 insertions(+), 27 deletions(-) create mode 100644 apps/api/src/modules/tournament-code/tournament-code-replacement.controller.spec.ts create mode 100644 apps/api/src/modules/tournament-code/tournament-code-replacement.controller.ts diff --git a/apps/api/src/modules/tournament-code/tournament-code-replacement.controller.spec.ts b/apps/api/src/modules/tournament-code/tournament-code-replacement.controller.spec.ts new file mode 100644 index 00000000..8c39a85b --- /dev/null +++ b/apps/api/src/modules/tournament-code/tournament-code-replacement.controller.spec.ts @@ -0,0 +1,47 @@ +import mongoose from 'mongoose'; +import { METADATA_KEY_IS_PUBLIC_ACCESS_ALLOWED } from '../auth/allow-public-access.decorator'; +import { TournamentCodeReplacementController } from './tournament-code-replacement.controller'; +import type { TournamentCodeService } from './tournament-code.service'; + +describe('TournamentCodeReplacementController', () => { + function createFixture() { + const service = { + replaceForMatchGame: jest.fn().mockResolvedValue({ + gameNumber: 1, + code: 'NEW-CODE', + }), + } as unknown as jest.Mocked< + Pick + >; + const controller = new TournamentCodeReplacementController( + service as unknown as TournamentCodeService, + ); + const communityId = new mongoose.Types.ObjectId(); + const matchId = new mongoose.Types.ObjectId(); + + return { controller, service, communityId, matchId }; + } + + it('allows public replacement requests', async () => { + const { controller, service, communityId, matchId } = createFixture(); + + await expect( + controller.replaceForMatchGame(communityId, matchId, 1), + ).resolves.toEqual(expect.objectContaining({ code: 'NEW-CODE' })); + + expect(service.replaceForMatchGame).toHaveBeenCalledWith({ + communityId, + matchId, + gameNumber: 1, + }); + }); + + it('marks the replacement endpoint as public access', () => { + expect( + Reflect.getMetadata( + METADATA_KEY_IS_PUBLIC_ACCESS_ALLOWED, + TournamentCodeReplacementController.prototype.replaceForMatchGame, + ), + ).toBe(true); + }); +}); diff --git a/apps/api/src/modules/tournament-code/tournament-code-replacement.controller.ts b/apps/api/src/modules/tournament-code/tournament-code-replacement.controller.ts new file mode 100644 index 00000000..61f0ead5 --- /dev/null +++ b/apps/api/src/modules/tournament-code/tournament-code-replacement.controller.ts @@ -0,0 +1,30 @@ +import { + Controller, + Param, + ParseIntPipe, + Post, +} from '@nestjs/common'; +import mongoose from 'mongoose'; +import { AllowPublicAccess } from '../auth/allow-public-access.decorator'; +import { ParseObjectIdPipe } from '../common/parse-object-id.pipe'; +import { TournamentCodeService } from './tournament-code.service'; + +@Controller('communities/:communityId/tournament-codes') +export class TournamentCodeReplacementController { + constructor(private readonly tournamentCodeService: TournamentCodeService) {} + + @Post('matches/:matchId/games/:gameNumber/replace') + @AllowPublicAccess() + public async replaceForMatchGame( + @Param('communityId', ParseObjectIdPipe) + communityId: mongoose.Types.ObjectId, + @Param('matchId', ParseObjectIdPipe) matchId: mongoose.Types.ObjectId, + @Param('gameNumber', ParseIntPipe) gameNumber: number, + ) { + return this.tournamentCodeService.replaceForMatchGame({ + communityId, + matchId, + gameNumber, + }); + } +} diff --git a/apps/api/src/modules/tournament-code/tournament-code.controller.ts b/apps/api/src/modules/tournament-code/tournament-code.controller.ts index aac93705..cd28cd47 100644 --- a/apps/api/src/modules/tournament-code/tournament-code.controller.ts +++ b/apps/api/src/modules/tournament-code/tournament-code.controller.ts @@ -7,7 +7,6 @@ import { HttpCode, HttpStatus, Param, - ParseIntPipe, Post, Query, UseGuards, @@ -72,21 +71,6 @@ export class TournamentCodeController { return this.tournamentCodeService.stats(); } - @Post('matches/:matchId/games/:gameNumber/replace') - public async replaceForMatchGame( - @CurrentUser() currentUser: UserDocument, - @Param('communityId', ParseObjectIdPipe) - communityId: mongoose.Types.ObjectId, - @Param('matchId', ParseObjectIdPipe) matchId: mongoose.Types.ObjectId, - @Param('gameNumber', ParseIntPipe) gameNumber: number, - ) { - this.assertSameCommunity(currentUser, communityId); - return this.tournamentCodeService.replaceForMatchGame({ - matchId, - gameNumber, - }); - } - @Post(':id/release') public async restore( @CurrentUser() currentUser: UserDocument, diff --git a/apps/api/src/modules/tournament-code/tournament-code.module.ts b/apps/api/src/modules/tournament-code/tournament-code.module.ts index f41cc726..47bd920e 100644 --- a/apps/api/src/modules/tournament-code/tournament-code.module.ts +++ b/apps/api/src/modules/tournament-code/tournament-code.module.ts @@ -1,10 +1,11 @@ import { Module } from '@nestjs/common'; import { TournamentCodeController } from './tournament-code.controller'; +import { TournamentCodeReplacementController } from './tournament-code-replacement.controller'; import { TournamentCodeScheduler } from './tournament-code.scheduler'; import { TournamentCodeService } from './tournament-code.service'; @Module({ - controllers: [TournamentCodeController], + controllers: [TournamentCodeController, TournamentCodeReplacementController], providers: [TournamentCodeService, TournamentCodeScheduler], exports: [TournamentCodeService], }) diff --git a/apps/api/src/modules/tournament-code/tournament-code.service.spec.ts b/apps/api/src/modules/tournament-code/tournament-code.service.spec.ts index 00415405..58acd93e 100644 --- a/apps/api/src/modules/tournament-code/tournament-code.service.spec.ts +++ b/apps/api/src/modules/tournament-code/tournament-code.service.spec.ts @@ -10,6 +10,7 @@ type TournamentCodeServiceInternals = TournamentCodeService & { ): { teamAIndex: number; teamBIndex: number }; ensureGameSchedule(match: MatchDocument): void; replaceForMatchGame(params: { + communityId: mongoose.Types.ObjectId; matchId: mongoose.Types.ObjectId; gameNumber: number; }): Promise; @@ -343,6 +344,7 @@ describe('TournamentCodeService code replacement', () => { replacementCode?: Record | null; match?: MatchDocument; }) { + const communityId = new mongoose.Types.ObjectId(); const matchId = new mongoose.Types.ObjectId(); const oldCodeId = new mongoose.Types.ObjectId(); const newCodeId = new mongoose.Types.ObjectId(); @@ -350,6 +352,7 @@ describe('TournamentCodeService code replacement', () => { params?.match ?? createMatch({ _id: matchId, + communityId, status: 'in-progress', gameCount: 1, participants: Array.from({ length: 10 }, () => ({})), @@ -378,7 +381,7 @@ describe('TournamentCodeService code replacement', () => { }); const MatchModel = { - findById: jest.fn().mockReturnValue(queryResult(match)), + findOne: jest.fn().mockReturnValue(queryResult(match)), }; const TournamentCodeModel = { findOne: jest.fn().mockReturnValue(queryResult(currentCode)), @@ -399,6 +402,7 @@ describe('TournamentCodeService code replacement', () => { return { match, + communityId, matchId, currentCode, replacementCode, @@ -409,10 +413,19 @@ describe('TournamentCodeService code replacement', () => { } it('replaces a reserved code on the same game slot', async () => { - const { match, matchId, currentCode, replacementCode, TournamentCodeModel, service } = - createReplacementFixture(); + const { + match, + communityId, + matchId, + currentCode, + replacementCode, + MatchModel, + TournamentCodeModel, + service, + } = createReplacementFixture(); const slot = await service.replaceForMatchGame({ + communityId, matchId, gameNumber: 1, }); @@ -426,6 +439,10 @@ describe('TournamentCodeService code replacement', () => { state: 'reserved', }), ); + expect(MatchModel.findOne).toHaveBeenCalledWith({ + _id: matchId, + communityId, + }); expect(TournamentCodeModel.findOneAndUpdate).toHaveBeenCalledWith( expect.objectContaining({ state: 'available', @@ -454,9 +471,11 @@ describe('TournamentCodeService code replacement', () => { it('rejects replacement after the game result is collected', async () => { const matchId = new mongoose.Types.ObjectId(); + const communityId = new mongoose.Types.ObjectId(); const fixture = createReplacementFixture({ match: createMatch({ _id: matchId, + communityId, status: 'in-progress', gameCount: 1, participants: Array.from({ length: 10 }, () => ({})), @@ -471,19 +490,24 @@ describe('TournamentCodeService code replacement', () => { }); await expect( - fixture.service.replaceForMatchGame({ matchId, gameNumber: 1 }), + fixture.service.replaceForMatchGame({ + communityId, + matchId, + gameNumber: 1, + }), ).rejects.toThrow('already has a collected result'); expect(fixture.TournamentCodeModel.findOne).not.toHaveBeenCalled(); expect(fixture.TournamentCodeModel.findOneAndUpdate).not.toHaveBeenCalled(); }); it('leaves the current code untouched when no replacement code is available', async () => { - const { match, matchId, currentCode, service } = createReplacementFixture({ - replacementCode: null, - }); + const { match, communityId, matchId, currentCode, service } = + createReplacementFixture({ + replacementCode: null, + }); await expect( - service.replaceForMatchGame({ matchId, gameNumber: 1 }), + service.replaceForMatchGame({ communityId, matchId, gameNumber: 1 }), ).rejects.toThrow('No available tournament code'); expect(currentCode?.save).not.toHaveBeenCalled(); expect(match.save).not.toHaveBeenCalled(); diff --git a/apps/api/src/modules/tournament-code/tournament-code.service.ts b/apps/api/src/modules/tournament-code/tournament-code.service.ts index f81b3760..eaa7b00d 100644 --- a/apps/api/src/modules/tournament-code/tournament-code.service.ts +++ b/apps/api/src/modules/tournament-code/tournament-code.service.ts @@ -274,13 +274,17 @@ export class TournamentCodeService { } public async replaceForMatchGame(params: { + communityId: mongoose.Types.ObjectId; matchId: mongoose.Types.ObjectId; gameNumber: number; }): Promise { const MatchModel = await this.connectionService.getModel('match'); const TournamentCodeModel = await this.connectionService.getModel('tournamentCode'); - const match = await MatchModel.findById(params.matchId).exec(); + const match = await MatchModel.findOne({ + _id: params.matchId, + communityId: params.communityId, + }).exec(); if (!match) throw new NotFoundException('Match not found.'); if (match.status !== 'team-built' && match.status !== 'in-progress') { throw new BadRequestException( diff --git a/apps/web/src/pages/MatchDetailPage.tsx b/apps/web/src/pages/MatchDetailPage.tsx index f8ed58f3..fa92761d 100644 --- a/apps/web/src/pages/MatchDetailPage.tsx +++ b/apps/web/src/pages/MatchDetailPage.tsx @@ -2918,7 +2918,7 @@ export default function MatchDetailPage() {