import { PageQuery } from '@/lib/contracts/page-queries/PageQuery'; import { Result } from '@/lib/contracts/Result'; import { LeaderboardsViewDataBuilder } from '@/lib/builders/view-data/LeaderboardsViewDataBuilder'; import type { LeaderboardsViewData } from '@/lib/view-data/LeaderboardsViewData'; import { LeaderboardsService } from '@/lib/services/leaderboards/LeaderboardsService'; import { mapToPresentationError, type PresentationError } from '@/lib/contracts/page-queries/PresentationError'; /** * Leaderboards page query * Returns Result * No DI container usage - constructs dependencies explicitly */ export class LeaderboardsPageQuery implements PageQuery { async execute(): Promise> { // Manual wiring: Service creates its own dependencies const service = new LeaderboardsService(); // Fetch data using service const serviceResult = await service.getLeaderboards(); if (serviceResult.isErr()) { // Map domain errors to presentation errors return Result.err(mapToPresentationError(serviceResult.getError())); } // Transform to ViewData using builder const apiDto = serviceResult.unwrap(); // Ensure we have data even if API returns empty if (!apiDto.drivers || !apiDto.drivers.drivers) { apiDto.drivers = { drivers: [] }; } if (!apiDto.teams) { apiDto.teams = { teams: [], topTeams: [], recruitingCount: 0, groupsBySkillLevel: '' }; } const viewData = LeaderboardsViewDataBuilder.build(apiDto); return Result.ok(viewData); } // Static method to avoid object construction in server code static async execute(): Promise> { const query = new LeaderboardsPageQuery(); return query.execute(); } }