feat: allow admins to add match participants
This commit is contained in:
1 parent
8aff1be225
commit
24983aeb09
11 files changed
+1019
-19
No files matched your search
@@ -193,3 +193,17 @@ export class JoinMatchDto {
|
||||
@IsString()
|
||||
userId?: string;
|
||||
}
|
||||
|
||||
export class AddParticipantDto {
|
||||
@IsOptional()
|
||||
@IsArray()
|
||||
@ArrayMinSize(1)
|
||||
@ArrayMaxSize(5)
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => PositionPreferenceDto)
|
||||
preferredPositions?: PositionPreferenceDto[];
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
riotPuuid?: string;
|
||||
}
|
||||
@@ -24,6 +24,7 @@ import type {
|
||||
UserDocument,
|
||||
} from '../global/database/schema';
|
||||
import {
|
||||
AddParticipantDto,
|
||||
CreateMatchDto,
|
||||
JoinMatchDto,
|
||||
UpdateMatchDto,
|
||||
@@ -149,6 +150,28 @@ export class MatchController {
|
||||
});
|
||||
}
|
||||
|
||||
// 관리자 전용 — 커뮤니티 유저를 모집중 매치에 직접 추가한다.
|
||||
@Post(':matchId/participants/:userId')
|
||||
@Roles('admin', 'owner')
|
||||
@UseGuards(RolesGuard)
|
||||
public async addParticipant(
|
||||
@CurrentUser() currentUser: UserDocument,
|
||||
@Param('communityId', ParseObjectIdPipe)
|
||||
communityId: mongoose.Types.ObjectId,
|
||||
@Param('matchId', ParseObjectIdPipe) matchId: mongoose.Types.ObjectId,
|
||||
@Param('userId', ParseObjectIdPipe) userId: mongoose.Types.ObjectId,
|
||||
@Body() body: AddParticipantDto | undefined,
|
||||
) {
|
||||
this.assertSameCommunity(currentUser, communityId);
|
||||
const match = await this.matchService.join({
|
||||
matchId,
|
||||
userId,
|
||||
preferredPositions: body?.preferredPositions,
|
||||
riotPuuid: body?.riotPuuid,
|
||||
});
|
||||
return this.matchService.serialize(match);
|
||||
}
|
||||
|
||||
// 관리자 전용 — 참가자 주/부 포지션 변경.
|
||||
@Patch(':matchId/participants/:userId')
|
||||
@Roles('admin', 'owner')
|
||||
|
||||
@@ -80,6 +80,47 @@ describe('MatchService', () => {
|
||||
};
|
||||
}
|
||||
|
||||
function createServiceWithJoinModels(params: {
|
||||
user: Record<string, unknown> | null;
|
||||
match: Record<string, unknown> | null;
|
||||
}) {
|
||||
const UserModel = {
|
||||
findById: jest.fn().mockResolvedValue(params.user),
|
||||
};
|
||||
const MatchModel = {
|
||||
findById: jest.fn().mockResolvedValue(params.match),
|
||||
};
|
||||
const connectionService = {
|
||||
getModel: jest.fn().mockImplementation(async (name: string) => {
|
||||
if (name === 'user') return UserModel;
|
||||
if (name === 'match') return MatchModel;
|
||||
return {};
|
||||
}),
|
||||
};
|
||||
const tournamentCodeService = {
|
||||
buildSlots: jest.fn().mockResolvedValue([]),
|
||||
releaseFromMatch: jest.fn().mockResolvedValue(undefined),
|
||||
};
|
||||
const communityStatsService = {
|
||||
invalidateCommunity: jest.fn().mockResolvedValue(undefined),
|
||||
};
|
||||
const matchStatsService = {
|
||||
buildAndUpsert: jest.fn().mockResolvedValue(undefined),
|
||||
removeByMatch: jest.fn().mockResolvedValue(undefined),
|
||||
};
|
||||
|
||||
return {
|
||||
service: new MatchService(
|
||||
connectionService as never,
|
||||
tournamentCodeService as never,
|
||||
communityStatsService as never,
|
||||
matchStatsService as never,
|
||||
),
|
||||
UserModel,
|
||||
MatchModel,
|
||||
};
|
||||
}
|
||||
|
||||
it('returns a paginated match list with total count', async () => {
|
||||
const communityId = new mongoose.Types.ObjectId();
|
||||
const { service, MatchModel, sort, skip, limit } =
|
||||
@@ -165,6 +206,54 @@ describe('MatchService', () => {
|
||||
expect(sort).toHaveBeenCalledWith({ scheduledAt: -1 });
|
||||
});
|
||||
|
||||
it('uses saved user preferences when joining without explicit positions', async () => {
|
||||
const communityId = new mongoose.Types.ObjectId();
|
||||
const userId = new mongoose.Types.ObjectId();
|
||||
const matchId = new mongoose.Types.ObjectId();
|
||||
const user = {
|
||||
_id: userId,
|
||||
communityId,
|
||||
preferredPositions: [{ position: 'MID', priority: 1 }],
|
||||
riotAccounts: [
|
||||
{
|
||||
puuid: 'puuid-1',
|
||||
gameName: 'Dahyeon',
|
||||
tagLine: 'KR1',
|
||||
profileIconId: 7,
|
||||
isPrimary: true,
|
||||
tiers: [],
|
||||
},
|
||||
],
|
||||
};
|
||||
const match = {
|
||||
_id: matchId,
|
||||
communityId,
|
||||
status: 'recruiting',
|
||||
maxParticipants: 10,
|
||||
participants: [],
|
||||
save: jest.fn().mockResolvedValue(undefined),
|
||||
};
|
||||
const { service } = createServiceWithJoinModels({ user, match });
|
||||
|
||||
await service.join({
|
||||
matchId,
|
||||
userId,
|
||||
});
|
||||
|
||||
expect(match.participants).toEqual([
|
||||
expect.objectContaining({
|
||||
userId,
|
||||
puuid: 'puuid-1',
|
||||
gameName: 'Dahyeon',
|
||||
tagLine: 'KR1',
|
||||
profileIconId: 7,
|
||||
preferredPositions: [{ position: 'MID', priority: 1 }],
|
||||
excluded: false,
|
||||
}),
|
||||
]);
|
||||
expect(match.save).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('clears game schedule when a built match returns to recruiting', async () => {
|
||||
const communityId = new mongoose.Types.ObjectId();
|
||||
const match = {
|
||||
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
MatchDocument,
|
||||
MatchSeriesResult,
|
||||
MatchStatus,
|
||||
POSITIONS,
|
||||
Position,
|
||||
PositionPreference,
|
||||
TierInfo,
|
||||
@@ -64,7 +65,7 @@ interface MatchListFilter {
|
||||
interface JoinMatchParams {
|
||||
matchId: mongoose.Types.ObjectId;
|
||||
userId: mongoose.Types.ObjectId;
|
||||
preferredPositions: PositionPreference[];
|
||||
preferredPositions?: PositionPreference[];
|
||||
// 특정 RiotAccount(puuid)로 참가하고 싶을 때 지정. 없으면 primary 사용.
|
||||
riotPuuid?: string;
|
||||
}
|
||||
@@ -293,7 +294,10 @@ export class MatchService {
|
||||
tagLine: selectedRiot?.tagLine,
|
||||
profileIconId: selectedRiot?.profileIconId,
|
||||
tier: selectedTier,
|
||||
preferredPositions: params.preferredPositions,
|
||||
preferredPositions: this.resolveJoinPreferredPositions(
|
||||
params.preferredPositions,
|
||||
user.preferredPositions,
|
||||
),
|
||||
joinedAt: new Date(),
|
||||
excluded: false,
|
||||
});
|
||||
@@ -436,6 +440,28 @@ export class MatchService {
|
||||
};
|
||||
}
|
||||
|
||||
private resolveJoinPreferredPositions(
|
||||
requested: PositionPreference[] | undefined,
|
||||
saved: PositionPreference[] | undefined,
|
||||
): PositionPreference[] {
|
||||
const source = requested?.length
|
||||
? requested
|
||||
: saved?.length
|
||||
? saved
|
||||
: POSITIONS.map((position, index) => ({
|
||||
position,
|
||||
priority: index + 1,
|
||||
}));
|
||||
|
||||
return [...source]
|
||||
.sort((left, right) => left.priority - right.priority)
|
||||
.slice(0, 5)
|
||||
.map((preference, index) => ({
|
||||
position: preference.position,
|
||||
priority: index + 1,
|
||||
}));
|
||||
}
|
||||
|
||||
private buildListFilter(query: ListMatchesQuery): MatchListFilter {
|
||||
const filter: MatchListFilter = {
|
||||
communityId: query.communityId,
|
||||
|
||||
Reference in new issue
Block a user