module creation

This commit is contained in:
2025-12-15 21:44:06 +01:00
parent b834f88bbd
commit 7c7267da72
88 changed files with 12119 additions and 4241 deletions

View File

@@ -1,151 +1,88 @@
/**
* 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';
import { IProtestRepository } from '@gridpilot/racing/domain/repositories/IProtestRepository';
import { Protest, ProtestStatus } from '@gridpilot/racing/domain/entities/Protest';
import { 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;
constructor(private readonly logger: ILogger, initialProtests: Protest[] = []) {
this.logger.info('InMemoryProtestRepository initialized.');
initialProtests.forEach(protest => {
for (const protest of initialProtests) {
this.protests.set(protest.id, protest);
this.logger.debug(`Seeded protest: ${protest.id}`);
});
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;
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(`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;
}
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(`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;
}
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(`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;
}
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('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;
}
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(`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;
}
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(`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;
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(`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;
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(`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;
}
this.logger.debug(`[InMemoryProtestRepository] Checking existence of protest with ID: ${id}.`);
return Promise.resolve(this.protests.has(id));
}
}
}