refactor
This commit is contained in:
@@ -5,36 +5,38 @@
|
||||
*/
|
||||
|
||||
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 { Payment } from '../../domain/entities/Payment';
|
||||
import { PaymentStatus } from '../../domain/entities/Payment';
|
||||
import type { UseCase } from '@core/shared/application/UseCase';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
export type UpdatePaymentStatusErrorCode = 'PAYMENT_NOT_FOUND';
|
||||
|
||||
export interface UpdatePaymentStatusInput {
|
||||
paymentId: string;
|
||||
status: PaymentStatus;
|
||||
}
|
||||
|
||||
export interface UpdatePaymentStatusResult {
|
||||
payment: Payment;
|
||||
}
|
||||
|
||||
export class UpdatePaymentStatusUseCase
|
||||
implements UseCase<UpdatePaymentStatusInput, UpdatePaymentStatusResultDTO, UpdatePaymentStatusViewModel, IUpdatePaymentStatusPresenter>
|
||||
implements UseCase<UpdatePaymentStatusInput, void, UpdatePaymentStatusErrorCode>
|
||||
{
|
||||
constructor(private readonly paymentRepository: IPaymentRepository) {}
|
||||
|
||||
async execute(
|
||||
input: UpdatePaymentStatusInput,
|
||||
presenter: IUpdatePaymentStatusPresenter,
|
||||
): Promise<void> {
|
||||
presenter.reset();
|
||||
constructor(
|
||||
private readonly paymentRepository: IPaymentRepository,
|
||||
private readonly output: UseCaseOutputPort<UpdatePaymentStatusResult>,
|
||||
) {}
|
||||
|
||||
async execute(input: UpdatePaymentStatusInput): Promise<Result<void, ApplicationErrorCode<UpdatePaymentStatusErrorCode>>> {
|
||||
const { paymentId, status } = input;
|
||||
|
||||
const existingPayment = await this.paymentRepository.findById(paymentId);
|
||||
if (!existingPayment) {
|
||||
throw new Error(`Payment with id ${paymentId} not found`);
|
||||
return Result.err({ code: 'PAYMENT_NOT_FOUND' as const });
|
||||
}
|
||||
|
||||
const updatedPayment = {
|
||||
@@ -43,27 +45,10 @@ export class UpdatePaymentStatusUseCase
|
||||
completedAt: status === PaymentStatus.COMPLETED ? new Date() : existingPayment.completedAt,
|
||||
};
|
||||
|
||||
const savedPayment = await this.paymentRepository.update(updatedPayment);
|
||||
const savedPayment = await this.paymentRepository.update(updatedPayment as Payment);
|
||||
|
||||
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,
|
||||
};
|
||||
this.output.present({ payment: savedPayment });
|
||||
|
||||
const result: UpdatePaymentStatusResultDTO = {
|
||||
payment: dto,
|
||||
};
|
||||
|
||||
presenter.present(result);
|
||||
return Result.ok(undefined);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user