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

refactor: create board service #1276

Merged
merged 12 commits into from
Mar 22, 2023
Merged
10 changes: 10 additions & 0 deletions backend/src/libs/exceptions/boardNotFoundException.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { NotFoundException } from '@nestjs/common';
import { BOARD_NOT_FOUND } from 'src/libs/exceptions/messages';

export class BoardNotFoundException extends NotFoundException {
private static defaultMessage = BOARD_NOT_FOUND;

constructor(message = BoardNotFoundException.defaultMessage) {
super(message);
}
}
10 changes: 10 additions & 0 deletions backend/src/libs/exceptions/createFailedBadRequestException.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { BadRequestException } from '@nestjs/common';
import { CREATE_FAILED } from './messages';

export class CreateFailedException extends BadRequestException {
private static defaultMessage = CREATE_FAILED;

constructor(message = CreateFailedException.defaultMessage) {
super(message);
}
}
11 changes: 10 additions & 1 deletion backend/src/libs/exceptions/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const EMAIL_DONT_MATCH = 'EMAIL_DONT_MATCH';
const BOARD_NOT_FOUND = 'BOARD_NOT_FOUND';
const BOARDS_NOT_FOUND = 'BOARDS_NOT_FOUND';
const BOARD_USER_NOT_FOUND = 'BOARD_USER_NOT_FOUND';
const BOARD_USERS_NOT_FOUND = 'BOARD_USERS_NOT_FOUND';
const BOARD_USER_EXISTS = 'BOARD_USER_EXISTS';

const COLUMN_NOT_FOUND = 'COLUMN_NOT_FOUND';
Expand All @@ -18,11 +19,15 @@ const UNAUTHORIZED = 'UNAUTHORIZED';
const UPDATE_FAILED = 'UPDATE_FAILED';
const DELETE_FAILED = 'DELETE_FAILED';
const INSERT_FAILED = 'INSERT_FAILED';
const CREATE_FAILED = 'CREATE_FAILED';

const CARD_NOT_REMOVED = "Card wasn't removed";
const CARD_NOT_INSERTED = "Card wasn't inserted";
const CARD_NOT_FOUND = "Card wasn't found";

const TEAM_USERS_NOT_FOUND = 'TEAM_USERS_NOT_FOUND';
const TEAM_NOT_FOUND = 'TEAM_NOT_FOUND';

const FORBIDDEN = 'FORBIDDEN';
const NOT_FOUND = 'NOT_FOUND';

Expand All @@ -33,6 +38,7 @@ export {
BOARD_NOT_FOUND,
BOARDS_NOT_FOUND,
BOARD_USER_NOT_FOUND,
BOARD_USERS_NOT_FOUND,
CARD_NOT_FOUND,
CARD_NOT_INSERTED,
CARD_NOT_REMOVED,
Expand All @@ -43,6 +49,7 @@ export {
EMAIL_NOT_EXISTS,
FORBIDDEN,
INSERT_FAILED,
CREATE_FAILED,
INVALID_CREDENTIALS,
NOT_FOUND,
TOKENS_NOT_MATCHING,
Expand All @@ -51,5 +58,7 @@ export {
USER_NOT_FOUND,
DELETE_VOTE_FAILED,
INSERT_VOTE_FAILED,
COLUMN_NOT_FOUND
COLUMN_NOT_FOUND,
TEAM_USERS_NOT_FOUND,
TEAM_NOT_FOUND
};
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ export interface BaseInterfaceRepository<T> {

aggregateByQuery<Q>(pipeline: PipelineStage[]): Promise<Q[]>;

create<Q>(item: Q): Promise<T>;
create<Q>(item: Q, withSession?: boolean): Promise<T>;

insertMany(listOfItems: T[]): Promise<T[]>;
insertMany<Q>(listOfItems: Q[], withSession?: boolean): Promise<T[]>;

update(id: string, item: T): Promise<T>;

Expand Down
14 changes: 10 additions & 4 deletions backend/src/libs/repositories/mongo/mongo-generic.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,18 @@ export class MongoGenericRepository<T> implements BaseInterfaceRepository<T> {
return this._repository.aggregate(pipeline).exec();
}

create<Q>(item: Q): Promise<T> {
return this._repository.create(item);
async create<Q>(item: Q, withSession?: boolean): Promise<T> {
return (
await this._repository.create([item], {
session: withSession ? this._session : undefined
})
)[0];
}

insertMany<Q>(listOfItems: Q[]): Promise<T[]> {
return this._repository.insertMany(listOfItems);
insertMany<Q>(listOfItems: Q[], withSession?: boolean): Promise<T[]> {
return this._repository.insertMany(listOfItems, {
session: withSession ? this._session : undefined
});
}

update(id: string, item: T): Promise<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface BoardUserRepositoryInterface extends BaseInterfaceRepository<Bo
getVotesCount(boardId: string): Promise<BoardUser[]>;
getBoardUser(board: string, user: string): Promise<BoardUser>;
getBoardUserPopulated(board: string, user: string): Promise<BoardUser>;
createBoardUsers(boardUsers: BoardUserDto[]): Promise<BoardUser[]>;
createBoardUsers(boardUsers: BoardUserDto[], withSession?: boolean);
updateBoardUserRole(boardId: string, userId: string, role: string): Promise<BoardUser>;
updateVoteUser(
boardId: string,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import BoardUser from 'src/modules/boardUsers/entities/board.user.schema';
import { SessionInterface } from 'src/libs/transactions/session.interface';
import BoardUserDto from '../../dto/board.user.dto';
import BoardUser from '../../entities/board.user.schema';

export interface CreateBoardUserServiceInterface {
saveBoardUsers(newUsers: BoardUserDto[], newBoardId?: string): Promise<BoardUser[]>;
export interface CreateBoardUserServiceInterface extends SessionInterface {
saveBoardUsers(
newUsers: BoardUserDto[],
newBoardId?: string,
withSession?: boolean
): Promise<BoardUser[]>;
createBoardUser(board: string, user: string): Promise<BoardUser>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ export class BoardUserRepository
}

/* CREATE BOARD USERS */
createBoardUsers(boardUsers: BoardUserDto[]) {
return this.insertMany<BoardUserDto>(boardUsers);
createBoardUsers(boardUsers: BoardUserDto[], withSession?: boolean) {
return this.insertMany<BoardUserDto>(boardUsers, withSession);
}

/* UPDATE BOARD USERS */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default class CreateBoardUserService implements CreateBoardUserServiceInt
private readonly boardUserRepository: BoardUserRepositoryInterface
) {}

saveBoardUsers(newUsers: BoardUserDto[], newBoardId?: string) {
saveBoardUsers(newUsers: BoardUserDto[], newBoardId?: string, withSession?: boolean) {
let boardUsersToInsert: BoardUserDto[] = newUsers;

if (newBoardId) {
Expand All @@ -23,7 +23,7 @@ export default class CreateBoardUserService implements CreateBoardUserServiceInt
}));
}

return this.boardUserRepository.createBoardUsers(boardUsersToInsert);
return this.boardUserRepository.createBoardUsers(boardUsersToInsert, withSession);
}

async createBoardUser(board: string, user: string) {
Expand All @@ -44,4 +44,20 @@ export default class CreateBoardUserService implements CreateBoardUserServiceInt

return boardUserCreated;
}

startTransaction(): Promise<void> {
return this.boardUserRepository.startTransaction();
}

commitTransaction(): Promise<void> {
return this.boardUserRepository.commitTransaction();
}

abortTransaction(): Promise<void> {
return this.boardUserRepository.abortTransaction();
}

endSession(): Promise<void> {
return this.boardUserRepository.endSession();
}
}
31 changes: 6 additions & 25 deletions backend/src/modules/boards/controller/boards.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ import { GetBoardGuard } from './../../../libs/guards/getBoardPermissions.guard'
import { TeamRoles } from 'src/libs/enum/team.roles';
import { BoardRoles } from 'src/modules/communication/dto/types';
import {
BadRequestException,
Body,
Controller,
Delete,
Get,
Inject,
NotFoundException,
Param,
Post,
Put,
Expand Down Expand Up @@ -36,7 +34,6 @@ import {
import { BaseParam } from 'src/libs/dto/param/base.param';
import { PaginationParams } from 'src/libs/dto/param/pagination.params';
import { BaseParamWSocket } from 'src/libs/dto/param/socket.param';
import { BOARD_NOT_FOUND, INSERT_FAILED } from 'src/libs/exceptions/messages';
import JwtAuthenticationGuard from 'src/libs/guards/jwtAuth.guard';
import RequestWithUser from 'src/libs/interfaces/requestWithUser.interface';
import { BadRequestResponse } from 'src/libs/swagger/errors/bad-request.swagger';
Expand Down Expand Up @@ -100,12 +97,8 @@ export default class BoardsController {
type: InternalServerErrorResponse
})
@Post()
async createBoard(@Req() request: RequestWithUser, @Body() boardData: BoardDto) {
const board = await this.createBoardApp.create(boardData, request.user._id);

if (!board) throw new BadRequestException(INSERT_FAILED);

return board;
createBoard(@Req() request: RequestWithUser, @Body() boardData: BoardDto) {
return this.createBoardApp.create(boardData, request.user._id);
}

@ApiOperation({ summary: 'Get Boards to show on dashboard' })
Expand Down Expand Up @@ -144,10 +137,7 @@ export default class BoardsController {
type: InternalServerErrorResponse
})
@Get()
async getAllBoards(
@Req() request: RequestWithUser,
@Query() { page, size, team }: PaginationParams
) {
getAllBoards(@Req() request: RequestWithUser, @Query() { page, size, team }: PaginationParams) {
const { _id: userId, isSAdmin } = request.user;

return this.getBoardApp.getAllBoards(team, userId, isSAdmin, page, size);
Expand All @@ -168,10 +158,7 @@ export default class BoardsController {
type: InternalServerErrorResponse
})
@Get('/personal')
async getPersonalBoards(
@Req() request: RequestWithUser,
@Query() { page, size }: PaginationParams
) {
getPersonalBoards(@Req() request: RequestWithUser, @Query() { page, size }: PaginationParams) {
const { _id: userId } = request.user;

return this.getBoardApp.getPersonalBoards(userId, page, size);
Expand Down Expand Up @@ -199,14 +186,8 @@ export default class BoardsController {
@BoardUser([TeamRoles.ADMIN, TeamRoles.STAKEHOLDER])
@UseGuards(GetBoardGuard)
@Get(':boardId')
async getBoard(@Param() { boardId }: BaseParam, @Req() request: RequestWithUser) {
const board = await this.getBoardApp.getBoard(boardId, request.user);

if (!board) {
throw new NotFoundException(BOARD_NOT_FOUND);
}

return board;
getBoard(@Param() { boardId }: BaseParam, @Req() request: RequestWithUser) {
return this.getBoardApp.getBoard(boardId, request.user);
}

@ApiOperation({ summary: 'Update a specific board' })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@ import Board from '../../entities/board.schema';
export interface CreateBoardServiceInterface {
create(boardData: BoardDto, userId: string): Promise<Board>;

splitBoardByTeam(ownerId: string, teamId: string, configs: Configs): Promise<string | null>;
splitBoardByTeam(
ownerId: string,
teamId: string,
configs: Configs,
teamName: string
): Promise<string | null>;
}
Loading