34 lines
1.1 KiB
TypeScript
34 lines
1.1 KiB
TypeScript
import { Inject, NotFoundException } from '@nestjs/common';
|
|
import { QueryHandler } from '@nestjs/cqrs';
|
|
import { UsersRepository } from '../../adapters/secondaries/users.repository';
|
|
import { FindUserByUuidQuery } from '../../queries/find-user-by-uuid.query';
|
|
import { User } from '../entities/user';
|
|
import { MESSAGE_PUBLISHER } from '../../../../app.constants';
|
|
import { IPublishMessage } from '../../../../interfaces/message-publisher';
|
|
|
|
@QueryHandler(FindUserByUuidQuery)
|
|
export class FindUserByUuidUseCase {
|
|
constructor(
|
|
private readonly repository: UsersRepository,
|
|
@Inject(MESSAGE_PUBLISHER)
|
|
private readonly messagePublisher: IPublishMessage,
|
|
) {}
|
|
|
|
execute = async (findUserByUuid: FindUserByUuidQuery): Promise<User> => {
|
|
try {
|
|
const user = await this.repository.findOneByUuid(findUserByUuid.uuid);
|
|
if (!user) throw new NotFoundException();
|
|
return user;
|
|
} catch (error) {
|
|
this.messagePublisher.publish(
|
|
'logging.user.read.warning',
|
|
JSON.stringify({
|
|
query: findUserByUuid,
|
|
error,
|
|
}),
|
|
);
|
|
throw error;
|
|
}
|
|
};
|
|
}
|