77 lines
2.2 KiB
TypeScript
77 lines
2.2 KiB
TypeScript
import type { UseCaseOutputPort } from '@core/shared/application';
|
|
import { Result } from '@core/shared/application/Result';
|
|
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
|
import type { League } from '../../domain/entities/League';
|
|
import type { Season } from '../../domain/entities/season/Season';
|
|
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
|
|
import type { ISeasonRepository } from '../../domain/repositories/ISeasonRepository';
|
|
|
|
export type GetLeagueSeasonsErrorCode = 'LEAGUE_NOT_FOUND' | 'REPOSITORY_ERROR';
|
|
|
|
export interface GetLeagueSeasonsInput {
|
|
leagueId: string;
|
|
}
|
|
|
|
export interface LeagueSeasonSummary {
|
|
season: Season;
|
|
isPrimary: boolean;
|
|
isParallelActive: boolean;
|
|
}
|
|
|
|
export interface GetLeagueSeasonsResult {
|
|
league: League;
|
|
seasons: LeagueSeasonSummary[];
|
|
}
|
|
|
|
export class GetLeagueSeasonsUseCase {
|
|
constructor(
|
|
private readonly seasonRepository: ISeasonRepository,
|
|
private readonly leagueRepository: ILeagueRepository,
|
|
readonly output: UseCaseOutputPort<GetLeagueSeasonsResult>,
|
|
) {}
|
|
|
|
async execute(
|
|
input: GetLeagueSeasonsInput,
|
|
): Promise<
|
|
Result<void, ApplicationErrorCode<GetLeagueSeasonsErrorCode, { message: string }>>
|
|
> {
|
|
try {
|
|
const { leagueId } = input;
|
|
const league = await this.leagueRepository.findById(leagueId);
|
|
|
|
if (!league) {
|
|
return Result.err({
|
|
code: 'LEAGUE_NOT_FOUND',
|
|
details: { message: 'League not found' },
|
|
});
|
|
}
|
|
|
|
const seasons = await this.seasonRepository.findByLeagueId(leagueId);
|
|
const activeCount = seasons.filter(season => season.status === 'active').length;
|
|
|
|
const result: GetLeagueSeasonsResult = {
|
|
league,
|
|
seasons: seasons.map(season => ({
|
|
season,
|
|
isPrimary: false,
|
|
isParallelActive:
|
|
season.status === 'active' && activeCount > 1,
|
|
})),
|
|
};
|
|
|
|
this.output.present(result);
|
|
|
|
return Result.ok(undefined);
|
|
} catch (error) {
|
|
return Result.err({
|
|
code: 'REPOSITORY_ERROR',
|
|
details: {
|
|
message:
|
|
error instanceof Error
|
|
? error.message
|
|
: 'Failed to load league seasons',
|
|
},
|
|
});
|
|
}
|
|
}
|
|
} |