diff --git a/backend/src/libs/utils/generateBoardData.ts b/backend/src/libs/utils/generateBoardData.ts index 51170d32c..445bf3e95 100644 --- a/backend/src/libs/utils/generateBoardData.ts +++ b/backend/src/libs/utils/generateBoardData.ts @@ -1,6 +1,13 @@ import BoardDto from 'src/modules/boards/dto/board.dto'; import BoardUserDto from 'src/modules/boardUsers/dto/board.user.dto'; -import { CreateBoardDto } from 'src/modules/boards/dto/createBoard.dto'; +import TeamDto from 'src/modules/teams/dto/team.dto'; + +interface CreateBoardDto { + maxUsers: number; + board: BoardDto; + team: TeamDto | null; + users: BoardUserDto[]; +} export const generateSubBoardDtoData = (index: number, users: BoardUserDto[] = []): BoardDto => { return { diff --git a/backend/src/modules/boards/applications/create-board.use-case.spec.ts b/backend/src/modules/boards/applications/create-board.use-case.spec.ts new file mode 100644 index 000000000..dd299d362 --- /dev/null +++ b/backend/src/modules/boards/applications/create-board.use-case.spec.ts @@ -0,0 +1,48 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import * as Boards from 'src/modules/boards/interfaces/types'; +import { DeepMocked, createMock } from '@golevelup/ts-jest'; +import { CreateBoardUserServiceInterface } from 'src/modules/boardUsers/interfaces/services/create.board.user.service.interface'; +import faker from '@faker-js/faker'; +import { BoardDtoFactory } from 'src/libs/test-utils/mocks/factories/dto/boardDto-factory.mock'; +import { CreateBoardServiceInterface } from '../interfaces/services/create.board.service.interface'; +import { CreateBoardUseCase } from './create-board.use-case'; + +describe('CreateBoardUseCase', () => { + let createBoardMock: CreateBoardUseCase; + let createBoardServiceMock: DeepMocked; + + beforeAll(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + CreateBoardUseCase, + { + provide: Boards.TYPES.services.CreateBoardService, + useValue: createMock() + } + ] + }).compile(); + + createBoardMock = module.get(CreateBoardUseCase); + + createBoardServiceMock = module.get(Boards.TYPES.services.CreateBoardService); + }); + + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('should be defined', () => { + expect(createBoardMock).toBeDefined(); + }); + + describe('execute', () => { + it('should be called with argument of type createBoardUseCaseDto', async () => { + const boardData = BoardDtoFactory.create(); + const userId = faker.datatype.uuid(); + + await createBoardMock.execute({ userId, boardData }); + + expect(createBoardServiceMock.create).toBeCalledWith(boardData, userId); + }); + }); +}); diff --git a/backend/src/modules/boards/applications/create-board.use-case.ts b/backend/src/modules/boards/applications/create-board.use-case.ts new file mode 100644 index 000000000..ba870d73e --- /dev/null +++ b/backend/src/modules/boards/applications/create-board.use-case.ts @@ -0,0 +1,20 @@ +import { Inject, Injectable } from '@nestjs/common'; +import { TYPES } from '../interfaces/types'; +import { UseCase } from 'src/libs/interfaces/use-case.interface'; +import Board from '../entities/board.schema'; +import { CreateBoardServiceInterface } from '../interfaces/services/create.board.service.interface'; +import CreateBoardUseCaseDto from '../dto/useCase/create-board.use-case.dto'; + +@Injectable() +export class CreateBoardUseCase implements UseCase { + constructor( + @Inject(TYPES.services.CreateBoardService) + private readonly createBoardService: CreateBoardServiceInterface + ) {} + + async execute(createBoardData: CreateBoardUseCaseDto) { + const { boardData, userId } = createBoardData; + + return this.createBoardService.create(boardData, userId); + } +} diff --git a/backend/src/modules/boards/applications/create.board.application.ts b/backend/src/modules/boards/applications/create.board.application.ts deleted file mode 100644 index b583b4d74..000000000 --- a/backend/src/modules/boards/applications/create.board.application.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Inject, Injectable } from '@nestjs/common'; -import BoardDto from '../dto/board.dto'; -import { CreateBoardApplicationInterface } from '../interfaces/applications/create.board.application.interface'; -import { CreateBoardServiceInterface } from '../interfaces/services/create.board.service.interface'; -import { TYPES } from '../interfaces/types'; -import Board from '../entities/board.schema'; - -@Injectable() -export class CreateBoardApplication implements CreateBoardApplicationInterface { - constructor( - @Inject(TYPES.services.CreateBoardService) - private createBoardService: CreateBoardServiceInterface - ) {} - - create(board: BoardDto, userId: string): Promise { - return this.createBoardService.create(board, userId); - } -} diff --git a/backend/src/modules/boards/boards.module.ts b/backend/src/modules/boards/boards.module.ts index 1d3d33df4..d32adc9ae 100644 --- a/backend/src/modules/boards/boards.module.ts +++ b/backend/src/modules/boards/boards.module.ts @@ -19,8 +19,8 @@ import { afterUserUpdatedDurationSubscriber, boardRepository, boardTimerRepository, - createBoardApplication, createBoardService, + createBoardUseCase, deleteBoardApplication, deleteBoardService, duplicateBoardUseCase, @@ -58,8 +58,8 @@ import PublicBoardsController from './controller/publicBoards.controller'; updateBoardService, deleteBoardService, getBoardService, - createBoardApplication, duplicateBoardUseCase, + createBoardUseCase, updateBoardApplication, deleteBoardApplication, getBoardApplication, diff --git a/backend/src/modules/boards/boards.providers.ts b/backend/src/modules/boards/boards.providers.ts index 2cd91e26d..646836a9b 100644 --- a/backend/src/modules/boards/boards.providers.ts +++ b/backend/src/modules/boards/boards.providers.ts @@ -10,7 +10,7 @@ import AfterUserRequestedTimerStateSubscriber from 'src/modules/boards/subscribe import AfterUserStartedTimerSubscriber from 'src/modules/boards/subscribers/after-user-started-timer.subscriber'; import AfterUserStoppedTimerSubscriber from 'src/modules/boards/subscribers/after-user-stopped-timer.subscriber'; import AfterUserUpdatedDurationSubscriber from 'src/modules/boards/subscribers/after-user-updated-duration.subscriber'; -import { CreateBoardApplication } from './applications/create.board.application'; +import { CreateBoardUseCase } from './applications/create-board.use-case'; import { DeleteBoardApplication } from './applications/delete.board.application'; import { DuplicateBoardUseCase } from './applications/duplicate-board.use-case'; import { GetBoardApplication } from './applications/get.board.application'; @@ -42,11 +42,6 @@ export const deleteBoardService = { useClass: DeleteBoardService }; -export const createBoardApplication = { - provide: TYPES.applications.CreateBoardApplication, - useClass: CreateBoardApplication -}; - export const duplicateBoardUseCase = { provide: TYPES.applications.DuplicateBoardUseCase, useClass: DuplicateBoardUseCase @@ -131,3 +126,8 @@ export const boardRepository = { provide: TYPES.repositories.BoardRepository, useClass: BoardRepository }; + +export const createBoardUseCase = { + provide: TYPES.applications.CreateBoardUseCase, + useClass: CreateBoardUseCase +}; diff --git a/backend/src/modules/boards/controller/boards.controller.spec.ts b/backend/src/modules/boards/controller/boards.controller.spec.ts index d30cdba8d..09e87bff4 100644 --- a/backend/src/modules/boards/controller/boards.controller.spec.ts +++ b/backend/src/modules/boards/controller/boards.controller.spec.ts @@ -7,12 +7,12 @@ import { GetBoardGuard } from 'src/libs/guards/getBoardPermissions.guard'; import { BoardUserGuard } from 'src/libs/guards/boardRoles.guard'; import { BoardPhases } from 'src/libs/enum/board.phases'; import { UpdateBoardApplicationInterface } from '../interfaces/applications/update.board.application.interface'; -import { CreateBoardApplicationInterface } from '../interfaces/applications/create.board.application.interface'; import { GetBoardApplicationInterface } from '../interfaces/applications/get.board.application.interface'; import { DeleteBoardApplicationInterface } from '../interfaces/applications/delete.board.application.interface'; import { UseCase } from 'src/libs/interfaces/use-case.interface'; import Board from '../entities/board.schema'; import { DuplicateBoardDto } from '../applications/duplicate-board.use-case'; +import CreateBoardUseCaseDto from '../dto/useCase/create-board.use-case.dto'; describe('BoardsController', () => { let controller: BoardsController; @@ -24,8 +24,8 @@ describe('BoardsController', () => { controllers: [BoardsController], providers: [ { - provide: Boards.TYPES.applications.CreateBoardApplication, - useValue: createMock() + provide: Boards.TYPES.applications.CreateBoardUseCase, + useValue: createMock>() }, { provide: Boards.TYPES.applications.DuplicateBoardUseCase, diff --git a/backend/src/modules/boards/controller/boards.controller.ts b/backend/src/modules/boards/controller/boards.controller.ts index 139efe79e..0a48fe21a 100644 --- a/backend/src/modules/boards/controller/boards.controller.ts +++ b/backend/src/modules/boards/controller/boards.controller.ts @@ -52,12 +52,12 @@ import BoardDto from '../dto/board.dto'; import UpdateBoardUserDto from 'src/modules/boardUsers/dto/update-board-user.dto'; import { UpdateBoardDto } from 'src/modules/boards/dto/update-board.dto'; import Board from '../entities/board.schema'; -import { CreateBoardApplicationInterface } from '../interfaces/applications/create.board.application.interface'; import { DeleteBoardApplicationInterface } from '../interfaces/applications/delete.board.application.interface'; import { GetBoardApplicationInterface } from '../interfaces/applications/get.board.application.interface'; import { UpdateBoardApplicationInterface } from '../interfaces/applications/update.board.application.interface'; import { TYPES } from '../interfaces/types'; import { DuplicateBoardDto } from '../applications/duplicate-board.use-case'; +import CreateBoardUseCaseDto from '../dto/useCase/create-board.use-case.dto'; const BoardUser = (permissions: string[]) => SetMetadata('permissions', permissions); @@ -67,8 +67,8 @@ const BoardUser = (permissions: string[]) => SetMetadata('permissions', permissi @Controller('boards') export default class BoardsController { constructor( - @Inject(TYPES.applications.CreateBoardApplication) - private createBoardApp: CreateBoardApplicationInterface, + @Inject(TYPES.applications.CreateBoardUseCase) + private createBoardUseCase: UseCase, @Inject(TYPES.applications.DuplicateBoardUseCase) private duplicateBoardUseCase: UseCase, @Inject(TYPES.applications.GetBoardApplication) @@ -103,7 +103,7 @@ export default class BoardsController { }) @Post() createBoard(@Req() request: RequestWithUser, @Body() boardData: BoardDto) { - return this.createBoardApp.create(boardData, request.user._id); + return this.createBoardUseCase.execute({ userId: request.user._id, boardData }); } @ApiOperation({ summary: 'Duplicate a board' }) diff --git a/backend/src/modules/boards/dto/createBoard.dto.ts b/backend/src/modules/boards/dto/createBoard.dto.ts deleted file mode 100644 index 360912fc3..000000000 --- a/backend/src/modules/boards/dto/createBoard.dto.ts +++ /dev/null @@ -1,10 +0,0 @@ -import BoardDto from 'src/modules/boards/dto/board.dto'; -import BoardUserDto from 'src/modules/boardUsers/dto/board.user.dto'; -import { TeamDto } from '../../communication/dto/team.dto'; - -export interface CreateBoardDto { - maxUsers: number; - board: BoardDto; - team: TeamDto | null; - users: BoardUserDto[]; -} diff --git a/backend/src/modules/boards/dto/useCase/create-board.use-case.dto.ts b/backend/src/modules/boards/dto/useCase/create-board.use-case.dto.ts new file mode 100644 index 000000000..89b71d6c5 --- /dev/null +++ b/backend/src/modules/boards/dto/useCase/create-board.use-case.dto.ts @@ -0,0 +1,14 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsNotEmpty, IsString } from 'class-validator'; +import BoardDto from '../board.dto'; + +export default class CreateBoardUseCaseDto { + @ApiProperty() + @IsString() + @IsNotEmpty() + userId: string; + + @ApiProperty({ type: BoardDto }) + @IsNotEmpty() + boardData: BoardDto; +} diff --git a/backend/src/modules/boards/interfaces/applications/create.board.application.interface.ts b/backend/src/modules/boards/interfaces/applications/create.board.application.interface.ts deleted file mode 100644 index 474b62964..000000000 --- a/backend/src/modules/boards/interfaces/applications/create.board.application.interface.ts +++ /dev/null @@ -1,6 +0,0 @@ -import BoardDto from '../../dto/board.dto'; -import Board from '../../entities/board.schema'; - -export interface CreateBoardApplicationInterface { - create(boardData: BoardDto, userId: string): Promise; -} diff --git a/backend/src/modules/boards/interfaces/types.ts b/backend/src/modules/boards/interfaces/types.ts index e457599fa..f772be923 100644 --- a/backend/src/modules/boards/interfaces/types.ts +++ b/backend/src/modules/boards/interfaces/types.ts @@ -12,11 +12,11 @@ export const TYPES = { UpdateBoardTimerDurationService: 'UpdateBoardTimerDurationService' }, applications: { - CreateBoardApplication: 'CreateBoardApplication', DuplicateBoardUseCase: 'DuplicateBoardUseCase', DeleteBoardApplication: 'DeleteBoardApplication', UpdateBoardApplication: 'UpdateBoardApplication', - GetBoardApplication: 'GetBoardApplication' + GetBoardApplication: 'GetBoardApplication', + CreateBoardUseCase: 'CreateBoardUseCase' }, repositories: { BoardTimerRepository: 'BoardTimerRepository', diff --git a/backend/src/modules/cards/cards.module.ts b/backend/src/modules/cards/cards.module.ts index e3e9a70a6..ae88dce5a 100644 --- a/backend/src/modules/cards/cards.module.ts +++ b/backend/src/modules/cards/cards.module.ts @@ -4,7 +4,7 @@ import SocketModule from '../socket/socket.module'; import { VotesModule } from '../votes/votes.module'; import { cardRepository, - creacteCardUseCase, + createCardUseCase, deleteCardApplication, deleteCardService, getCardService, @@ -26,7 +26,7 @@ import CardsController from './controller/cards.controller'; updateCardApplication, deleteCardApplication, cardRepository, - creacteCardUseCase, + createCardUseCase, unmergeCardUseCase, mergeCardUseCase ], diff --git a/backend/src/modules/cards/cards.providers.ts b/backend/src/modules/cards/cards.providers.ts index 6429bebcb..c7af05654 100644 --- a/backend/src/modules/cards/cards.providers.ts +++ b/backend/src/modules/cards/cards.providers.ts @@ -39,7 +39,7 @@ export const cardRepository = { useClass: CardRepository }; -export const creacteCardUseCase = { +export const createCardUseCase = { provide: TYPES.applications.CreateCardUseCase, useClass: CreateCardUseCase };