refactor racing use cases
This commit is contained in:
@@ -5,28 +5,56 @@
|
||||
* The penalty can be standalone or linked to an upheld protest.
|
||||
*/
|
||||
|
||||
import { Penalty } from '../../domain/entities/Penalty';
|
||||
import { Penalty } from '../../domain/entities/penalty/Penalty';
|
||||
import type { IPenaltyRepository } from '../../domain/repositories/IPenaltyRepository';
|
||||
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';
|
||||
import type { AsyncUseCase , Logger } from '@core/shared/application';
|
||||
import type { Logger } from '@core/shared/application';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type { ApplyPenaltyCommandPort } from '../ports/input/ApplyPenaltyCommandPort';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
|
||||
export class ApplyPenaltyUseCase
|
||||
implements AsyncUseCase<ApplyPenaltyCommandPort, { penaltyId: string }, string> {
|
||||
export interface ApplyPenaltyInput {
|
||||
raceId: string;
|
||||
driverId: string;
|
||||
stewardId: string;
|
||||
type: Penalty['type'];
|
||||
value?: Penalty['value'];
|
||||
reason: string;
|
||||
protestId?: string;
|
||||
notes?: string;
|
||||
}
|
||||
|
||||
export interface ApplyPenaltyResult {
|
||||
penaltyId: string;
|
||||
}
|
||||
|
||||
export class ApplyPenaltyUseCase {
|
||||
constructor(
|
||||
private readonly penaltyRepository: IPenaltyRepository,
|
||||
private readonly protestRepository: IProtestRepository,
|
||||
private readonly raceRepository: IRaceRepository,
|
||||
private readonly leagueMembershipRepository: ILeagueMembershipRepository,
|
||||
private readonly logger: Logger,
|
||||
private readonly output: UseCaseOutputPort<ApplyPenaltyResult>,
|
||||
) {}
|
||||
|
||||
async execute(command: ApplyPenaltyCommandPort): Promise<Result<{ penaltyId: string }, ApplicationErrorCode<string>>> {
|
||||
async execute(
|
||||
command: ApplyPenaltyInput,
|
||||
): Promise<
|
||||
Result<
|
||||
void,
|
||||
ApplicationErrorCode<
|
||||
| 'RACE_NOT_FOUND'
|
||||
| 'INSUFFICIENT_AUTHORITY'
|
||||
| 'PROTEST_NOT_FOUND'
|
||||
| 'PROTEST_NOT_UPHELD'
|
||||
| 'PROTEST_NOT_FOR_RACE'
|
||||
>
|
||||
>
|
||||
> {
|
||||
this.logger.debug('ApplyPenaltyUseCase: Executing with command', command);
|
||||
|
||||
// Validate race exists
|
||||
@@ -84,8 +112,13 @@ export class ApplyPenaltyUseCase
|
||||
});
|
||||
|
||||
await this.penaltyRepository.create(penalty);
|
||||
this.logger.info(`ApplyPenaltyUseCase: Successfully applied penalty ${penalty.id} for driver ${command.driverId} in race ${command.raceId}.`);
|
||||
this.logger.info(
|
||||
`ApplyPenaltyUseCase: Successfully applied penalty ${penalty.id} for driver ${command.driverId} in race ${command.raceId}.`,
|
||||
);
|
||||
|
||||
return Result.ok({ penaltyId: penalty.id });
|
||||
const result: ApplyPenaltyResult = { penaltyId: penalty.id };
|
||||
this.output.present(result);
|
||||
|
||||
return Result.ok(undefined);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user