64 lines
2.1 KiB
TypeScript
64 lines
2.1 KiB
TypeScript
import { Result } from '@core/shared/domain/Result';
|
|
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
|
import type { Season } from '../../domain/entities/season/Season';
|
|
import type { LeagueRepository } from '../../domain/repositories/LeagueRepository';
|
|
import type { SeasonRepository } from '../../domain/repositories/SeasonRepository';
|
|
|
|
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: LeagueRepository,
|
|
private readonly seasonRepository: SeasonRepository,
|
|
) {}
|
|
|
|
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 },
|
|
});
|
|
}
|
|
}
|
|
} |