inmemory to postgres
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
|
||||
import { LoggingModule } from '../../domain/logging/LoggingModule';
|
||||
|
||||
import type { Logger } from '@core/shared/application/Logger';
|
||||
|
||||
import { InMemoryAchievementRepository } from '@adapters/identity/persistence/inmemory/InMemoryAchievementRepository';
|
||||
|
||||
import { ACHIEVEMENT_REPOSITORY_TOKEN } from '../achievement/AchievementPersistenceTokens';
|
||||
import { Achievement, AchievementCategory } from '@core/identity/domain/entities/Achievement';
|
||||
import { UserAchievement } from '@core/identity/domain/entities/UserAchievement';
|
||||
|
||||
// Adapter to convert between domain repository interface and application use case interface
|
||||
class InMemoryAchievementRepositoryAdapter {
|
||||
constructor(private readonly inMemoryRepo: InMemoryAchievementRepository) {}
|
||||
|
||||
// Application use case interface methods
|
||||
async save(achievement: Achievement): Promise<void> {
|
||||
await this.inMemoryRepo.createAchievement(achievement);
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<Achievement | null> {
|
||||
return await this.inMemoryRepo.findAchievementById(id);
|
||||
}
|
||||
|
||||
// Delegate all other methods to the underlying in-memory repository
|
||||
async findAchievementById(id: string): Promise<Achievement | null> {
|
||||
return await this.inMemoryRepo.findAchievementById(id);
|
||||
}
|
||||
|
||||
async findAllAchievements(): Promise<Achievement[]> {
|
||||
return await this.inMemoryRepo.findAllAchievements();
|
||||
}
|
||||
|
||||
async findAchievementsByCategory(category: AchievementCategory): Promise<Achievement[]> {
|
||||
return await this.inMemoryRepo.findAchievementsByCategory(category);
|
||||
}
|
||||
|
||||
async createAchievement(achievement: Achievement): Promise<Achievement> {
|
||||
return await this.inMemoryRepo.createAchievement(achievement);
|
||||
}
|
||||
|
||||
async findUserAchievementById(id: string): Promise<UserAchievement | null> {
|
||||
return await this.inMemoryRepo.findUserAchievementById(id);
|
||||
}
|
||||
|
||||
async findUserAchievementsByUserId(userId: string): Promise<UserAchievement[]> {
|
||||
return await this.inMemoryRepo.findUserAchievementsByUserId(userId);
|
||||
}
|
||||
|
||||
async findUserAchievementByUserAndAchievement(userId: string, achievementId: string): Promise<UserAchievement | null> {
|
||||
return await this.inMemoryRepo.findUserAchievementByUserAndAchievement(userId, achievementId);
|
||||
}
|
||||
|
||||
async hasUserEarnedAchievement(userId: string, achievementId: string): Promise<boolean> {
|
||||
return await this.inMemoryRepo.hasUserEarnedAchievement(userId, achievementId);
|
||||
}
|
||||
|
||||
async createUserAchievement(userAchievement: UserAchievement): Promise<UserAchievement> {
|
||||
return await this.inMemoryRepo.createUserAchievement(userAchievement);
|
||||
}
|
||||
|
||||
async updateUserAchievement(userAchievement: UserAchievement): Promise<UserAchievement> {
|
||||
return await this.inMemoryRepo.updateUserAchievement(userAchievement);
|
||||
}
|
||||
|
||||
async getAchievementLeaderboard(limit: number): Promise<{ userId: string; points: number; count: number }[]> {
|
||||
return await this.inMemoryRepo.getAchievementLeaderboard(limit);
|
||||
}
|
||||
|
||||
async getUserAchievementStats(userId: string): Promise<{
|
||||
total: number;
|
||||
points: number;
|
||||
byCategory: Record<AchievementCategory, number>;
|
||||
}> {
|
||||
return await this.inMemoryRepo.getUserAchievementStats(userId);
|
||||
}
|
||||
}
|
||||
|
||||
@Module({
|
||||
imports: [LoggingModule],
|
||||
providers: [
|
||||
{
|
||||
provide: ACHIEVEMENT_REPOSITORY_TOKEN,
|
||||
useFactory: (logger: Logger) =>
|
||||
new InMemoryAchievementRepositoryAdapter(new InMemoryAchievementRepository(logger)),
|
||||
inject: ['Logger'],
|
||||
},
|
||||
],
|
||||
exports: [ACHIEVEMENT_REPOSITORY_TOKEN],
|
||||
})
|
||||
export class InMemoryAchievementPersistenceModule {}
|
||||
@@ -0,0 +1,51 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
|
||||
import { LoggingModule } from '../../domain/logging/LoggingModule';
|
||||
|
||||
import type { Logger } from '@core/shared/application/Logger';
|
||||
|
||||
import type { IAvatarGenerationRepository } from '@core/media/domain/repositories/IAvatarGenerationRepository';
|
||||
import type { IMediaRepository } from '@core/media/domain/repositories/IMediaRepository';
|
||||
import type { IAvatarRepository } from '@core/media/domain/repositories/IAvatarRepository';
|
||||
|
||||
import { InMemoryAvatarGenerationRepository } from '@adapters/media/persistence/inmemory/InMemoryAvatarGenerationRepository';
|
||||
|
||||
import { AVATAR_GENERATION_REPOSITORY_TOKEN, MEDIA_REPOSITORY_TOKEN, AVATAR_REPOSITORY_TOKEN } from '../media/MediaPersistenceTokens';
|
||||
|
||||
// Mock implementations for Media and Avatar repositories (inmemory only has AvatarGeneration)
|
||||
class MockMediaRepository implements IMediaRepository {
|
||||
async save(): Promise<void> {}
|
||||
async findById(): Promise<null> { return null; }
|
||||
async findByUploadedBy(): Promise<[]> { return []; }
|
||||
async delete(): Promise<void> {}
|
||||
}
|
||||
|
||||
class MockAvatarRepository implements IAvatarRepository {
|
||||
async save(): Promise<void> {}
|
||||
async findById(): Promise<null> { return null; }
|
||||
async findActiveByDriverId(): Promise<null> { return null; }
|
||||
async findByDriverId(): Promise<[]> { return []; }
|
||||
async delete(): Promise<void> {}
|
||||
}
|
||||
|
||||
@Module({
|
||||
imports: [LoggingModule],
|
||||
providers: [
|
||||
{
|
||||
provide: AVATAR_GENERATION_REPOSITORY_TOKEN,
|
||||
useFactory: (logger: Logger): IAvatarGenerationRepository =>
|
||||
new InMemoryAvatarGenerationRepository(logger),
|
||||
inject: ['Logger'],
|
||||
},
|
||||
{
|
||||
provide: MEDIA_REPOSITORY_TOKEN,
|
||||
useClass: MockMediaRepository,
|
||||
},
|
||||
{
|
||||
provide: AVATAR_REPOSITORY_TOKEN,
|
||||
useClass: MockAvatarRepository,
|
||||
},
|
||||
],
|
||||
exports: [AVATAR_GENERATION_REPOSITORY_TOKEN, MEDIA_REPOSITORY_TOKEN, AVATAR_REPOSITORY_TOKEN],
|
||||
})
|
||||
export class InMemoryMediaPersistenceModule {}
|
||||
@@ -0,0 +1,67 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
|
||||
import { LoggingModule } from '../../domain/logging/LoggingModule';
|
||||
|
||||
import type { Logger } from '@core/shared/application/Logger';
|
||||
|
||||
import type { INotificationRepository } from '@core/notifications/domain/repositories/INotificationRepository';
|
||||
import type { INotificationPreferenceRepository } from '@core/notifications/domain/repositories/INotificationPreferenceRepository';
|
||||
import type { NotificationService } from '@core/notifications/application/ports/NotificationService';
|
||||
import type { NotificationGatewayRegistry } from '@core/notifications/application/ports/NotificationGateway';
|
||||
|
||||
import { InMemoryNotificationRepository } from '@adapters/notifications/persistence/inmemory/InMemoryNotificationRepository';
|
||||
import { InMemoryNotificationPreferenceRepository } from '@adapters/notifications/persistence/inmemory/InMemoryNotificationPreferenceRepository';
|
||||
import { NotificationServiceAdapter } from '@adapters/notifications/ports/NotificationServiceAdapter';
|
||||
import { InMemoryNotificationGatewayRegistry } from '@adapters/notifications/ports/InMemoryNotificationGatewayRegistry';
|
||||
|
||||
import { NOTIFICATION_REPOSITORY_TOKEN, NOTIFICATION_PREFERENCE_REPOSITORY_TOKEN } from '../notifications/NotificationsPersistenceTokens';
|
||||
|
||||
export const NOTIFICATION_SERVICE_TOKEN = 'INotificationService';
|
||||
export const NOTIFICATION_GATEWAY_REGISTRY_TOKEN = 'INotificationGatewayRegistry';
|
||||
|
||||
@Module({
|
||||
imports: [LoggingModule],
|
||||
providers: [
|
||||
{
|
||||
provide: NOTIFICATION_REPOSITORY_TOKEN,
|
||||
useFactory: (logger: Logger): INotificationRepository =>
|
||||
new InMemoryNotificationRepository(logger),
|
||||
inject: ['Logger'],
|
||||
},
|
||||
{
|
||||
provide: NOTIFICATION_PREFERENCE_REPOSITORY_TOKEN,
|
||||
useFactory: (logger: Logger): INotificationPreferenceRepository =>
|
||||
new InMemoryNotificationPreferenceRepository(logger),
|
||||
inject: ['Logger'],
|
||||
},
|
||||
{
|
||||
provide: NOTIFICATION_GATEWAY_REGISTRY_TOKEN,
|
||||
useFactory: (): NotificationGatewayRegistry =>
|
||||
new InMemoryNotificationGatewayRegistry(),
|
||||
inject: [],
|
||||
},
|
||||
{
|
||||
provide: NOTIFICATION_SERVICE_TOKEN,
|
||||
useFactory: (
|
||||
notificationRepo: INotificationRepository,
|
||||
preferenceRepo: INotificationPreferenceRepository,
|
||||
gatewayRegistry: NotificationGatewayRegistry,
|
||||
logger: Logger,
|
||||
): NotificationService =>
|
||||
new NotificationServiceAdapter(notificationRepo, preferenceRepo, gatewayRegistry, logger),
|
||||
inject: [
|
||||
NOTIFICATION_REPOSITORY_TOKEN,
|
||||
NOTIFICATION_PREFERENCE_REPOSITORY_TOKEN,
|
||||
NOTIFICATION_GATEWAY_REGISTRY_TOKEN,
|
||||
'Logger',
|
||||
],
|
||||
},
|
||||
],
|
||||
exports: [
|
||||
NOTIFICATION_REPOSITORY_TOKEN,
|
||||
NOTIFICATION_PREFERENCE_REPOSITORY_TOKEN,
|
||||
NOTIFICATION_SERVICE_TOKEN,
|
||||
NOTIFICATION_GATEWAY_REGISTRY_TOKEN,
|
||||
],
|
||||
})
|
||||
export class InMemoryNotificationsPersistenceModule {}
|
||||
Reference in New Issue
Block a user