refactor
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
@@ -9,6 +9,8 @@ import type { IProtestRepository } from '../../domain/repositories/IProtestRepos
|
||||
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 type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
export interface RequestProtestDefenseCommand {
|
||||
protestId: string;
|
||||
@@ -28,38 +30,38 @@ export class RequestProtestDefenseUseCase {
|
||||
private readonly membershipRepository: ILeagueMembershipRepository,
|
||||
) {}
|
||||
|
||||
async execute(command: RequestProtestDefenseCommand): Promise<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) {
|
||||
throw new Error('Protest not found');
|
||||
return Result.err({ code: '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');
|
||||
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)) {
|
||||
throw new Error('Only stewards and admins can request defense');
|
||||
return Result.err({ code: 'INSUFFICIENT_PERMISSIONS' });
|
||||
}
|
||||
|
||||
// Check if defense can be requested
|
||||
if (!protest.canRequestDefense()) {
|
||||
throw new Error('Defense cannot be requested for this protest');
|
||||
return Result.err({ code: 'DEFENSE_CANNOT_BE_REQUESTED' });
|
||||
}
|
||||
|
||||
// Request defense
|
||||
const updatedProtest = protest.requestDefense(command.stewardId);
|
||||
await this.protestRepository.update(updatedProtest);
|
||||
|
||||
return {
|
||||
return Result.ok({
|
||||
success: true,
|
||||
accusedDriverId: protest.accusedDriverId,
|
||||
protestId: protest.id,
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user