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: add useCase for create board #1322

Merged
merged 4 commits into from
Apr 3, 2023
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
9 changes: 8 additions & 1 deletion backend/src/libs/utils/generateBoardData.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<CreateBoardServiceInterface>;

beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
CreateBoardUseCase,
{
provide: Boards.TYPES.services.CreateBoardService,
useValue: createMock<CreateBoardUserServiceInterface>()
}
]
}).compile();

createBoardMock = module.get<CreateBoardUseCase>(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);
});
});
});
20 changes: 20 additions & 0 deletions backend/src/modules/boards/applications/create-board.use-case.ts
Original file line number Diff line number Diff line change
@@ -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<CreateBoardUseCaseDto, Board> {
constructor(
@Inject(TYPES.services.CreateBoardService)
private readonly createBoardService: CreateBoardServiceInterface
) {}

async execute(createBoardData: CreateBoardUseCaseDto) {
const { boardData, userId } = createBoardData;

return this.createBoardService.create(boardData, userId);
}
}

This file was deleted.

4 changes: 2 additions & 2 deletions backend/src/modules/boards/boards.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import {
afterUserUpdatedDurationSubscriber,
boardRepository,
boardTimerRepository,
createBoardApplication,
createBoardService,
createBoardUseCase,
deleteBoardApplication,
deleteBoardService,
duplicateBoardUseCase,
Expand Down Expand Up @@ -58,8 +58,8 @@ import PublicBoardsController from './controller/publicBoards.controller';
updateBoardService,
deleteBoardService,
getBoardService,
createBoardApplication,
duplicateBoardUseCase,
createBoardUseCase,
updateBoardApplication,
deleteBoardApplication,
getBoardApplication,
Expand Down
12 changes: 6 additions & 6 deletions backend/src/modules/boards/boards.providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -131,3 +126,8 @@ export const boardRepository = {
provide: TYPES.repositories.BoardRepository,
useClass: BoardRepository
};

export const createBoardUseCase = {
provide: TYPES.applications.CreateBoardUseCase,
useClass: CreateBoardUseCase
};
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,8 +24,8 @@ describe('BoardsController', () => {
controllers: [BoardsController],
providers: [
{
provide: Boards.TYPES.applications.CreateBoardApplication,
useValue: createMock<CreateBoardApplicationInterface>()
provide: Boards.TYPES.applications.CreateBoardUseCase,
useValue: createMock<UseCase<CreateBoardUseCaseDto, Board>>()
},
{
provide: Boards.TYPES.applications.DuplicateBoardUseCase,
Expand Down
8 changes: 4 additions & 4 deletions backend/src/modules/boards/controller/boards.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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<CreateBoardUseCaseDto, Board>,
@Inject(TYPES.applications.DuplicateBoardUseCase)
private duplicateBoardUseCase: UseCase<DuplicateBoardDto, Board>,
@Inject(TYPES.applications.GetBoardApplication)
Expand Down Expand Up @@ -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' })
Expand Down
10 changes: 0 additions & 10 deletions backend/src/modules/boards/dto/createBoard.dto.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
}

This file was deleted.

4 changes: 2 additions & 2 deletions backend/src/modules/boards/interfaces/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions backend/src/modules/cards/cards.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import SocketModule from '../socket/socket.module';
import { VotesModule } from '../votes/votes.module';
import {
cardRepository,
creacteCardUseCase,
createCardUseCase,
deleteCardApplication,
deleteCardService,
getCardService,
Expand All @@ -26,7 +26,7 @@ import CardsController from './controller/cards.controller';
updateCardApplication,
deleteCardApplication,
cardRepository,
creacteCardUseCase,
createCardUseCase,
unmergeCardUseCase,
mergeCardUseCase
],
Expand Down
2 changes: 1 addition & 1 deletion backend/src/modules/cards/cards.providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const cardRepository = {
useClass: CardRepository
};

export const creacteCardUseCase = {
export const createCardUseCase = {
provide: TYPES.applications.CreateCardUseCase,
useClass: CreateCardUseCase
};
Expand Down