89 lines
4.1 KiB
TypeScript
89 lines
4.1 KiB
TypeScript
import { IProtestRepository } from '@gridpilot/racing/domain/repositories/IProtestRepository';
|
|
import { Protest, ProtestStatus } from '@gridpilot/racing/domain/entities/Protest';
|
|
import { Logger } from '@gridpilot/shared/logging/Logger';
|
|
|
|
export class InMemoryProtestRepository implements IProtestRepository {
|
|
private protests: Map<string, Protest> = new Map();
|
|
|
|
constructor(private readonly logger: Logger, initialProtests: Protest[] = []) {
|
|
this.logger.info('InMemoryProtestRepository initialized.');
|
|
for (const protest of initialProtests) {
|
|
this.protests.set(protest.id, protest);
|
|
this.logger.debug(`Seeded protest: ${protest.id}.`);
|
|
}
|
|
}
|
|
|
|
async findById(id: string): Promise<Protest | null> {
|
|
this.logger.debug(`[InMemoryProtestRepository] Finding protest by ID: ${id}`);
|
|
const protest = this.protests.get(id) ?? null;
|
|
if (protest) {
|
|
this.logger.info(`Found protest by ID: ${id}.`);
|
|
} else {
|
|
this.logger.warn(`Protest with ID ${id} not found.`);
|
|
}
|
|
return Promise.resolve(protest);
|
|
}
|
|
|
|
async findByRaceId(raceId: string): Promise<Protest[]> {
|
|
this.logger.debug(`[InMemoryProtestRepository] Finding protests for race: ${raceId}.`);
|
|
const protests = Array.from(this.protests.values()).filter(p => p.raceId === raceId);
|
|
this.logger.info(`Found ${protests.length} protests for race ${raceId}.`);
|
|
return Promise.resolve(protests);
|
|
}
|
|
|
|
async findByProtestingDriverId(driverId: string): Promise<Protest[]> {
|
|
this.logger.debug(`[InMemoryProtestRepository] Finding protests by protesting driver ID: ${driverId}.`);
|
|
const protests = Array.from(this.protests.values()).filter(p => p.protestingDriverId === driverId);
|
|
this.logger.info(`Found ${protests.length} protests by protesting driver ${driverId}.`);
|
|
return Promise.resolve(protests);
|
|
}
|
|
|
|
async findByAccusedDriverId(driverId: string): Promise<Protest[]> {
|
|
this.logger.debug(`[InMemoryProtestRepository] Finding protests by accused driver ID: ${driverId}.`);
|
|
const protests = Array.from(this.protests.values()).filter(p => p.accusedDriverId === driverId);
|
|
this.logger.info(`Found ${protests.length} protests by accused driver ${driverId}.`);
|
|
return Promise.resolve(protests);
|
|
}
|
|
|
|
async findPending(): Promise<Protest[]> {
|
|
this.logger.debug('[InMemoryProtestRepository] Finding all pending protests.');
|
|
const pendingProtests = Array.from(this.protests.values()).filter(p => p.status === 'pending');
|
|
this.logger.info(`Found ${pendingProtests.length} pending protests.`);
|
|
return Promise.resolve(pendingProtests);
|
|
}
|
|
|
|
async findUnderReviewBy(stewardId: string): Promise<Protest[]> {
|
|
this.logger.debug(`[InMemoryProtestRepository] Finding protests under review by steward: ${stewardId}.`);
|
|
const underReviewProtests = Array.from(this.protests.values()).filter(p => p.reviewedBy === stewardId && p.status === 'under_review');
|
|
this.logger.info(`Found ${underReviewProtests.length} protests under review by steward ${stewardId}.`);
|
|
return Promise.resolve(underReviewProtests);
|
|
}
|
|
|
|
async create(protest: Protest): Promise<void> {
|
|
this.logger.debug(`[InMemoryProtestRepository] Creating protest: ${protest.id}.`);
|
|
if (this.protests.has(protest.id)) {
|
|
this.logger.warn(`Protest with ID ${protest.id} already exists.`);
|
|
throw new Error('Protest already exists');
|
|
}
|
|
this.protests.set(protest.id, protest);
|
|
this.logger.info(`Protest ${protest.id} created successfully.`);
|
|
return Promise.resolve();
|
|
}
|
|
|
|
async update(protest: Protest): Promise<void> {
|
|
this.logger.debug(`[InMemoryProtestRepository] Updating protest: ${protest.id}.`);
|
|
if (!this.protests.has(protest.id)) {
|
|
this.logger.warn(`Protest with ID ${protest.id} not found for update.`);
|
|
throw new Error('Protest not found');
|
|
}
|
|
this.protests.set(protest.id, protest);
|
|
this.logger.info(`Protest ${protest.id} updated successfully.`);
|
|
return Promise.resolve();
|
|
}
|
|
|
|
async exists(id: string): Promise<boolean> {
|
|
this.logger.debug(`[InMemoryProtestRepository] Checking existence of protest with ID: ${id}.`);
|
|
return Promise.resolve(this.protests.has(id));
|
|
}
|
|
}
|