-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: 잘못된 이벤트 파라미터 수정 * feat: 그룹 즐겨찾기 해제 기능 구현
- Loading branch information
Showing
14 changed files
with
342 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/app/application/group/command/removeBookmark/RemoveBookmarkCommand.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export class RemoveBookmarkCommand { | ||
constructor( | ||
readonly groupId: string, | ||
readonly userId: string, | ||
) {} | ||
} |
112 changes: 112 additions & 0 deletions
112
src/app/application/group/command/removeBookmark/RemoveBookmarkCommandHandler.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |
59 changes: 59 additions & 0 deletions
59
src/app/application/group/command/removeBookmark/RemoveBookmarkCommandHandler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.