user updated message handler

This commit is contained in:
sbriat
2023-07-11 15:16:11 +02:00
parent 487ae9c38e
commit 85746fde48
10 changed files with 196 additions and 23 deletions

View File

@@ -0,0 +1,42 @@
import { Injectable } from '@nestjs/common';
import { CommandBus } from '@nestjs/cqrs';
import { RabbitSubscribe } from '@mobicoop/message-broker-module';
import { Type } from '@modules/authentication/core/domain/username.types';
import { UpdateUsernameCommand } from '@modules/authentication/core/application/commands/update-username/update-username.command';
@Injectable()
export class UserUpdatedMessageHandler {
constructor(private readonly commandBus: CommandBus) {}
@RabbitSubscribe({
name: 'userUpdated',
})
public async userUpdated(message: string) {
const updatedUser = JSON.parse(message);
try {
if (!updatedUser.hasOwnProperty('userId')) throw new Error();
if (updatedUser.hasOwnProperty('email') && updatedUser.email) {
await this.commandBus.execute(
new UpdateUsernameCommand({
userId: updatedUser.userId,
username: {
name: updatedUser.email,
type: Type.EMAIL,
},
}),
);
}
if (updatedUser.hasOwnProperty('phone') && updatedUser.phone) {
await this.commandBus.execute(
new UpdateUsernameCommand({
userId: updatedUser.userId,
username: {
name: updatedUser.phone,
type: Type.PHONE,
},
}),
);
}
} catch (e: any) {}
}
}