2022-12-14 11:49:43 +00:00
|
|
|
import { CommandHandler } from '@nestjs/cqrs';
|
|
|
|
import { UsersRepository } from '../../adapters/secondaries/users.repository';
|
|
|
|
import { DeleteUserCommand } from '../../commands/delete-user.command';
|
2022-12-22 13:24:51 +00:00
|
|
|
import { User } from '../entities/user';
|
2023-06-02 13:56:23 +00:00
|
|
|
import { IPublishMessage } from '../../../../interfaces/message-publisher';
|
|
|
|
import { Inject } from '@nestjs/common';
|
|
|
|
import { MESSAGE_PUBLISHER } from '../../../../app.constants';
|
2022-12-14 11:49:43 +00:00
|
|
|
|
|
|
|
@CommandHandler(DeleteUserCommand)
|
|
|
|
export class DeleteUserUseCase {
|
2022-12-22 13:24: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-22 13:24:51 +00:00
|
|
|
) {}
|
2022-12-14 11:49:43 +00:00
|
|
|
|
2023-05-05 14:56:30 +00:00
|
|
|
execute = async (command: DeleteUserCommand): Promise<User> => {
|
2022-12-23 14:14:51 +00:00
|
|
|
try {
|
2023-05-05 14:56:30 +00:00
|
|
|
const user = await this.repository.delete(command.uuid);
|
2023-06-02 13:56:23 +00:00
|
|
|
this.messagePublisher.publish(
|
|
|
|
'user.delete',
|
|
|
|
JSON.stringify({ uuid: user.uuid }),
|
|
|
|
);
|
|
|
|
this.messagePublisher.publish(
|
2023-03-15 16:01:23 +00:00
|
|
|
'logging.user.delete.info',
|
2022-12-22 13:24:51 +00:00
|
|
|
JSON.stringify({ uuid: user.uuid }),
|
|
|
|
);
|
2022-12-23 14:14:51 +00:00
|
|
|
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.delete.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 11:49:43 +00:00
|
|
|
}
|