refactor
This commit is contained in:
@@ -1,24 +1,18 @@
|
||||
/**
|
||||
* Application Use Case: FileProtestUseCase
|
||||
*
|
||||
*
|
||||
* Allows a driver to file a protest against another driver for an incident during a race.
|
||||
*/
|
||||
|
||||
import { Protest, type ProtestIncident } from '../../domain/entities/Protest';
|
||||
import { Protest } from '../../domain/entities/Protest';
|
||||
import type { IProtestRepository } from '../../domain/repositories/IProtestRepository';
|
||||
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
|
||||
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
|
||||
import { Result } from '@core/shared/result/Result';
|
||||
import { RacingDomainValidationError } from '../../domain/errors/RacingDomainError';
|
||||
import type { FileProtestCommand } from './FileProtestCommand';
|
||||
import { randomUUID } from 'crypto';
|
||||
|
||||
export interface FileProtestCommand {
|
||||
raceId: string;
|
||||
protestingDriverId: string;
|
||||
accusedDriverId: string;
|
||||
incident: ProtestIncident;
|
||||
comment?: string;
|
||||
proofVideoUrl?: string;
|
||||
}
|
||||
|
||||
export class FileProtestUseCase {
|
||||
constructor(
|
||||
private readonly protestRepository: IProtestRepository,
|
||||
@@ -26,16 +20,16 @@ export class FileProtestUseCase {
|
||||
private readonly leagueMembershipRepository: ILeagueMembershipRepository,
|
||||
) {}
|
||||
|
||||
async execute(command: FileProtestCommand): Promise<{ protestId: string }> {
|
||||
async execute(command: FileProtestCommand): Promise<Result<{ protestId: string }, RacingDomainValidationError>> {
|
||||
// Validate race exists
|
||||
const race = await this.raceRepository.findById(command.raceId);
|
||||
if (!race) {
|
||||
throw new Error('Race not found');
|
||||
return Result.err(new RacingDomainValidationError('Race not found'));
|
||||
}
|
||||
|
||||
// Validate drivers are not the same
|
||||
if (command.protestingDriverId === command.accusedDriverId) {
|
||||
throw new Error('Cannot file a protest against yourself');
|
||||
return Result.err(new RacingDomainValidationError('Cannot file a protest against yourself'));
|
||||
}
|
||||
|
||||
// Validate protesting driver is a member of the league
|
||||
@@ -43,9 +37,9 @@ export class FileProtestUseCase {
|
||||
const protestingDriverMembership = memberships.find(
|
||||
m => m.driverId === command.protestingDriverId && m.status === 'active'
|
||||
);
|
||||
|
||||
|
||||
if (!protestingDriverMembership) {
|
||||
throw new Error('Protesting driver is not an active member of this league');
|
||||
return Result.err(new RacingDomainValidationError('Protesting driver is not an active member of this league'));
|
||||
}
|
||||
|
||||
// Create the protest
|
||||
@@ -63,6 +57,6 @@ export class FileProtestUseCase {
|
||||
|
||||
await this.protestRepository.create(protest);
|
||||
|
||||
return { protestId: protest.id };
|
||||
return Result.ok({ protestId: protest.id });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user