Files
gridpilot.gg/adapters/racing/persistence/inmemory/InMemoryTransactionRepository.ts
2025-12-16 11:52:26 +01:00

128 lines
4.6 KiB
TypeScript

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