refactor racing use cases
This commit is contained in:
@@ -1,33 +1,77 @@
|
||||
import type { ISeasonRepository } from '../../domain/repositories/ISeasonRepository';
|
||||
import type { GetLeagueSeasonsOutputPort } from '../ports/output/GetLeagueSeasonsOutputPort';
|
||||
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';
|
||||
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
|
||||
import type { ISeasonRepository } from '../../domain/repositories/ISeasonRepository';
|
||||
|
||||
export interface GetLeagueSeasonsUseCaseParams {
|
||||
export type GetLeagueSeasonsErrorCode = 'LEAGUE_NOT_FOUND' | 'REPOSITORY_ERROR';
|
||||
|
||||
export interface GetLeagueSeasonsInput {
|
||||
leagueId: string;
|
||||
}
|
||||
|
||||
export class GetLeagueSeasonsUseCase {
|
||||
constructor(private readonly seasonRepository: ISeasonRepository) {}
|
||||
export interface LeagueSeasonSummary {
|
||||
season: Season;
|
||||
isPrimary: boolean;
|
||||
isParallelActive: boolean;
|
||||
}
|
||||
|
||||
async execute(params: GetLeagueSeasonsUseCaseParams): Promise<Result<GetLeagueSeasonsOutputPort, ApplicationErrorCode<'REPOSITORY_ERROR', { message: string }>>> {
|
||||
export interface GetLeagueSeasonsResult {
|
||||
league: League;
|
||||
seasons: LeagueSeasonSummary[];
|
||||
}
|
||||
|
||||
export class GetLeagueSeasonsUseCase {
|
||||
constructor(
|
||||
private readonly seasonRepository: ISeasonRepository,
|
||||
private readonly leagueRepository: ILeagueRepository,
|
||||
private readonly output: UseCaseOutputPort<GetLeagueSeasonsResult>,
|
||||
) {}
|
||||
|
||||
async execute(
|
||||
input: GetLeagueSeasonsInput,
|
||||
): Promise<
|
||||
Result<void, ApplicationErrorCode<GetLeagueSeasonsErrorCode, { 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(),
|
||||
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: s.status === 'active' && activeCount > 1
|
||||
}))
|
||||
isParallelActive:
|
||||
season.status === 'active' && activeCount > 1,
|
||||
})),
|
||||
};
|
||||
return Result.ok(output);
|
||||
} catch {
|
||||
return Result.err({ code: 'REPOSITORY_ERROR', details: { message: 'Failed to fetch seasons' } });
|
||||
|
||||
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',
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user