refactor use cases
This commit is contained in:
@@ -1,75 +1,63 @@
|
||||
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';
|
||||
import type { Season } from '../../domain/entities/season/Season';
|
||||
|
||||
export interface GetLeagueSeasonsInput {
|
||||
leagueId: string;
|
||||
}
|
||||
|
||||
export interface LeagueSeasonSummary {
|
||||
export type GetLeagueSeasonsErrorCode = 'LEAGUE_NOT_FOUND' | 'REPOSITORY_ERROR';
|
||||
|
||||
export interface SeasonSummary {
|
||||
season: Season;
|
||||
isPrimary: boolean;
|
||||
isParallelActive: boolean;
|
||||
}
|
||||
|
||||
export interface GetLeagueSeasonsResult {
|
||||
league: League;
|
||||
seasons: LeagueSeasonSummary[];
|
||||
seasons: SeasonSummary[];
|
||||
}
|
||||
|
||||
export class GetLeagueSeasonsUseCase {
|
||||
constructor(
|
||||
private readonly seasonRepository: ISeasonRepository,
|
||||
private readonly leagueRepository: ILeagueRepository,
|
||||
readonly output: UseCaseOutputPort<GetLeagueSeasonsResult>,
|
||||
private readonly seasonRepository: ISeasonRepository,
|
||||
) {}
|
||||
|
||||
async execute(
|
||||
input: GetLeagueSeasonsInput,
|
||||
): Promise<
|
||||
Result<void, ApplicationErrorCode<GetLeagueSeasonsErrorCode, { message: string }>>
|
||||
> {
|
||||
): Promise<Result<GetLeagueSeasonsResult, ApplicationErrorCode<GetLeagueSeasonsErrorCode, { message: string }>>> {
|
||||
try {
|
||||
const { leagueId } = input;
|
||||
const league = await this.leagueRepository.findById(leagueId);
|
||||
const leagueExists = await this.leagueRepository.exists(input.leagueId);
|
||||
|
||||
if (!league) {
|
||||
if (!leagueExists) {
|
||||
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.isActive()).length;
|
||||
const seasons = await this.seasonRepository.findByLeagueId(input.leagueId);
|
||||
|
||||
const result: GetLeagueSeasonsResult = {
|
||||
league,
|
||||
seasons: seasons.map(season => ({
|
||||
season,
|
||||
isPrimary: false,
|
||||
isParallelActive: season.status.isActive() && activeCount > 1,
|
||||
})),
|
||||
};
|
||||
// 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;
|
||||
|
||||
this.output.present(result);
|
||||
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.ok(undefined);
|
||||
} catch (error) {
|
||||
return Result.err({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: {
|
||||
message:
|
||||
error instanceof Error
|
||||
? error.message
|
||||
: 'Failed to load league seasons',
|
||||
},
|
||||
details: { message },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user