refactor use cases

This commit is contained in:
2026-01-08 15:34:51 +01:00
parent d984ab24a8
commit 52e9a2f6a7
362 changed files with 5192 additions and 8409 deletions

View File

@@ -9,7 +9,6 @@ import type { ILeagueRepository } from '../../domain/repositories/ILeagueReposit
import type { ILeagueWalletRepository } from '../../domain/repositories/ILeagueWalletRepository';
import type { ITransactionRepository } from '../../domain/repositories/ITransactionRepository';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
import { League } from '../../domain/entities/League';
import { LeagueWallet } from '../../domain/entities/league-wallet/LeagueWallet';
import { Money } from '../../domain/value-objects/Money';
@@ -19,7 +18,6 @@ describe('WithdrawFromLeagueWalletUseCase', () => {
let walletRepository: { findByLeagueId: Mock; update: Mock };
let transactionRepository: { create: Mock };
let logger: Logger & { error: Mock };
let output: UseCaseOutputPort<WithdrawFromLeagueWalletResult> & { present: Mock };
let useCase: WithdrawFromLeagueWalletUseCase;
beforeEach(() => {
@@ -29,17 +27,12 @@ describe('WithdrawFromLeagueWalletUseCase', () => {
logger = { error: vi.fn() } as unknown as Logger & { error: Mock };
output = { present: vi.fn() } as unknown as UseCaseOutputPort<WithdrawFromLeagueWalletResult> & {
present: Mock;
};
useCase = new WithdrawFromLeagueWalletUseCase(
leagueRepository as unknown as ILeagueRepository,
useCase = new WithdrawFromLeagueWalletUseCase(leagueRepository as unknown as ILeagueRepository,
walletRepository as unknown as ILeagueWalletRepository,
transactionRepository as unknown as ITransactionRepository,
logger,
output,
);
logger);
});
it('returns LEAGUE_NOT_FOUND when league is missing', async () => {
@@ -62,8 +55,7 @@ describe('WithdrawFromLeagueWalletUseCase', () => {
expect(err.code).toBe('LEAGUE_NOT_FOUND');
expect(err.details.message).toBe('League with id league-1 not found');
expect(output.present).not.toHaveBeenCalled();
});
});
it('returns WALLET_NOT_FOUND when wallet is missing', async () => {
const league = League.create({
@@ -93,8 +85,7 @@ describe('WithdrawFromLeagueWalletUseCase', () => {
expect(err.code).toBe('WALLET_NOT_FOUND');
expect(err.details.message).toBe('Wallet for league league-1 not found');
expect(output.present).not.toHaveBeenCalled();
});
});
it('returns UNAUTHORIZED_WITHDRAWAL when requester is not owner', async () => {
const league = League.create({
@@ -130,8 +121,7 @@ describe('WithdrawFromLeagueWalletUseCase', () => {
expect(err.code).toBe('UNAUTHORIZED_WITHDRAWAL');
expect(err.details.message).toBe('Only the league owner can withdraw from the league wallet');
expect(output.present).not.toHaveBeenCalled();
});
});
it('returns INSUFFICIENT_FUNDS when wallet cannot withdraw amount', async () => {
const league = League.create({
@@ -167,8 +157,7 @@ describe('WithdrawFromLeagueWalletUseCase', () => {
expect(err.code).toBe('INSUFFICIENT_FUNDS');
expect(err.details.message).toBe('Insufficient balance for withdrawal');
expect(output.present).not.toHaveBeenCalled();
});
});
it('creates withdrawal transaction and updates wallet on success', async () => {
vi.useFakeTimers();
@@ -233,10 +222,7 @@ describe('WithdrawFromLeagueWalletUseCase', () => {
expect(updatedWallet.balance.currency).toBe('USD');
expect(updatedWallet.getTransactionIds()).toContain(expectedTransactionId);
expect(output.present).toHaveBeenCalledTimes(1);
const presented = (output.present as Mock).mock.calls[0]![0] as WithdrawFromLeagueWalletResult;
expect(presented.leagueId).toBe('league-1');
const presented = (expect(presented.leagueId).toBe('league-1');
expect(presented.amount.amount).toBe(250);
expect(presented.amount.currency).toBe('USD');
expect(presented.transactionId).toBe(expectedTransactionId);
@@ -286,7 +272,6 @@ describe('WithdrawFromLeagueWalletUseCase', () => {
expect(err.code).toBe('REPOSITORY_ERROR');
expect(err.details.message).toBe('DB down');
expect(output.present).not.toHaveBeenCalled();
expect(logger.error).toHaveBeenCalledTimes(1);
});
});