fix data flow issues

This commit is contained in:
2025-12-19 23:18:53 +01:00
parent ec177a75ce
commit 5c74837d73
45 changed files with 2726 additions and 746 deletions

View File

@@ -0,0 +1,3 @@
export interface ReopenRaceCommandDTO {
raceId: string;
}

View File

@@ -0,0 +1,55 @@
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
import type { AsyncUseCase } from '@core/shared/application';
import type { Logger } from '@core/shared/application';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { ReopenRaceCommandDTO } from '../dto/ReopenRaceCommandDTO';
/**
* Use Case: ReopenRaceUseCase
*
* Encapsulates the workflow for re-opening a race:
* - loads the race by id
* - returns error if the race does not exist
* - delegates transition rules to the Race domain entity via `reopen()`
* - persists the updated race via the repository.
*/
export class ReopenRaceUseCase
implements AsyncUseCase<ReopenRaceCommandDTO, void, string> {
constructor(
private readonly raceRepository: IRaceRepository,
private readonly logger: Logger,
) {}
async execute(command: ReopenRaceCommandDTO): Promise<Result<void, ApplicationErrorCode<string>>> {
const { raceId } = command;
this.logger.debug(`[ReopenRaceUseCase] Executing for raceId: ${raceId}`);
try {
const race = await this.raceRepository.findById(raceId);
if (!race) {
this.logger.warn(`[ReopenRaceUseCase] Race with ID ${raceId} not found.`);
return Result.err({ code: 'RACE_NOT_FOUND' });
}
const reopenedRace = race.reopen();
await this.raceRepository.update(reopenedRace);
this.logger.info(`[ReopenRaceUseCase] Race ${raceId} re-opened successfully.`);
return Result.ok(undefined);
} catch (error) {
if (error instanceof Error && error.message.includes('already scheduled')) {
this.logger.warn(`[ReopenRaceUseCase] Domain error re-opening race ${raceId}: ${error.message}`);
return Result.err({ code: 'RACE_ALREADY_SCHEDULED' });
}
if (error instanceof Error && error.message.includes('running race')) {
this.logger.warn(`[ReopenRaceUseCase] Domain error re-opening race ${raceId}: ${error.message}`);
return Result.err({ code: 'CANNOT_REOPEN_RUNNING_RACE' });
}
this.logger.error(
`[ReopenRaceUseCase] Unexpected error re-opening race ${raceId}`,
error instanceof Error ? error : new Error(String(error)),
);
return Result.err({ code: 'UNEXPECTED_ERROR' });
}
}
}

View File

@@ -240,6 +240,48 @@ export class Race implements IEntity<string> {
return Race.create(props);
}
/**
* Re-open a previously completed or cancelled race
*/
reopen(): Race {
if (this.status === 'scheduled') {
throw new RacingDomainInvariantError('Race is already scheduled');
}
if (this.status === 'running') {
throw new RacingDomainInvariantError('Cannot re-open a running race');
}
const base = {
id: this.id,
leagueId: this.leagueId,
scheduledAt: this.scheduledAt,
track: this.track,
car: this.car,
sessionType: this.sessionType,
status: 'scheduled' as RaceStatus,
};
const withTrackId =
this.trackId !== undefined ? { ...base, trackId: this.trackId } : base;
const withCarId =
this.carId !== undefined ? { ...withTrackId, carId: this.carId } : withTrackId;
const withSof =
this.strengthOfField !== undefined
? { ...withCarId, strengthOfField: this.strengthOfField }
: withCarId;
const withRegistered =
this.registeredCount !== undefined
? { ...withSof, registeredCount: this.registeredCount }
: withSof;
const props =
this.maxParticipants !== undefined
? { ...withRegistered, maxParticipants: this.maxParticipants }
: withRegistered;
return Race.create(props);
}
/**
* Update SOF and participant count
*/