refactor
This commit is contained in:
@@ -4,21 +4,17 @@
|
||||
* Mock repository for testing and development
|
||||
*/
|
||||
|
||||
import type { Transaction, TransactionType } from '../../domain/entities/Transaction';
|
||||
import type { ITransactionRepository } from '../../domain/repositories/ITransactionRepository';
|
||||
import type { Transaction, TransactionType } from '@core/racing/domain/entities/league-wallet/Transaction';
|
||||
import type { ITransactionRepository } from '@core/racing/domain/repositories/ITransactionRepository';
|
||||
import type { Logger } from '@core/shared/application';
|
||||
|
||||
export class InMemoryTransactionRepository implements ITransactionRepository {
|
||||
private transactions: Map<string, Transaction> = new Map();
|
||||
private readonly logger: Logger;
|
||||
|
||||
constructor(logger: Logger, seedData?: Transaction[]) {
|
||||
constructor(logger: Logger) {
|
||||
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> {
|
||||
@@ -40,7 +36,7 @@ export class InMemoryTransactionRepository implements ITransactionRepository {
|
||||
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);
|
||||
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) {
|
||||
@@ -64,11 +60,11 @@ export class InMemoryTransactionRepository implements ITransactionRepository {
|
||||
async create(transaction: Transaction): Promise<Transaction> {
|
||||
this.logger.debug(`Creating transaction: ${transaction.id}`);
|
||||
try {
|
||||
if (this.transactions.has(transaction.id)) {
|
||||
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, transaction);
|
||||
this.transactions.set(transaction.id.toString(), transaction);
|
||||
this.logger.info(`Transaction ${transaction.id} created successfully.`);
|
||||
return transaction;
|
||||
} catch (error) {
|
||||
@@ -80,11 +76,11 @@ export class InMemoryTransactionRepository implements ITransactionRepository {
|
||||
async update(transaction: Transaction): Promise<Transaction> {
|
||||
this.logger.debug(`Updating transaction: ${transaction.id}`);
|
||||
try {
|
||||
if (!this.transactions.has(transaction.id)) {
|
||||
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, transaction);
|
||||
this.transactions.set(transaction.id.toString(), transaction);
|
||||
this.logger.info(`Transaction ${transaction.id} updated successfully.`);
|
||||
return transaction;
|
||||
} catch (error) {
|
||||
@@ -96,11 +92,13 @@ export class InMemoryTransactionRepository implements ITransactionRepository {
|
||||
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 {
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user