refactor
This commit is contained in:
@@ -5,40 +5,41 @@
|
||||
*/
|
||||
|
||||
import type { IWalletRepository, ITransactionRepository } from '../../domain/repositories/IWalletRepository';
|
||||
import type { Wallet } from '../../domain/entities/Wallet';
|
||||
import type {
|
||||
IGetWalletPresenter,
|
||||
GetWalletResultDTO,
|
||||
GetWalletViewModel,
|
||||
} from '../presenters/IGetWalletPresenter';
|
||||
import type { Wallet, Transaction } 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 type GetWalletErrorCode = 'INVALID_INPUT';
|
||||
|
||||
export interface GetWalletInput {
|
||||
leagueId: string;
|
||||
}
|
||||
|
||||
export interface GetWalletResult {
|
||||
wallet: Wallet;
|
||||
transactions: Transaction[];
|
||||
}
|
||||
|
||||
export class GetWalletUseCase
|
||||
implements UseCase<GetWalletInput, GetWalletResultDTO, GetWalletViewModel, IGetWalletPresenter>
|
||||
implements UseCase<GetWalletInput, void, GetWalletErrorCode>
|
||||
{
|
||||
constructor(
|
||||
private readonly walletRepository: IWalletRepository,
|
||||
private readonly transactionRepository: ITransactionRepository,
|
||||
private readonly output: UseCaseOutputPort<GetWalletResult>,
|
||||
) {}
|
||||
|
||||
async execute(
|
||||
input: GetWalletInput,
|
||||
presenter: IGetWalletPresenter,
|
||||
): Promise<void> {
|
||||
presenter.reset();
|
||||
|
||||
async execute(input: GetWalletInput): Promise<Result<void, ApplicationErrorCode<GetWalletErrorCode>>> {
|
||||
const { leagueId } = input;
|
||||
|
||||
if (!leagueId) {
|
||||
throw new Error('LeagueId is required');
|
||||
return Result.err({ code: 'INVALID_INPUT' 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 = {
|
||||
@@ -57,29 +58,8 @@ export class GetWalletUseCase
|
||||
const transactions = await this.transactionRepository.findByWalletId(wallet.id);
|
||||
transactions.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
|
||||
|
||||
const dto: GetWalletResultDTO = {
|
||||
wallet: {
|
||||
id: wallet.id,
|
||||
leagueId: wallet.leagueId,
|
||||
balance: wallet.balance,
|
||||
totalRevenue: wallet.totalRevenue,
|
||||
totalPlatformFees: wallet.totalPlatformFees,
|
||||
totalWithdrawn: wallet.totalWithdrawn,
|
||||
currency: wallet.currency,
|
||||
createdAt: wallet.createdAt,
|
||||
},
|
||||
transactions: transactions.map(t => ({
|
||||
id: t.id,
|
||||
walletId: t.walletId,
|
||||
type: t.type,
|
||||
amount: t.amount,
|
||||
description: t.description,
|
||||
referenceId: t.referenceId,
|
||||
referenceType: t.referenceType,
|
||||
createdAt: t.createdAt,
|
||||
})),
|
||||
};
|
||||
this.output.present({ wallet, transactions });
|
||||
|
||||
presenter.present(dto);
|
||||
return Result.ok(undefined);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user