Files
gridpilot.gg/core/payments/application/use-cases/UpdatePaymentStatusUseCase.ts
2025-12-19 01:22:45 +01:00

69 lines
2.0 KiB
TypeScript

/**
* Application Use Case: UpdatePaymentStatusUseCase
*
* Updates the status of a payment.
*/
import type { IPaymentRepository } from '../../domain/repositories/IPaymentRepository';
import type { PaymentStatus } from '../../domain/entities/Payment';
import type {
IUpdatePaymentStatusPresenter,
UpdatePaymentStatusResultDTO,
UpdatePaymentStatusViewModel,
PaymentDto,
} from '../presenters/IUpdatePaymentStatusPresenter';
import type { UseCase } from '@core/shared/application/UseCase';
export interface UpdatePaymentStatusInput {
paymentId: string;
status: PaymentStatus;
}
export class UpdatePaymentStatusUseCase
implements UseCase<UpdatePaymentStatusInput, UpdatePaymentStatusResultDTO, UpdatePaymentStatusViewModel, IUpdatePaymentStatusPresenter>
{
constructor(private readonly paymentRepository: IPaymentRepository) {}
async execute(
input: UpdatePaymentStatusInput,
presenter: IUpdatePaymentStatusPresenter,
): Promise<void> {
presenter.reset();
const { paymentId, status } = input;
const existingPayment = await this.paymentRepository.findById(paymentId);
if (!existingPayment) {
throw new Error(`Payment with id ${paymentId} not found`);
}
const updatedPayment = {
...existingPayment,
status,
completedAt: status === PaymentStatus.COMPLETED ? new Date() : existingPayment.completedAt,
};
const savedPayment = await this.paymentRepository.update(updatedPayment);
const dto: PaymentDto = {
id: savedPayment.id,
type: savedPayment.type,
amount: savedPayment.amount,
platformFee: savedPayment.platformFee,
netAmount: savedPayment.netAmount,
payerId: savedPayment.payerId,
payerType: savedPayment.payerType,
leagueId: savedPayment.leagueId,
seasonId: savedPayment.seasonId,
status: savedPayment.status,
createdAt: savedPayment.createdAt,
completedAt: savedPayment.completedAt,
};
const result: UpdatePaymentStatusResultDTO = {
payment: dto,
};
presenter.present(result);
}
}