refactor
This commit is contained in:
@@ -1,24 +1,36 @@
|
||||
import { SeasonScheduleGenerator } from '../../domain/services/SeasonScheduleGenerator';
|
||||
import type { LeagueSchedulePreviewDTO, LeagueScheduleDTO } from '../dto/LeagueScheduleDTO';
|
||||
import { scheduleDTOToSeasonSchedule } from '../dto/LeagueScheduleDTO';
|
||||
import type { ILeagueSchedulePreviewPresenter } from '../presenters/ILeagueSchedulePreviewPresenter';
|
||||
import type { AsyncUseCase } from '@core/shared/application/AsyncUseCase';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type { Logger } from '@core/shared/application';
|
||||
import type { SeasonSchedule } from '../../domain/value-objects/SeasonSchedule';
|
||||
|
||||
interface PreviewLeagueScheduleQueryParams {
|
||||
schedule: LeagueScheduleDTO;
|
||||
maxRounds?: number;
|
||||
}
|
||||
|
||||
export class PreviewLeagueScheduleUseCase {
|
||||
type PreviewLeagueScheduleErrorCode = 'INVALID_SCHEDULE';
|
||||
|
||||
type PreviewLeagueScheduleApplicationError = ApplicationErrorCode<PreviewLeagueScheduleErrorCode, { message: string }>;
|
||||
|
||||
export class PreviewLeagueScheduleUseCase implements AsyncUseCase<PreviewLeagueScheduleQueryParams, LeagueSchedulePreviewDTO, PreviewLeagueScheduleErrorCode> {
|
||||
constructor(
|
||||
private readonly scheduleGenerator: typeof SeasonScheduleGenerator = SeasonScheduleGenerator,
|
||||
private readonly presenter: ILeagueSchedulePreviewPresenter,
|
||||
private readonly logger: Logger,
|
||||
) {}
|
||||
|
||||
execute(params: PreviewLeagueScheduleQueryParams): void {
|
||||
const seasonSchedule = scheduleDTOToSeasonSchedule(params.schedule);
|
||||
async execute(params: PreviewLeagueScheduleQueryParams): Promise<Result<LeagueSchedulePreviewDTO, PreviewLeagueScheduleApplicationError>> {
|
||||
this.logger.debug('Previewing league schedule', { params });
|
||||
|
||||
if (!seasonSchedule) {
|
||||
throw new Error('Invalid schedule data');
|
||||
let seasonSchedule: SeasonSchedule;
|
||||
try {
|
||||
seasonSchedule = scheduleDTOToSeasonSchedule(params.schedule);
|
||||
} catch (error) {
|
||||
this.logger.warn('Invalid schedule data provided', { schedule: params.schedule, error: error instanceof Error ? error.message : 'Unknown error' });
|
||||
return Result.err({ code: 'INVALID_SCHEDULE', details: { message: 'Invalid schedule data' } });
|
||||
}
|
||||
|
||||
const maxRounds =
|
||||
@@ -36,10 +48,13 @@ export class PreviewLeagueScheduleUseCase {
|
||||
|
||||
const summary = this.buildSummary(params.schedule, rounds);
|
||||
|
||||
this.presenter.present({
|
||||
const result: LeagueSchedulePreviewDTO = {
|
||||
rounds,
|
||||
summary,
|
||||
});
|
||||
};
|
||||
|
||||
this.logger.info('Successfully generated league schedule preview', { roundCount: rounds.length });
|
||||
return Result.ok(result);
|
||||
}
|
||||
|
||||
private buildSummary(
|
||||
|
||||
Reference in New Issue
Block a user