This commit is contained in:
2025-12-21 17:05:36 +01:00
parent 08b0d59e45
commit f2d8a23583
66 changed files with 1131 additions and 1342 deletions

View File

@@ -5,13 +5,12 @@
*/
import type { IWalletRepository, ITransactionRepository } from '../../domain/repositories/IWalletRepository';
import type { Wallet, Transaction, TransactionType, ReferenceType } from '../../domain/entities/Wallet';
import type {
IProcessWalletTransactionPresenter,
ProcessWalletTransactionResultDTO,
ProcessWalletTransactionViewModel,
} from '../presenters/IProcessWalletTransactionPresenter';
import type { Wallet, Transaction } from '../../domain/entities/Wallet';
import { TransactionType, ReferenceType } from '../../domain/entities/Wallet';
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 ProcessWalletTransactionInput {
leagueId: string;
@@ -22,32 +21,35 @@ export interface ProcessWalletTransactionInput {
referenceType?: ReferenceType;
}
export interface ProcessWalletTransactionResult {
wallet: Wallet;
transaction: Transaction;
}
export type ProcessWalletTransactionErrorCode = 'MISSING_REQUIRED_FIELDS' | 'INVALID_TYPE' | 'INSUFFICIENT_BALANCE';
export class ProcessWalletTransactionUseCase
implements UseCase<ProcessWalletTransactionInput, ProcessWalletTransactionResultDTO, ProcessWalletTransactionViewModel, IProcessWalletTransactionPresenter>
implements UseCase<ProcessWalletTransactionInput, void, ProcessWalletTransactionErrorCode>
{
constructor(
private readonly walletRepository: IWalletRepository,
private readonly transactionRepository: ITransactionRepository,
private readonly output: UseCaseOutputPort<ProcessWalletTransactionResult>,
) {}
async execute(
input: ProcessWalletTransactionInput,
presenter: IProcessWalletTransactionPresenter,
): Promise<void> {
presenter.reset();
async execute(input: ProcessWalletTransactionInput): Promise<Result<void, ApplicationErrorCode<ProcessWalletTransactionErrorCode>>> {
const { leagueId, type, amount, description, referenceId, referenceType } = input;
if (!leagueId || !type || amount === undefined || !description) {
throw new Error('Missing required fields: leagueId, type, amount, description');
return Result.err({ code: 'MISSING_REQUIRED_FIELDS' as const });
}
if (type !== ('deposit' as TransactionType) && type !== ('withdrawal' as TransactionType)) {
throw new Error('Type must be "deposit" or "withdrawal"');
if (type !== TransactionType.DEPOSIT && type !== TransactionType.WITHDRAWAL) {
return Result.err({ code: 'INVALID_TYPE' as const });
}
let wallet = await this.walletRepository.findByLeagueId(leagueId);
if (!wallet) {
const id = `wallet-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
const newWallet: Wallet = {
@@ -63,9 +65,9 @@ export class ProcessWalletTransactionUseCase
wallet = await this.walletRepository.create(newWallet);
}
if (type === ('withdrawal' as TransactionType)) {
if (type === TransactionType.WITHDRAWAL) {
if (amount > wallet.balance) {
throw new Error('Insufficient balance');
return Result.err({ code: 'INSUFFICIENT_BALANCE' as const });
}
}
@@ -76,14 +78,14 @@ export class ProcessWalletTransactionUseCase
type,
amount,
description,
referenceId,
referenceType,
createdAt: new Date(),
...(referenceId !== undefined ? { referenceId } : {}),
...(referenceType !== undefined ? { referenceType } : {}),
};
const createdTransaction = await this.transactionRepository.create(transaction);
if (type === ('deposit' as TransactionType)) {
if (type === TransactionType.DEPOSIT) {
wallet.balance += amount;
wallet.totalRevenue += amount;
} else {
@@ -93,29 +95,8 @@ export class ProcessWalletTransactionUseCase
const updatedWallet = await this.walletRepository.update(wallet);
const dto: ProcessWalletTransactionResultDTO = {
wallet: {
id: updatedWallet.id,
leagueId: updatedWallet.leagueId,
balance: updatedWallet.balance,
totalRevenue: updatedWallet.totalRevenue,
totalPlatformFees: updatedWallet.totalPlatformFees,
totalWithdrawn: updatedWallet.totalWithdrawn,
currency: updatedWallet.currency,
createdAt: updatedWallet.createdAt,
},
transaction: {
id: createdTransaction.id,
walletId: createdTransaction.walletId,
type: createdTransaction.type,
amount: createdTransaction.amount,
description: createdTransaction.description,
referenceId: createdTransaction.referenceId,
referenceType: createdTransaction.referenceType,
createdAt: createdTransaction.createdAt,
},
};
this.output.present({ wallet: updatedWallet, transaction: createdTransaction });
presenter.present(dto);
return Result.ok(undefined);
}
}