import { BadRequestException, Controller, Get, Param, Query, } from '@nestjs/common'; import mongoose from 'mongoose'; import { AllowPublicAccess } from '../auth/allow-public-access.decorator'; import { ParseObjectIdPipe } from '../common/parse-object-id.pipe'; import { CommunityStatsService } from './community-stats.service'; import type { CommunityStatsFilters } from './community-stats.types'; @Controller('communities/:communityId') export class CommunityStatsController { constructor(private readonly communityStatsService: CommunityStatsService) {} @Get('internal-stats/seasons') @AllowPublicAccess() public getSeasons( @Param('communityId', ParseObjectIdPipe) communityId: mongoose.Types.ObjectId, ) { return this.communityStatsService.getSeasons(communityId); } @Get('internal-stats/matches') @AllowPublicAccess() public getMatches( @Param('communityId', ParseObjectIdPipe) communityId: mongoose.Types.ObjectId, @Query('seasonId') seasonId?: string, @Query('from') from?: string, @Query('to') to?: string, @Query('championId') championId?: string, @Query('cursor') cursor?: string, @Query('limit') limit?: string, @Query('q') query?: string, @Query('format') format?: string, @Query('playerId') playerId?: string, @Query('cursorMatchId') cursorMatchId?: string, ) { if (format !== undefined && format !== 'pair' && format !== 'tournament') { throw new BadRequestException('Invalid format.'); } if (playerId !== undefined && !mongoose.isObjectIdOrHexString(playerId)) { throw new BadRequestException('Invalid playerId.'); } if ( cursorMatchId !== undefined && (!mongoose.isObjectIdOrHexString(cursorMatchId) || this.toNumber(cursor) === undefined) ) { throw new BadRequestException('Invalid cursor.'); } if ( cursor !== undefined && Number.isNaN(new Date(this.toNumber(cursor) ?? NaN).getTime()) ) { throw new BadRequestException('Invalid cursor.'); } return this.communityStatsService.getMatches({ communityId, filters: this.toFilters({ seasonId, from, to, championId, cursor, limit, query, format, playerId, cursorMatchId, }), }); } @Get('internal-stats/leaderboard') @AllowPublicAccess() public getLeaderboard( @Param('communityId', ParseObjectIdPipe) communityId: mongoose.Types.ObjectId, @Query('seasonId') seasonId?: string, @Query('from') from?: string, @Query('to') to?: string, @Query('championId') championId?: string, @Query('q') query?: string, ) { return this.communityStatsService.getLeaderboard({ communityId, filters: this.toFilters({ seasonId, from, to, championId, query }), }); } @Get('internal-stats/team-insights') @AllowPublicAccess() public getTeamInsights( @Param('communityId', ParseObjectIdPipe) communityId: mongoose.Types.ObjectId, @Query('seasonId') seasonId?: string, @Query('from') from?: string, @Query('to') to?: string, @Query('championId') championId?: string, ) { return this.communityStatsService.getTeamInsights({ communityId, filters: this.toFilters({ seasonId, from, to, championId }), }); } @Get('internal-stats/match/:matchId') @AllowPublicAccess() public getMatchDetail( @Param('communityId', ParseObjectIdPipe) communityId: mongoose.Types.ObjectId, @Param('matchId', ParseObjectIdPipe) matchId: mongoose.Types.ObjectId, ) { return this.communityStatsService.getMatchDetail({ communityId, matchId }); } @Get('users/:userId/internal-history') @AllowPublicAccess() public getUserInternalHistory( @Param('communityId', ParseObjectIdPipe) communityId: mongoose.Types.ObjectId, @Param('userId', ParseObjectIdPipe) userId: mongoose.Types.ObjectId, @Query('seasonId') seasonId?: string, @Query('from') from?: string, @Query('to') to?: string, @Query('championId') championId?: string, @Query('cursor') cursor?: string, @Query('limit') limit?: string, ) { return this.communityStatsService.getUserInternalHistory({ communityId, userId, filters: this.toFilters({ seasonId, from, to, championId, cursor, limit, }), }); } private toFilters(params: { seasonId?: string; from?: string; to?: string; championId?: string; cursor?: string; limit?: string; query?: string; format?: CommunityStatsFilters['format']; playerId?: string; cursorMatchId?: string; }): CommunityStatsFilters { if ( params.query !== undefined && (typeof params.query !== 'string' || params.query.length > 100) ) { throw new BadRequestException('Invalid search query.'); } return { seasonId: params.seasonId, from: params.from, to: params.to, championId: this.toNumber(params.championId), cursor: this.toNumber(params.cursor), limit: this.toNumber(params.limit), query: params.query?.trim(), format: params.format, playerId: params.playerId, cursorMatchId: params.cursorMatchId, }; } private toNumber(value: string | undefined): number | undefined { if (!value) return undefined; const parsed = Number(value); return Number.isFinite(parsed) ? parsed : undefined; } }