refactor
This commit is contained in:
@@ -12,13 +12,13 @@ import type { IRaceRepository } from '../../domain/repositories/IRaceRepository'
|
||||
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
|
||||
import { randomUUID } from 'crypto';
|
||||
import type { AsyncUseCase } from '@core/shared/application';
|
||||
import { Result } from '@core/shared/result/Result';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { Logger } from '@core/shared/application';
|
||||
import { RacingDomainValidationError } from '../../domain/errors/RacingDomainError';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type { ApplyPenaltyCommand } from './ApplyPenaltyCommand';
|
||||
|
||||
export class ApplyPenaltyUseCase
|
||||
implements AsyncUseCase<ApplyPenaltyCommand, Result<{ penaltyId: string }, RacingDomainValidationError>> {
|
||||
implements AsyncUseCase<ApplyPenaltyCommand, { penaltyId: string }, string> {
|
||||
constructor(
|
||||
private readonly penaltyRepository: IPenaltyRepository,
|
||||
private readonly protestRepository: IProtestRepository,
|
||||
@@ -27,14 +27,14 @@ export class ApplyPenaltyUseCase
|
||||
private readonly logger: Logger,
|
||||
) {}
|
||||
|
||||
async execute(command: ApplyPenaltyCommand): Promise<Result<{ penaltyId: string }, RacingDomainValidationError>> {
|
||||
async execute(command: ApplyPenaltyCommand): Promise<Result<{ penaltyId: string }, ApplicationErrorCode<string>>> {
|
||||
this.logger.debug('ApplyPenaltyUseCase: Executing with command', command);
|
||||
|
||||
// Validate race exists
|
||||
const race = await this.raceRepository.findById(command.raceId);
|
||||
if (!race) {
|
||||
this.logger.warn(`ApplyPenaltyUseCase: Race with ID ${command.raceId} not found.`);
|
||||
return Result.err(new RacingDomainValidationError('Race not found'));
|
||||
return Result.err({ code: 'RACE_NOT_FOUND' });
|
||||
}
|
||||
this.logger.debug(`ApplyPenaltyUseCase: Race ${race.id} found.`);
|
||||
|
||||
@@ -46,7 +46,7 @@ export class ApplyPenaltyUseCase
|
||||
|
||||
if (!stewardMembership || (stewardMembership.role !== 'owner' && stewardMembership.role !== 'admin')) {
|
||||
this.logger.warn(`ApplyPenaltyUseCase: Steward ${command.stewardId} does not have authority for league ${race.leagueId}.`);
|
||||
return Result.err(new RacingDomainValidationError('Only league owners and admins can apply penalties'));
|
||||
return Result.err({ code: 'INSUFFICIENT_AUTHORITY' });
|
||||
}
|
||||
this.logger.debug(`ApplyPenaltyUseCase: Steward ${command.stewardId} has authority.`);
|
||||
|
||||
@@ -55,15 +55,15 @@ export class ApplyPenaltyUseCase
|
||||
const protest = await this.protestRepository.findById(command.protestId);
|
||||
if (!protest) {
|
||||
this.logger.warn(`ApplyPenaltyUseCase: Protest with ID ${command.protestId} not found.`);
|
||||
return Result.err(new RacingDomainValidationError('Protest not found'));
|
||||
return Result.err({ code: 'PROTEST_NOT_FOUND' });
|
||||
}
|
||||
if (protest.status !== 'upheld') {
|
||||
this.logger.warn(`ApplyPenaltyUseCase: Protest ${protest.id} is not upheld. Status: ${protest.status}`);
|
||||
return Result.err(new RacingDomainValidationError('Can only create penalties for upheld protests'));
|
||||
return Result.err({ code: 'PROTEST_NOT_UPHELD' });
|
||||
}
|
||||
if (protest.raceId !== command.raceId) {
|
||||
this.logger.warn(`ApplyPenaltyUseCase: Protest ${protest.id} is for race ${protest.raceId}, not ${command.raceId}.`);
|
||||
return Result.err(new RacingDomainValidationError('Protest is not for this race'));
|
||||
return Result.err({ code: 'PROTEST_NOT_FOR_RACE' });
|
||||
}
|
||||
this.logger.debug(`ApplyPenaltyUseCase: Protest ${protest.id} is valid and upheld.`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user