import { BadRequestException, Body, Controller, Delete, Get, HttpCode, HttpStatus, Param, Post, UseGuards, } from '@nestjs/common'; import mongoose from 'mongoose'; import { AllowPublicAccess } from '../auth/allow-public-access.decorator'; 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 { UserDocument } from '../global/database/schema'; import { AuctionService } from './auction.service'; import { AuctionGateway } from './auction.gateway'; import { PlaceBidDto, SoldDto, StartAuctionDto, UndoAuctionDto, } from './dto/auction.dto'; @Controller('communities/:communityId/matches/:matchId/auction') export class AuctionController { constructor( private readonly auctionService: AuctionService, private readonly auctionGateway: AuctionGateway, ) {} @Post() @Roles('admin', 'owner') @UseGuards(RolesGuard) public async startAuction( @CurrentUser() currentUser: UserDocument, @Param('communityId', ParseObjectIdPipe) communityId: mongoose.Types.ObjectId, @Param('matchId', ParseObjectIdPipe) matchId: mongoose.Types.ObjectId, @Body() body: StartAuctionDto, ) { this.assertSameCommunity(currentUser, communityId); return this.auctionService.start({ matchId, communityId, captains: body.captains.map((c) => ({ teamIndex: c.teamIndex, userId: new mongoose.Types.ObjectId(c.userId), initialPoints: c.initialPoints, })), initialPoints: body.initialPoints ?? 1000, maxTimerSeconds: body.maxTimerSeconds ?? 30, bidExtensionSeconds: body.bidExtensionSeconds ?? 5, topTierThreshold: body.topTierThreshold ?? 'NONE', manualTopTierUserIds: (body.manualTopTierUserIds ?? []).map( (userId) => new mongoose.Types.ObjectId(userId), ), }); } @Get() @AllowPublicAccess() public async getAuction( @Param('matchId', ParseObjectIdPipe) matchId: mongoose.Types.ObjectId, ) { return this.auctionService.getByMatchId(matchId); } // 공개 — 팀장 2명만 쓸 엔드포인트(트러스트 기반, 10명 동시 관전). @Post('open-bidding') @AllowPublicAccess() @HttpCode(HttpStatus.OK) public async openBidding( @Param('matchId', ParseObjectIdPipe) matchId: mongoose.Types.ObjectId, ) { return this.auctionService.openBidding(matchId); } @Post('bids') @AllowPublicAccess() @HttpCode(HttpStatus.OK) public async placeBid( @CurrentUser() currentUser: UserDocument | undefined, @Param('matchId', ParseObjectIdPipe) matchId: mongoose.Types.ObjectId, @Body() body: PlaceBidDto, ) { return this.auctionService.placeBid({ matchId, teamIndex: body.teamIndex, amount: body.amount, bidderUserId: this.resolveBidder(currentUser, body.bidderUserId), }); } @Post('sold') @AllowPublicAccess() @HttpCode(HttpStatus.OK) public async markSold( @Param('matchId', ParseObjectIdPipe) matchId: mongoose.Types.ObjectId, @Body() body: SoldDto, ) { return this.auctionService.markSold({ matchId, assignedPosition: body.assignedPosition, }); } @Post('next') @AllowPublicAccess() @HttpCode(HttpStatus.OK) public async next( @Param('matchId', ParseObjectIdPipe) matchId: mongoose.Types.ObjectId, ) { return this.auctionService.next(matchId); } @Get('history') @Roles('admin', 'owner') @UseGuards(RolesGuard) public async listHistory( @CurrentUser() currentUser: UserDocument, @Param('communityId', ParseObjectIdPipe) communityId: mongoose.Types.ObjectId, @Param('matchId', ParseObjectIdPipe) matchId: mongoose.Types.ObjectId, ) { this.assertSameCommunity(currentUser, communityId); return this.auctionService.listHistory({ matchId, communityId }); } @Post('undo') @Roles('admin', 'owner') @UseGuards(RolesGuard) @HttpCode(HttpStatus.OK) public async undo( @CurrentUser() currentUser: UserDocument, @Param('communityId', ParseObjectIdPipe) communityId: mongoose.Types.ObjectId, @Param('matchId', ParseObjectIdPipe) matchId: mongoose.Types.ObjectId, @Body() body: UndoAuctionDto, ) { this.assertSameCommunity(currentUser, communityId); const auction = await this.auctionService.undoTo({ matchId, communityId, sequence: body.sequence, }); this.auctionGateway.broadcastState(matchId.toString(), auction); return auction; } @Delete() @Roles('admin', 'owner') @UseGuards(RolesGuard) @HttpCode(HttpStatus.NO_CONTENT) public async resetAuction( @CurrentUser() currentUser: UserDocument, @Param('communityId', ParseObjectIdPipe) communityId: mongoose.Types.ObjectId, @Param('matchId', ParseObjectIdPipe) matchId: mongoose.Types.ObjectId, ) { this.assertSameCommunity(currentUser, communityId); await this.auctionService.reset({ matchId, communityId }); this.auctionGateway.broadcastReset(matchId.toString()); } private resolveBidder( currentUser: UserDocument | undefined, bodyBidder?: string, ): mongoose.Types.ObjectId | undefined { if (currentUser) return currentUser._id; if (!bodyBidder) return undefined; if (!mongoose.isValidObjectId(bodyBidder)) { throw new BadRequestException('Invalid bidderUserId.'); } return new mongoose.Types.ObjectId(bodyBidder); } private assertSameCommunity( user: UserDocument, communityId: mongoose.Types.ObjectId, ): void { if (!user.communityId.equals(communityId)) { throw new BadRequestException('Different community.'); } } }