services refactor

This commit is contained in:
2025-12-17 22:17:02 +01:00
parent 26f7a2b6aa
commit 055a7f67b5
93 changed files with 7434 additions and 659 deletions

View File

@@ -1,28 +1,76 @@
import { api as api } from '../../api';
import { presentLeagueSummaries, presentLeagueStandings } from '../../presenters';
import { LeagueSummaryViewModel, LeagueStandingsViewModel } from '../../view-models';
import type { LeaguesApiClient } from '../../api/leagues/LeaguesApiClient';
import type { LeagueSummaryPresenter } from '../../presenters/LeagueSummaryPresenter';
import type { LeagueStandingsPresenter } from '../../presenters/LeagueStandingsPresenter';
import type { LeagueSummaryViewModel, LeagueStandingsViewModel } from '../../view-models';
import type { CreateLeagueInputDto, CreateLeagueOutputDto, LeagueStatsDto, LeagueScheduleDto, LeagueMembershipsDto } from '../../dtos';
export async function getAllLeagues(): Promise<LeagueSummaryViewModel[]> {
const dto = await api.leagues.getAllWithCapacity();
return presentLeagueSummaries(dto.leagues);
}
/**
* League Service
*
* Orchestrates league operations by coordinating API calls and presentation logic.
* All dependencies are injected via constructor.
*/
export class LeagueService {
constructor(
private readonly apiClient: LeaguesApiClient,
private readonly leagueSummaryPresenter: LeagueSummaryPresenter,
private readonly leagueStandingsPresenter: LeagueStandingsPresenter
) {}
export async function getLeagueStandings(leagueId: string, currentUserId?: string): Promise<LeagueStandingsViewModel> {
const dto = await api.leagues.getStandings(leagueId);
// TODO: include drivers and memberships in dto
const dtoWithExtras = {
...dto,
drivers: [], // TODO: fetch drivers
memberships: [], // TODO: fetch memberships
};
return presentLeagueStandings(dtoWithExtras, currentUserId || '');
}
/**
* Get all leagues with presentation transformation
*/
async getAllLeagues(): Promise<LeagueSummaryViewModel[]> {
const dto = await this.apiClient.getAllWithCapacity();
return this.leagueSummaryPresenter.present(dto);
}
export async function createLeague(input: any): Promise<any> {
return await api.leagues.create(input);
}
/**
* Get league standings with presentation 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 this.leagueStandingsPresenter.present(dtoWithExtras, currentUserId);
}
export async function getLeagueAdminView(leagueId: string): Promise<any> {
// TODO: implement
return {};
/**
* 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);
}
}