import { CommandHandler } from '@nestjs/cqrs'; import { UsersRepository } from '../../adapters/secondaries/users.repository'; import { DeleteUserCommand } from '../../commands/delete-user.command'; import { User } from '../entities/user'; import { IPublishMessage } from '../../../../interfaces/message-publisher'; import { Inject } from '@nestjs/common'; import { MESSAGE_PUBLISHER } from '../../../../app.constants'; @CommandHandler(DeleteUserCommand) export class DeleteUserUseCase { constructor( private readonly repository: UsersRepository, @Inject(MESSAGE_PUBLISHER) private readonly messagePublisher: IPublishMessage, ) {} execute = async (command: DeleteUserCommand): Promise => { try { const user = await this.repository.delete(command.uuid); this.messagePublisher.publish( 'user.delete', JSON.stringify({ uuid: user.uuid }), ); this.messagePublisher.publish( 'logging.user.delete.info', JSON.stringify({ uuid: user.uuid }), ); return user; } catch (error) { this.messagePublisher.publish( 'logging.user.delete.crit', JSON.stringify({ command, error, }), ); throw error; } }; }