mirror of
https://gitlab.com/mobicoop/v3/service/auth.git
synced 2026-04-05 05:00:21 +00:00
user updated message handler
This commit is contained in:
@@ -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) {}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user