auth/src/modules/authentication/infrastructure/username.repository.ts

84 lines
2.1 KiB
TypeScript

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<UsernameEntity> =>
this.findOne({
authUuid: userId,
type,
});
findByName = async (name: string): Promise<UsernameEntity> =>
this.findOne({
username: name,
});
updateUsername = async (
oldName: string,
entity: UsernameEntity,
): Promise<void> => this.update(oldName, entity, 'username');
deleteUsername = async (entity: UsernameEntity): Promise<boolean> =>
this.delete(entity, 'username');
countUsernames = async (userId: string): Promise<number> =>
this.count({
authUuid: userId,
});
}