mirror of
https://gitlab.com/mobicoop/v3/service/user.git
synced 2026-01-10 09:52:40 +00:00
send messages on CUD
This commit is contained in:
12
src/modules/users/domain/interfaces/user-messager.ts
Normal file
12
src/modules/users/domain/interfaces/user-messager.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
@Injectable()
|
||||
export abstract class IMessageUser {
|
||||
exchange: string;
|
||||
|
||||
constructor(exchange: string) {
|
||||
this.exchange = exchange;
|
||||
}
|
||||
|
||||
abstract publish(routingKey: string, message: string): void;
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Mapper } from '@automapper/core';
|
||||
import { InjectMapper } from '@automapper/nestjs';
|
||||
import { CommandHandler } from '@nestjs/cqrs';
|
||||
import { UserMessager } from '../../adapters/secondaries/user.messager';
|
||||
import { UsersRepository } from '../../adapters/secondaries/users.repository';
|
||||
import { CreateUserCommand } from '../../commands/create-user.command';
|
||||
import { CreateUserRequest } from '../dtos/create-user.request';
|
||||
@@ -10,6 +11,7 @@ import { User } from '../entities/user';
|
||||
export class CreateUserUseCase {
|
||||
constructor(
|
||||
private readonly _repository: UsersRepository,
|
||||
private readonly _messager: UserMessager,
|
||||
@InjectMapper() private readonly _mapper: Mapper,
|
||||
) {}
|
||||
|
||||
@@ -20,6 +22,12 @@ export class CreateUserUseCase {
|
||||
User,
|
||||
);
|
||||
|
||||
return this._repository.create(entity);
|
||||
const user = await this._repository.create(entity);
|
||||
|
||||
if (user) {
|
||||
this._messager.publish('user.create', JSON.stringify(user));
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,26 @@
|
||||
import { CommandHandler } from '@nestjs/cqrs';
|
||||
import { UserMessager } from '../../adapters/secondaries/user.messager';
|
||||
import { UsersRepository } from '../../adapters/secondaries/users.repository';
|
||||
import { DeleteUserCommand } from '../../commands/delete-user.command';
|
||||
import { User } from '../entities/user';
|
||||
|
||||
@CommandHandler(DeleteUserCommand)
|
||||
export class DeleteUserUseCase {
|
||||
constructor(private readonly _repository: UsersRepository) {}
|
||||
constructor(
|
||||
private readonly _repository: UsersRepository,
|
||||
private readonly _messager: UserMessager,
|
||||
) {}
|
||||
|
||||
async execute(command: DeleteUserCommand): Promise<void> {
|
||||
return this._repository.delete(command.uuid);
|
||||
async execute(command: DeleteUserCommand): Promise<User> {
|
||||
const user = await this._repository.delete(command.uuid);
|
||||
|
||||
if (user) {
|
||||
this._messager.publish(
|
||||
'user.delete',
|
||||
JSON.stringify({ uuid: user.uuid }),
|
||||
);
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Mapper } from '@automapper/core';
|
||||
import { InjectMapper } from '@automapper/nestjs';
|
||||
import { CommandHandler } from '@nestjs/cqrs';
|
||||
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';
|
||||
@@ -10,6 +11,7 @@ import { User } from '../entities/user';
|
||||
export class UpdateUserUseCase {
|
||||
constructor(
|
||||
private readonly _repository: UsersRepository,
|
||||
private readonly _messager: UserMessager,
|
||||
@InjectMapper() private readonly _mapper: Mapper,
|
||||
) {}
|
||||
|
||||
@@ -20,6 +22,17 @@ export class UpdateUserUseCase {
|
||||
User,
|
||||
);
|
||||
|
||||
return this._repository.update(command.updateUserRequest.uuid, entity);
|
||||
const user = await this._repository.update(
|
||||
command.updateUserRequest.uuid,
|
||||
entity,
|
||||
);
|
||||
|
||||
if (user) {
|
||||
this._messager.publish(
|
||||
'user.update',
|
||||
JSON.stringify(command.updateUserRequest),
|
||||
);
|
||||
}
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user