/** * InMemory implementation of ISponsorshipRequestRepository */ import type { ISponsorshipRequestRepository } from '../../domain/repositories/ISponsorshipRequestRepository'; import { SponsorshipRequest, type SponsorableEntityType, type SponsorshipRequestStatus } from '../../domain/entities/SponsorshipRequest'; import type { ILogger } from '@gridpilot/shared/logging/ILogger'; export class InMemorySponsorshipRequestRepository implements ISponsorshipRequestRepository { private requests: Map = new Map(); private readonly logger: ILogger; constructor(logger: ILogger, seedData?: SponsorshipRequest[]) { this.logger = logger; this.logger.info('InMemorySponsorshipRequestRepository initialized.'); if (seedData) { this.seed(seedData); } } async findById(id: string): Promise { this.logger.debug(`Finding sponsorship request by id: ${id}`); try { const request = this.requests.get(id) ?? null; if (request) { this.logger.info(`Found sponsorship request: ${id}.`); } else { this.logger.warn(`Sponsorship request with id ${id} not found.`); } return request; } catch (error) { this.logger.error(`Error finding sponsorship request by id ${id}:`, error); throw error; } } async findByEntity(entityType: SponsorableEntityType, entityId: string): Promise { this.logger.debug(`Finding sponsorship requests by entity: ${entityType}, ${entityId}`); try { const requests = Array.from(this.requests.values()).filter( request => request.entityType === entityType && request.entityId === entityId ); this.logger.info(`Found ${requests.length} sponsorship requests for entity: ${entityType}, ${entityId}.`); return requests; } catch (error) { this.logger.error(`Error finding sponsorship requests by entity ${entityType}, ${entityId}:`, error); throw error; } } async findPendingByEntity(entityType: SponsorableEntityType, entityId: string): Promise { this.logger.debug(`Finding pending sponsorship requests by entity: ${entityType}, ${entityId}`); try { const requests = Array.from(this.requests.values()).filter( request => request.entityType === entityType && request.entityId === entityId && request.status === 'pending' ); this.logger.info(`Found ${requests.length} pending sponsorship requests for entity: ${entityType}, ${entityId}.`); return requests; } catch (error) { this.logger.error(`Error finding pending sponsorship requests by entity ${entityType}, ${entityId}:`, error); throw error; } } async findBySponsorId(sponsorId: string): Promise { this.logger.debug(`Finding sponsorship requests by sponsor id: ${sponsorId}`); try { const requests = Array.from(this.requests.values()).filter( request => request.sponsorId === sponsorId ); this.logger.info(`Found ${requests.length} sponsorship requests for sponsor id: ${sponsorId}.`); return requests; } catch (error) { this.logger.error(`Error finding sponsorship requests by sponsor id ${sponsorId}:`, error); throw error; } } async findByStatus(status: SponsorshipRequestStatus): Promise { this.logger.debug(`Finding sponsorship requests by status: ${status}`); try { const requests = Array.from(this.requests.values()).filter( request => request.status === status ); this.logger.info(`Found ${requests.length} sponsorship requests with status: ${status}.`); return requests; } catch (error) { this.logger.error(`Error finding sponsorship requests by status ${status}:`, error); throw error; } } async findBySponsorIdAndStatus(sponsorId: string, status: SponsorshipRequestStatus): Promise { this.logger.debug(`Finding sponsorship requests by sponsor id: ${sponsorId} and status: ${status}`); try { const requests = Array.from(this.requests.values()).filter( request => request.sponsorId === sponsorId && request.status === status ); this.logger.info(`Found ${requests.length} sponsorship requests for sponsor id: ${sponsorId}, status: ${status}.`); return requests; } catch (error) { this.logger.error(`Error finding sponsorship requests by sponsor id ${sponsorId}, status ${status}:`, error); throw error; } } async hasPendingRequest(sponsorId: string, entityType: SponsorableEntityType, entityId: string): Promise { this.logger.debug(`Checking for pending request from sponsor: ${sponsorId} for entity: ${entityType}, ${entityId}`); try { const has = Array.from(this.requests.values()).some( request => request.sponsorId === sponsorId && request.entityType === entityType && request.entityId === entityId && request.status === 'pending' ); this.logger.debug(`Pending request exists: ${has}.`); return has; } catch (error) { this.logger.error(`Error checking for pending request from sponsor ${sponsorId} for entity ${entityType}, ${entityId}:`, error); throw error; } } async countPendingByEntity(entityType: SponsorableEntityType, entityId: string): Promise { this.logger.debug(`Counting pending requests for entity: ${entityType}, ${entityId}`); try { const count = Array.from(this.requests.values()).filter( request => request.entityType === entityType && request.entityId === entityId && request.status === 'pending' ).length; this.logger.info(`Counted ${count} pending requests for entity: ${entityType}, ${entityId}.`); return count; } catch (error) { this.logger.error(`Error counting pending requests for entity ${entityType}, ${entityId}:`, error); throw error; } } async create(request: SponsorshipRequest): Promise { this.logger.debug(`Creating sponsorship request: ${request.id}`); try { if (this.requests.has(request.id)) { this.logger.warn(`SponsorshipRequest with ID ${request.id} already exists.`); throw new Error(`SponsorshipRequest with ID ${request.id} already exists`); } this.requests.set(request.id, request); this.logger.info(`SponsorshipRequest ${request.id} created successfully.`); return request; } catch (error) { this.logger.error(`Error creating sponsorship request ${request.id}:`, error); throw error; } } async update(request: SponsorshipRequest): Promise { this.logger.debug(`Updating sponsorship request: ${request.id}`); try { if (!this.requests.has(request.id)) { this.logger.warn(`SponsorshipRequest ${request.id} not found for update.`); throw new Error(`SponsorshipRequest ${request.id} not found`); } this.requests.set(request.id, request); this.logger.info(`SponsorshipRequest ${request.id} updated successfully.`); return request; } catch (error) { this.logger.error(`Error updating sponsorship request ${request.id}:`, error); throw error; } } async delete(id: string): Promise { this.logger.debug(`Deleting sponsorship request: ${id}`); try { if (this.requests.delete(id)) { this.logger.info(`SponsorshipRequest ${id} deleted successfully.`); } else { this.logger.warn(`SponsorshipRequest with id ${id} not found for deletion.`); } } catch (error) { this.logger.error(`Error deleting sponsorship request ${id}:`, error); throw error; } } async exists(id: string): Promise { this.logger.debug(`Checking existence of sponsorship request with id: ${id}`); try { const exists = this.requests.has(id); this.logger.debug(`Sponsorship request ${id} exists: ${exists}.`); return exists; } catch (error) { this.logger.error(`Error checking existence of sponsorship request with id ${id}:`, error); throw error; } } /** * Seed initial data */ seed(requests: SponsorshipRequest[]): void { this.logger.debug(`Seeding ${requests.length} sponsorship requests.`); try { for (const request of requests) { this.requests.set(request.id, request); this.logger.debug(`Seeded sponsorship request: ${request.id}.`); } this.logger.info(`Successfully seeded ${requests.length} sponsorship requests.`); } catch (error) { this.logger.error(`Error seeding sponsorship requests:`, error); throw error; } } /** * Clear all data (for testing) */ clear(): void { this.logger.debug('Clearing all sponsorship requests.'); this.requests.clear(); this.logger.info('All sponsorship requests cleared.'); } }