2023-06-02 13:56:23 +00:00
|
|
|
import { Inject, NotFoundException } from '@nestjs/common';
|
2022-12-13 17:00:07 +00:00
|
|
|
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';
|
2023-06-02 13:56:23 +00:00
|
|
|
import { MESSAGE_PUBLISHER } from '../../../../app.constants';
|
|
|
|
import { IPublishMessage } from '../../../../interfaces/message-publisher';
|
2022-12-13 17:00:07 +00:00
|
|
|
|
|
|
|
@QueryHandler(FindUserByUuidQuery)
|
|
|
|
export class FindUserByUuidUseCase {
|
2022-12-23 14:14:51 +00:00
|
|
|
constructor(
|
2023-05-05 14:56:30 +00:00
|
|
|
private readonly repository: UsersRepository,
|
2023-06-02 13:56:23 +00:00
|
|
|
@Inject(MESSAGE_PUBLISHER)
|
|
|
|
private readonly messagePublisher: IPublishMessage,
|
2022-12-23 14:14:51 +00:00
|
|
|
) {}
|
2022-12-13 17:00:07 +00:00
|
|
|
|
2023-05-05 14:56:30 +00:00
|
|
|
execute = async (findUserByUuid: FindUserByUuidQuery): Promise<User> => {
|
2022-12-23 14:14:51 +00:00
|
|
|
try {
|
2023-05-05 14:56:30 +00:00
|
|
|
const user = await this.repository.findOneByUuid(findUserByUuid.uuid);
|
2022-12-23 14:14:51 +00:00
|
|
|
if (!user) throw new NotFoundException();
|
|
|
|
return user;
|
|
|
|
} catch (error) {
|
2023-06-02 13:56:23 +00:00
|
|
|
this.messagePublisher.publish(
|
2023-03-15 16:01:23 +00:00
|
|
|
'logging.user.read.warning',
|
2022-12-23 14:14:51 +00:00
|
|
|
JSON.stringify({
|
|
|
|
query: findUserByUuid,
|
|
|
|
error,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
throw error;
|
|
|
|
}
|
2023-05-05 14:56:30 +00:00
|
|
|
};
|
2022-12-13 17:00:07 +00:00
|
|
|
}
|