fix issues in core

This commit is contained in:
2025-12-23 16:16:12 +01:00
parent 120d3bb1a1
commit d04a21fe02
40 changed files with 280 additions and 841 deletions

View File

@@ -1,15 +1,71 @@
import { SeasonScheduleGenerator } from '../../domain/services/SeasonScheduleGenerator';
import {
scheduleDTOToSeasonSchedule,
type SeasonScheduleConfigDTO,
} from '../dto/LeagueScheduleDTO';
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';
import { SeasonSchedule } from '../../domain/value-objects/SeasonSchedule';
import { RaceTimeOfDay } from '../../domain/value-objects/RaceTimeOfDay';
import { LeagueTimezone } from '../../domain/value-objects/LeagueTimezone';
import { RecurrenceStrategyFactory } from '../../domain/value-objects/RecurrenceStrategy';
import { WeekdaySet } from '../../domain/value-objects/WeekdaySet';
import { MonthlyRecurrencePattern } from '../../domain/value-objects/MonthlyRecurrencePattern';
import { ALL_WEEKDAYS, type Weekday } from '../../domain/types/Weekday';
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
export type PreviewLeagueScheduleSeasonConfig = SeasonScheduleConfigDTO;
export type SeasonScheduleConfig = {
seasonStartDate: string;
recurrenceStrategy: 'weekly' | 'everyNWeeks' | 'monthlyNthWeekday';
weekdays?: string[];
raceStartTime: string;
timezoneId: string;
plannedRounds: number;
intervalWeeks?: number;
monthlyOrdinal?: 1 | 2 | 3 | 4;
monthlyWeekday?: string;
};
function toWeekdaySet(values: string[] | undefined): WeekdaySet {
const weekdays = (values ?? []).filter((v): v is Weekday =>
ALL_WEEKDAYS.includes(v as Weekday),
);
return WeekdaySet.fromArray(weekdays.length > 0 ? weekdays : ['Mon']);
}
function scheduleConfigToSeasonSchedule(dto: SeasonScheduleConfig): SeasonSchedule {
const startDate = new Date(dto.seasonStartDate);
const timeOfDay = RaceTimeOfDay.fromString(dto.raceStartTime);
const timezone = LeagueTimezone.create(dto.timezoneId);
const recurrence = (() => {
switch (dto.recurrenceStrategy) {
case 'everyNWeeks':
return RecurrenceStrategyFactory.everyNWeeks(
dto.intervalWeeks ?? 2,
toWeekdaySet(dto.weekdays),
);
case 'monthlyNthWeekday': {
const pattern = MonthlyRecurrencePattern.create(
dto.monthlyOrdinal ?? 1,
((dto.monthlyWeekday ?? 'Mon') as Weekday),
);
return RecurrenceStrategyFactory.monthlyNthWeekday(pattern);
}
case 'weekly':
default:
return RecurrenceStrategyFactory.weekly(toWeekdaySet(dto.weekdays));
}
})();
return new SeasonSchedule({
startDate,
timeOfDay,
timezone,
recurrence,
plannedRounds: dto.plannedRounds,
});
}
export type PreviewLeagueScheduleSeasonConfig = SeasonScheduleConfig;
export type PreviewLeagueScheduleInput = {
schedule: PreviewLeagueScheduleSeasonConfig;
@@ -54,7 +110,7 @@ export class PreviewLeagueScheduleUseCase {
try {
let seasonSchedule: SeasonSchedule;
try {
seasonSchedule = scheduleDTOToSeasonSchedule(params.schedule);
seasonSchedule = scheduleConfigToSeasonSchedule(params.schedule);
} catch (error) {
this.logger.warn('Invalid schedule data provided', {
schedule: params.schedule,