refactor
This commit is contained in:
@@ -5,14 +5,11 @@
|
||||
*/
|
||||
|
||||
import type { IPaymentRepository } from '../../domain/repositories/IPaymentRepository';
|
||||
import type { PaymentType } from '../../domain/entities/Payment';
|
||||
import type {
|
||||
IGetPaymentsPresenter,
|
||||
GetPaymentsResultDTO,
|
||||
GetPaymentsViewModel,
|
||||
PaymentDto,
|
||||
} from '../presenters/IGetPaymentsPresenter';
|
||||
import type { Payment, PaymentType } 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 interface GetPaymentsInput {
|
||||
leagueId?: string;
|
||||
@@ -20,40 +17,32 @@ export interface GetPaymentsInput {
|
||||
type?: PaymentType;
|
||||
}
|
||||
|
||||
export interface GetPaymentsResult {
|
||||
payments: Payment[];
|
||||
}
|
||||
|
||||
export type GetPaymentsErrorCode = never;
|
||||
|
||||
export class GetPaymentsUseCase
|
||||
implements UseCase<GetPaymentsInput, GetPaymentsResultDTO, GetPaymentsViewModel, IGetPaymentsPresenter>
|
||||
implements UseCase<GetPaymentsInput, void, GetPaymentsErrorCode>
|
||||
{
|
||||
constructor(private readonly paymentRepository: IPaymentRepository) {}
|
||||
|
||||
async execute(
|
||||
input: GetPaymentsInput,
|
||||
presenter: IGetPaymentsPresenter,
|
||||
): Promise<void> {
|
||||
presenter.reset();
|
||||
constructor(
|
||||
private readonly paymentRepository: IPaymentRepository,
|
||||
private readonly output: UseCaseOutputPort<GetPaymentsResult>,
|
||||
) {}
|
||||
|
||||
async execute(input: GetPaymentsInput): Promise<Result<void, ApplicationErrorCode<GetPaymentsErrorCode>>> {
|
||||
const { leagueId, payerId, type } = input;
|
||||
|
||||
const payments = await this.paymentRepository.findByFilters({ leagueId, payerId, type });
|
||||
const filters: { leagueId?: string; payerId?: string; type?: PaymentType } = {};
|
||||
if (leagueId !== undefined) filters.leagueId = leagueId;
|
||||
if (payerId !== undefined) filters.payerId = payerId;
|
||||
if (type !== undefined) filters.type = type;
|
||||
|
||||
const dtos: PaymentDto[] = payments.map(payment => ({
|
||||
id: payment.id,
|
||||
type: payment.type,
|
||||
amount: payment.amount,
|
||||
platformFee: payment.platformFee,
|
||||
netAmount: payment.netAmount,
|
||||
payerId: payment.payerId,
|
||||
payerType: payment.payerType,
|
||||
leagueId: payment.leagueId,
|
||||
seasonId: payment.seasonId,
|
||||
status: payment.status,
|
||||
createdAt: payment.createdAt,
|
||||
completedAt: payment.completedAt,
|
||||
}));
|
||||
const payments = await this.paymentRepository.findByFilters(filters);
|
||||
|
||||
const dto: GetPaymentsResultDTO = {
|
||||
payments: dtos,
|
||||
};
|
||||
this.output.present({ payments });
|
||||
|
||||
presenter.present(dto);
|
||||
return Result.ok(undefined);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user