refactor
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
/**
|
||||
* Application Use Case: SubmitProtestDefenseUseCase
|
||||
*
|
||||
*
|
||||
* Allows the accused driver to submit their defense statement for a protest.
|
||||
*/
|
||||
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type { IProtestRepository } from '../../domain/repositories/IProtestRepository';
|
||||
|
||||
export interface SubmitProtestDefenseCommand {
|
||||
@@ -13,40 +15,34 @@ export interface SubmitProtestDefenseCommand {
|
||||
videoUrl?: string;
|
||||
}
|
||||
|
||||
export interface SubmitProtestDefenseResult {
|
||||
success: boolean;
|
||||
protestId: string;
|
||||
}
|
||||
type SubmitProtestDefenseErrorCode = 'PROTEST_NOT_FOUND' | 'NOT_ACCUSED_DRIVER' | 'DEFENSE_CANNOT_BE_SUBMITTED';
|
||||
|
||||
export class SubmitProtestDefenseUseCase {
|
||||
constructor(
|
||||
private readonly protestRepository: IProtestRepository,
|
||||
) {}
|
||||
|
||||
async execute(command: SubmitProtestDefenseCommand): Promise<SubmitProtestDefenseResult> {
|
||||
async execute(command: SubmitProtestDefenseCommand): Promise<Result<{ protestId: string }, ApplicationErrorCode<SubmitProtestDefenseErrorCode>>> {
|
||||
// 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' });
|
||||
}
|
||||
|
||||
// Verify the submitter is the accused driver
|
||||
if (protest.accusedDriverId !== command.driverId) {
|
||||
throw new Error('Only the accused driver can submit a defense');
|
||||
return Result.err({ code: 'NOT_ACCUSED_DRIVER' });
|
||||
}
|
||||
|
||||
// Check if defense can be submitted
|
||||
if (!protest.canSubmitDefense()) {
|
||||
throw new Error('Defense cannot be submitted for this protest');
|
||||
return Result.err({ code: 'DEFENSE_CANNOT_BE_SUBMITTED' });
|
||||
}
|
||||
|
||||
// Submit defense
|
||||
const updatedProtest = protest.submitDefense(command.statement, command.videoUrl);
|
||||
await this.protestRepository.update(updatedProtest);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
protestId: protest.id,
|
||||
};
|
||||
return Result.ok({ protestId: protest.id });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user