From 1314e9b5baf3818206d3bceee20992a406a68613 Mon Sep 17 00:00:00 2001 From: alexeh Date: Fri, 18 Oct 2024 05:54:41 +0200 Subject: [PATCH] improve logging --- api/src/modules/admin/admin.module.ts | 3 +- .../events/handlers/new-user-event.handler.ts | 47 +++++++++++++++++++ .../events/handlers/user-signed-up.handler.ts | 27 ----------- .../modules/admin/events/new-user.event.ts | 11 +++++ .../admin/events/user-signed-up.event.ts | 6 --- .../modules/api-events/api-events.service.ts | 3 +- .../modules/auth/authentication.service.ts | 10 +++- 7 files changed, 70 insertions(+), 37 deletions(-) create mode 100644 api/src/modules/admin/events/handlers/new-user-event.handler.ts delete mode 100644 api/src/modules/admin/events/handlers/user-signed-up.handler.ts create mode 100644 api/src/modules/admin/events/new-user.event.ts delete mode 100644 api/src/modules/admin/events/user-signed-up.event.ts diff --git a/api/src/modules/admin/admin.module.ts b/api/src/modules/admin/admin.module.ts index 60d0e54e..67167877 100644 --- a/api/src/modules/admin/admin.module.ts +++ b/api/src/modules/admin/admin.module.ts @@ -1,11 +1,12 @@ import { Module } from '@nestjs/common'; import { AdminController } from './admin.controller'; import { AuthModule } from '@api/modules/auth/auth.module'; - import { ImportModule } from '@api/modules/import/import.module'; +import { NewUserEventHandler } from '@api/modules/admin/events/handlers/new-user-event.handler'; @Module({ imports: [AuthModule, ImportModule], + providers: [NewUserEventHandler], controllers: [AdminController], }) export class AdminModule {} diff --git a/api/src/modules/admin/events/handlers/new-user-event.handler.ts b/api/src/modules/admin/events/handlers/new-user-event.handler.ts new file mode 100644 index 00000000..23eae416 --- /dev/null +++ b/api/src/modules/admin/events/handlers/new-user-event.handler.ts @@ -0,0 +1,47 @@ +import { EventsHandler, IEventHandler } from '@nestjs/cqrs'; +import { NewUserEvent } from '../new-user.event'; +import { ApiEventsService } from '@api/modules/api-events/api-events.service'; +import { API_EVENT_TYPES } from '@api/modules/api-events/events.enum'; + +@EventsHandler(NewUserEvent) +export class NewUserEventHandler implements IEventHandler { + constructor(private readonly apiEventsService: ApiEventsService) {} + + async handle(event: NewUserEvent): Promise { + const { userId, email, type: eventType } = event; + if (eventType === API_EVENT_TYPES.USER_CREATED) { + await this.registerUserCreatedEvent(userId, { email }); + } + if (eventType === API_EVENT_TYPES.USER_SIGNED_UP) { + await this.registerUserSignedUpEvent(userId, { email }); + } + } + + private async registerUserCreatedEvent( + userId: string, + payload: { email: string }, + ) { + await this.apiEventsService.create({ + eventType: API_EVENT_TYPES.USER_CREATED, + resourceId: userId, + payload, + }); + this.apiEventsService.logger.warn( + `New user added to the system with email: ${payload.email}`, + ); + } + + private async registerUserSignedUpEvent( + userId: string, + payload: { email: string }, + ) { + await this.apiEventsService.create({ + eventType: API_EVENT_TYPES.USER_SIGNED_UP, + resourceId: userId, + payload, + }); + this.apiEventsService.logger.warn( + `New user signed up to the system with email: ${payload.email}`, + ); + } +} diff --git a/api/src/modules/admin/events/handlers/user-signed-up.handler.ts b/api/src/modules/admin/events/handlers/user-signed-up.handler.ts deleted file mode 100644 index 92f3856d..00000000 --- a/api/src/modules/admin/events/handlers/user-signed-up.handler.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { EventsHandler, IEventHandler } from '@nestjs/cqrs'; -import { UserSignedUpEvent } from '../user-signed-up.event'; -import { ApiEventsService } from '@api/modules/api-events/api-events.service'; -import { API_EVENT_TYPES } from '@api/modules/api-events/events.enum'; - -@EventsHandler(UserSignedUpEvent) -export class UserSignedUpEventHandler - implements IEventHandler -{ - constructor(private readonly apiEventsService: ApiEventsService) {} - - async handle(event: UserSignedUpEvent): Promise { - const { userId, email } = event; - await this.registerUserSignedUpEvent(userId, { email }); - } - - private async registerUserSignedUpEvent( - userId: string, - payload: { email: string }, - ) { - await this.apiEventsService.create({ - eventType: API_EVENT_TYPES.USER_SIGNED_UP, - resourceId: userId, - payload, - }); - } -} diff --git a/api/src/modules/admin/events/new-user.event.ts b/api/src/modules/admin/events/new-user.event.ts new file mode 100644 index 00000000..168a3d15 --- /dev/null +++ b/api/src/modules/admin/events/new-user.event.ts @@ -0,0 +1,11 @@ +import { API_EVENT_TYPES } from '@api/modules/api-events/events.enum'; + +export class NewUserEvent { + constructor( + public readonly userId: string, + public readonly email: string, + public readonly type: + | API_EVENT_TYPES.USER_CREATED + | API_EVENT_TYPES.USER_SIGNED_UP, + ) {} +} diff --git a/api/src/modules/admin/events/user-signed-up.event.ts b/api/src/modules/admin/events/user-signed-up.event.ts deleted file mode 100644 index 4991c392..00000000 --- a/api/src/modules/admin/events/user-signed-up.event.ts +++ /dev/null @@ -1,6 +0,0 @@ -export class UserSignedUpEvent { - constructor( - public readonly userId: string, - public readonly email: string, - ) {} -} diff --git a/api/src/modules/api-events/api-events.service.ts b/api/src/modules/api-events/api-events.service.ts index efaee678..c8b48796 100644 --- a/api/src/modules/api-events/api-events.service.ts +++ b/api/src/modules/api-events/api-events.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@nestjs/common'; +import { Injectable, Logger } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { ApiEventsEntity } from './api-events.entity'; @@ -12,6 +12,7 @@ type CreateApiEvent = { @Injectable() export class ApiEventsService { + logger: Logger = new Logger(ApiEventsService.name); constructor( @InjectRepository(ApiEventsEntity) private readonly apiEventsRepository: Repository, diff --git a/api/src/modules/auth/authentication.service.ts b/api/src/modules/auth/authentication.service.ts index ac30d85b..ac38723b 100644 --- a/api/src/modules/auth/authentication.service.ts +++ b/api/src/modules/auth/authentication.service.ts @@ -15,11 +15,12 @@ import { randomBytes } from 'node:crypto'; import { SendWelcomeEmailCommand } from '@api/modules/notifications/email/commands/send-welcome-email.command'; import { JwtManager } from '@api/modules/auth/services/jwt.manager'; import { SignUpDto } from '@shared/schemas/auth/sign-up.schema'; -import { UserSignedUpEvent } from '@api/modules/admin/events/user-signed-up.event'; +import { NewUserEvent } from '@api/modules/admin/events/new-user.event'; import { UpdateUserPasswordDto } from '@shared/dtos/users/update-user-password.dto'; import { RequestEmailUpdateDto } from '@shared/dtos/users/request-email-update.dto'; import { SendEmailConfirmationEmailCommand } from '@api/modules/notifications/email/commands/send-email-confirmation-email.command'; import { PasswordManager } from '@api/modules/auth/services/password.manager'; +import { API_EVENT_TYPES } from '@api/modules/api-events/events.enum'; @Injectable() export class AuthenticationService { @@ -70,6 +71,9 @@ export class AuthenticationService { partnerName, isActive: false, }); + this.eventBus.publish( + new NewUserEvent(newUser.id, newUser.email, API_EVENT_TYPES.USER_CREATED), + ); return { newUser, plainTextPassword, @@ -89,7 +93,9 @@ export class AuthenticationService { user.isActive = true; user.password = await this.passwordManager.hashPassword(newPassword); await this.usersService.saveUser(user); - this.eventBus.publish(new UserSignedUpEvent(user.id, user.email)); + this.eventBus.publish( + new NewUserEvent(user.id, user.email, API_EVENT_TYPES.USER_SIGNED_UP), + ); } async verifyToken(token: string, type: TOKEN_TYPE_ENUM): Promise {