Files
gridpilot.gg/core/racing/application/use-cases/GetLeagueSeasonsUseCase.ts
2026-01-08 15:34:51 +01:00

64 lines
2.1 KiB
TypeScript

import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
import type { ISeasonRepository } from '../../domain/repositories/ISeasonRepository';
import type { Season } from '../../domain/entities/season/Season';
export interface GetLeagueSeasonsInput {
leagueId: string;
}
export type GetLeagueSeasonsErrorCode = 'LEAGUE_NOT_FOUND' | 'REPOSITORY_ERROR';
export interface SeasonSummary {
season: Season;
isPrimary: boolean;
isParallelActive: boolean;
}
export interface GetLeagueSeasonsResult {
seasons: SeasonSummary[];
}
export class GetLeagueSeasonsUseCase {
constructor(
private readonly leagueRepository: ILeagueRepository,
private readonly seasonRepository: ISeasonRepository,
) {}
async execute(
input: GetLeagueSeasonsInput,
): Promise<Result<GetLeagueSeasonsResult, ApplicationErrorCode<GetLeagueSeasonsErrorCode, { message: string }>>> {
try {
const leagueExists = await this.leagueRepository.exists(input.leagueId);
if (!leagueExists) {
return Result.err({
code: 'LEAGUE_NOT_FOUND',
details: { message: 'League not found' },
});
}
const seasons = await this.seasonRepository.findByLeagueId(input.leagueId);
// Determine which season is primary (the active one, or the first planned one if none active)
const activeSeasons = seasons.filter(s => s.status.isActive());
const hasMultipleActive = activeSeasons.length > 1;
const seasonSummaries = seasons.map((season) => ({
season,
isPrimary: season.status.isActive(),
isParallelActive: hasMultipleActive && season.status.isActive(),
}));
return Result.ok({ seasons: seasonSummaries });
} catch (error: unknown) {
const message = error instanceof Error ? error.message : 'Failed to get league seasons';
return Result.err({
code: 'REPOSITORY_ERROR',
details: { message },
});
}
}
}