rename to core
This commit is contained in:
68
core/racing/application/use-cases/FileProtestUseCase.ts
Normal file
68
core/racing/application/use-cases/FileProtestUseCase.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
/**
|
||||
* 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 type { IProtestRepository } from '../../domain/repositories/IProtestRepository';
|
||||
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
|
||||
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
|
||||
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,
|
||||
private readonly raceRepository: IRaceRepository,
|
||||
private readonly leagueMembershipRepository: ILeagueMembershipRepository,
|
||||
) {}
|
||||
|
||||
async execute(command: FileProtestCommand): Promise<{ protestId: string }> {
|
||||
// Validate race exists
|
||||
const race = await this.raceRepository.findById(command.raceId);
|
||||
if (!race) {
|
||||
throw new Error('Race not found');
|
||||
}
|
||||
|
||||
// Validate drivers are not the same
|
||||
if (command.protestingDriverId === command.accusedDriverId) {
|
||||
throw new Error('Cannot file a protest against yourself');
|
||||
}
|
||||
|
||||
// Validate protesting driver is a member of the league
|
||||
const memberships = await this.leagueMembershipRepository.getLeagueMembers(race.leagueId);
|
||||
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');
|
||||
}
|
||||
|
||||
// Create the protest
|
||||
const protest = Protest.create({
|
||||
id: randomUUID(),
|
||||
raceId: command.raceId,
|
||||
protestingDriverId: command.protestingDriverId,
|
||||
accusedDriverId: command.accusedDriverId,
|
||||
incident: command.incident,
|
||||
...(command.comment !== undefined ? { comment: command.comment } : {}),
|
||||
...(command.proofVideoUrl !== undefined ? { proofVideoUrl: command.proofVideoUrl } : {}),
|
||||
status: 'pending',
|
||||
filedAt: new Date(),
|
||||
});
|
||||
|
||||
await this.protestRepository.create(protest);
|
||||
|
||||
return { protestId: protest.id };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user