This commit is contained in:
dahyeon committed 2026-04-24 13:40:39 -07:00
1 parent a197185ad0
commit 30dfb7ce58
27 files changed
+1584 -45

No files matched your search

@@ -0,0 +1,163 @@
import {
BadRequestException,
Body,
Controller,
Delete,
Get,
HttpCode,
HttpStatus,
Param,
Patch,
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 {
MatchDocument,
MatchStatus,
UserDocument,
} from '../global/database/schema';
import {
CreateMatchDto,
JoinMatchDto,
UpdateMatchDto,
} from './dto/create-match.dto';
import { MatchService } from './match.service';
@Controller('communities/:communityId/matches')
export class MatchController {
constructor(private readonly matchService: MatchService) {}
@Post()
@Roles('admin', 'owner')
@UseGuards(RolesGuard)
public async createMatch(
@CurrentUser() currentUser: UserDocument,
@Param('communityId', ParseObjectIdPipe)
communityId: mongoose.Types.ObjectId,
@Body() body: CreateMatchDto,
) {
this.assertSameCommunity(currentUser, communityId);
return this.matchService.create({
communityId,
hostUserId: currentUser._id,
data: {
title: body.title,
description: body.description ?? '',
mapId: body.mapId ?? 11,
scheduledAt: body.scheduledAt,
maxParticipants: body.maxParticipants ?? 10,
prizes: body.prizes ?? [],
notices: body.notices ?? [],
schedule: body.schedule ?? [],
},
});
}
// 공개 — 커뮤니티 내 매치 리스트. 로그인 시 본인 참여 여부를 함께 반환.
@Get()
public async listMatches(
@CurrentUser() currentUser: UserDocument | undefined,
@Param('communityId', ParseObjectIdPipe)
communityId: mongoose.Types.ObjectId,
@Query('status') status?: MatchStatus,
@Query('limit') limit?: string,
) {
const matches = await this.matchService.list({
communityId,
status,
limit: limit ? Number(limit) : undefined,
});
if (!currentUser) return { matches };
const myId = currentUser._id;
return {
matches: matches.map((m) => this.annotateJoined(m, myId)),
};
}
@Get(':matchId')
public async getMatch(
@Param('matchId', ParseObjectIdPipe) matchId: mongoose.Types.ObjectId,
) {
return this.matchService.getById(matchId);
}
@Patch(':matchId')
@Roles('admin', 'owner')
@UseGuards(RolesGuard)
public async updateMatch(
@CurrentUser() currentUser: UserDocument,
@Param('communityId', ParseObjectIdPipe)
communityId: mongoose.Types.ObjectId,
@Param('matchId', ParseObjectIdPipe) matchId: mongoose.Types.ObjectId,
@Body() body: UpdateMatchDto,
) {
this.assertSameCommunity(currentUser, communityId);
return this.matchService.update({ matchId, communityId, patch: body });
}
@Delete(':matchId')
@Roles('admin', 'owner')
@UseGuards(RolesGuard)
@HttpCode(HttpStatus.NO_CONTENT)
public async deleteMatch(
@CurrentUser() currentUser: UserDocument,
@Param('communityId', ParseObjectIdPipe)
communityId: mongoose.Types.ObjectId,
@Param('matchId', ParseObjectIdPipe) matchId: mongoose.Types.ObjectId,
) {
this.assertSameCommunity(currentUser, communityId);
await this.matchService.remove({ matchId, communityId });
}
// 공개 — 참여 (로그인된 유저는 자기 자신, 비로그인은 body.userId 사용).
@Post(':matchId/participants')
public async joinMatch(
@CurrentUser() currentUser: UserDocument | undefined,
@Param('matchId', ParseObjectIdPipe) matchId: mongoose.Types.ObjectId,
@Body() body: JoinMatchDto,
) {
const userId = this.resolveJoinerUserId(currentUser, body.userId);
return this.matchService.join({
matchId,
userId,
preferredPositions: body.preferredPositions,
});
}
private resolveJoinerUserId(
currentUser: UserDocument | undefined,
bodyUserId?: string,
): mongoose.Types.ObjectId {
if (currentUser) return currentUser._id;
if (!bodyUserId || !mongoose.isValidObjectId(bodyUserId)) {
throw new BadRequestException(
'userId is required when not authenticated.',
);
}
return new mongoose.Types.ObjectId(bodyUserId);
}
private annotateJoined(
match: MatchDocument,
userId: mongoose.Types.ObjectId,
) {
const obj = match.toObject();
const joined = match.participants.some((p) => p.userId.equals(userId));
return { ...obj, joined };
}
private assertSameCommunity(
user: UserDocument,
communityId: mongoose.Types.ObjectId,
): void {
if (!user.communityId.equals(communityId)) {
throw new BadRequestException('Different community.');
}
}
}