fix: allow public tournament code replacement

This commit is contained in:
dahyeon committed 2026-06-19 03:56:44 -07:00
1 parent 303886ed53
commit b9881fcb6f
7 files changed
+115 -25

No files matched your search

@@ -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<TournamentCodeService, 'replaceForMatchGame'>
>;
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);
});
});
@@ -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,
});
}
}
@@ -7,7 +7,6 @@ import {
HttpCode, HttpCode,
HttpStatus, HttpStatus,
Param, Param,
ParseIntPipe,
Post, Post,
Query, Query,
UseGuards, UseGuards,
@@ -72,21 +71,6 @@ export class TournamentCodeController {
return this.tournamentCodeService.stats(); 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') @Post(':id/release')
public async restore( public async restore(
@CurrentUser() currentUser: UserDocument, @CurrentUser() currentUser: UserDocument,
@@ -1,10 +1,11 @@
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { TournamentCodeController } from './tournament-code.controller'; import { TournamentCodeController } from './tournament-code.controller';
import { TournamentCodeReplacementController } from './tournament-code-replacement.controller';
import { TournamentCodeScheduler } from './tournament-code.scheduler'; import { TournamentCodeScheduler } from './tournament-code.scheduler';
import { TournamentCodeService } from './tournament-code.service'; import { TournamentCodeService } from './tournament-code.service';
@Module({ @Module({
controllers: [TournamentCodeController], controllers: [TournamentCodeController, TournamentCodeReplacementController],
providers: [TournamentCodeService, TournamentCodeScheduler], providers: [TournamentCodeService, TournamentCodeScheduler],
exports: [TournamentCodeService], exports: [TournamentCodeService],
}) })
@@ -10,6 +10,7 @@ type TournamentCodeServiceInternals = TournamentCodeService & {
): { teamAIndex: number; teamBIndex: number }; ): { teamAIndex: number; teamBIndex: number };
ensureGameSchedule(match: MatchDocument): void; ensureGameSchedule(match: MatchDocument): void;
replaceForMatchGame(params: { replaceForMatchGame(params: {
communityId: mongoose.Types.ObjectId;
matchId: mongoose.Types.ObjectId; matchId: mongoose.Types.ObjectId;
gameNumber: number; gameNumber: number;
}): Promise<unknown>; }): Promise<unknown>;
@@ -343,6 +344,7 @@ describe('TournamentCodeService code replacement', () => {
replacementCode?: Record<string, unknown> | null; replacementCode?: Record<string, unknown> | null;
match?: MatchDocument; match?: MatchDocument;
}) { }) {
const communityId = new mongoose.Types.ObjectId();
const matchId = new mongoose.Types.ObjectId(); const matchId = new mongoose.Types.ObjectId();
const oldCodeId = new mongoose.Types.ObjectId(); const oldCodeId = new mongoose.Types.ObjectId();
const newCodeId = new mongoose.Types.ObjectId(); const newCodeId = new mongoose.Types.ObjectId();
@@ -350,6 +352,7 @@ describe('TournamentCodeService code replacement', () => {
params?.match ?? params?.match ??
createMatch({ createMatch({
_id: matchId, _id: matchId,
communityId,
status: 'in-progress', status: 'in-progress',
gameCount: 1, gameCount: 1,
participants: Array.from({ length: 10 }, () => ({})), participants: Array.from({ length: 10 }, () => ({})),
@@ -378,7 +381,7 @@ describe('TournamentCodeService code replacement', () => {
}); });
const MatchModel = { const MatchModel = {
findById: jest.fn().mockReturnValue(queryResult(match)), findOne: jest.fn().mockReturnValue(queryResult(match)),
}; };
const TournamentCodeModel = { const TournamentCodeModel = {
findOne: jest.fn().mockReturnValue(queryResult(currentCode)), findOne: jest.fn().mockReturnValue(queryResult(currentCode)),
@@ -399,6 +402,7 @@ describe('TournamentCodeService code replacement', () => {
return { return {
match, match,
communityId,
matchId, matchId,
currentCode, currentCode,
replacementCode, replacementCode,
@@ -409,10 +413,19 @@ describe('TournamentCodeService code replacement', () => {
} }
it('replaces a reserved code on the same game slot', async () => { it('replaces a reserved code on the same game slot', async () => {
const { match, matchId, currentCode, replacementCode, TournamentCodeModel, service } = const {
createReplacementFixture(); match,
communityId,
matchId,
currentCode,
replacementCode,
MatchModel,
TournamentCodeModel,
service,
} = createReplacementFixture();
const slot = await service.replaceForMatchGame({ const slot = await service.replaceForMatchGame({
communityId,
matchId, matchId,
gameNumber: 1, gameNumber: 1,
}); });
@@ -426,6 +439,10 @@ describe('TournamentCodeService code replacement', () => {
state: 'reserved', state: 'reserved',
}), }),
); );
expect(MatchModel.findOne).toHaveBeenCalledWith({
_id: matchId,
communityId,
});
expect(TournamentCodeModel.findOneAndUpdate).toHaveBeenCalledWith( expect(TournamentCodeModel.findOneAndUpdate).toHaveBeenCalledWith(
expect.objectContaining({ expect.objectContaining({
state: 'available', state: 'available',
@@ -454,9 +471,11 @@ describe('TournamentCodeService code replacement', () => {
it('rejects replacement after the game result is collected', async () => { it('rejects replacement after the game result is collected', async () => {
const matchId = new mongoose.Types.ObjectId(); const matchId = new mongoose.Types.ObjectId();
const communityId = new mongoose.Types.ObjectId();
const fixture = createReplacementFixture({ const fixture = createReplacementFixture({
match: createMatch({ match: createMatch({
_id: matchId, _id: matchId,
communityId,
status: 'in-progress', status: 'in-progress',
gameCount: 1, gameCount: 1,
participants: Array.from({ length: 10 }, () => ({})), participants: Array.from({ length: 10 }, () => ({})),
@@ -471,19 +490,24 @@ describe('TournamentCodeService code replacement', () => {
}); });
await expect( await expect(
fixture.service.replaceForMatchGame({ matchId, gameNumber: 1 }), fixture.service.replaceForMatchGame({
communityId,
matchId,
gameNumber: 1,
}),
).rejects.toThrow('already has a collected result'); ).rejects.toThrow('already has a collected result');
expect(fixture.TournamentCodeModel.findOne).not.toHaveBeenCalled(); expect(fixture.TournamentCodeModel.findOne).not.toHaveBeenCalled();
expect(fixture.TournamentCodeModel.findOneAndUpdate).not.toHaveBeenCalled(); expect(fixture.TournamentCodeModel.findOneAndUpdate).not.toHaveBeenCalled();
}); });
it('leaves the current code untouched when no replacement code is available', async () => { it('leaves the current code untouched when no replacement code is available', async () => {
const { match, matchId, currentCode, service } = createReplacementFixture({ const { match, communityId, matchId, currentCode, service } =
createReplacementFixture({
replacementCode: null, replacementCode: null,
}); });
await expect( await expect(
service.replaceForMatchGame({ matchId, gameNumber: 1 }), service.replaceForMatchGame({ communityId, matchId, gameNumber: 1 }),
).rejects.toThrow('No available tournament code'); ).rejects.toThrow('No available tournament code');
expect(currentCode?.save).not.toHaveBeenCalled(); expect(currentCode?.save).not.toHaveBeenCalled();
expect(match.save).not.toHaveBeenCalled(); expect(match.save).not.toHaveBeenCalled();
@@ -274,13 +274,17 @@ export class TournamentCodeService {
} }
public async replaceForMatchGame(params: { public async replaceForMatchGame(params: {
communityId: mongoose.Types.ObjectId;
matchId: mongoose.Types.ObjectId; matchId: mongoose.Types.ObjectId;
gameNumber: number; gameNumber: number;
}): Promise<TournamentCodeSlotView> { }): Promise<TournamentCodeSlotView> {
const MatchModel = await this.connectionService.getModel('match'); const MatchModel = await this.connectionService.getModel('match');
const TournamentCodeModel = const TournamentCodeModel =
await this.connectionService.getModel('tournamentCode'); 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) throw new NotFoundException('Match not found.');
if (match.status !== 'team-built' && match.status !== 'in-progress') { if (match.status !== 'team-built' && match.status !== 'in-progress') {
throw new BadRequestException( throw new BadRequestException(
+1 -1
View File
@@ -2918,7 +2918,7 @@ export default function MatchDetailPage() {
<TournamentCodeList <TournamentCodeList
slots={match.tournamentCodes} slots={match.tournamentCodes}
gameMode={match.gameMode} gameMode={match.gameMode}
canReplace={canManageAuction} canReplace
replacingGameNumber={ replacingGameNumber={
replaceTournamentCodeMutation.isPending replaceTournamentCodeMutation.isPending
? replaceTournamentCodeMutation.variables?.gameNumber ?? null ? replaceTournamentCodeMutation.variables?.gameNumber ?? null