/** * Application Use Case: CreatePaymentUseCase * * Creates a new payment. */ import type { IPaymentRepository } from '../../domain/repositories/IPaymentRepository'; import type { Payment, PaymentType, PayerType, PaymentStatus } from '../../domain/entities/Payment'; import type { ICreatePaymentPresenter, CreatePaymentResultDTO, CreatePaymentViewModel, PaymentDto, } from '../presenters/ICreatePaymentPresenter'; import type { UseCase } from '@core/shared/application/UseCase'; export interface CreatePaymentInput { type: PaymentType; amount: number; payerId: string; payerType: PayerType; leagueId: string; seasonId?: string; } export class CreatePaymentUseCase implements UseCase { constructor(private readonly paymentRepository: IPaymentRepository) {} async execute( input: CreatePaymentInput, presenter: ICreatePaymentPresenter, ): Promise { presenter.reset(); const { type, amount, payerId, payerType, leagueId, seasonId } = input; // Calculate platform fee (assume 5% for now) const platformFee = Math.round(amount * 0.05 * 100) / 100; const netAmount = amount - platformFee; const id = `payment-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`; const payment: Payment = { id, type, amount, platformFee, netAmount, payerId, payerType, leagueId, seasonId, status: PaymentStatus.PENDING, createdAt: new Date(), }; const createdPayment = await this.paymentRepository.create(payment); const dto: PaymentDto = { id: createdPayment.id, type: createdPayment.type, amount: createdPayment.amount, platformFee: createdPayment.platformFee, netAmount: createdPayment.netAmount, payerId: createdPayment.payerId, payerType: createdPayment.payerType, leagueId: createdPayment.leagueId, seasonId: createdPayment.seasonId, status: createdPayment.status, createdAt: createdPayment.createdAt, completedAt: createdPayment.completedAt, }; const result: CreatePaymentResultDTO = { payment: dto, }; presenter.present(result); } }