From 267a9d11229231e2a667af1d0fcd7342d3afce88 Mon Sep 17 00:00:00 2001 From: "Kim Hyeon Woo, Lery" Date: Mon, 15 Jan 2024 01:04:23 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EA=B7=B8=EB=A3=B9=20=EB=A1=9C?= =?UTF-8?q?=EA=B1=B0=EB=A5=BC=20=EC=82=AC=EC=9A=A9=ED=95=98=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EC=88=98=EC=A0=95=20(#51)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ChangeGroupStateCommandHandler.spec.ts | 24 +++++----------- .../ChangeGroupStateCommandHandler.ts | 21 ++++---------- .../GroupPortfolioEnabledHandler.ts | 28 ++++++++----------- .../eventHandler/GroupUpdatedHandler.spec.ts | 28 +++++++------------ .../group/eventHandler/GroupUpdatedHandler.ts | 22 ++++----------- src/app/infra/logger/GroupLogger.spec.ts | 8 +++--- src/app/infra/logger/GroupLogger.ts | 2 +- 7 files changed, 45 insertions(+), 88 deletions(-) diff --git a/src/app/application/group/command/changeGroupState/ChangeGroupStateCommandHandler.spec.ts b/src/app/application/group/command/changeGroupState/ChangeGroupStateCommandHandler.spec.ts index 1bc14c7..64e4339 100644 --- a/src/app/application/group/command/changeGroupState/ChangeGroupStateCommandHandler.spec.ts +++ b/src/app/application/group/command/changeGroupState/ChangeGroupStateCommandHandler.spec.ts @@ -5,13 +5,12 @@ import { ChangeGroupStateCommand } from '@sight/app/application/group/command/ch import { ChangeGroupStateCommandHandler } from '@sight/app/application/group/command/changeGroupState/ChangeGroupStateCommandHandler'; import { ChangeGroupStateCommandResult } from '@sight/app/application/group/command/changeGroupState/ChangeGroupStateCommandResult'; -import { GroupLogFactory } from '@sight/app/domain/group/GroupLogFactory'; import { GroupState } from '@sight/app/domain/group/model/constant'; import { Group } from '@sight/app/domain/group/model/Group'; import { - GroupLogRepository, - IGroupLogRepository, -} from '@sight/app/domain/group/IGroupLogRepository'; + GroupLogger, + IGroupLogger, +} from '@sight/app/domain/group/IGroupLogger'; import { GroupRepository, IGroupRepository, @@ -24,8 +23,7 @@ import { Message } from '@sight/constant/message'; describe('ChangeGroupStateCommandHandler', () => { let handler: ChangeGroupStateCommandHandler; let groupRepository: jest.Mocked; - let groupLogFactory: jest.Mocked; - let groupLogRepository: jest.Mocked; + let groupLogger: jest.Mocked; beforeAll(async () => { advanceTo(new Date()); @@ -33,18 +31,13 @@ describe('ChangeGroupStateCommandHandler', () => { const testModule = await Test.createTestingModule({ providers: [ ChangeGroupStateCommandHandler, - ...generateEmptyProviders( - GroupRepository, - GroupLogFactory, - GroupLogRepository, - ), + ...generateEmptyProviders(GroupRepository, GroupLogger), ], }).compile(); handler = testModule.get(ChangeGroupStateCommandHandler); groupRepository = testModule.get(GroupRepository); - groupLogFactory = testModule.get(GroupLogFactory); - groupLogRepository = testModule.get(GroupLogRepository); + groupLogger = testModule.get(GroupLogger); }); afterAll(() => { @@ -62,17 +55,14 @@ describe('ChangeGroupStateCommandHandler', () => { group = DomainFixture.generateGroup({ adminUserId: requesterUserId, }); - const log = DomainFixture.generateGroupLog(); group.isCustomerServiceGroup = jest.fn().mockReturnValue(false); group.isPracticeGroup = jest.fn().mockReturnValue(false); - groupLogFactory.create = jest.fn().mockReturnValue(log); groupRepository.findById = jest.fn().mockResolvedValue(group); - groupLogRepository.nextId = jest.fn().mockReturnValue('some-id'); group.changeState = jest.fn(); groupRepository.save = jest.fn(); - groupLogRepository.save = jest.fn(); + groupLogger.log = jest.fn(); }); test('그룹이 존재하지 않으면 예외를 발생시켜야 한다', async () => { diff --git a/src/app/application/group/command/changeGroupState/ChangeGroupStateCommandHandler.ts b/src/app/application/group/command/changeGroupState/ChangeGroupStateCommandHandler.ts index 8fccee5..f690c6e 100644 --- a/src/app/application/group/command/changeGroupState/ChangeGroupStateCommandHandler.ts +++ b/src/app/application/group/command/changeGroupState/ChangeGroupStateCommandHandler.ts @@ -11,12 +11,11 @@ import { Transactional } from '@sight/core/persistence/transaction/Transactional import { ChangeGroupStateCommand } from '@sight/app/application/group/command/changeGroupState/ChangeGroupStateCommand'; import { ChangeGroupStateCommandResult } from '@sight/app/application/group/command/changeGroupState/ChangeGroupStateCommandResult'; -import { GroupLogFactory } from '@sight/app/domain/group/GroupLogFactory'; import { GroupState } from '@sight/app/domain/group/model/constant'; import { - GroupLogRepository, - IGroupLogRepository, -} from '@sight/app/domain/group/IGroupLogRepository'; + GroupLogger, + IGroupLogger, +} from '@sight/app/domain/group/IGroupLogger'; import { GroupRepository, IGroupRepository, @@ -32,10 +31,8 @@ export class ChangeGroupStateCommandHandler constructor( @Inject(GroupRepository) private readonly groupRepository: IGroupRepository, - @Inject(GroupLogFactory) - private readonly groupLogFactory: GroupLogFactory, - @Inject(GroupLogRepository) - private readonly groupLogRepository: IGroupLogRepository, + @Inject(GroupLogger) + private readonly groupLogger: IGroupLogger, ) {} @Transactional() @@ -65,13 +62,7 @@ export class ChangeGroupStateCommandHandler group.wake(); await this.groupRepository.save(group); - const newGroupLog = this.groupLogFactory.create({ - id: this.groupLogRepository.nextId(), - groupId, - userId: requester.userId, - message: this.buildMessage(nextState), - }); - await this.groupLogRepository.save(newGroupLog); + await this.groupLogger.log(groupId, this.buildMessage(nextState)); return new ChangeGroupStateCommandResult(nextState); } diff --git a/src/app/application/group/eventHandler/GroupPortfolioEnabledHandler.ts b/src/app/application/group/eventHandler/GroupPortfolioEnabledHandler.ts index edc6fe6..e22a9ff 100644 --- a/src/app/application/group/eventHandler/GroupPortfolioEnabledHandler.ts +++ b/src/app/application/group/eventHandler/GroupPortfolioEnabledHandler.ts @@ -4,16 +4,15 @@ import { EventsHandler, IEventHandler } from '@nestjs/cqrs'; import { Transactional } from '@sight/core/persistence/transaction/Transactional'; import { GroupPortfolioEnabled } from '@sight/app/domain/group/event/GroupPortfolioEnabled'; -import { GroupLogFactory } from '@sight/app/domain/group/GroupLogFactory'; import { SlackMessageCategory } from '@sight/app/domain/message/model/constant'; import { ISlackSender, SlackSender, } from '@sight/app/domain/adapter/ISlackSender'; import { - GroupLogRepository, - IGroupLogRepository, -} from '@sight/app/domain/group/IGroupLogRepository'; + GroupLogger, + IGroupLogger, +} from '@sight/app/domain/group/IGroupLogger'; import { GroupMemberRepository, IGroupMemberRepository, @@ -28,6 +27,8 @@ import { } from '@sight/app/domain/user/IUserRepository'; import { Point } from '@sight/constant/point'; +import { ClsService } from 'nestjs-cls'; +import { IRequester } from '@sight/core/auth/IRequester'; @EventsHandler(GroupPortfolioEnabled) export class GroupPortfolioEnabledHandler @@ -38,14 +39,13 @@ export class GroupPortfolioEnabledHandler private readonly groupRepository: IGroupRepository, @Inject(GroupMemberRepository) private readonly groupMemberRepository: IGroupMemberRepository, - @Inject(GroupLogFactory) - private readonly groupLogFactory: GroupLogFactory, - @Inject(GroupLogRepository) - private readonly groupLogRepository: IGroupLogRepository, + @Inject(GroupLogger) + private readonly groupLogger: IGroupLogger, @Inject(UserRepository) private readonly userRepository: IUserRepository, @Inject(SlackSender) private readonly slackSender: ISlackSender, + private readonly clsService: ClsService, ) {} @Transactional() @@ -70,20 +70,14 @@ export class GroupPortfolioEnabledHandler ); await this.userRepository.save(...users); - // TODO: 그룹 로거를 만들어서 사용하도록 수정 - const groupLog = this.groupLogFactory.create({ - id: this.groupLogRepository.nextId(), - groupId, - userId: '', // TODO: 요청자 정보에 접근할 수 있을 때 수정 - message: '포트폴리오가 발행 중입니다.', - }); - await this.groupLogRepository.save(groupLog); + await this.groupLogger.log(groupId, '포트폴리오가 발행 중입니다.'); + const requester: IRequester = this.clsService.get('requester'); this.slackSender.send({ category: SlackMessageCategory.GROUP_ACTIVITY, message: `${group.title} 그룹의 포트폴리오가 발행 중입니다.`, sourceUserId: null, - targetUserId: '', // TODO: 요청자 정보에 접근할 수 있을 때 수정 + targetUserId: requester.userId, // TODO: 요청자 정보에 접근할 수 있을 때 수정 }); } } diff --git a/src/app/application/group/eventHandler/GroupUpdatedHandler.spec.ts b/src/app/application/group/eventHandler/GroupUpdatedHandler.spec.ts index f130854..3fd018f 100644 --- a/src/app/application/group/eventHandler/GroupUpdatedHandler.spec.ts +++ b/src/app/application/group/eventHandler/GroupUpdatedHandler.spec.ts @@ -3,15 +3,10 @@ import { advanceTo, clear } from 'jest-date-mock'; import { GroupUpdatedHandler } from '@sight/app/application/group/eventHandler/GroupUpdatedHandler'; -import { GroupLogFactory } from '@sight/app/domain/group/GroupLogFactory'; import { ISlackSender, SlackSender, } from '@sight/app/domain/adapter/ISlackSender'; -import { - GroupLogRepository, - IGroupLogRepository, -} from '@sight/app/domain/group/IGroupLogRepository'; import { GroupMemberRepository, IGroupMemberRepository, @@ -32,11 +27,14 @@ import { } from '@sight/app/domain/group/event/GroupUpdated'; import { DomainFixture } from '@sight/__test__/fixtures'; import { GroupMember } from '@sight/app/domain/group/model/GroupMember'; +import { + GroupLogger, + IGroupLogger, +} from '@sight/app/domain/group/IGroupLogger'; describe('GroupUpdatedHandler', () => { let handler: GroupUpdatedHandler; - let groupLogFactory: jest.Mocked; - let groupLogRepository: jest.Mocked; + let groupLogger: jest.Mocked; let groupRepository: jest.Mocked; let groupMemberRepository: jest.Mocked; let messageBuilder: jest.Mocked; @@ -49,8 +47,7 @@ describe('GroupUpdatedHandler', () => { providers: [ GroupUpdatedHandler, ...generateEmptyProviders( - GroupLogFactory, - GroupLogRepository, + GroupLogger, GroupRepository, GroupMemberRepository, GroupUpdatedMessageBuilder, @@ -60,8 +57,7 @@ describe('GroupUpdatedHandler', () => { }).compile(); handler = testModule.get(GroupUpdatedHandler); - groupLogFactory = testModule.get(GroupLogFactory); - groupLogRepository = testModule.get(GroupLogRepository); + groupLogger = testModule.get(GroupLogger); groupRepository = testModule.get(GroupRepository); groupMemberRepository = testModule.get(GroupMemberRepository); messageBuilder = testModule.get(GroupUpdatedMessageBuilder); @@ -91,17 +87,14 @@ describe('GroupUpdatedHandler', () => { id: groupId, adminUserId: groupAdminUserId, }); - const groupLog = DomainFixture.generateGroupLog({ groupId }); groupRepository.findById = jest.fn().mockResolvedValue(group); messageBuilder.build = jest.fn().mockReturnValue(message); - groupLogFactory.create = jest.fn().mockReturnValue(groupLog); - groupLogRepository.nextId = jest.fn().mockReturnValue(groupLog.id); groupMemberRepository.findByGroupId = jest .fn() .mockResolvedValue(groupMembers); - groupLogRepository.save = jest.fn(); + groupLogger.log = jest.fn(); slackSender.send = jest.fn(); }); @@ -110,14 +103,13 @@ describe('GroupUpdatedHandler', () => { await handler.handle(event); - expect(groupLogRepository.save).not.toBeCalled(); + expect(groupLogger.log).not.toBeCalled(); }); test('로그를 생성하여 저장해야 한다', async () => { await handler.handle(event); - expect(groupLogFactory.create).toBeCalledTimes(1); - expect(groupLogRepository.save).toBeCalledTimes(1); + expect(groupLogger.log).toBeCalledTimes(1); }); test('모든 그룹 멤버들에게 메시지를 보내야 한다', async () => { diff --git a/src/app/application/group/eventHandler/GroupUpdatedHandler.ts b/src/app/application/group/eventHandler/GroupUpdatedHandler.ts index 9260452..f99bd8a 100644 --- a/src/app/application/group/eventHandler/GroupUpdatedHandler.ts +++ b/src/app/application/group/eventHandler/GroupUpdatedHandler.ts @@ -4,16 +4,15 @@ import { EventsHandler, IEventHandler } from '@nestjs/cqrs'; import { Transactional } from '@sight/core/persistence/transaction/Transactional'; import { GroupUpdated } from '@sight/app/domain/group/event/GroupUpdated'; -import { GroupLogFactory } from '@sight/app/domain/group/GroupLogFactory'; import { SlackMessageCategory } from '@sight/app/domain/message/model/constant'; import { ISlackSender, SlackSender, } from '@sight/app/domain/adapter/ISlackSender'; import { - GroupLogRepository, - IGroupLogRepository, -} from '@sight/app/domain/group/IGroupLogRepository'; + GroupLogger, + IGroupLogger, +} from '@sight/app/domain/group/IGroupLogger'; import { GroupMemberRepository, IGroupMemberRepository, @@ -30,10 +29,8 @@ import { @EventsHandler(GroupUpdated) export class GroupUpdatedHandler implements IEventHandler { constructor( - @Inject(GroupLogFactory) - private readonly groupLogFactory: GroupLogFactory, - @Inject(GroupLogRepository) - private readonly groupLogRepository: IGroupLogRepository, + @Inject(GroupLogger) + private readonly groupLogger: IGroupLogger, @Inject(GroupRepository) private readonly groupRepository: IGroupRepository, @Inject(GroupMemberRepository) @@ -54,14 +51,7 @@ export class GroupUpdatedHandler implements IEventHandler { } const message = this.messageBuilder.build(updatedItem); - - const newGroupLog = this.groupLogFactory.create({ - id: this.groupLogRepository.nextId(), - groupId, - userId: group.adminUserId, - message, - }); - await this.groupLogRepository.save(newGroupLog); + await this.groupLogger.log(groupId, message); const members = await this.groupMemberRepository.findByGroupId(groupId); members.forEach((member) => diff --git a/src/app/infra/logger/GroupLogger.spec.ts b/src/app/infra/logger/GroupLogger.spec.ts index 39d0b79..d33d84d 100644 --- a/src/app/infra/logger/GroupLogger.spec.ts +++ b/src/app/infra/logger/GroupLogger.spec.ts @@ -5,7 +5,7 @@ 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 { GroupLoggerImpl } from '@sight/app/infra/logger/GroupLogger'; import { GroupLogFactory } from '@sight/app/domain/group/GroupLogFactory'; import { @@ -16,7 +16,7 @@ import { import { generateEmptyProviders } from '@sight/__test__/util'; describe('GroupLogger', () => { - let groupLogger: GroupLogger; + let groupLogger: GroupLoggerImpl; let groupLogFactory: GroupLogFactory; let groupLogRepository: jest.Mocked; let clsService: jest.Mocked; @@ -26,13 +26,13 @@ describe('GroupLogger', () => { const testModule = await Test.createTestingModule({ providers: [ - GroupLogger, + GroupLoggerImpl, GroupLogFactory, ...generateEmptyProviders(ClsService, GroupLogRepository), ], }).compile(); - groupLogger = testModule.get(GroupLogger); + groupLogger = testModule.get(GroupLoggerImpl); groupLogFactory = testModule.get(GroupLogFactory); groupLogRepository = testModule.get(GroupLogRepository); clsService = testModule.get(ClsService); diff --git a/src/app/infra/logger/GroupLogger.ts b/src/app/infra/logger/GroupLogger.ts index 9220838..bc787e2 100644 --- a/src/app/infra/logger/GroupLogger.ts +++ b/src/app/infra/logger/GroupLogger.ts @@ -11,7 +11,7 @@ import { } from '@sight/app/domain/group/IGroupLogRepository'; @Injectable() -export class GroupLogger implements IGroupLogger { +export class GroupLoggerImpl implements IGroupLogger { constructor( private readonly clsService: ClsService, private readonly groupLogFactory: GroupLogFactory,