import { Mapper } from '@automapper/core'; import { InjectMapper } from '@automapper/nestjs'; import { CommandHandler } from '@nestjs/cqrs'; import { LoggingMessager } from '../../adapters/secondaries/logging.messager'; import { UserMessager } from '../../adapters/secondaries/user.messager'; import { UsersRepository } from '../../adapters/secondaries/users.repository'; import { UpdateUserCommand } from '../../commands/update-user.command'; import { UpdateUserRequest } from '../dtos/update-user.request'; import { User } from '../entities/user'; @CommandHandler(UpdateUserCommand) export class UpdateUserUseCase { constructor( private readonly _repository: UsersRepository, private readonly _userMessager: UserMessager, private readonly _loggingMessager: LoggingMessager, @InjectMapper() private readonly _mapper: Mapper, ) {} async execute(command: UpdateUserCommand): Promise { const entity = this._mapper.map( command.updateUserRequest, UpdateUserRequest, User, ); try { const user = await this._repository.update( command.updateUserRequest.uuid, entity, ); this._userMessager.publish( 'update', JSON.stringify(command.updateUserRequest), ); this._loggingMessager.publish( 'user.update.info', JSON.stringify(command.updateUserRequest), ); return user; } catch (error) { this._loggingMessager.publish( 'user.update.critical', JSON.stringify({ command, error, }), ); throw error; } } }