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

41 lines
1.2 KiB
TypeScript
Raw Normal View History

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';
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,
@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);
this.messagePublisher.publish(
'user.delete',
JSON.stringify({ uuid: user.uuid }),
);
this.messagePublisher.publish(
'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) {
this.messagePublisher.publish(
'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
}