Files
gridpilot.gg/packages/racing/infrastructure/repositories/InMemoryProtestRepository.ts
2025-12-14 18:11:59 +01:00

151 lines
5.3 KiB
TypeScript

/**
* In-Memory Implementation: InMemoryProtestRepository
*
* Provides an in-memory storage implementation for protests.
*/
import type { Protest } from '../../domain/entities/Protest';
import type { IProtestRepository } from '../../domain/repositories/IProtestRepository';
import type { ILogger } from '@gridpilot/shared/logging/ILogger';
export class InMemoryProtestRepository implements IProtestRepository {
private protests: Map<string, Protest> = new Map();
private readonly logger: ILogger;
constructor(logger: ILogger, initialProtests: Protest[] = []) {
this.logger = logger;
this.logger.info('InMemoryProtestRepository initialized.');
initialProtests.forEach(protest => {
this.protests.set(protest.id, protest);
this.logger.debug(`Seeded protest: ${protest.id}`);
});
}
async findById(id: string): Promise<Protest | null> {
this.logger.debug(`Finding protest by id: ${id}`);
try {
const protest = this.protests.get(id) || null;
if (protest) {
this.logger.info(`Found protest with id: ${id}.`);
} else {
this.logger.warn(`Protest with id ${id} not found.`);
}
return protest;
} catch (error) {
this.logger.error(`Error finding protest by id ${id}:`, error);
throw error;
}
}
async findByRaceId(raceId: string): Promise<Protest[]> {
this.logger.debug(`Finding protests by race id: ${raceId}`);
try {
const protests = Array.from(this.protests.values()).filter(
protest => protest.raceId === raceId
);
this.logger.info(`Found ${protests.length} protests for race id: ${raceId}.`);
return protests;
} catch (error) {
this.logger.error(`Error finding protests by race id ${raceId}:`, error);
throw error;
}
}
async findByProtestingDriverId(driverId: string): Promise<Protest[]> {
this.logger.debug(`Finding protests by protesting driver id: ${driverId}`);
try {
const protests = Array.from(this.protests.values()).filter(
protest => protest.protestingDriverId === driverId
);
this.logger.info(`Found ${protests.length} protests by protesting driver id: ${driverId}.`);
return protests;
} catch (error) {
this.logger.error(`Error finding protests by protesting driver id ${driverId}:`, error);
throw error;
}
}
async findByAccusedDriverId(driverId: string): Promise<Protest[]> {
this.logger.debug(`Finding protests by accused driver id: ${driverId}`);
try {
const protests = Array.from(this.protests.values()).filter(
protest => protest.accusedDriverId === driverId
);
this.logger.info(`Found ${protests.length} protests by accused driver id: ${driverId}.`);
return protests;
} catch (error) {
this.logger.error(`Error finding protests by accused driver id ${driverId}:`, error);
throw error;
}
}
async findPending(): Promise<Protest[]> {
this.logger.debug('Finding pending protests.');
try {
const protests = Array.from(this.protests.values()).filter(
protest => protest.isPending()
);
this.logger.info(`Found ${protests.length} pending protests.`);
return protests;
} catch (error) {
this.logger.error('Error finding pending protests:', error);
throw error;
}
}
async findUnderReviewBy(stewardId: string): Promise<Protest[]> {
this.logger.debug(`Finding protests under review by steward: ${stewardId}`);
try {
const protests = Array.from(this.protests.values()).filter(
protest => protest.reviewedBy === stewardId && protest.isUnderReview()
);
this.logger.info(`Found ${protests.length} protests under review by steward: ${stewardId}.`);
return protests;
} catch (error) {
this.logger.error(`Error finding protests under review by steward ${stewardId}:`, error);
throw error;
}
}
async create(protest: Protest): Promise<void> {
this.logger.debug(`Creating protest: ${protest.id}`);
try {
if (this.protests.has(protest.id)) {
this.logger.warn(`Protest with ID ${protest.id} already exists.`);
throw new Error(`Protest with ID ${protest.id} already exists`);
}
this.protests.set(protest.id, protest);
this.logger.info(`Protest ${protest.id} created successfully.`);
} catch (error) {
this.logger.error(`Error creating protest ${protest.id}:`, error);
throw error;
}
}
async update(protest: Protest): Promise<void> {
this.logger.debug(`Updating protest: ${protest.id}`);
try {
if (!this.protests.has(protest.id)) {
this.logger.warn(`Protest with ID ${protest.id} not found for update.`);
throw new Error(`Protest with ID ${protest.id} not found`);
}
this.protests.set(protest.id, protest);
this.logger.info(`Protest ${protest.id} updated successfully.`);
} catch (error) {
this.logger.error(`Error updating protest ${protest.id}:`, error);
throw error;
}
}
async exists(id: string): Promise<boolean> {
this.logger.debug(`Checking existence of protest with id: ${id}`);
try {
const exists = this.protests.has(id);
this.logger.debug(`Protest ${id} exists: ${exists}.`);
return exists;
} catch (error) {
this.logger.error(`Error checking existence of protest with id ${id}:`, error);
throw error;
}
}
}