.
This commit is contained in:
1 parent
a197185ad0
commit
30dfb7ce58
27 files changed
+1584
-45
No files matched your search
@@ -0,0 +1,138 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import mongoose from 'mongoose';
|
||||
import { ConnectionService } from '../global/database/connection.service';
|
||||
import {
|
||||
Match,
|
||||
MatchDocument,
|
||||
MatchStatus,
|
||||
PositionPreference,
|
||||
} from '../global/database/schema';
|
||||
|
||||
interface CreateMatchParams {
|
||||
communityId: mongoose.Types.ObjectId;
|
||||
hostUserId?: mongoose.Types.ObjectId;
|
||||
data: Partial<Match> & Pick<Match, 'title' | 'scheduledAt'>;
|
||||
}
|
||||
|
||||
interface UpdateMatchParams {
|
||||
matchId: mongoose.Types.ObjectId;
|
||||
communityId: mongoose.Types.ObjectId;
|
||||
patch: Partial<Match>;
|
||||
}
|
||||
|
||||
interface ListMatchesQuery {
|
||||
communityId: mongoose.Types.ObjectId;
|
||||
status?: MatchStatus;
|
||||
limit?: number;
|
||||
}
|
||||
|
||||
interface JoinMatchParams {
|
||||
matchId: mongoose.Types.ObjectId;
|
||||
userId: mongoose.Types.ObjectId;
|
||||
preferredPositions: PositionPreference[];
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class MatchService {
|
||||
constructor(private readonly connectionService: ConnectionService) {}
|
||||
|
||||
public async create(params: CreateMatchParams): Promise<MatchDocument> {
|
||||
const MatchModel = await this.connectionService.getModel('match');
|
||||
return MatchModel.create({
|
||||
...params.data,
|
||||
communityId: params.communityId,
|
||||
hostUserId: params.hostUserId,
|
||||
});
|
||||
}
|
||||
|
||||
public async list(query: ListMatchesQuery): Promise<MatchDocument[]> {
|
||||
const MatchModel = await this.connectionService.getModel('match');
|
||||
const filter: mongoose.FilterQuery<Match> = {
|
||||
communityId: query.communityId,
|
||||
};
|
||||
if (query.status) filter.status = query.status;
|
||||
return MatchModel.find(filter)
|
||||
.sort({ scheduledAt: -1 })
|
||||
.limit(Math.min(query.limit ?? 50, 200))
|
||||
.exec();
|
||||
}
|
||||
|
||||
public async getById(
|
||||
matchId: mongoose.Types.ObjectId,
|
||||
): Promise<MatchDocument> {
|
||||
const MatchModel = await this.connectionService.getModel('match');
|
||||
const match = await MatchModel.findById(matchId);
|
||||
if (!match) throw new NotFoundException('Match not found.');
|
||||
return match;
|
||||
}
|
||||
|
||||
public async update(params: UpdateMatchParams): Promise<MatchDocument> {
|
||||
const MatchModel = await this.connectionService.getModel('match');
|
||||
const match = await MatchModel.findById(params.matchId);
|
||||
if (!match) throw new NotFoundException('Match not found.');
|
||||
if (!match.communityId.equals(params.communityId)) {
|
||||
throw new BadRequestException('Match does not belong to this community.');
|
||||
}
|
||||
match.set(params.patch);
|
||||
await match.save();
|
||||
return match;
|
||||
}
|
||||
|
||||
public async remove(params: {
|
||||
matchId: mongoose.Types.ObjectId;
|
||||
communityId: mongoose.Types.ObjectId;
|
||||
}): Promise<void> {
|
||||
const MatchModel = await this.connectionService.getModel('match');
|
||||
const match = await MatchModel.findById(params.matchId);
|
||||
if (!match) throw new NotFoundException('Match not found.');
|
||||
if (!match.communityId.equals(params.communityId)) {
|
||||
throw new BadRequestException('Match does not belong to this community.');
|
||||
}
|
||||
await match.deleteOne();
|
||||
}
|
||||
|
||||
public async join(params: JoinMatchParams): Promise<MatchDocument> {
|
||||
const MatchModel = await this.connectionService.getModel('match');
|
||||
const UserModel = await this.connectionService.getModel('user');
|
||||
|
||||
const user = await UserModel.findById(params.userId);
|
||||
if (!user) throw new NotFoundException('User not found.');
|
||||
|
||||
const match = await MatchModel.findById(params.matchId);
|
||||
if (!match) throw new NotFoundException('Match not found.');
|
||||
if (!match.communityId.equals(user.communityId)) {
|
||||
throw new BadRequestException(
|
||||
'User and match belong to different communities.',
|
||||
);
|
||||
}
|
||||
if (match.status !== 'recruiting') {
|
||||
throw new BadRequestException('Match is not recruiting.');
|
||||
}
|
||||
if (match.participants.length >= match.maxParticipants) {
|
||||
throw new BadRequestException('Match is full.');
|
||||
}
|
||||
const alreadyIn = match.participants.some((p) =>
|
||||
p.userId.equals(user._id),
|
||||
);
|
||||
if (alreadyIn) {
|
||||
throw new BadRequestException('User already joined this match.');
|
||||
}
|
||||
|
||||
const primaryRiot = user.riotAccounts.find((a) => a.isPrimary);
|
||||
match.participants.push({
|
||||
userId: user._id,
|
||||
puuid: primaryRiot?.puuid,
|
||||
gameName: primaryRiot?.gameName,
|
||||
tagLine: primaryRiot?.tagLine,
|
||||
profileIconId: primaryRiot?.profileIconId,
|
||||
preferredPositions: params.preferredPositions,
|
||||
joinedAt: new Date(),
|
||||
});
|
||||
await match.save();
|
||||
return match;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user