Skip to content

Commit

Permalink
feat: 그룹 즐겨찾기 해제 기능 구현 (#56)
Browse files Browse the repository at this point in the history
* fix: 잘못된 이벤트 파라미터 수정

* feat: 그룹 즐겨찾기 해제 기능 구현
  • Loading branch information
Coalery authored Jan 28, 2024
1 parent 66be5ef commit 617d315
Show file tree
Hide file tree
Showing 14 changed files with 342 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,16 @@ describe('AddBookmarkCommandHandler', () => {
).rejects.toThrowError(Message.DEFAULT_BOOKMARKED_GROUP);
});

test('이미 즐겨찾기 중이라면 예외가 발생해야 한다', async () => {
test('이미 즐겨찾기 중이라면 새로운 즐겨찾기를 생성하지 않아야 한다', async () => {
const bookmark = DomainFixture.generateGroupBookmark();
groupBookmarkRepository.findByGroupIdAndUserId = jest
.fn()
.mockResolvedValue(bookmark);
jest.spyOn(groupBookmarkFactory, 'create');

await expect(
handler.execute(new AddBookmarkCommand(groupId, userId)),
).rejects.toThrowError(Message.ALREADY_BOOKMARKED_GROUP);
await handler.execute(new AddBookmarkCommand(groupId, userId));

expect(groupBookmarkFactory.create).not.toBeCalled();
});

test('즐겨찾기를 생성해야 한다', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class AddBookmarkCommandHandler
userId,
);
if (prevBookmark) {
throw new UnprocessableEntityException(Message.ALREADY_BOOKMARKED_GROUP);
return;
}

const newBookmark = this.groupBookmarkFactory.create({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class RemoveBookmarkCommand {
constructor(
readonly groupId: string,
readonly userId: string,
) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { Test } from '@nestjs/testing';
import { advanceTo, clear } from 'jest-date-mock';

import { AddBookmarkCommand } from '@sight/app/application/group/command/addBookmark/AddBookmarkCommand';
import { RemoveBookmarkCommandHandler } from '@sight/app/application/group/command/removeBookmark/RemoveBookmarkCommandHandler';

import {
GroupBookmarkRepository,
IGroupBookmarkRepository,
} from '@sight/app/domain/group/IGroupBookmarkRepository';
import {
GroupRepository,
IGroupRepository,
} from '@sight/app/domain/group/IGroupRepository';
import {
CUSTOMER_SERVICE_GROUP_ID,
PRACTICE_GROUP_ID,
} from '@sight/app/domain/group/model/constant';

import { DomainFixture } from '@sight/__test__/fixtures';
import { generateEmptyProviders } from '@sight/__test__/util';
import { Message } from '@sight/constant/message';

describe('RemoveBookmarkCommandHandler', () => {
let handler: RemoveBookmarkCommandHandler;
let groupRepository: jest.Mocked<IGroupRepository>;
let groupBookmarkRepository: jest.Mocked<IGroupBookmarkRepository>;

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

const testModule = await Test.createTestingModule({
providers: [
RemoveBookmarkCommandHandler,
...generateEmptyProviders(GroupRepository, GroupBookmarkRepository),
],
}).compile();

handler = testModule.get(RemoveBookmarkCommandHandler);
groupRepository = testModule.get(GroupRepository);
groupBookmarkRepository = testModule.get(GroupBookmarkRepository);
});

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

describe('execute', () => {
const groupId = 'groupId';
const userId = 'userId';

beforeEach(() => {
const group = DomainFixture.generateGroup();
const groupBookmark = DomainFixture.generateGroupBookmark();

groupRepository.findById = jest.fn().mockResolvedValue(group);
groupBookmarkRepository.findByGroupIdAndUserId = jest
.fn()
.mockResolvedValue(groupBookmark);

groupBookmarkRepository.remove = jest.fn();
});

test('그룹이 존재하지 않으면 예외가 발생해야 한다', async () => {
groupRepository.findById = jest.fn().mockResolvedValue(null);

await expect(
handler.execute(new AddBookmarkCommand(groupId, userId)),
).rejects.toThrowError(Message.GROUP_NOT_FOUND);
});

test('고객 센터 그룹이라면 예외가 발생해야 한다', async () => {
const customerServiceGroup = DomainFixture.generateGroup({
id: CUSTOMER_SERVICE_GROUP_ID,
});
groupRepository.findById = jest
.fn()
.mockResolvedValue(customerServiceGroup);

await expect(
handler.execute(new AddBookmarkCommand(groupId, userId)),
).rejects.toThrowError(Message.DEFAULT_BOOKMARKED_GROUP);
});

test('그룹 활용 실습 그룹이라면 예외가 발생해야 한다', async () => {
const practiceGroup = DomainFixture.generateGroup({
id: PRACTICE_GROUP_ID,
});
groupRepository.findById = jest.fn().mockResolvedValue(practiceGroup);

await expect(
handler.execute(new AddBookmarkCommand(groupId, userId)),
).rejects.toThrowError(Message.DEFAULT_BOOKMARKED_GROUP);
});

test('아직 그룹을 즐겨찾기하지 않았다면 무시해야 한다', async () => {
groupBookmarkRepository.findByGroupIdAndUserId = jest
.fn()
.mockResolvedValue(null);

await handler.execute(new AddBookmarkCommand(groupId, userId));

expect(groupBookmarkRepository.remove).not.toBeCalled();
});

test('그룹을 즐겨찾기 해제해야 한다', async () => {
await handler.execute(new AddBookmarkCommand(groupId, userId));

expect(groupBookmarkRepository.remove).toBeCalled();
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
import {
Inject,
NotFoundException,
UnprocessableEntityException,
} from '@nestjs/common';

import { Transactional } from '@sight/core/persistence/transaction/Transactional';

import { RemoveBookmarkCommand } from '@sight/app/application/group/command/removeBookmark/RemoveBookmarkCommand';

import {
GroupBookmarkRepository,
IGroupBookmarkRepository,
} from '@sight/app/domain/group/IGroupBookmarkRepository';
import {
GroupRepository,
IGroupRepository,
} from '@sight/app/domain/group/IGroupRepository';

import { Message } from '@sight/constant/message';

@CommandHandler(RemoveBookmarkCommand)
export class RemoveBookmarkCommandHandler
implements ICommandHandler<RemoveBookmarkCommand>
{
constructor(
@Inject(GroupRepository)
private readonly groupRepository: IGroupRepository,
@Inject(GroupBookmarkRepository)
private readonly groupBookmarkRepository: IGroupBookmarkRepository,
) {}

@Transactional()
async execute(command: RemoveBookmarkCommand): Promise<void> {
const { groupId, userId } = command;

const group = await this.groupRepository.findById(groupId);
if (!group) {
throw new NotFoundException(Message.GROUP_NOT_FOUND);
}

if (group.isCustomerServiceGroup() || group.isPracticeGroup()) {
throw new UnprocessableEntityException(Message.DEFAULT_BOOKMARKED_GROUP);
}

const prevBookmark =
await this.groupBookmarkRepository.findByGroupIdAndUserId(
groupId,
userId,
);
if (!prevBookmark) {
return;
}

prevBookmark.remove();
await this.groupBookmarkRepository.remove(prevBookmark);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
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 { MessageBuilder } from '@sight/core/message/MessageBuilder';

import { GroupBookmarkCreatedHandler } from '@sight/app/application/group/eventHandler/GroupBookmarkCreatedHandler';
Expand All @@ -23,7 +20,6 @@ import { generateEmptyProviders } from '@sight/__test__/util';

describe('GroupBookmarkCreatedHandler', () => {
let handler: GroupBookmarkCreatedHandler;
let clsService: jest.Mocked<ClsService>;
let messageBuilder: jest.Mocked<MessageBuilder>;
let slackSender: jest.Mocked<ISlackSender>;
let groupRepository: jest.Mocked<IGroupRepository>;
Expand All @@ -34,17 +30,11 @@ describe('GroupBookmarkCreatedHandler', () => {
const testModule = await Test.createTestingModule({
providers: [
GroupBookmarkCreatedHandler,
...generateEmptyProviders(
ClsService,
MessageBuilder,
SlackSender,
GroupRepository,
),
...generateEmptyProviders(MessageBuilder, SlackSender, GroupRepository),
],
}).compile();

handler = testModule.get(GroupBookmarkCreatedHandler);
clsService = testModule.get(ClsService);
messageBuilder = testModule.get(MessageBuilder);
slackSender = testModule.get(SlackSender);
groupRepository = testModule.get(GroupRepository);
Expand All @@ -55,25 +45,20 @@ describe('GroupBookmarkCreatedHandler', () => {
});

describe('handle', () => {
const requesterUserId = 'requesterUserId';
const groupId = 'groupId';
const userId = 'userId';

beforeEach(() => {
const requester: IRequester = {
userId: requesterUserId,
role: UserRole.USER,
};
const group = DomainFixture.generateGroup();

clsService.get = jest.fn().mockReturnValue(requester);
groupRepository.findById = jest.fn().mockResolvedValue(group);
messageBuilder.build = jest.fn().mockReturnValue('message');

slackSender.send = jest.fn();
});

test('그룹이 존재하지 않으면 메시지를 보내지 않아야 한다', async () => {
const groupId = 'groupId';
const event = new GroupBookmarkCreated(groupId);
const event = new GroupBookmarkCreated(groupId, userId);

groupRepository.findById.mockResolvedValue(null);

Expand All @@ -84,13 +69,13 @@ describe('GroupBookmarkCreatedHandler', () => {

test('요청자에게 메시지를 보내야 한다', async () => {
const groupId = 'groupId';
const event = new GroupBookmarkCreated(groupId);
const event = new GroupBookmarkCreated(groupId, userId);

await handler.handle(event);

expect(slackSender.send).toBeCalledTimes(1);
expect(slackSender.send).toBeCalledWith(
expect.objectContaining({ targetUserId: requesterUserId }),
expect.objectContaining({ targetUserId: userId }),
);
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Inject } from '@nestjs/common';
import { EventsHandler, IEventHandler } from '@nestjs/cqrs';
import { ClsService } from 'nestjs-cls';

import { IRequester } from '@sight/core/auth/IRequester';
import { MessageBuilder } from '@sight/core/message/MessageBuilder';
import { Transactional } from '@sight/core/persistence/transaction/Transactional';

Expand All @@ -24,8 +22,6 @@ export class GroupBookmarkCreatedHandler
implements IEventHandler<GroupBookmarkCreated>
{
constructor(
@Inject(ClsService)
private readonly clsService: ClsService,
@Inject(MessageBuilder)
private readonly messageBuilder: MessageBuilder,
@Inject(SlackSender)
Expand All @@ -36,22 +32,20 @@ export class GroupBookmarkCreatedHandler

@Transactional()
async handle(event: GroupBookmarkCreated): Promise<void> {
const { groupId } = event;
const { groupId, userId } = event;

const group = await this.groupRepository.findById(groupId);
if (!group) {
return;
}

const requester: IRequester = this.clsService.get('requester');

const message = this.messageBuilder.build(
Template.ADD_GROUP_BOOKMARK.notification,
{ groupId, groupTitle: group.title },
);
this.slackSender.send({
category: SlackMessageCategory.GROUP_ACTIVITY_FOR_ME,
targetUserId: requester.userId,
targetUserId: userId,
message,
});
}
Expand Down
Loading

0 comments on commit 617d315

Please sign in to comment.