51 lines
1.4 KiB
TypeScript
51 lines
1.4 KiB
TypeScript
/**
|
|
* Application Use Case: AwardPrizeUseCase
|
|
*
|
|
* Awards a prize to a driver.
|
|
*/
|
|
|
|
import type { UseCase } from '@core/shared/application/UseCase';
|
|
import { Result } from '@core/shared/domain/Result';
|
|
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
|
import type { Prize } from '../../domain/entities/Prize';
|
|
import type { PrizeRepository } from '../../domain/repositories/PrizeRepository';
|
|
|
|
export interface AwardPrizeInput {
|
|
prizeId: string;
|
|
driverId: string;
|
|
}
|
|
|
|
export interface AwardPrizeResult {
|
|
prize: Prize;
|
|
}
|
|
|
|
export type AwardPrizeErrorCode = 'PRIZE_NOT_FOUND' | 'PRIZE_ALREADY_AWARDED';
|
|
|
|
export class AwardPrizeUseCase
|
|
implements UseCase<AwardPrizeInput, AwardPrizeResult, AwardPrizeErrorCode>
|
|
{
|
|
constructor(
|
|
private readonly prizeRepository: PrizeRepository,
|
|
) {}
|
|
|
|
async execute(input: AwardPrizeInput): Promise<Result<AwardPrizeResult, ApplicationErrorCode<AwardPrizeErrorCode>>> {
|
|
const { prizeId, driverId } = input;
|
|
|
|
const prize = await this.prizeRepository.findById(prizeId);
|
|
if (!prize) {
|
|
return Result.err({ code: 'PRIZE_NOT_FOUND' as const });
|
|
}
|
|
|
|
if (prize.awarded) {
|
|
return Result.err({ code: 'PRIZE_ALREADY_AWARDED' as const });
|
|
}
|
|
|
|
prize.awarded = true;
|
|
prize.awardedTo = driverId;
|
|
prize.awardedAt = new Date();
|
|
|
|
const updatedPrize = await this.prizeRepository.update(prize);
|
|
|
|
return Result.ok({ prize: updatedPrize });
|
|
}
|
|
} |