user/.old/domain/usecases/update-user.usecase.ts

54 lines
1.6 KiB
TypeScript
Raw Normal View History

2022-12-14 10:37:13 +00:00
import { Mapper } from '@automapper/core';
import { InjectMapper } from '@automapper/nestjs';
import { CommandHandler } from '@nestjs/cqrs';
import { UsersRepository } from '../../adapters/secondaries/users.repository';
import { UpdateUserCommand } from '../../commands/update-user.command';
2022-12-16 16:31:08 +00:00
import { UpdateUserRequest } from '../dtos/update-user.request';
2022-12-14 10:37:13 +00:00
import { User } from '../entities/user';
import { MESSAGE_PUBLISHER } from '../../../../app.constants';
import { Inject } from '@nestjs/common';
import { IPublishMessage } from '../../../../interfaces/message-publisher';
2022-12-14 10:37:13 +00:00
@CommandHandler(UpdateUserCommand)
export class UpdateUserUseCase {
constructor(
2023-05-05 14:56:30 +00:00
private readonly repository: UsersRepository,
@Inject(MESSAGE_PUBLISHER)
private readonly messagePublisher: IPublishMessage,
2023-05-05 14:56:30 +00:00
@InjectMapper() private readonly mapper: Mapper,
2022-12-14 10:37:13 +00:00
) {}
2023-05-05 14:56:30 +00:00
execute = async (command: UpdateUserCommand): Promise<User> => {
const entity = this.mapper.map(
2022-12-14 10:37:13 +00:00
command.updateUserRequest,
UpdateUserRequest,
User,
);
2022-12-23 14:14:51 +00:00
try {
2023-05-05 14:56:30 +00:00
const user = await this.repository.update(
2022-12-23 14:14:51 +00:00
command.updateUserRequest.uuid,
entity,
);
this.messagePublisher.publish(
'user.update',
2022-12-22 13:24:51 +00:00
JSON.stringify(command.updateUserRequest),
);
this.messagePublisher.publish(
'logging.user.update.info',
2022-12-23 14:14:51 +00:00
JSON.stringify(command.updateUserRequest),
);
return user;
} catch (error) {
this.messagePublisher.publish(
'logging.user.update.crit',
2022-12-23 14:14:51 +00:00
JSON.stringify({
command,
error,
}),
);
throw error;
2022-12-22 13:24:51 +00:00
}
2023-05-05 14:56:30 +00:00
};
2022-12-14 10:37:13 +00:00
}