import { Inject, Injectable, Logger } from '@nestjs/common'; import { EventEmitter2 } from '@nestjs/event-emitter'; import { LoggerBase, MessagePublisherPort, PrismaRepositoryBase, } from '@mobicoop/ddd-library'; import { PrismaService } from './prisma.service'; import { MESSAGE_PUBLISHER } from '@src/app.di-tokens'; import { UsernameEntity } from '../core/domain/username.entity'; import { UsernameRepositoryPort } from '../core/application/ports/username.repository.port'; import { UsernameMapper } from '../username.mapper'; import { Type } from '../core/domain/username.types'; type UsernameBaseModel = { username: string; authUuid: string; type: string; }; export type UsernameReadModel = UsernameBaseModel & { createdAt?: Date; updatedAt?: Date; }; export type UsernameWriteModel = UsernameBaseModel; /** * Repository is used for retrieving/saving domain entities * */ @Injectable() export class UsernameRepository extends PrismaRepositoryBase< UsernameEntity, UsernameReadModel, UsernameWriteModel > implements UsernameRepositoryPort { constructor( prisma: PrismaService, mapper: UsernameMapper, eventEmitter: EventEmitter2, @Inject(MESSAGE_PUBLISHER) protected readonly messagePublisher: MessagePublisherPort, ) { super( prisma.username, prisma, mapper, eventEmitter, new LoggerBase({ logger: new Logger(UsernameRepository.name), domain: 'auth.username', messagePublisher, }), ); } findByType = async (userId: string, type: Type): Promise => this.findOne({ authUuid: userId, type, }); findByName = async (name: string): Promise => this.findOne({ username: name, }); updateUsername = async ( oldName: string, entity: UsernameEntity, ): Promise => this.update(oldName, entity, 'username'); deleteUsername = async (entity: UsernameEntity): Promise => this.delete(entity, 'username'); countUsernames = async (userId: string): Promise => this.count({ authUuid: userId, }); }