refactor racing use cases

This commit is contained in:
2025-12-21 00:43:42 +01:00
parent e9d6f90bb2
commit c12656d671
308 changed files with 14401 additions and 7419 deletions

View File

@@ -10,58 +10,83 @@ import type { IRaceRepository } from '../../domain/repositories/IRaceRepository'
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
import { isLeagueStewardOrHigherRole } from '../../domain/types/LeagueRoles';
import { Result } from '@core/shared/application/Result';
import { Logger, UseCaseOutputPort } from '@core/shared/application';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
export interface RequestProtestDefenseCommand {
export type RequestProtestDefenseInput = {
protestId: string;
stewardId: string;
}
};
export interface RequestProtestDefenseResult {
success: boolean;
accusedDriverId: string;
export type RequestProtestDefenseResult = {
leagueId: string;
protestId: string;
}
accusedDriverId: string;
status: 'defense_requested';
};
export type RequestProtestDefenseErrorCode =
| 'PROTEST_NOT_FOUND'
| 'RACE_NOT_FOUND'
| 'INSUFFICIENT_PERMISSIONS'
| 'DEFENSE_CANNOT_BE_REQUESTED'
| 'REPOSITORY_ERROR';
export class RequestProtestDefenseUseCase {
constructor(
private readonly protestRepository: IProtestRepository,
private readonly raceRepository: IRaceRepository,
private readonly membershipRepository: ILeagueMembershipRepository,
private readonly logger: Logger,
private readonly output: UseCaseOutputPort<RequestProtestDefenseResult>,
) {}
async execute(command: RequestProtestDefenseCommand): Promise<Result<RequestProtestDefenseResult, ApplicationErrorCode<'PROTEST_NOT_FOUND' | 'RACE_NOT_FOUND' | 'INSUFFICIENT_PERMISSIONS' | 'DEFENSE_CANNOT_BE_REQUESTED'>>> {
// Get the protest
const protest = await this.protestRepository.findById(command.protestId);
if (!protest) {
return Result.err({ code: 'PROTEST_NOT_FOUND' });
async execute(
input: RequestProtestDefenseInput,
): Promise<Result<void, ApplicationErrorCode<RequestProtestDefenseErrorCode, { message: string }>>> {
try {
const protest = await this.protestRepository.findById(input.protestId);
if (!protest) {
return Result.err({ code: 'PROTEST_NOT_FOUND', details: { message: 'Protest not found' } });
}
const race = await this.raceRepository.findById(protest.raceId);
if (!race) {
return Result.err({ code: 'RACE_NOT_FOUND', details: { message: 'Race not found' } });
}
const membership = await this.membershipRepository.getMembership(race.leagueId, input.stewardId);
if (!membership || !isLeagueStewardOrHigherRole(membership.role)) {
return Result.err({
code: 'INSUFFICIENT_PERMISSIONS',
details: { message: 'Insufficient permissions to request defense' },
});
}
if (!protest.canRequestDefense()) {
return Result.err({
code: 'DEFENSE_CANNOT_BE_REQUESTED',
details: { message: 'Defense cannot be requested for this protest' },
});
}
const updatedProtest = protest.requestDefense(input.stewardId);
await this.protestRepository.update(updatedProtest);
const result: RequestProtestDefenseResult = {
leagueId: race.leagueId,
protestId: protest.id,
accusedDriverId: protest.accusedDriverId,
status: 'defense_requested',
};
this.output.present(result);
return Result.ok(undefined);
} catch (error: unknown) {
const message = error instanceof Error ? error.message : 'Failed to request protest defense';
this.logger.error('RequestProtestDefenseUseCase.execute failed', error instanceof Error ? error : undefined);
return Result.err({ code: 'REPOSITORY_ERROR', details: { message } });
}
// Get the race to find the league
const race = await this.raceRepository.findById(protest.raceId);
if (!race) {
return Result.err({ code: 'RACE_NOT_FOUND' });
}
// Verify the steward has permission
const membership = await this.membershipRepository.getMembership(race.leagueId, command.stewardId);
if (!membership || !isLeagueStewardOrHigherRole(membership.role)) {
return Result.err({ code: 'INSUFFICIENT_PERMISSIONS' });
}
// Check if defense can be requested
if (!protest.canRequestDefense()) {
return Result.err({ code: 'DEFENSE_CANNOT_BE_REQUESTED' });
}
// Request defense
const updatedProtest = protest.requestDefense(command.stewardId);
await this.protestRepository.update(updatedProtest);
return Result.ok({
success: true,
accusedDriverId: protest.accusedDriverId,
protestId: protest.id,
});
}
}
}