feat(api): add tournament code pool
This commit is contained in:
1 parent
6bad4861e6
commit
95c8ed3c8b
5 files changed
+834
No files matched your search
@@ -0,0 +1,105 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
Get,
|
||||
HttpCode,
|
||||
HttpStatus,
|
||||
Param,
|
||||
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(':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.');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user