Files
gridpilot.gg/adapters/payments/persistence/inmemory/InMemoryWalletRepository.ts
2026-01-23 14:51:33 +01:00

91 lines
2.8 KiB
TypeScript

/**
* In-Memory Implementation: InMemoryWalletRepository
*/
import type { Transaction, Wallet } from '@core/payments/domain/entities/Wallet';
import type { WalletRepository, TransactionRepository } from '@core/payments/domain/repositories/WalletRepository';
import type { Logger } from '@core/shared/domain/Logger';
import type { LeagueWalletRepository } from '@core/racing/domain/repositories/LeagueWalletRepository';
const wallets: Map<string, any> = new Map();
const transactions: Map<string, any> = new Map();
export class InMemoryWalletRepository implements WalletRepository, LeagueWalletRepository {
constructor(private readonly logger: Logger) {}
async findById(id: string): Promise<any | null> {
this.logger.debug('[InMemoryWalletRepository] findById', { id });
return wallets.get(id) || null;
}
async findByLeagueId(leagueId: string): Promise<any | null> {
this.logger.debug('[InMemoryWalletRepository] findByLeagueId', { leagueId });
return Array.from(wallets.values()).find(w => w.leagueId.toString() === leagueId) || null;
}
async create(wallet: any): Promise<any> {
this.logger.debug('[InMemoryWalletRepository] create', { wallet });
wallets.set(wallet.id.toString(), wallet);
return wallet;
}
async update(wallet: any): Promise<any> {
this.logger.debug('[InMemoryWalletRepository] update', { wallet });
wallets.set(wallet.id.toString(), wallet);
return wallet;
}
async delete(id: string): Promise<void> {
wallets.delete(id);
}
async exists(id: string): Promise<boolean> {
return wallets.has(id);
}
clear(): void {
wallets.clear();
}
}
export class InMemoryTransactionRepository implements TransactionRepository {
constructor(private readonly logger: Logger) {}
async findById(id: string): Promise<any | null> {
this.logger.debug('[InMemoryTransactionRepository] findById', { id });
return transactions.get(id) || null;
}
async findByWalletId(walletId: string): Promise<any[]> {
this.logger.debug('[InMemoryTransactionRepository] findByWalletId', { walletId });
return Array.from(transactions.values()).filter(t => t.walletId.toString() === walletId);
}
async create(transaction: any): Promise<any> {
this.logger.debug('[InMemoryTransactionRepository] create', { transaction });
transactions.set(transaction.id.toString(), transaction);
return transaction;
}
async update(transaction: any): Promise<any> {
transactions.set(transaction.id.toString(), transaction);
return transaction;
}
async delete(id: string): Promise<void> {
transactions.delete(id);
}
async exists(id: string): Promise<boolean> {
return transactions.has(id);
}
findByType(type: any): Promise<any[]> {
return Promise.resolve(Array.from(transactions.values()).filter(t => t.type === type));
}
clear(): void {
transactions.clear();
}
}