This commit is contained in:
dahyeon committed 2026-04-27 14:04:04 -07:00
1 parent 7a43a667f1
commit c00545f281
21 files changed
+1920 -5

No files matched your search

+35 -1
View File
@@ -12,6 +12,8 @@ import {
PositionPreference,
TierInfo,
} from '../global/database/schema';
import { DEFAULT_TOURNAMENT_GAME_COUNT } from '../tournament-code/tournament-code.constants';
import { TournamentCodeService } from '../tournament-code/tournament-code.service';
interface CreateMatchParams {
communityId: mongoose.Types.ObjectId;
@@ -41,12 +43,24 @@ interface JoinMatchParams {
@Injectable()
export class MatchService {
constructor(private readonly connectionService: ConnectionService) {}
constructor(
private readonly connectionService: ConnectionService,
private readonly tournamentCodeService: TournamentCodeService,
) {}
public async create(params: CreateMatchParams): Promise<MatchDocument> {
const MatchModel = await this.connectionService.getModel('match');
const CommunityModel = await this.connectionService.getModel('community');
const community = await CommunityModel.findById(params.communityId)
.select({ matchGameCount: 1 })
.lean()
.exec();
return MatchModel.create({
...params.data,
gameCount:
params.data.gameCount ??
community?.matchGameCount ??
DEFAULT_TOURNAMENT_GAME_COUNT,
communityId: params.communityId,
hostUserId: params.hostUserId,
});
@@ -76,6 +90,15 @@ export class MatchService {
return match;
}
public async serialize(match: MatchDocument) {
const obj = match.toObject();
return {
...obj,
gameCount: match.gameCount || DEFAULT_TOURNAMENT_GAME_COUNT,
tournamentCodes: await this.tournamentCodeService.buildSlots(match),
};
}
public async update(params: UpdateMatchParams): Promise<MatchDocument> {
const MatchModel = await this.connectionService.getModel('match');
const match = await MatchModel.findById(params.matchId);
@@ -83,7 +106,17 @@ export class MatchService {
if (!match.communityId.equals(params.communityId)) {
throw new BadRequestException('Match does not belong to this community.');
}
const previousStatus = match.status;
match.set(params.patch);
if (
previousStatus !== 'recruiting' &&
match.status === 'recruiting' &&
match.tournamentCodeIds.length > 0
) {
await this.tournamentCodeService.releaseFromMatch(match);
match.tournamentCodeIds = [];
match.teamBuiltAt = undefined;
}
await match.save();
return match;
}
@@ -98,6 +131,7 @@ export class MatchService {
if (!match.communityId.equals(params.communityId)) {
throw new BadRequestException('Match does not belong to this community.');
}
await this.tournamentCodeService.releaseFromMatch(match);
await match.deleteOne();
}