user/.old/domain/usecases/find-user-by-uuid.usecase.ts

34 lines
1.1 KiB
TypeScript
Raw Normal View History

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';
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,
@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) {
this.messagePublisher.publish(
'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
}