This commit is contained in:
2025-12-09 22:22:06 +01:00
parent e34a11ae7c
commit 3adf2e5e94
62 changed files with 6079 additions and 998 deletions

View File

@@ -31,6 +31,8 @@ export * from './use-cases/ReviewProtestUseCase';
export * from './use-cases/ApplyPenaltyUseCase';
export * from './use-cases/GetRaceProtestsQuery';
export * from './use-cases/GetRacePenaltiesQuery';
export * from './use-cases/RequestProtestDefenseUseCase';
export * from './use-cases/SubmitProtestDefenseUseCase';
// Export ports
export * from './ports/DriverRatingProvider';

View File

@@ -0,0 +1,65 @@
/**
* Application Use Case: RequestProtestDefenseUseCase
*
* Allows a steward to request defense from the accused driver before making a decision.
* This will trigger a notification to the accused driver.
*/
import type { IProtestRepository } from '../../domain/repositories/IProtestRepository';
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
import { isLeagueStewardOrHigherRole } from '../../domain/value-objects/LeagueRoles';
export interface RequestProtestDefenseCommand {
protestId: string;
stewardId: string;
}
export interface RequestProtestDefenseResult {
success: boolean;
accusedDriverId: string;
protestId: string;
}
export class RequestProtestDefenseUseCase {
constructor(
private readonly protestRepository: IProtestRepository,
private readonly raceRepository: IRaceRepository,
private readonly membershipRepository: ILeagueMembershipRepository,
) {}
async execute(command: RequestProtestDefenseCommand): Promise<RequestProtestDefenseResult> {
// Get the protest
const protest = await this.protestRepository.findById(command.protestId);
if (!protest) {
throw new Error('Protest not found');
}
// Get the race to find the league
const race = await this.raceRepository.findById(protest.raceId);
if (!race) {
throw new Error('Race not found');
}
// Verify the steward has permission
const membership = await this.membershipRepository.getMembership(race.leagueId, command.stewardId);
if (!membership || !isLeagueStewardOrHigherRole(membership.role)) {
throw new Error('Only stewards and admins can request defense');
}
// Check if defense can be requested
if (!protest.canRequestDefense()) {
throw new Error('Defense cannot be requested for this protest');
}
// Request defense
const updatedProtest = protest.requestDefense(command.stewardId);
await this.protestRepository.update(updatedProtest);
return {
success: true,
accusedDriverId: protest.accusedDriverId,
protestId: protest.id,
};
}
}

View File

@@ -0,0 +1,52 @@
/**
* Application Use Case: SubmitProtestDefenseUseCase
*
* Allows the accused driver to submit their defense statement for a protest.
*/
import type { IProtestRepository } from '../../domain/repositories/IProtestRepository';
export interface SubmitProtestDefenseCommand {
protestId: string;
driverId: string;
statement: string;
videoUrl?: string;
}
export interface SubmitProtestDefenseResult {
success: boolean;
protestId: string;
}
export class SubmitProtestDefenseUseCase {
constructor(
private readonly protestRepository: IProtestRepository,
) {}
async execute(command: SubmitProtestDefenseCommand): Promise<SubmitProtestDefenseResult> {
// Get the protest
const protest = await this.protestRepository.findById(command.protestId);
if (!protest) {
throw new Error('Protest not found');
}
// Verify the submitter is the accused driver
if (protest.accusedDriverId !== command.driverId) {
throw new Error('Only the accused driver can submit a defense');
}
// Check if defense can be submitted
if (!protest.canSubmitDefense()) {
throw new Error('Defense cannot be submitted for this protest');
}
// Submit defense
const updatedProtest = protest.submitDefense(command.statement, command.videoUrl);
await this.protestRepository.update(updatedProtest);
return {
success: true,
protestId: protest.id,
};
}
}

View File

@@ -0,0 +1,56 @@
import type {
ILeagueMembershipRepository,
} from '@gridpilot/racing/domain/repositories/ILeagueMembershipRepository';
import type { ILeagueRepository } from '@gridpilot/racing/domain/repositories/ILeagueRepository';
import type {
LeagueMembership,
MembershipRole,
} from '@gridpilot/racing/domain/entities/LeagueMembership';
export interface TransferLeagueOwnershipCommandDTO {
leagueId: string;
currentOwnerId: string;
newOwnerId: string;
}
export class TransferLeagueOwnershipUseCase {
constructor(
private readonly leagueRepository: ILeagueRepository,
private readonly membershipRepository: ILeagueMembershipRepository
) {}
async execute(command: TransferLeagueOwnershipCommandDTO): Promise<void> {
const { leagueId, currentOwnerId, newOwnerId } = command;
const league = await this.leagueRepository.findById(leagueId);
if (!league) {
throw new Error('League not found');
}
if (league.ownerId !== currentOwnerId) {
throw new Error('Only the current owner can transfer ownership');
}
const newOwnerMembership = await this.membershipRepository.getMembership(leagueId, newOwnerId);
if (!newOwnerMembership || newOwnerMembership.status !== 'active') {
throw new Error('New owner must be an active member of the league');
}
const currentOwnerMembership = await this.membershipRepository.getMembership(leagueId, currentOwnerId);
await this.membershipRepository.saveMembership({
...newOwnerMembership,
role: 'owner' as MembershipRole,
});
if (currentOwnerMembership) {
await this.membershipRepository.saveMembership({
...currentOwnerMembership,
role: 'admin' as MembershipRole,
});
}
const updatedLeague = league.update({ ownerId: newOwnerId });
await this.leagueRepository.update(updatedLeague);
}
}