Files
gridpilot.gg/packages/racing/infrastructure/repositories/InMemorySponsorshipPricingRepository.ts
2025-12-10 12:38:55 +01:00

67 lines
2.3 KiB
TypeScript

/**
* InMemory implementation of ISponsorshipPricingRepository
*/
import type { ISponsorshipPricingRepository } from '../../domain/repositories/ISponsorshipPricingRepository';
import { SponsorshipPricing } from '../../domain/value-objects/SponsorshipPricing';
import type { SponsorableEntityType } from '../../domain/entities/SponsorshipRequest';
interface StorageKey {
entityType: SponsorableEntityType;
entityId: string;
}
export class InMemorySponsorshipPricingRepository implements ISponsorshipPricingRepository {
private pricings: Map<string, { entityType: SponsorableEntityType; entityId: string; pricing: SponsorshipPricing }> = new Map();
private makeKey(entityType: SponsorableEntityType, entityId: string): string {
return `${entityType}:${entityId}`;
}
async findByEntity(entityType: SponsorableEntityType, entityId: string): Promise<SponsorshipPricing | null> {
const key = this.makeKey(entityType, entityId);
const entry = this.pricings.get(key);
return entry?.pricing ?? null;
}
async save(entityType: SponsorableEntityType, entityId: string, pricing: SponsorshipPricing): Promise<void> {
const key = this.makeKey(entityType, entityId);
this.pricings.set(key, { entityType, entityId, pricing });
}
async delete(entityType: SponsorableEntityType, entityId: string): Promise<void> {
const key = this.makeKey(entityType, entityId);
this.pricings.delete(key);
}
async exists(entityType: SponsorableEntityType, entityId: string): Promise<boolean> {
const key = this.makeKey(entityType, entityId);
return this.pricings.has(key);
}
async findAcceptingApplications(entityType: SponsorableEntityType): Promise<Array<{
entityId: string;
pricing: SponsorshipPricing;
}>> {
return Array.from(this.pricings.values())
.filter(entry => entry.entityType === entityType && entry.pricing.acceptingApplications)
.map(entry => ({ entityId: entry.entityId, pricing: entry.pricing }));
}
/**
* Seed initial data
*/
seed(data: Array<{ entityType: SponsorableEntityType; entityId: string; pricing: SponsorshipPricing }>): void {
for (const entry of data) {
const key = this.makeKey(entry.entityType, entry.entityId);
this.pricings.set(key, entry);
}
}
/**
* Clear all data (for testing)
*/
clear(): void {
this.pricings.clear();
}
}