44 lines
1.4 KiB
TypeScript
44 lines
1.4 KiB
TypeScript
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';
|
|
import { USER_UPDATED_MESSAGE_HANDLER } from '@src/app.constants';
|
|
|
|
@Injectable()
|
|
export class UserUpdatedMessageHandler {
|
|
constructor(private readonly commandBus: CommandBus) {}
|
|
|
|
@RabbitSubscribe({
|
|
name: USER_UPDATED_MESSAGE_HANDLER,
|
|
})
|
|
public async userUpdated(message: string) {
|
|
const updatedUser = JSON.parse(message);
|
|
try {
|
|
if (!updatedUser.hasOwnProperty('id')) throw new Error();
|
|
if (updatedUser.hasOwnProperty('email') && updatedUser.email) {
|
|
await this.commandBus.execute(
|
|
new UpdateUsernameCommand({
|
|
userId: updatedUser.id,
|
|
username: {
|
|
name: updatedUser.email,
|
|
type: Type.EMAIL,
|
|
},
|
|
}),
|
|
);
|
|
}
|
|
if (updatedUser.hasOwnProperty('phone') && updatedUser.phone) {
|
|
await this.commandBus.execute(
|
|
new UpdateUsernameCommand({
|
|
userId: updatedUser.if,
|
|
username: {
|
|
name: updatedUser.phone,
|
|
type: Type.PHONE,
|
|
},
|
|
}),
|
|
);
|
|
}
|
|
} catch (e: any) {}
|
|
}
|
|
}
|