Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 그룹 로거 구현 #50

Merged
merged 1 commit into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/app/domain/group/IGroupLogger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const GroupLogger = Symbol('GroupLogger');

Check warning on line 1 in src/app/domain/group/IGroupLogger.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

export interface IGroupLogger {
log: (groupId: string, message: string) => Promise<void>;
}
82 changes: 82 additions & 0 deletions src/app/infra/logger/GroupLogger.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { Test } from '@nestjs/testing';
import { advanceTo, clear } from 'jest-date-mock';
import { ClsService } from 'nestjs-cls';

import { IRequester } from '@sight/core/auth/IRequester';
import { UserRole } from '@sight/core/auth/UserRole';

import { GroupLogger } from '@sight/app/infra/logger/GroupLogger';

import { GroupLogFactory } from '@sight/app/domain/group/GroupLogFactory';
import {
GroupLogRepository,
IGroupLogRepository,
} from '@sight/app/domain/group/IGroupLogRepository';

import { generateEmptyProviders } from '@sight/__test__/util';

describe('GroupLogger', () => {
let groupLogger: GroupLogger;
let groupLogFactory: GroupLogFactory;
let groupLogRepository: jest.Mocked<IGroupLogRepository>;
let clsService: jest.Mocked<ClsService>;

beforeAll(async () => {
advanceTo(new Date());

const testModule = await Test.createTestingModule({
providers: [
GroupLogger,
GroupLogFactory,
...generateEmptyProviders(ClsService, GroupLogRepository),
],
}).compile();

groupLogger = testModule.get(GroupLogger);
groupLogFactory = testModule.get(GroupLogFactory);
groupLogRepository = testModule.get(GroupLogRepository);
clsService = testModule.get(ClsService);
});

afterAll(() => {
clear();
});

describe('log', () => {
let requester: IRequester;

const newLogId = 'newLogId';
const groupId = 'groupId';
const message = 'message';

beforeEach(() => {
requester = { userId: 'userId', role: UserRole.USER };

clsService.get = jest.fn().mockReturnValue(requester);
groupLogRepository.nextId = jest.fn().mockReturnValue(newLogId);

jest.spyOn(groupLogFactory, 'create');
groupLogRepository.save = jest.fn();
});

test('요청자 정보로 새로운 그룹 로그를 생성해야 한다', async () => {
await groupLogger.log(groupId, message);

expect(groupLogFactory.create).toBeCalledTimes(1);
});

test('의도대로 그룹 로그를 생성해서 저장해야 한다', async () => {
const expected = groupLogFactory.create({
id: newLogId,
groupId,
userId: requester.userId,
message,
});

await groupLogger.log(groupId, message);

expect(groupLogRepository.save).toBeCalledTimes(1);
expect(groupLogRepository.save).toBeCalledWith(expected);
});
});
});
33 changes: 33 additions & 0 deletions src/app/infra/logger/GroupLogger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Inject, Injectable } from '@nestjs/common';
import { ClsService } from 'nestjs-cls';

import { IRequester } from '@sight/core/auth/IRequester';

import { GroupLogFactory } from '@sight/app/domain/group/GroupLogFactory';
import { IGroupLogger } from '@sight/app/domain/group/IGroupLogger';
import {
GroupLogRepository,
IGroupLogRepository,
} from '@sight/app/domain/group/IGroupLogRepository';

@Injectable()
export class GroupLogger implements IGroupLogger {
constructor(
private readonly clsService: ClsService,

Check warning on line 16 in src/app/infra/logger/GroupLogger.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
private readonly groupLogFactory: GroupLogFactory,

Check warning on line 17 in src/app/infra/logger/GroupLogger.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
@Inject(GroupLogRepository)
private readonly groupLogRepository: IGroupLogRepository,

Check warning on line 19 in src/app/infra/logger/GroupLogger.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
) {}

async log(groupId: string, message: string): Promise<void> {
const requester: IRequester = this.clsService.get('requester');

const groupLog = this.groupLogFactory.create({
id: this.groupLogRepository.nextId(),
groupId,
userId: requester.userId,
message,
});
await this.groupLogRepository.save(groupLog);
}
}
2 changes: 1 addition & 1 deletion src/core/auth/AuthGuard.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Request } from 'express';
import { ClsService } from 'nestjs-cls';
import {
CanActivate,
ExecutionContext,
Expand All @@ -10,7 +11,6 @@ import {
import { ITokenVerifier, TokenVerifier } from '@sight/core/auth/ITokenVerifier';

import { Message } from '@sight/constant/message';
import { ClsService } from 'nestjs-cls';

@Injectable()
export class AuthGuard implements CanActivate {
Expand Down