module cleanup
This commit is contained in:
@@ -5,16 +5,15 @@
|
||||
*/
|
||||
|
||||
import type { IPaymentRepository } from '../../domain/repositories/IPaymentRepository';
|
||||
import type { PaymentType, PayerType, PaymentStatus, Payment } from '../../domain/entities/Payment';
|
||||
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';
|
||||
|
||||
const PLATFORM_FEE_RATE = 0.10;
|
||||
|
||||
export interface CreatePaymentInput {
|
||||
type: PaymentType;
|
||||
amount: number;
|
||||
@@ -37,7 +36,8 @@ export class CreatePaymentUseCase
|
||||
|
||||
const { type, amount, payerId, payerType, leagueId, seasonId } = input;
|
||||
|
||||
const platformFee = amount * PLATFORM_FEE_RATE;
|
||||
// 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)}`;
|
||||
@@ -51,29 +51,31 @@ export class CreatePaymentUseCase
|
||||
payerType,
|
||||
leagueId,
|
||||
seasonId,
|
||||
status: 'pending' as PaymentStatus,
|
||||
status: PaymentStatus.PENDING,
|
||||
createdAt: new Date(),
|
||||
};
|
||||
|
||||
const createdPayment = await this.paymentRepository.create(payment);
|
||||
|
||||
const dto: CreatePaymentResultDTO = {
|
||||
payment: {
|
||||
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 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,
|
||||
};
|
||||
|
||||
presenter.present(dto);
|
||||
const result: CreatePaymentResultDTO = {
|
||||
payment: dto,
|
||||
};
|
||||
|
||||
presenter.present(result);
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import type {
|
||||
IGetPaymentsPresenter,
|
||||
GetPaymentsResultDTO,
|
||||
GetPaymentsViewModel,
|
||||
PaymentDto,
|
||||
} from '../presenters/IGetPaymentsPresenter';
|
||||
import type { UseCase } from '@core/shared/application/UseCase';
|
||||
|
||||
@@ -30,27 +31,27 @@ export class GetPaymentsUseCase
|
||||
): Promise<void> {
|
||||
presenter.reset();
|
||||
|
||||
const payments = await this.paymentRepository.findByFilters({
|
||||
leagueId: input.leagueId,
|
||||
payerId: input.payerId,
|
||||
type: input.type,
|
||||
});
|
||||
const { leagueId, payerId, type } = input;
|
||||
|
||||
const payments = await this.paymentRepository.findByFilters({ leagueId, payerId, 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 dto: GetPaymentsResultDTO = {
|
||||
payments: 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,
|
||||
})),
|
||||
payments: dtos,
|
||||
};
|
||||
|
||||
presenter.present(dto);
|
||||
|
||||
@@ -10,6 +10,7 @@ import type {
|
||||
IUpdatePaymentStatusPresenter,
|
||||
UpdatePaymentStatusResultDTO,
|
||||
UpdatePaymentStatusViewModel,
|
||||
PaymentDto,
|
||||
} from '../presenters/IUpdatePaymentStatusPresenter';
|
||||
import type { UseCase } from '@core/shared/application/UseCase';
|
||||
|
||||
@@ -31,35 +32,38 @@ export class UpdatePaymentStatusUseCase
|
||||
|
||||
const { paymentId, status } = input;
|
||||
|
||||
const payment = await this.paymentRepository.findById(paymentId);
|
||||
if (!payment) {
|
||||
throw new Error('Payment not found');
|
||||
const existingPayment = await this.paymentRepository.findById(paymentId);
|
||||
if (!existingPayment) {
|
||||
throw new Error(`Payment with id ${paymentId} not found`);
|
||||
}
|
||||
|
||||
payment.status = status;
|
||||
if (status === ('completed' as PaymentStatus)) {
|
||||
payment.completedAt = new Date();
|
||||
}
|
||||
|
||||
const updatedPayment = await this.paymentRepository.update(payment);
|
||||
|
||||
const dto: UpdatePaymentStatusResultDTO = {
|
||||
payment: {
|
||||
id: updatedPayment.id,
|
||||
type: updatedPayment.type,
|
||||
amount: updatedPayment.amount,
|
||||
platformFee: updatedPayment.platformFee,
|
||||
netAmount: updatedPayment.netAmount,
|
||||
payerId: updatedPayment.payerId,
|
||||
payerType: updatedPayment.payerType,
|
||||
leagueId: updatedPayment.leagueId,
|
||||
seasonId: updatedPayment.seasonId,
|
||||
status: updatedPayment.status,
|
||||
createdAt: updatedPayment.createdAt,
|
||||
completedAt: updatedPayment.completedAt,
|
||||
},
|
||||
const updatedPayment = {
|
||||
...existingPayment,
|
||||
status,
|
||||
completedAt: status === PaymentStatus.COMPLETED ? new Date() : existingPayment.completedAt,
|
||||
};
|
||||
|
||||
presenter.present(dto);
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user