Files
gridpilot.gg/apps/website/lib/services/leagues/LeagueService.ts
2025-12-18 01:20:23 +01:00

77 lines
2.4 KiB
TypeScript

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<unknown> };
type LeagueMembershipsDto = { memberships: Array<unknown> };
/**
* 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<LeagueSummaryViewModel[]> {
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<LeagueStandingsViewModel> {
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<LeagueStatsDto> {
return await this.apiClient.getTotal();
}
/**
* Get league schedule
*/
async getLeagueSchedule(leagueId: string): Promise<LeagueScheduleDto> {
return await this.apiClient.getSchedule(leagueId);
}
/**
* Get league memberships
*/
async getLeagueMemberships(leagueId: string): Promise<LeagueMembershipsDto> {
return await this.apiClient.getMemberships(leagueId);
}
/**
* Create a new league
*/
async createLeague(input: CreateLeagueInputDTO): Promise<CreateLeagueOutputDTO> {
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);
}
}