release/86 #2
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,
|
||||
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,
|
||||
|
||||
@@ -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],
|
||||
})
|
||||
|
||||
@@ -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<unknown>;
|
||||
@@ -343,6 +344,7 @@ describe('TournamentCodeService code replacement', () => {
|
||||
replacementCode?: Record<string, unknown> | 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();
|
||||
|
||||
@@ -274,13 +274,17 @@ export class TournamentCodeService {
|
||||
}
|
||||
|
||||
public async replaceForMatchGame(params: {
|
||||
communityId: mongoose.Types.ObjectId;
|
||||
matchId: mongoose.Types.ObjectId;
|
||||
gameNumber: number;
|
||||
}): Promise<TournamentCodeSlotView> {
|
||||
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(
|
||||
|
||||
@@ -2918,7 +2918,7 @@ export default function MatchDetailPage() {
|
||||
<TournamentCodeList
|
||||
slots={match.tournamentCodes}
|
||||
gameMode={match.gameMode}
|
||||
canReplace={canManageAuction}
|
||||
canReplace
|
||||
replacingGameNumber={
|
||||
replaceTournamentCodeMutation.isPending
|
||||
? replaceTournamentCodeMutation.variables?.gameNumber ?? null
|
||||
|
||||
Reference in new issue
Block a user