122 lines
3.5 KiB
TypeScript
122 lines
3.5 KiB
TypeScript
import {
|
|
BadRequestException,
|
|
Body,
|
|
Controller,
|
|
Delete,
|
|
Get,
|
|
HttpCode,
|
|
HttpStatus,
|
|
Param,
|
|
ParseIntPipe,
|
|
Post,
|
|
Query,
|
|
UseGuards,
|
|
} from '@nestjs/common';
|
|
import mongoose from 'mongoose';
|
|
import { CurrentUser } from '../auth/current-user.decorator';
|
|
import { Roles } from '../auth/roles.decorator';
|
|
import { RolesGuard } from '../auth/roles.guard';
|
|
import { ParseObjectIdPipe } from '../common/parse-object-id.pipe';
|
|
import type {
|
|
TournamentCodeState,
|
|
UserDocument,
|
|
} from '../global/database/schema';
|
|
import { TournamentCodeService } from './tournament-code.service';
|
|
import type { BulkUploadTournamentCodesRequest } from './tournament-code.types';
|
|
|
|
@Controller('communities/:communityId/tournament-codes')
|
|
@Roles('owner', 'admin')
|
|
@UseGuards(RolesGuard)
|
|
export class TournamentCodeController {
|
|
constructor(private readonly tournamentCodeService: TournamentCodeService) {}
|
|
|
|
@Post()
|
|
public async bulkUpload(
|
|
@CurrentUser() currentUser: UserDocument,
|
|
@Param('communityId', ParseObjectIdPipe)
|
|
communityId: mongoose.Types.ObjectId,
|
|
@Body() body: BulkUploadTournamentCodesRequest,
|
|
) {
|
|
this.assertSameCommunity(currentUser, communityId);
|
|
return this.tournamentCodeService.bulkUpload({
|
|
communityId,
|
|
uploadedBy: currentUser._id,
|
|
codes: body.codes ?? [],
|
|
});
|
|
}
|
|
|
|
@Get()
|
|
public async list(
|
|
@CurrentUser() currentUser: UserDocument,
|
|
@Param('communityId', ParseObjectIdPipe)
|
|
communityId: mongoose.Types.ObjectId,
|
|
@Query('state') state?: TournamentCodeState,
|
|
@Query('limit') limit?: string,
|
|
) {
|
|
this.assertSameCommunity(currentUser, communityId);
|
|
return {
|
|
codes: await this.tournamentCodeService.list({
|
|
state,
|
|
limit: limit ? Number(limit) : undefined,
|
|
}),
|
|
};
|
|
}
|
|
|
|
@Get('stats')
|
|
public async stats(
|
|
@CurrentUser() currentUser: UserDocument,
|
|
@Param('communityId', ParseObjectIdPipe)
|
|
communityId: mongoose.Types.ObjectId,
|
|
) {
|
|
this.assertSameCommunity(currentUser, communityId);
|
|
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,
|
|
@Param('communityId', ParseObjectIdPipe)
|
|
communityId: mongoose.Types.ObjectId,
|
|
@Param('id', ParseObjectIdPipe) id: mongoose.Types.ObjectId,
|
|
) {
|
|
this.assertSameCommunity(currentUser, communityId);
|
|
return this.tournamentCodeService.restoreToAvailable(id);
|
|
}
|
|
|
|
@Delete(':id')
|
|
@HttpCode(HttpStatus.NO_CONTENT)
|
|
public async discard(
|
|
@CurrentUser() currentUser: UserDocument,
|
|
@Param('communityId', ParseObjectIdPipe)
|
|
communityId: mongoose.Types.ObjectId,
|
|
@Param('id', ParseObjectIdPipe) id: mongoose.Types.ObjectId,
|
|
) {
|
|
this.assertSameCommunity(currentUser, communityId);
|
|
await this.tournamentCodeService.discardAvailable(id);
|
|
}
|
|
|
|
private assertSameCommunity(
|
|
user: UserDocument,
|
|
communityId: mongoose.Types.ObjectId,
|
|
): void {
|
|
if (!user.communityId.equals(communityId)) {
|
|
throw new BadRequestException('Different community.');
|
|
}
|
|
}
|
|
}
|