65 lines
1.9 KiB
TypeScript
65 lines
1.9 KiB
TypeScript
/**
|
|
* Infrastructure Adapter: InMemoryUserRatingRepository
|
|
*
|
|
* In-memory implementation of IUserRatingRepository
|
|
*/
|
|
|
|
import { UserRating } from '../../domain/value-objects/UserRating';
|
|
import type { IUserRatingRepository } from '../../domain/repositories/IUserRatingRepository';
|
|
|
|
export class InMemoryUserRatingRepository implements IUserRatingRepository {
|
|
private ratings: Map<string, UserRating> = new Map();
|
|
|
|
async findByUserId(userId: string): Promise<UserRating | null> {
|
|
return this.ratings.get(userId) ?? null;
|
|
}
|
|
|
|
async findByUserIds(userIds: string[]): Promise<UserRating[]> {
|
|
const results: UserRating[] = [];
|
|
for (const userId of userIds) {
|
|
const rating = this.ratings.get(userId);
|
|
if (rating) {
|
|
results.push(rating);
|
|
}
|
|
}
|
|
return results;
|
|
}
|
|
|
|
async save(rating: UserRating): Promise<UserRating> {
|
|
this.ratings.set(rating.userId, rating);
|
|
return rating;
|
|
}
|
|
|
|
async getTopDrivers(limit: number): Promise<UserRating[]> {
|
|
return Array.from(this.ratings.values())
|
|
.filter(r => r.driver.sampleSize > 0)
|
|
.sort((a, b) => b.driver.value - a.driver.value)
|
|
.slice(0, limit);
|
|
}
|
|
|
|
async getTopTrusted(limit: number): Promise<UserRating[]> {
|
|
return Array.from(this.ratings.values())
|
|
.filter(r => r.trust.sampleSize > 0)
|
|
.sort((a, b) => b.trust.value - a.trust.value)
|
|
.slice(0, limit);
|
|
}
|
|
|
|
async getEligibleStewards(): Promise<UserRating[]> {
|
|
return Array.from(this.ratings.values())
|
|
.filter(r => r.canBeSteward());
|
|
}
|
|
|
|
async findByDriverTier(tier: 'rookie' | 'amateur' | 'semi-pro' | 'pro' | 'elite'): Promise<UserRating[]> {
|
|
return Array.from(this.ratings.values())
|
|
.filter(r => r.getDriverTier() === tier);
|
|
}
|
|
|
|
async delete(userId: string): Promise<void> {
|
|
this.ratings.delete(userId);
|
|
}
|
|
|
|
// Test helper
|
|
clear(): void {
|
|
this.ratings.clear();
|
|
}
|
|
} |