Files
lolympus-tournament-support/apps/api/src/modules/auction/auction.controller.ts
T
dahyeon f649490cd2 fix(auction): harden lifecycle and durable bid storage
Serialize lifecycle changes and make cancellation, deadlines, and match side effects recoverable and idempotent.

Archive accepted bids durably, quarantine invalid payloads, and preserve compatibility with empty round IDs. Synchronize HTTP polling and socket state with scoped resync events.

Add cancellation cleanup index migration and regression coverage. Validated with 744 API tests, 65 Redis integration tests, and 28 Mongo integration tests.
2026-11-09 23:24:11 +09:00

249 lines
8.1 KiB
TypeScript

import {
BadRequestException,
Body,
Controller,
Delete,
Get,
HttpCode,
HttpStatus,
Param,
Post,
UseGuards,
Query,
} 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 { AuctionBidArchiveService } from './auction-bid-archive.service';
import {
NextAuctionDto,
OpenBiddingDto,
PassedAuctionDto,
PlaceBidDto,
ResetAuctionDto,
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,
private readonly auctionBidArchiveService: AuctionBidArchiveService,
) {}
@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,
startAuctionRequestId: body.startAuctionRequestId,
actorUserId: currentUser._id,
captains: body.captains.map((c) => ({
teamIndex: c.teamIndex,
userId: new mongoose.Types.ObjectId(c.userId),
initialTeamBidPointAmount: c.initialTeamBidPointAmount,
})),
initialTeamBidPointAmount: body.initialTeamBidPointAmount ?? 1000,
maximumBidRoundDurationSeconds: body.maximumBidRoundDurationSeconds ?? 30,
bidRoundExtensionSeconds: body.bidRoundExtensionSeconds ?? 5,
topTierAuctionPriorityThreshold:
body.topTierAuctionPriorityThreshold ?? 'NONE',
manualTopTierAuctionPriorityUserIds: (
body.manualTopTierAuctionPriorityUserIds ?? []
).map((userId) => new mongoose.Types.ObjectId(userId)),
});
}
@Get()
@AllowPublicAccess()
public async getAuction(
@Param('matchId', ParseObjectIdPipe) matchId: mongoose.Types.ObjectId,
) {
return this.auctionService.getStateByMatchId(matchId);
}
// 공개 — 팀장 2명만 쓸 엔드포인트(트러스트 기반, 10명 동시 관전).
@Get('bids/history')
@AllowPublicAccess()
public async getBidHistory(
@Param('matchId', ParseObjectIdPipe) matchId: mongoose.Types.ObjectId,
@Query('afterRevision') afterRevision?: string,
@Query('limit') limit?: string,
) {
return this.auctionBidArchiveService.list(
matchId,
afterRevision === undefined ? -1 : Number(afterRevision),
limit === undefined ? 100 : Number(limit),
);
}
@Post('open-bidding')
@AllowPublicAccess()
@HttpCode(HttpStatus.OK)
public async openBidding(
@Param('matchId', ParseObjectIdPipe) matchId: mongoose.Types.ObjectId,
@Body() body: OpenBiddingDto = {},
) {
return this.auctionService.openBidding(matchId, body);
}
@Post('bids')
@AllowPublicAccess()
@HttpCode(HttpStatus.OK)
public async placeBid(
@Param('matchId', ParseObjectIdPipe) matchId: mongoose.Types.ObjectId,
@Body() body: PlaceBidDto,
) {
return this.auctionService.placeBid({
matchId,
teamIndex: body.bidderTeamIndex,
amount: body.bidPointAmount,
bidRequestId: body.bidRequestId,
clientObservedBidRoundId: body.clientObservedBidRoundId,
clientObservedAuctionLifecycleId: body.clientObservedAuctionLifecycleId,
clientObservedCurrentBidRoundPlayerUserId:
body.clientObservedCurrentBidRoundPlayerUserId,
});
}
@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,
auctionActionRequestId: body.auctionActionRequestId,
clientObservedAuctionLifecycleId: body.clientObservedAuctionLifecycleId,
clientObservedCurrentAuctionPlayerOrderIndex:
body.clientObservedCurrentAuctionPlayerOrderIndex,
clientObservedCurrentBidRoundPlayerUserId:
body.clientObservedCurrentBidRoundPlayerUserId,
clientObservedPhase: body.clientObservedPhase,
clientObservedRevision: body.clientObservedRevision,
});
}
@Post('passed')
@AllowPublicAccess()
@HttpCode(HttpStatus.OK)
public async markPassed(
@Param('matchId', ParseObjectIdPipe) matchId: mongoose.Types.ObjectId,
@Body() body: PassedAuctionDto = {},
) {
return this.auctionService.markPassed(matchId, body);
}
@Post('next')
@AllowPublicAccess()
@HttpCode(HttpStatus.OK)
public async next(
@Param('matchId', ParseObjectIdPipe) matchId: mongoose.Types.ObjectId,
@Body() body: NextAuctionDto = {},
) {
return this.auctionService.next(matchId, body);
}
@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,
auctionActionRequestId: body.auctionActionRequestId,
clientObservedAuctionLifecycleId: body.clientObservedAuctionLifecycleId,
clientObservedCurrentAuctionPlayerOrderIndex:
body.clientObservedCurrentAuctionPlayerOrderIndex,
clientObservedCurrentBidRoundPlayerUserId:
body.clientObservedCurrentBidRoundPlayerUserId,
clientObservedPhase: body.clientObservedPhase,
clientObservedRevision: body.clientObservedRevision,
});
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,
@Body() body: ResetAuctionDto = {},
) {
this.assertSameCommunity(currentUser, communityId);
await this.auctionService.reset({
matchId,
communityId,
auctionActionRequestId: body.auctionActionRequestId,
clientObservedAuctionLifecycleId: body.clientObservedAuctionLifecycleId,
clientObservedCurrentAuctionPlayerOrderIndex:
body.clientObservedCurrentAuctionPlayerOrderIndex,
clientObservedCurrentBidRoundPlayerUserId:
body.clientObservedCurrentBidRoundPlayerUserId,
clientObservedPhase: body.clientObservedPhase,
clientObservedRevision: body.clientObservedRevision,
});
this.auctionGateway.broadcastReset(
matchId.toString(),
body.clientObservedAuctionLifecycleId,
);
}
private assertSameCommunity(
user: UserDocument,
communityId: mongoose.Types.ObjectId,
): void {
if (!user.communityId.equals(communityId)) {
throw new BadRequestException('Different community.');
}
}
}