Expose mentor match API

This commit is contained in:
dahyeon committed 2026-07-03 02:04:11 -07:00
1 parent 4f70e07cb7
commit 1d8037adf1
4 files changed
+259 -2

No files matched your search

@@ -3,6 +3,7 @@ import {
FEATURE_SCHEMA_VERSION,
METRICS,
type AnalysisJobStatusDto,
type MentorMatchesDto,
type MetricValueDto,
type PlayerAnalysisDto,
} from '@persona-lenz/shared';
@@ -13,6 +14,8 @@ import type {
PlayerProfile,
TierInfo,
} from '../global/database/schemas';
import { MentorMatchService } from './mentor-match.service';
import type { MentorMatch } from './analysis.types';
type JobStatusRecord = Pick<
AnalysisJob,
@@ -24,6 +27,8 @@ type AccountSummaryRecord = Pick<
'gameName' | 'profileIconId' | 'tagLine' | 'tiers'
>;
type MentorAccountRecord = Pick<Account, 'gameName' | 'puuid' | 'tagLine'>;
type PlayerProfileRecord = Pick<
PlayerProfile,
| 'championPool'
@@ -38,7 +43,10 @@ type PlayerProfileRecord = Pick<
@Injectable()
export class AnalysisReadService {
constructor(private readonly connectionService: ConnectionService) {}
constructor(
private readonly connectionService: ConnectionService,
private readonly mentorMatchService?: MentorMatchService,
) {}
public async getJobStatus(jobId: string): Promise<AnalysisJobStatusDto> {
const analysisJobModel =
@@ -105,6 +113,34 @@ export class AnalysisReadService {
};
}
public async getMentorMatches(
puuid: string,
queueId: number,
limit: number,
): Promise<MentorMatchesDto> {
if (!this.mentorMatchService) {
throw new Error('MentorMatchService is not configured.');
}
const mentorMatchesResult = await this.mentorMatchService.findMentorMatches(
puuid,
queueId,
limit,
);
const mentorAccountsByPuuid = await this.findMentorAccounts(
mentorMatchesResult.matches.map((match) => match.puuid),
);
return {
puuid: mentorMatchesResult.puuid,
queueId: mentorMatchesResult.queueId,
profileUpdatedAt: mentorMatchesResult.profileUpdatedAt.toISOString(),
matches: mentorMatchesResult.matches.map((match) =>
this.toMentorMatchDto(match, mentorAccountsByPuuid.get(match.puuid)),
),
};
}
private async findAccount(
puuid: string,
): Promise<AccountSummaryRecord | null> {
@@ -112,6 +148,21 @@ export class AnalysisReadService {
return accountModel.findOne({ puuid }).lean<AccountSummaryRecord>().exec();
}
private async findMentorAccounts(
puuids: string[],
): Promise<Map<string, MentorAccountRecord>> {
const uniquePuuids = Array.from(new Set(puuids));
if (uniquePuuids.length === 0) return new Map();
const accountModel = await this.connectionService.getModel('account');
const accounts = await accountModel
.find({ puuid: { $in: uniquePuuids } })
.lean<MentorAccountRecord[]>()
.exec();
return new Map(accounts.map((account) => [account.puuid, account]));
}
private async findPlayerProfile(
puuid: string,
queueId: number,
@@ -170,6 +221,31 @@ export class AnalysisReadService {
};
}
private toMentorMatchDto(
match: MentorMatch,
account: MentorAccountRecord | undefined,
): MentorMatchesDto['matches'][number] {
return {
puuid: match.puuid,
riotId: account
? {
gameName: account.gameName,
tagLine: account.tagLine,
}
: null,
similarityScore: match.similarityScore,
finalScore: match.finalScore,
tier: match.tier,
division: match.division,
leaguePoints: match.leaguePoints,
mainRole: match.mainRole,
mainChampions: match.mainChampions,
sampleGameCount: match.sampleGameCount,
similarMetrics: match.similarMetrics,
gapMetrics: match.gapMetrics,
};
}
private publicJobError(error: string | undefined): string | null {
if (!error) return null;
if (
@@ -12,6 +12,7 @@ import {
import type {
AnalysisJobStatusDto,
AnalysisRequestAcceptedDto,
MentorMatchesDto,
PlayerAnalysisDto,
} from '@persona-lenz/shared';
import {
@@ -69,6 +70,19 @@ export class AnalysisController {
);
}
@Get('players/:puuid/mentor-matches')
public async getMentorMatches(
@Param('puuid') puuid: string,
@Query('queueId') rawQueueId?: string,
@Query('limit') rawLimit?: string,
): Promise<MentorMatchesDto> {
return this.analysisReadService.getMentorMatches(
this.parsePuuid(puuid),
this.parseQueueId(rawQueueId),
this.parseMentorMatchLimit(rawLimit),
);
}
private parseAnalysisRequest(body: unknown): UserAnalysisJobData {
const record = this.requireRecord(body);
const gameName = this.parseTrimmedString(
@@ -151,6 +165,15 @@ export class AnalysisController {
return queueId;
}
private parseMentorMatchLimit(value?: string): number {
if (value === undefined) return 10;
const limit = Number(value);
if (!Number.isInteger(limit) || limit < 1) {
throw new BadRequestException('limit is out of range.');
}
return Math.min(limit, 10);
}
private resolveClientKey(request: Request): string {
return request.ip || request.socket.remoteAddress || 'unknown-client';
}