import type { ILeagueWalletRepository } from '../../domain/repositories/ILeagueWalletRepository'; import type { ITransactionRepository } from '../../domain/repositories/ITransactionRepository'; import type { WithdrawFromLeagueWalletOutputPort } from '../ports/output/WithdrawFromLeagueWalletOutputPort'; import { Result } from '@core/shared/application/Result'; import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode'; import { Money } from '../../domain/value-objects/Money'; import { Transaction } from '../../domain/entities/league-wallet/Transaction'; import { TransactionId } from '../../domain/entities/league-wallet/TransactionId'; import { LeagueWalletId } from '../../domain/entities/league-wallet/LeagueWalletId'; export interface WithdrawFromLeagueWalletUseCaseParams { leagueId: string; amount: number; currency: string; seasonId: string; destinationAccount: string; } /** * Use Case for withdrawing from league wallet. */ export class WithdrawFromLeagueWalletUseCase { constructor( private readonly leagueWalletRepository: ILeagueWalletRepository, private readonly transactionRepository: ITransactionRepository, ) {} async execute( params: WithdrawFromLeagueWalletUseCaseParams, ): Promise>> { try { const wallet = await this.leagueWalletRepository.findByLeagueId(params.leagueId); if (!wallet) { return Result.err({ code: 'REPOSITORY_ERROR', message: 'Wallet not found' }); } // Check if withdrawal is allowed (for now, always false as per mock) if (!wallet.canWithdraw(Money.create(params.amount, params.currency))) { return Result.err({ code: 'INSUFFICIENT_BALANCE', message: 'Insufficient balance for withdrawal' }); } // For now, always block withdrawal return Result.err({ code: 'WITHDRAWAL_NOT_ALLOWED', message: 'Season 2 is still active. Withdrawals are available after season completion.' }); // If allowed, create transaction and update wallet // const transactionId = TransactionId.create(`txn-${Date.now()}`); // const transaction = Transaction.create({ // id: transactionId, // walletId: LeagueWalletId.create(wallet.id.toString()), // type: 'withdrawal', // amount: Money.create(params.amount, params.currency), // description: `Bank Transfer - ${params.seasonId} Payout`, // metadata: { destinationAccount: params.destinationAccount, seasonId: params.seasonId }, // }); // const updatedWallet = wallet.withdrawFunds(Money.create(params.amount, params.currency), transactionId.toString()); // await this.transactionRepository.create(transaction); // await this.leagueWalletRepository.update(updatedWallet); // return Result.ok({ success: true }); } catch { return Result.err({ code: 'REPOSITORY_ERROR', message: 'Failed to process withdrawal' }); } } }