import type { LeaguesApiClient } from '../../api/leagues/LeaguesApiClient'; import { LeagueSummaryViewModel, LeagueStandingsViewModel } from '../../view-models'; import type { CreateLeagueInputDTO, CreateLeagueOutputDTO, LeagueWithCapacityDTO } from '../../types/generated'; // TODO: Move these types to apps/website/lib/types/generated when available type LeagueStatsDto = { totalLeagues: number }; type LeagueScheduleDto = { races: Array }; type LeagueMembershipsDto = { memberships: Array }; /** * League Service * * Orchestrates league operations by coordinating API calls and view model creation. * All dependencies are injected via constructor. */ export class LeagueService { constructor( private readonly apiClient: LeaguesApiClient ) {} /** * Get all leagues with view model transformation */ async getAllLeagues(): Promise { const dto = await this.apiClient.getAllWithCapacity(); return dto.leagues.map((league: LeagueWithCapacityDTO) => new LeagueSummaryViewModel(league)); } /** * Get league standings with view model transformation */ async getLeagueStandings(leagueId: string, currentUserId: string): Promise { const dto = await this.apiClient.getStandings(leagueId); // TODO: include drivers and memberships in dto const dtoWithExtras = { ...dto, drivers: [], // TODO: fetch drivers memberships: [], // TODO: fetch memberships }; return new LeagueStandingsViewModel(dtoWithExtras, currentUserId); } /** * Get league statistics */ async getLeagueStats(): Promise { return await this.apiClient.getTotal(); } /** * Get league schedule */ async getLeagueSchedule(leagueId: string): Promise { return await this.apiClient.getSchedule(leagueId); } /** * Get league memberships */ async getLeagueMemberships(leagueId: string): Promise { return await this.apiClient.getMemberships(leagueId); } /** * Create a new league */ async createLeague(input: CreateLeagueInputDTO): Promise { return await this.apiClient.create(input); } /** * Remove a member from league */ async removeMember(leagueId: string, performerDriverId: string, targetDriverId: string): Promise<{ success: boolean }> { return await this.apiClient.removeMember(leagueId, performerDriverId, targetDriverId); } }