/** * In-Memory Implementation: ITransactionRepository * * Mock repository for testing and development */ import type { Transaction, TransactionType } from '@core/racing/domain/entities/league-wallet/Transaction'; import type { TransactionRepository } from '@core/racing/domain/repositories/TransactionRepository'; import type { Logger } from '@core/shared/domain/Logger'; export class InMemoryTransactionRepository implements TransactionRepository { private transactions: Map = new Map(); private readonly logger: Logger; constructor(logger: Logger) { this.logger = logger; this.logger.info('InMemoryTransactionRepository initialized.'); } async findById(id: string): Promise { this.logger.debug(`Finding transaction by id: ${id}`); try { const transaction = this.transactions.get(id) ?? null; if (transaction) { this.logger.info(`Found transaction: ${id}.`); } else { this.logger.warn(`Transaction with id ${id} not found.`); } return transaction; } catch (error) { this.logger.error(`Error finding transaction by id ${id}:`, error instanceof Error ? error : new Error(String(error))); throw error; } } async findByWalletId(walletId: string): Promise { this.logger.debug(`Finding transactions by wallet id: ${walletId}`); try { const transactions = Array.from(this.transactions.values()).filter(t => t.walletId.toString() === walletId); this.logger.info(`Found ${transactions.length} transactions for wallet id: ${walletId}.`); return transactions; } catch (error) { this.logger.error(`Error finding transactions by wallet id ${walletId}:`, error instanceof Error ? error : new Error(String(error))); throw error; } } async findByType(type: TransactionType): Promise { this.logger.debug(`Finding transactions by type: ${type}`); try { const transactions = Array.from(this.transactions.values()).filter(t => t.type === type); this.logger.info(`Found ${transactions.length} transactions of type: ${type}.`); return transactions; } catch (error) { this.logger.error(`Error finding transactions by type ${type}:`, error instanceof Error ? error : new Error(String(error))); throw error; } } async create(transaction: Transaction): Promise { this.logger.debug(`Creating transaction: ${transaction.id}`); try { if (this.transactions.has(transaction.id.toString())) { this.logger.warn(`Transaction with ID ${transaction.id} already exists.`); throw new Error('Transaction with this ID already exists'); } this.transactions.set(transaction.id.toString(), transaction); this.logger.info(`Transaction ${transaction.id} created successfully.`); return transaction; } catch (error) { this.logger.error(`Error creating transaction ${transaction.id}:`, error instanceof Error ? error : new Error(String(error))); throw error; } } async update(transaction: Transaction): Promise { this.logger.debug(`Updating transaction: ${transaction.id}`); try { if (!this.transactions.has(transaction.id.toString())) { this.logger.warn(`Transaction with ID ${transaction.id} not found for update.`); throw new Error('Transaction not found'); } this.transactions.set(transaction.id.toString(), transaction); this.logger.info(`Transaction ${transaction.id} updated successfully.`); return transaction; } catch (error) { this.logger.error(`Error updating transaction ${transaction.id}:`, error instanceof Error ? error : new Error(String(error))); throw error; } } async delete(id: string): Promise { this.logger.debug(`Deleting transaction: ${id}`); try { if (!this.transactions.has(id)) { this.logger.warn(`Transaction with id ${id} not found for deletion.`); throw new Error(`Transaction with ID ${id} not found`); } this.transactions.delete(id); this.logger.info(`Transaction ${id} deleted successfully.`); } catch (error) { this.logger.error(`Error deleting transaction ${id}:`, error instanceof Error ? error : new Error(String(error))); throw error; } } async exists(id: string): Promise { this.logger.debug(`Checking existence of transaction with id: ${id}`); try { const exists = this.transactions.has(id); this.logger.debug(`Transaction ${id} exists: ${exists}.`); return exists; } catch (error) { this.logger.error(`Error checking existence of transaction with id ${id}:`, error instanceof Error ? error : new Error(String(error))); throw error; } } // Test helper clear(): void { this.logger.debug('Clearing all transactions.'); this.transactions.clear(); this.logger.info('All transactions cleared.'); } }