refactor
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
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/result/Result';
|
||||
import { RacingDomainValidationError, RacingDomainInvariantError } from '../../domain/errors/RacingDomainError';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type { CancelRaceCommandDTO } from '../dto/CancelRaceCommandDTO';
|
||||
|
||||
/**
|
||||
@@ -15,13 +15,13 @@ import type { CancelRaceCommandDTO } from '../dto/CancelRaceCommandDTO';
|
||||
* - persists the updated race via the repository.
|
||||
*/
|
||||
export class CancelRaceUseCase
|
||||
implements AsyncUseCase<CancelRaceCommandDTO, Result<void, RacingDomainValidationError | RacingDomainInvariantError>> {
|
||||
implements AsyncUseCase<CancelRaceCommandDTO, void, string> {
|
||||
constructor(
|
||||
private readonly raceRepository: IRaceRepository,
|
||||
private readonly logger: Logger,
|
||||
) {}
|
||||
|
||||
async execute(command: CancelRaceCommandDTO): Promise<Result<void, RacingDomainValidationError | RacingDomainInvariantError>> {
|
||||
async execute(command: CancelRaceCommandDTO): Promise<Result<void, ApplicationErrorCode<string>>> {
|
||||
const { raceId } = command;
|
||||
this.logger.debug(`[CancelRaceUseCase] Executing for raceId: ${raceId}`);
|
||||
|
||||
@@ -29,7 +29,7 @@ export class CancelRaceUseCase
|
||||
const race = await this.raceRepository.findById(raceId);
|
||||
if (!race) {
|
||||
this.logger.warn(`[CancelRaceUseCase] Race with ID ${raceId} not found.`);
|
||||
return Result.err(new RacingDomainValidationError('Race not found'));
|
||||
return Result.err({ code: 'RACE_NOT_FOUND' });
|
||||
}
|
||||
|
||||
const cancelledRace = race.cancel();
|
||||
@@ -37,12 +37,16 @@ export class CancelRaceUseCase
|
||||
this.logger.info(`[CancelRaceUseCase] Race ${raceId} cancelled successfully.`);
|
||||
return Result.ok(undefined);
|
||||
} catch (error) {
|
||||
if (error instanceof RacingDomainInvariantError || error instanceof RacingDomainValidationError) {
|
||||
if (error instanceof Error && error.message.includes('already cancelled')) {
|
||||
this.logger.warn(`[CancelRaceUseCase] Domain error cancelling race ${raceId}: ${error.message}`);
|
||||
return Result.err(error);
|
||||
return Result.err({ code: 'RACE_ALREADY_CANCELLED' });
|
||||
}
|
||||
if (error instanceof Error && error.message.includes('completed race')) {
|
||||
this.logger.warn(`[CancelRaceUseCase] Domain error cancelling race ${raceId}: ${error.message}`);
|
||||
return Result.err({ code: 'CANNOT_CANCEL_COMPLETED_RACE' });
|
||||
}
|
||||
this.logger.error(`[CancelRaceUseCase] Unexpected error cancelling race ${raceId}`, error instanceof Error ? error : new Error(String(error)));
|
||||
throw error;
|
||||
return Result.err({ code: 'UNEXPECTED_ERROR' });
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user