Expose mentor match API
This commit is contained in:
1 parent
4f70e07cb7
commit
1d8037adf1
4 files changed
+259
-2
No files matched your search
@@ -3,6 +3,7 @@ import {
|
|||||||
FEATURE_SCHEMA_VERSION,
|
FEATURE_SCHEMA_VERSION,
|
||||||
METRICS,
|
METRICS,
|
||||||
type AnalysisJobStatusDto,
|
type AnalysisJobStatusDto,
|
||||||
|
type MentorMatchesDto,
|
||||||
type MetricValueDto,
|
type MetricValueDto,
|
||||||
type PlayerAnalysisDto,
|
type PlayerAnalysisDto,
|
||||||
} from '@persona-lenz/shared';
|
} from '@persona-lenz/shared';
|
||||||
@@ -13,6 +14,8 @@ import type {
|
|||||||
PlayerProfile,
|
PlayerProfile,
|
||||||
TierInfo,
|
TierInfo,
|
||||||
} from '../global/database/schemas';
|
} from '../global/database/schemas';
|
||||||
|
import { MentorMatchService } from './mentor-match.service';
|
||||||
|
import type { MentorMatch } from './analysis.types';
|
||||||
|
|
||||||
type JobStatusRecord = Pick<
|
type JobStatusRecord = Pick<
|
||||||
AnalysisJob,
|
AnalysisJob,
|
||||||
@@ -24,6 +27,8 @@ type AccountSummaryRecord = Pick<
|
|||||||
'gameName' | 'profileIconId' | 'tagLine' | 'tiers'
|
'gameName' | 'profileIconId' | 'tagLine' | 'tiers'
|
||||||
>;
|
>;
|
||||||
|
|
||||||
|
type MentorAccountRecord = Pick<Account, 'gameName' | 'puuid' | 'tagLine'>;
|
||||||
|
|
||||||
type PlayerProfileRecord = Pick<
|
type PlayerProfileRecord = Pick<
|
||||||
PlayerProfile,
|
PlayerProfile,
|
||||||
| 'championPool'
|
| 'championPool'
|
||||||
@@ -38,7 +43,10 @@ type PlayerProfileRecord = Pick<
|
|||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AnalysisReadService {
|
export class AnalysisReadService {
|
||||||
constructor(private readonly connectionService: ConnectionService) {}
|
constructor(
|
||||||
|
private readonly connectionService: ConnectionService,
|
||||||
|
private readonly mentorMatchService?: MentorMatchService,
|
||||||
|
) {}
|
||||||
|
|
||||||
public async getJobStatus(jobId: string): Promise<AnalysisJobStatusDto> {
|
public async getJobStatus(jobId: string): Promise<AnalysisJobStatusDto> {
|
||||||
const analysisJobModel =
|
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(
|
private async findAccount(
|
||||||
puuid: string,
|
puuid: string,
|
||||||
): Promise<AccountSummaryRecord | null> {
|
): Promise<AccountSummaryRecord | null> {
|
||||||
@@ -112,6 +148,21 @@ export class AnalysisReadService {
|
|||||||
return accountModel.findOne({ puuid }).lean<AccountSummaryRecord>().exec();
|
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(
|
private async findPlayerProfile(
|
||||||
puuid: string,
|
puuid: string,
|
||||||
queueId: number,
|
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 {
|
private publicJobError(error: string | undefined): string | null {
|
||||||
if (!error) return null;
|
if (!error) return null;
|
||||||
if (
|
if (
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import {
|
|||||||
import type {
|
import type {
|
||||||
AnalysisJobStatusDto,
|
AnalysisJobStatusDto,
|
||||||
AnalysisRequestAcceptedDto,
|
AnalysisRequestAcceptedDto,
|
||||||
|
MentorMatchesDto,
|
||||||
PlayerAnalysisDto,
|
PlayerAnalysisDto,
|
||||||
} from '@persona-lenz/shared';
|
} from '@persona-lenz/shared';
|
||||||
import {
|
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 {
|
private parseAnalysisRequest(body: unknown): UserAnalysisJobData {
|
||||||
const record = this.requireRecord(body);
|
const record = this.requireRecord(body);
|
||||||
const gameName = this.parseTrimmedString(
|
const gameName = this.parseTrimmedString(
|
||||||
@@ -151,6 +165,15 @@ export class AnalysisController {
|
|||||||
return queueId;
|
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 {
|
private resolveClientKey(request: Request): string {
|
||||||
return request.ip || request.socket.remoteAddress || 'unknown-client';
|
return request.ip || request.socket.remoteAddress || 'unknown-client';
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { Test, TestingModule } from '@nestjs/testing';
|
|||||||
import { INestApplication } from '@nestjs/common';
|
import { INestApplication } from '@nestjs/common';
|
||||||
import {
|
import {
|
||||||
FEATURE_SCHEMA_VERSION,
|
FEATURE_SCHEMA_VERSION,
|
||||||
|
type MentorMatchesDto,
|
||||||
type PlayerAnalysisDto,
|
type PlayerAnalysisDto,
|
||||||
} from '@persona-lenz/shared';
|
} from '@persona-lenz/shared';
|
||||||
import request from 'supertest';
|
import request from 'supertest';
|
||||||
@@ -10,7 +11,11 @@ import { App } from 'supertest/types';
|
|||||||
import { configureApp } from './../src/app.setup';
|
import { configureApp } from './../src/app.setup';
|
||||||
import { AppModule } from './../src/app.module';
|
import { AppModule } from './../src/app.module';
|
||||||
import { AnalysisQueueService } from './../src/modules/analysis/analysis-queue.service';
|
import { AnalysisQueueService } from './../src/modules/analysis/analysis-queue.service';
|
||||||
import { ANALYSIS_QUEUE_NAME } from './../src/modules/analysis/analysis.constants';
|
import {
|
||||||
|
ANALYSIS_QUEUE_NAME,
|
||||||
|
SEED_COLLECTION_QUEUE_NAME,
|
||||||
|
} from './../src/modules/analysis/analysis.constants';
|
||||||
|
import { MentorMatchService } from './../src/modules/analysis/mentor-match.service';
|
||||||
import { ConnectionService } from './../src/modules/global/database/connection.service';
|
import { ConnectionService } from './../src/modules/global/database/connection.service';
|
||||||
import { REDIS_CLIENT } from './../src/modules/global/redis/redis.constants';
|
import { REDIS_CLIENT } from './../src/modules/global/redis/redis.constants';
|
||||||
import { SecretService } from './../src/modules/global/secret/secret.service';
|
import { SecretService } from './../src/modules/global/secret/secret.service';
|
||||||
@@ -20,6 +25,9 @@ describe('AppController (e2e)', () => {
|
|||||||
let analysisQueueService: {
|
let analysisQueueService: {
|
||||||
enqueueUserAnalysis: jest.Mock;
|
enqueueUserAnalysis: jest.Mock;
|
||||||
};
|
};
|
||||||
|
let mentorMatchService: {
|
||||||
|
findMentorMatches: jest.Mock;
|
||||||
|
};
|
||||||
let connectionService: {
|
let connectionService: {
|
||||||
getModel: jest.Mock;
|
getModel: jest.Mock;
|
||||||
};
|
};
|
||||||
@@ -36,6 +44,9 @@ describe('AppController (e2e)', () => {
|
|||||||
analysisQueueService = {
|
analysisQueueService = {
|
||||||
enqueueUserAnalysis: jest.fn(),
|
enqueueUserAnalysis: jest.fn(),
|
||||||
};
|
};
|
||||||
|
mentorMatchService = {
|
||||||
|
findMentorMatches: jest.fn(),
|
||||||
|
};
|
||||||
connectionService = {
|
connectionService = {
|
||||||
getModel: jest.fn(),
|
getModel: jest.fn(),
|
||||||
};
|
};
|
||||||
@@ -46,6 +57,8 @@ describe('AppController (e2e)', () => {
|
|||||||
.useValue(connectionService)
|
.useValue(connectionService)
|
||||||
.overrideProvider(AnalysisQueueService)
|
.overrideProvider(AnalysisQueueService)
|
||||||
.useValue(analysisQueueService)
|
.useValue(analysisQueueService)
|
||||||
|
.overrideProvider(MentorMatchService)
|
||||||
|
.useValue(mentorMatchService)
|
||||||
.overrideProvider(SecretService)
|
.overrideProvider(SecretService)
|
||||||
.useValue({
|
.useValue({
|
||||||
get: jest.fn((key: string) => {
|
get: jest.fn((key: string) => {
|
||||||
@@ -65,6 +78,10 @@ describe('AppController (e2e)', () => {
|
|||||||
.useValue({
|
.useValue({
|
||||||
add: jest.fn(),
|
add: jest.fn(),
|
||||||
})
|
})
|
||||||
|
.overrideProvider(getQueueToken(SEED_COLLECTION_QUEUE_NAME))
|
||||||
|
.useValue({
|
||||||
|
add: jest.fn(),
|
||||||
|
})
|
||||||
.compile();
|
.compile();
|
||||||
|
|
||||||
app = moduleFixture.createNestApplication();
|
app = moduleFixture.createNestApplication();
|
||||||
@@ -334,6 +351,7 @@ describe('AppController (e2e)', () => {
|
|||||||
expect(profileFindOne).toHaveBeenCalledWith({
|
expect(profileFindOne).toHaveBeenCalledWith({
|
||||||
puuid: 'target-puuid',
|
puuid: 'target-puuid',
|
||||||
queueId: 420,
|
queueId: 420,
|
||||||
|
featureSchemaVersion: FEATURE_SCHEMA_VERSION,
|
||||||
});
|
});
|
||||||
expect(responseBody).toEqual({
|
expect(responseBody).toEqual({
|
||||||
account: {
|
account: {
|
||||||
@@ -410,6 +428,105 @@ describe('AppController (e2e)', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('/api/players/:puuid/mentor-matches (GET) returns mentor matches with display Riot IDs', async () => {
|
||||||
|
mentorMatchService.findMentorMatches.mockResolvedValue({
|
||||||
|
puuid: 'target-puuid',
|
||||||
|
queueId: 420,
|
||||||
|
profileUpdatedAt: new Date('2026-07-02T00:00:00.000Z'),
|
||||||
|
matches: [
|
||||||
|
{
|
||||||
|
puuid: 'mentor-puuid',
|
||||||
|
similarityScore: 0.87,
|
||||||
|
finalScore: 0.81,
|
||||||
|
tier: 'CHALLENGER',
|
||||||
|
leaguePoints: 900,
|
||||||
|
mainRole: 'MIDDLE',
|
||||||
|
mainChampions: [{ championId: 103, games: 20, wins: 14 }],
|
||||||
|
sampleGameCount: 40,
|
||||||
|
similarMetrics: [
|
||||||
|
{
|
||||||
|
metricId: 'kda',
|
||||||
|
currentValue: 0.3,
|
||||||
|
mentorValue: 0.31,
|
||||||
|
absoluteGap: 0.01,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
gapMetrics: [
|
||||||
|
{
|
||||||
|
metricId: 'goldPerMin',
|
||||||
|
currentValue: 0.5,
|
||||||
|
mentorValue: 0.8,
|
||||||
|
mentorAdvantage: 0.3,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
const accountFind = jest.fn(() =>
|
||||||
|
createLeanExec([
|
||||||
|
{
|
||||||
|
puuid: 'mentor-puuid',
|
||||||
|
gameName: '프리리',
|
||||||
|
tagLine: 'KR1',
|
||||||
|
},
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
connectionService.getModel.mockResolvedValue({
|
||||||
|
find: accountFind,
|
||||||
|
});
|
||||||
|
|
||||||
|
const response = await request(app.getHttpServer())
|
||||||
|
.get('/api/players/target-puuid/mentor-matches?limit=3')
|
||||||
|
.expect(200);
|
||||||
|
const responseBody = response.body as MentorMatchesDto;
|
||||||
|
|
||||||
|
expect(mentorMatchService.findMentorMatches).toHaveBeenCalledWith(
|
||||||
|
'target-puuid',
|
||||||
|
420,
|
||||||
|
3,
|
||||||
|
);
|
||||||
|
expect(accountFind).toHaveBeenCalledWith({
|
||||||
|
puuid: { $in: ['mentor-puuid'] },
|
||||||
|
});
|
||||||
|
expect(responseBody).toEqual({
|
||||||
|
puuid: 'target-puuid',
|
||||||
|
queueId: 420,
|
||||||
|
profileUpdatedAt: '2026-07-02T00:00:00.000Z',
|
||||||
|
matches: [
|
||||||
|
{
|
||||||
|
puuid: 'mentor-puuid',
|
||||||
|
riotId: {
|
||||||
|
gameName: '프리리',
|
||||||
|
tagLine: 'KR1',
|
||||||
|
},
|
||||||
|
similarityScore: 0.87,
|
||||||
|
finalScore: 0.81,
|
||||||
|
tier: 'CHALLENGER',
|
||||||
|
leaguePoints: 900,
|
||||||
|
mainRole: 'MIDDLE',
|
||||||
|
mainChampions: [{ championId: 103, games: 20, wins: 14 }],
|
||||||
|
sampleGameCount: 40,
|
||||||
|
similarMetrics: [
|
||||||
|
{
|
||||||
|
metricId: 'kda',
|
||||||
|
currentValue: 0.3,
|
||||||
|
mentorValue: 0.31,
|
||||||
|
absoluteGap: 0.01,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
gapMetrics: [
|
||||||
|
{
|
||||||
|
metricId: 'goldPerMin',
|
||||||
|
currentValue: 0.5,
|
||||||
|
mentorValue: 0.8,
|
||||||
|
mentorAdvantage: 0.3,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
afterEach(async () => {
|
afterEach(async () => {
|
||||||
await app.close();
|
await app.close();
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -96,3 +96,44 @@ export interface PlayerAnalysisDto {
|
|||||||
weaknesses: MetricValueDto[];
|
weaknesses: MetricValueDto[];
|
||||||
updatedAt: string;
|
updatedAt: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface MentorRiotIdDto {
|
||||||
|
gameName: string;
|
||||||
|
tagLine: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface SimilarMentorMetricDto {
|
||||||
|
metricId: MetricId;
|
||||||
|
currentValue: number;
|
||||||
|
mentorValue: number;
|
||||||
|
absoluteGap: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface GapMentorMetricDto {
|
||||||
|
metricId: MetricId;
|
||||||
|
currentValue: number;
|
||||||
|
mentorValue: number;
|
||||||
|
mentorAdvantage: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface MentorMatchDto {
|
||||||
|
puuid: string;
|
||||||
|
riotId: MentorRiotIdDto | null;
|
||||||
|
similarityScore: number;
|
||||||
|
finalScore: number;
|
||||||
|
tier?: string;
|
||||||
|
division?: string;
|
||||||
|
leaguePoints?: number;
|
||||||
|
mainRole: Role;
|
||||||
|
mainChampions: ChampionPoolEntryDto[];
|
||||||
|
sampleGameCount: number;
|
||||||
|
similarMetrics: SimilarMentorMetricDto[];
|
||||||
|
gapMetrics: GapMentorMetricDto[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface MentorMatchesDto {
|
||||||
|
puuid: string;
|
||||||
|
queueId: number;
|
||||||
|
profileUpdatedAt: string;
|
||||||
|
matches: MentorMatchDto[];
|
||||||
|
}
|
||||||
Reference in new issue
Block a user