refactor racing use cases
This commit is contained in:
@@ -1,8 +1,20 @@
|
||||
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
|
||||
import type { AsyncUseCase , Logger } 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 { CancelRaceCommandDTO } from '../dto/CancelRaceCommandDTO';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
import type { Race } from '../../domain/entities/Race';
|
||||
|
||||
export type CancelRaceInput = {
|
||||
raceId: string;
|
||||
cancelledById: string;
|
||||
};
|
||||
|
||||
export type CancelRaceErrorCode = 'RACE_NOT_FOUND' | 'NOT_AUTHORIZED' | 'REPOSITORY_ERROR';
|
||||
|
||||
export type CancelRaceResult = {
|
||||
race: Race;
|
||||
};
|
||||
|
||||
/**
|
||||
* Use Case: CancelRaceUseCase
|
||||
@@ -13,14 +25,16 @@ import type { CancelRaceCommandDTO } from '../dto/CancelRaceCommandDTO';
|
||||
* - delegates cancellation rules to the Race domain entity
|
||||
* - persists the updated race via the repository.
|
||||
*/
|
||||
export class CancelRaceUseCase
|
||||
implements AsyncUseCase<CancelRaceCommandDTO, void, string> {
|
||||
export class CancelRaceUseCase {
|
||||
constructor(
|
||||
private readonly raceRepository: IRaceRepository,
|
||||
private readonly logger: Logger,
|
||||
private readonly output: UseCaseOutputPort<CancelRaceResult>,
|
||||
) {}
|
||||
|
||||
async execute(command: CancelRaceCommandDTO): Promise<Result<void, ApplicationErrorCode<string>>> {
|
||||
async execute(command: CancelRaceInput): Promise<
|
||||
Result<void, ApplicationErrorCode<CancelRaceErrorCode>>
|
||||
> {
|
||||
const { raceId } = command;
|
||||
this.logger.debug(`[CancelRaceUseCase] Executing for raceId: ${raceId}`);
|
||||
|
||||
@@ -34,18 +48,39 @@ export class CancelRaceUseCase
|
||||
const cancelledRace = race.cancel();
|
||||
await this.raceRepository.update(cancelledRace);
|
||||
this.logger.info(`[CancelRaceUseCase] Race ${raceId} cancelled successfully.`);
|
||||
|
||||
const result: CancelRaceResult = {
|
||||
race: cancelledRace,
|
||||
};
|
||||
|
||||
this.output.present(result);
|
||||
|
||||
return Result.ok(undefined);
|
||||
} catch (error) {
|
||||
if (error instanceof Error && error.message.includes('already cancelled')) {
|
||||
this.logger.warn(`[CancelRaceUseCase] Domain error cancelling race ${raceId}: ${error.message}`);
|
||||
return Result.err({ code: 'RACE_ALREADY_CANCELLED' });
|
||||
return Result.err({
|
||||
code: 'NOT_AUTHORIZED',
|
||||
details: { message: error.message },
|
||||
});
|
||||
}
|
||||
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' });
|
||||
return Result.err({
|
||||
code: 'NOT_AUTHORIZED',
|
||||
details: { message: error.message },
|
||||
});
|
||||
}
|
||||
this.logger.error(`[CancelRaceUseCase] Unexpected error cancelling race ${raceId}`, error instanceof Error ? error : new Error(String(error)));
|
||||
return Result.err({ code: 'UNEXPECTED_ERROR' });
|
||||
this.logger.error(
|
||||
`[CancelRaceUseCase] Unexpected error cancelling race ${raceId}`,
|
||||
error instanceof Error ? error : new Error(String(error)),
|
||||
);
|
||||
return Result.err({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: {
|
||||
message: error instanceof Error ? error.message : String(error),
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user