Skip to content

Commit

Permalink
add logger to RocketChatUser and to RocketChat Services for delete me…
Browse files Browse the repository at this point in the history
…thods
  • Loading branch information
WojciechGrancow committed Jan 2, 2024
1 parent 37e31c7 commit 6f4f906
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Module } from '@nestjs/common';
import { LoggerModule } from '@src/core/logger';
import { RocketChatUserRepo } from './repo';
import { RocketChatUserService } from './service/rocket-chat-user.service';

@Module({
imports: [LoggerModule],
providers: [RocketChatUserRepo, RocketChatUserService],
exports: [RocketChatUserService],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { ObjectId } from '@mikro-orm/mongodb';
import { Test, TestingModule } from '@nestjs/testing';
import { setupEntities } from '@shared/testing';
import { LegacyLogger } from '@src/core/logger';
import { RocketChatUserService } from './rocket-chat-user.service';
import { RocketChatUserRepo } from '../repo';
import { rocketChatUserFactory } from '../domain/testing/rocket-chat-user.factory';
Expand All @@ -20,6 +21,10 @@ describe(RocketChatUserService.name, () => {
provide: RocketChatUserRepo,
useValue: createMock<RocketChatUserRepo>(),
},
{
provide: LegacyLogger,
useValue: createMock<LegacyLogger>(),
},
],
}).compile();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { Injectable } from '@nestjs/common';
import { EntityId } from '@shared/domain/types';
import { LegacyLogger } from '@src/core/logger';
import { RocketChatUser } from '../domain';
import { RocketChatUserRepo } from '../repo';

@Injectable()
export class RocketChatUserService {
constructor(private readonly rocketChatUserRepo: RocketChatUserRepo) {}
constructor(private readonly rocketChatUserRepo: RocketChatUserRepo, private readonly logger: LegacyLogger) {
this.logger.setContext(RocketChatUserService.name);
}

public async findByUserId(userId: EntityId): Promise<RocketChatUser> {
const user: RocketChatUser = await this.rocketChatUserRepo.findByUserId(userId);
Expand All @@ -14,6 +17,11 @@ export class RocketChatUserService {
}

public deleteByUserId(userId: EntityId): Promise<number> {
return this.rocketChatUserRepo.deleteByUserId(userId);
this.logger.log({ action: 'Deleting rocketChatUser ', userId });
const deletedRocketChatUser: Promise<number> = this.rocketChatUserRepo.deleteByUserId(userId);

this.logger.log({ action: 'Deleted rocketChatUser ', userId });

return deletedRocketChatUser;
}
}
3 changes: 2 additions & 1 deletion apps/server/src/modules/rocketchat/rocket-chat.module.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { HttpModule } from '@nestjs/axios';
import { DynamicModule, Module } from '@nestjs/common';
import { LoggerModule } from '@src/core/logger';
import { RocketChatOptions, RocketChatService } from './rocket-chat.service';

@Module({})
export class RocketChatModule {
static forRoot(options: RocketChatOptions): DynamicModule {
return {
module: RocketChatModule,
imports: [HttpModule],
imports: [HttpModule, LoggerModule],
providers: [
RocketChatService,
{
Expand Down
15 changes: 12 additions & 3 deletions apps/server/src/modules/rocketchat/rocket-chat.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* istanbul ignore file */
import { HttpService } from '@nestjs/axios';
import { Inject, Injectable } from '@nestjs/common';
import { LegacyLogger } from '@src/core/logger';
import { lastValueFrom } from 'rxjs';
import { catchError } from 'rxjs/operators';

Expand Down Expand Up @@ -73,8 +74,11 @@ export class RocketChatService {

constructor(
@Inject('ROCKET_CHAT_OPTIONS') private readonly options: RocketChatOptions,
private readonly httpService: HttpService
) {}
private readonly httpService: HttpService,
private readonly logger: LegacyLogger
) {
this.logger.setContext(RocketChatService.name);
}

public async me(authToken: string, userId: string): Promise<GenericData> {
return this.get('/api/v1/me', authToken, userId);
Expand Down Expand Up @@ -181,9 +185,14 @@ export class RocketChatService {
}

public async deleteUser(username: string): Promise<GenericData> {
return this.postAsAdmin('/api/v1/users.delete', {
this.logger.log({ action: 'Deleting user Data from RocketChat Service for user ', username });
const deletedUser: Promise<GenericData> = this.postAsAdmin('/api/v1/users.delete', {
username,
});

this.logger.log({ action: 'Deleting user Data from RocketChat Service for user ', username });

return deletedUser;
}

private async postAsAdmin(path: string, body: GenericData): Promise<GenericData> {
Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/modules/user/service/user.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import { UserRepo } from '@shared/repo';
import { UserDORepo } from '@shared/repo/user/user-do.repo';
import { roleFactory, setupEntities, userDoFactory, userFactory } from '@shared/testing';
import { ObjectId } from '@mikro-orm/mongodb';
import { LegacyLogger } from '@src/core/logger';
import { UserDto } from '../uc/dto/user.dto';
import { UserQuery } from './user-query.type';
import { UserService } from './user.service';
import { LegacyLogger } from '@src/core/logger';

describe('UserService', () => {
let service: UserService;
Expand Down

0 comments on commit 6f4f906

Please sign in to comment.