Files
gridpilot.gg/core/racing/application/use-cases/GetLeagueSeasonsUseCase.ts
2025-12-19 19:42:19 +01:00

33 lines
1.3 KiB
TypeScript

import type { ISeasonRepository } from '../../domain/repositories/ISeasonRepository';
import type { GetLeagueSeasonsOutputPort } from '../ports/output/GetLeagueSeasonsOutputPort';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
export interface GetLeagueSeasonsUseCaseParams {
leagueId: string;
}
export class GetLeagueSeasonsUseCase {
constructor(private readonly seasonRepository: ISeasonRepository) {}
async execute(params: GetLeagueSeasonsUseCaseParams): Promise<Result<GetLeagueSeasonsOutputPort, ApplicationErrorCode<'REPOSITORY_ERROR', { message: string }>>> {
try {
const seasons = await this.seasonRepository.findByLeagueId(params.leagueId);
const activeCount = seasons.filter(s => s.status === 'active').length;
const output: GetLeagueSeasonsOutputPort = {
seasons: seasons.map(s => ({
seasonId: s.id,
name: s.name,
status: s.status,
startDate: s.startDate ?? new Date(),
endDate: s.endDate ?? new Date(),
isPrimary: false,
isParallelActive: s.status === 'active' && activeCount > 1
}))
};
return Result.ok(output);
} catch {
return Result.err({ code: 'REPOSITORY_ERROR', details: { message: 'Failed to fetch seasons' } });
}
}
}