Files
gridpilot.gg/core/payments/application/use-cases/CreatePaymentUseCase.ts
2025-12-21 17:05:36 +01:00

66 lines
2.0 KiB
TypeScript

/**
* Application Use Case: CreatePaymentUseCase
*
* Creates a new payment.
*/
import type { IPaymentRepository } from '../../domain/repositories/IPaymentRepository';
import type { Payment, PaymentType, PayerType } 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 interface CreatePaymentInput {
type: PaymentType;
amount: number;
payerId: string;
payerType: PayerType;
leagueId: string;
seasonId?: string;
}
export interface CreatePaymentResult {
payment: Payment;
}
export type CreatePaymentErrorCode = never;
export class CreatePaymentUseCase
implements UseCase<CreatePaymentInput, void, CreatePaymentErrorCode>
{
constructor(
private readonly paymentRepository: IPaymentRepository,
private readonly output: UseCaseOutputPort<CreatePaymentResult>,
) {}
async execute(input: CreatePaymentInput): Promise<Result<void, ApplicationErrorCode<CreatePaymentErrorCode>>> {
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,
status: PaymentStatus.PENDING,
createdAt: new Date(),
...(seasonId !== undefined ? { seasonId } : {}),
};
const createdPayment = await this.paymentRepository.create(payment);
this.output.present({ payment: createdPayment });
return Result.ok(undefined);
}
}