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

54 lines
1.6 KiB
TypeScript

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';
import { UpdateUserRequest } from '../dtos/update-user.request';
import { User } from '../entities/user';
import { MESSAGE_PUBLISHER } from '../../../../app.constants';
import { Inject } from '@nestjs/common';
import { IPublishMessage } from '../../../../interfaces/message-publisher';
@CommandHandler(UpdateUserCommand)
export class UpdateUserUseCase {
constructor(
private readonly repository: UsersRepository,
@Inject(MESSAGE_PUBLISHER)
private readonly messagePublisher: IPublishMessage,
@InjectMapper() private readonly mapper: Mapper,
) {}
execute = async (command: UpdateUserCommand): Promise<User> => {
const entity = this.mapper.map(
command.updateUserRequest,
UpdateUserRequest,
User,
);
try {
const user = await this.repository.update(
command.updateUserRequest.uuid,
entity,
);
this.messagePublisher.publish(
'user.update',
JSON.stringify(command.updateUserRequest),
);
this.messagePublisher.publish(
'logging.user.update.info',
JSON.stringify(command.updateUserRequest),
);
return user;
} catch (error) {
this.messagePublisher.publish(
'logging.user.update.crit',
JSON.stringify({
command,
error,
}),
);
throw error;
}
};
}