Skip to content

Commit

Permalink
improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Oct 18, 2024
1 parent b608dfa commit ca98935
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 37 deletions.
4 changes: 3 additions & 1 deletion api/src/modules/admin/admin.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
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';
import { PasswordRecoveryRequestedEventHandler } from '@api/modules/admin/events/handlers/password-recovery-requested.handler';

@Module({
imports: [AuthModule, ImportModule],
providers: [NewUserEventHandler, PasswordRecoveryRequestedEventHandler],
controllers: [AdminController],
})
export class AdminModule {}
47 changes: 47 additions & 0 deletions api/src/modules/admin/events/handlers/new-user-event.handler.ts
Original file line number Diff line number Diff line change
@@ -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<NewUserEvent> {
constructor(private readonly apiEventsService: ApiEventsService) {}

async handle(event: NewUserEvent): Promise<void> {
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}`,
);
}
}
27 changes: 0 additions & 27 deletions api/src/modules/admin/events/handlers/user-signed-up.handler.ts

This file was deleted.

11 changes: 11 additions & 0 deletions api/src/modules/admin/events/new-user.event.ts
Original file line number Diff line number Diff line change
@@ -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,
) {}
}
6 changes: 0 additions & 6 deletions api/src/modules/admin/events/user-signed-up.event.ts

This file was deleted.

3 changes: 2 additions & 1 deletion api/src/modules/api-events/api-events.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -12,6 +12,7 @@ type CreateApiEvent = {

@Injectable()
export class ApiEventsService {
logger: Logger = new Logger(ApiEventsService.name);
constructor(
@InjectRepository(ApiEventsEntity)
private readonly apiEventsRepository: Repository<ApiEventsEntity>,
Expand Down
10 changes: 8 additions & 2 deletions api/src/modules/auth/authentication.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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<boolean> {
Expand Down

0 comments on commit ca98935

Please sign in to comment.