import { 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/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, ) { return this.communityStatsService.getMatches({ communityId, filters: this.toFilters({ seasonId, from, to, championId, cursor, limit }), }); } @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, ) { return this.communityStatsService.getLeaderboard({ communityId, filters: this.toFilters({ seasonId, from, to, championId }), }); } @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; }): CommunityStatsFilters { 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), }; } private toNumber(value: string | undefined): number | undefined { if (!value) return undefined; const parsed = Number(value); return Number.isFinite(parsed) ? parsed : undefined; } }