refactor racing use cases

This commit is contained in:
2025-12-21 00:43:42 +01:00
parent e9d6f90bb2
commit c12656d671
308 changed files with 14401 additions and 7419 deletions

View File

@@ -12,15 +12,16 @@ import { RecurrenceStrategyFactory } from '../../domain/value-objects/Recurrence
import { WeekdaySet } from '../../domain/value-objects/WeekdaySet';
import { MonthlyRecurrencePattern } from '../../domain/value-objects/MonthlyRecurrencePattern';
import type { Weekday } from '../../domain/types/Weekday';
import { normalizeVisibility } from '../dto/LeagueConfigFormDTO';
import { LeagueVisibility } from '../../domain/value-objects/LeagueVisibility';
import { v4 as uuidv4 } from 'uuid';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
/**
* DTOs and helpers shared across Season-focused use cases.
* Input, result and error models shared across Season-focused use cases.
*/
export interface CreateSeasonForLeagueCommand {
export interface CreateSeasonForLeagueInput {
leagueId: string;
name: string;
gameId: string;
@@ -32,86 +33,95 @@ export interface CreateSeasonForLeagueCommand {
config?: LeagueConfigFormModel;
}
export interface CreateSeasonForLeagueResultDTO {
seasonId: string;
}
// Backwards-compatible alias for existing wiring/tests.
export type CreateSeasonForLeagueCommand = CreateSeasonForLeagueInput;
export interface SeasonSummaryDTO {
seasonId: string;
leagueId: string;
name: string;
status: import('../../domain/entities/Season').SeasonStatus;
startDate?: Date;
endDate?: Date;
isPrimary: boolean;
}
export type CreateSeasonForLeagueResult = {
season: Season;
};
export interface ListSeasonsForLeagueQuery {
export type CreateSeasonForLeagueErrorCode =
| 'LEAGUE_NOT_FOUND'
| 'SOURCE_SEASON_NOT_FOUND'
| 'REPOSITORY_ERROR';
export type CreateSeasonForLeagueApplicationError = ApplicationErrorCode<
CreateSeasonForLeagueErrorCode,
{ message: string }
>;
export interface ListSeasonsForLeagueInput {
leagueId: string;
}
export interface ListSeasonsForLeagueResultDTO {
items: SeasonSummaryDTO[];
// Backwards-compatible alias for existing wiring/tests.
export type ListSeasonsForLeagueQuery = ListSeasonsForLeagueInput;
export interface ListSeasonsForLeagueResult {
seasons: Season[];
}
export interface GetSeasonDetailsQuery {
export type ListSeasonsForLeagueErrorCode =
| 'LEAGUE_NOT_FOUND'
| 'REPOSITORY_ERROR';
export type ListSeasonsForLeagueApplicationError = ApplicationErrorCode<
ListSeasonsForLeagueErrorCode,
{ message: string }
>;
export interface GetSeasonDetailsInput {
leagueId: string;
seasonId: string;
}
export interface SeasonDetailsDTO {
seasonId: string;
leagueId: string;
gameId: string;
name: string;
status: import('../../domain/entities/Season').SeasonStatus;
startDate?: Date;
endDate?: Date;
maxDrivers?: number;
schedule?: {
startDate: Date;
plannedRounds: number;
};
scoring?: {
scoringPresetId: string;
customScoringEnabled: boolean;
};
dropPolicy?: {
strategy: import('../../domain/value-objects/SeasonDropPolicy').SeasonDropStrategy;
n?: number;
};
stewarding?: {
decisionMode: import('../../domain/entities/League').StewardingDecisionMode;
requiredVotes?: number;
requireDefense: boolean;
defenseTimeLimit: number;
voteTimeLimit: number;
protestDeadlineHours: number;
stewardingClosesHours: number;
notifyAccusedOnProtest: boolean;
notifyOnVoteRequired: boolean;
};
// Backwards-compatible alias for existing wiring/tests.
export type GetSeasonDetailsQuery = GetSeasonDetailsInput;
export interface GetSeasonDetailsResult {
season: Season;
}
export type GetSeasonDetailsErrorCode =
| 'LEAGUE_NOT_FOUND'
| 'SEASON_NOT_FOUND'
| 'REPOSITORY_ERROR';
export type GetSeasonDetailsApplicationError = ApplicationErrorCode<
GetSeasonDetailsErrorCode,
{ message: string }
>;
export type SeasonLifecycleTransition =
| 'activate'
| 'complete'
| 'archive'
| 'cancel';
export interface ManageSeasonLifecycleCommand {
export interface ManageSeasonLifecycleInput {
leagueId: string;
seasonId: string;
transition: SeasonLifecycleTransition;
}
export interface ManageSeasonLifecycleResultDTO {
seasonId: string;
status: import('../../domain/entities/Season').SeasonStatus;
startDate?: Date;
endDate?: Date;
// Backwards-compatible alias for existing wiring/tests.
export type ManageSeasonLifecycleCommand = ManageSeasonLifecycleInput;
export interface ManageSeasonLifecycleResult {
season: Season;
}
export type ManageSeasonLifecycleErrorCode =
| 'LEAGUE_NOT_FOUND'
| 'SEASON_NOT_FOUND'
| 'INVALID_LIFECYCLE_TRANSITION'
| 'REPOSITORY_ERROR';
export type ManageSeasonLifecycleApplicationError = ApplicationErrorCode<
ManageSeasonLifecycleErrorCode,
{ message: string }
>;
/**
* CreateSeasonForLeagueUseCase
*
@@ -122,73 +132,99 @@ export class CreateSeasonForLeagueUseCase {
constructor(
private readonly leagueRepository: ILeagueRepository,
private readonly seasonRepository: ISeasonRepository,
private readonly output: UseCaseOutputPort<CreateSeasonForLeagueResult>,
) {}
async execute(
command: CreateSeasonForLeagueCommand,
): Promise<CreateSeasonForLeagueResultDTO> {
const league = await this.leagueRepository.findById(command.leagueId);
if (!league) {
throw new Error(`League not found: ${command.leagueId}`);
}
let baseSeasonProps: {
schedule?: SeasonSchedule;
scoringConfig?: SeasonScoringConfig;
dropPolicy?: SeasonDropPolicy;
stewardingConfig?: SeasonStewardingConfig;
maxDrivers?: number;
} = {};
if (command.sourceSeasonId) {
const source = await this.seasonRepository.findById(command.sourceSeasonId);
if (!source) {
throw new Error(`Source Season not found: ${command.sourceSeasonId}`);
): Promise<Result<void, CreateSeasonForLeagueApplicationError>> {
try {
const league = await this.leagueRepository.findById(command.leagueId);
if (!league) {
return Result.err({
code: 'LEAGUE_NOT_FOUND',
details: {
message: `League not found: ${command.leagueId}`,
},
});
}
baseSeasonProps = {
...(source.schedule !== undefined ? { schedule: source.schedule } : {}),
...(source.scoringConfig !== undefined
? { scoringConfig: source.scoringConfig }
let baseSeasonProps: {
schedule?: SeasonSchedule;
scoringConfig?: SeasonScoringConfig;
dropPolicy?: SeasonDropPolicy;
stewardingConfig?: SeasonStewardingConfig;
maxDrivers?: number;
} = {};
if (command.sourceSeasonId) {
const source = await this.seasonRepository.findById(command.sourceSeasonId);
if (!source) {
return Result.err({
code: 'SOURCE_SEASON_NOT_FOUND',
details: {
message: `Source Season not found: ${command.sourceSeasonId}`,
},
});
}
baseSeasonProps = {
...(source.schedule !== undefined ? { schedule: source.schedule } : {}),
...(source.scoringConfig !== undefined
? { scoringConfig: source.scoringConfig }
: {}),
...(source.dropPolicy !== undefined
? { dropPolicy: source.dropPolicy }
: {}),
...(source.stewardingConfig !== undefined
? { stewardingConfig: source.stewardingConfig }
: {}),
...(source.maxDrivers !== undefined
? { maxDrivers: source.maxDrivers }
: {}),
};
} else if (command.config) {
baseSeasonProps = this.deriveSeasonPropsFromConfig(command.config);
}
const seasonId = uuidv4();
const season = Season.create({
id: seasonId,
leagueId: league.id,
gameId: command.gameId,
name: command.name,
year: new Date().getFullYear(),
status: 'planned',
...(baseSeasonProps?.schedule
? { schedule: baseSeasonProps.schedule }
: {}),
...(source.dropPolicy !== undefined ? { dropPolicy: source.dropPolicy } : {}),
...(source.stewardingConfig !== undefined
? { stewardingConfig: source.stewardingConfig }
...(baseSeasonProps?.scoringConfig
? { scoringConfig: baseSeasonProps.scoringConfig }
: {}),
...(source.maxDrivers !== undefined ? { maxDrivers: source.maxDrivers } : {}),
};
} else if (command.config) {
baseSeasonProps = this.deriveSeasonPropsFromConfig(command.config);
...(baseSeasonProps?.dropPolicy
? { dropPolicy: baseSeasonProps.dropPolicy }
: {}),
...(baseSeasonProps?.stewardingConfig
? { stewardingConfig: baseSeasonProps.stewardingConfig }
: {}),
...(baseSeasonProps?.maxDrivers !== undefined
? { maxDrivers: baseSeasonProps.maxDrivers }
: {}),
});
await this.seasonRepository.add(season);
this.output.present({ season });
return Result.ok(undefined);
} catch (error) {
return Result.err({
code: 'REPOSITORY_ERROR',
details: {
message: error instanceof Error ? error.message : 'Unknown error',
},
});
}
const seasonId = uuidv4();
const season = Season.create({
id: seasonId,
leagueId: league.id,
gameId: command.gameId,
name: command.name,
year: new Date().getFullYear(),
status: 'planned',
...(baseSeasonProps?.schedule
? { schedule: baseSeasonProps.schedule }
: {}),
...(baseSeasonProps?.scoringConfig
? { scoringConfig: baseSeasonProps.scoringConfig }
: {}),
...(baseSeasonProps?.dropPolicy
? { dropPolicy: baseSeasonProps.dropPolicy }
: {}),
...(baseSeasonProps?.stewardingConfig
? { stewardingConfig: baseSeasonProps.stewardingConfig }
: {}),
...(baseSeasonProps?.maxDrivers !== undefined
? { maxDrivers: baseSeasonProps.maxDrivers }
: {}),
});
await this.seasonRepository.add(season);
return { seasonId };
}
private deriveSeasonPropsFromConfig(config: LeagueConfigFormModel): {
@@ -226,7 +262,7 @@ export class CreateSeasonForLeagueUseCase {
typeof structure.maxDrivers === 'number' && structure.maxDrivers > 0
? structure.maxDrivers
: undefined;
return {
...(schedule !== undefined ? { schedule } : {}),
scoringConfig,
@@ -297,29 +333,36 @@ export class ListSeasonsForLeagueUseCase {
constructor(
private readonly leagueRepository: ILeagueRepository,
private readonly seasonRepository: ISeasonRepository,
private readonly output: UseCaseOutputPort<ListSeasonsForLeagueResult>,
) {}
async execute(
query: ListSeasonsForLeagueQuery,
): Promise<ListSeasonsForLeagueResultDTO> {
const league = await this.leagueRepository.findById(query.leagueId);
if (!league) {
throw new Error(`League not found: ${query.leagueId}`);
): Promise<Result<void, ListSeasonsForLeagueApplicationError>> {
try {
const league = await this.leagueRepository.findById(query.leagueId);
if (!league) {
return Result.err({
code: 'LEAGUE_NOT_FOUND',
details: {
message: `League not found: ${query.leagueId}`,
},
});
}
const seasons = await this.seasonRepository.listByLeague(league.id);
this.output.present({ seasons });
return Result.ok(undefined);
} catch (error) {
return Result.err({
code: 'REPOSITORY_ERROR',
details: {
message: error instanceof Error ? error.message : 'Unknown error',
},
});
}
const seasons = await this.seasonRepository.listByLeague(league.id);
const items: SeasonSummaryDTO[] = seasons.map((s) => ({
seasonId: s.id,
leagueId: s.leagueId,
name: s.name,
status: s.status,
...(s.startDate !== undefined ? { startDate: s.startDate } : {}),
...(s.endDate !== undefined ? { endDate: s.endDate } : {}),
// League currently does not track primarySeasonId, so mark false for now.
isPrimary: false,
}));
return { items };
}
}
@@ -330,79 +373,44 @@ export class GetSeasonDetailsUseCase {
constructor(
private readonly leagueRepository: ILeagueRepository,
private readonly seasonRepository: ISeasonRepository,
private readonly output: UseCaseOutputPort<GetSeasonDetailsResult>,
) {}
async execute(query: GetSeasonDetailsQuery): Promise<SeasonDetailsDTO> {
const league = await this.leagueRepository.findById(query.leagueId);
if (!league) {
throw new Error(`League not found: ${query.leagueId}`);
}
async execute(
query: GetSeasonDetailsQuery,
): Promise<Result<void, GetSeasonDetailsApplicationError>> {
try {
const league = await this.leagueRepository.findById(query.leagueId);
if (!league) {
return Result.err({
code: 'LEAGUE_NOT_FOUND',
details: {
message: `League not found: ${query.leagueId}`,
},
});
}
const season = await this.seasonRepository.findById(query.seasonId);
if (!season || season.leagueId !== league.id) {
throw new Error(
`Season ${query.seasonId} does not belong to league ${league.id}`,
);
}
const season = await this.seasonRepository.findById(query.seasonId);
if (!season || season.leagueId !== league.id) {
return Result.err({
code: 'SEASON_NOT_FOUND',
details: {
message: `Season ${query.seasonId} does not belong to league ${league.id}`,
},
});
}
return {
seasonId: season.id,
leagueId: season.leagueId,
gameId: season.gameId,
name: season.name,
status: season.status,
...(season.startDate !== undefined ? { startDate: season.startDate } : {}),
...(season.endDate !== undefined ? { endDate: season.endDate } : {}),
...(season.maxDrivers !== undefined ? { maxDrivers: season.maxDrivers } : {}),
...(season.schedule
? {
schedule: {
startDate: season.schedule.startDate,
plannedRounds: season.schedule.plannedRounds,
},
}
: {}),
...(season.scoringConfig
? {
scoring: {
scoringPresetId: season.scoringConfig.scoringPresetId,
customScoringEnabled:
season.scoringConfig.customScoringEnabled ?? false,
},
}
: {}),
...(season.dropPolicy
? {
dropPolicy: {
strategy: season.dropPolicy.strategy,
...(season.dropPolicy.n !== undefined
? { n: season.dropPolicy.n }
: {}),
},
}
: {}),
...(season.stewardingConfig
? {
stewarding: {
decisionMode: season.stewardingConfig.decisionMode,
...(season.stewardingConfig.requiredVotes !== undefined
? { requiredVotes: season.stewardingConfig.requiredVotes }
: {}),
requireDefense: season.stewardingConfig.requireDefense,
defenseTimeLimit: season.stewardingConfig.defenseTimeLimit,
voteTimeLimit: season.stewardingConfig.voteTimeLimit,
protestDeadlineHours:
season.stewardingConfig.protestDeadlineHours,
stewardingClosesHours:
season.stewardingConfig.stewardingClosesHours,
notifyAccusedOnProtest:
season.stewardingConfig.notifyAccusedOnProtest,
notifyOnVoteRequired:
season.stewardingConfig.notifyOnVoteRequired,
},
}
: {}),
};
this.output.present({ season });
return Result.ok(undefined);
} catch (error) {
return Result.err({
code: 'REPOSITORY_ERROR',
details: {
message: error instanceof Error ? error.message : 'Unknown error',
},
});
}
}
}
@@ -413,48 +421,87 @@ export class ManageSeasonLifecycleUseCase {
constructor(
private readonly leagueRepository: ILeagueRepository,
private readonly seasonRepository: ISeasonRepository,
private readonly output: UseCaseOutputPort<ManageSeasonLifecycleResult>,
) {}
async execute(
command: ManageSeasonLifecycleCommand,
): Promise<ManageSeasonLifecycleResultDTO> {
const league = await this.leagueRepository.findById(command.leagueId);
if (!league) {
throw new Error(`League not found: ${command.leagueId}`);
): Promise<Result<void, ManageSeasonLifecycleApplicationError>> {
try {
const league = await this.leagueRepository.findById(command.leagueId);
if (!league) {
return Result.err({
code: 'LEAGUE_NOT_FOUND',
details: {
message: `League not found: ${command.leagueId}`,
},
});
}
const season = await this.seasonRepository.findById(command.seasonId);
if (!season || season.leagueId !== league.id) {
return Result.err({
code: 'SEASON_NOT_FOUND',
details: {
message: `Season ${command.seasonId} does not belong to league ${league.id}`,
},
});
}
let updated: Season;
try {
switch (command.transition) {
case 'activate':
updated = season.activate();
break;
case 'complete':
updated = season.complete();
break;
case 'archive':
updated = season.archive();
break;
case 'cancel':
updated = season.cancel();
break;
default:
return Result.err({
code: 'INVALID_LIFECYCLE_TRANSITION',
details: {
message: `Unsupported Season lifecycle transition: ${command.transition}`,
},
});
}
} catch (error) {
return Result.err({
code: 'INVALID_LIFECYCLE_TRANSITION',
details: {
message:
error instanceof Error ? error.message : 'Invalid lifecycle transition',
},
});
}
try {
await this.seasonRepository.update(updated);
} catch (error) {
return Result.err({
code: 'REPOSITORY_ERROR',
details: {
message: error instanceof Error ? error.message : 'Unknown error',
},
});
}
this.output.present({ season: updated });
return Result.ok(undefined);
} catch (error) {
return Result.err({
code: 'REPOSITORY_ERROR',
details: {
message: error instanceof Error ? error.message : 'Unknown error',
},
});
}
const season = await this.seasonRepository.findById(command.seasonId);
if (!season || season.leagueId !== league.id) {
throw new Error(
`Season ${command.seasonId} does not belong to league ${league.id}`,
);
}
let updated: Season;
switch (command.transition) {
case 'activate':
updated = season.activate();
break;
case 'complete':
updated = season.complete();
break;
case 'archive':
updated = season.archive();
break;
case 'cancel':
updated = season.cancel();
break;
default:
throw new Error(`Unsupported Season lifecycle transition`);
}
await this.seasonRepository.update(updated);
return {
seasonId: updated.id,
status: updated.status,
...(updated.startDate !== undefined ? { startDate: updated.startDate } : {}),
...(updated.endDate !== undefined ? { endDate: updated.endDate } : {}),
};
}
}
}