Skip to content

Commit

Permalink
refactor: delete board (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
nunocaseiro authored Apr 12, 2022
1 parent 1d569d6 commit e72638b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 9 deletions.
4 changes: 2 additions & 2 deletions backend/src/modules/auth/shared/login.auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ export const signIn = async (
getTokenService: GetTokenAuthService | GetTokenAuthApplication,
strategy: string,
) => {
const { email, firstName, lastName, _id } = user;
const { email, firstName, lastName, _id, isSAdmin } = user;
const jwt = await getTokenService.getTokens(_id);

if (!jwt) return null;

return { ...jwt, email, firstName, lastName, strategy, id: _id };
return { ...jwt, email, firstName, lastName, strategy, id: _id, isSAdmin };
};
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default class CreateBoardServiceImpl implements CreateBoardService {
const newBoardsIds = await Promise.allSettled(
boards.map(async (board) => {
const { users } = board;
const { _id } = await this.create(board, userId);
const { _id } = await this.createBoard(board, userId);

if (!isEmpty(users)) {
this.saveBoardUsers(users, _id);
Expand Down
77 changes: 71 additions & 6 deletions backend/src/modules/boards/services/delete.board.service.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,87 @@
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { Model, ObjectId, ClientSession } from 'mongoose';
import { DELETE_FAILED } from '../../../libs/exceptions/messages';
import isEmpty from '../../../libs/utils/isEmpty';
import { DeleteBoardService } from '../interfaces/services/delete.board.service.interface';
import Board, { BoardDocument } from '../schemas/board.schema';
import BoardUser, { BoardUserDocument } from '../schemas/board.user.schema';

@Injectable()
export default class DeleteBoardServiceImpl implements DeleteBoardService {
constructor(
@InjectModel(Board.name) private boardModel: Model<BoardDocument>,
@InjectModel(BoardUser.name)
private boardUserModel: Model<BoardUserDocument>,
) {}

async deleteSubBoards(
dividedBoards: Board[] | ObjectId[],
boardSession: ClientSession,
) {
const { deletedCount } = await this.boardModel
.deleteMany({ _id: { $in: dividedBoards } }, { session: boardSession })
.exec();

if (deletedCount !== dividedBoards.length) throw Error(DELETE_FAILED);
}

async deleteBoardUsers(
dividedBoards: Board[] | ObjectId[],
boardSession: ClientSession,
boardId: ObjectId,
) {
const { deletedCount } = await this.boardUserModel
.deleteMany(
{ board: { $in: [...dividedBoards, boardId] } },
{ session: boardSession },
)
.exec();

if (deletedCount <= 0) throw Error(DELETE_FAILED);
}

async deleteBoard(
boardId: string,
userId: string,
boardSession: ClientSession,
) {
const result = await this.boardModel.findOneAndRemove(
{
_id: boardId,
createdBy: userId,
},
{ session: boardSession },
);

if (!result) throw Error(DELETE_FAILED);
return { dividedBoards: result.dividedBoards, _id: result._id };
}

async delete(boardId: string, userId: string) {
const result = await this.boardModel.deleteOne({
_id: boardId,
createdBy: userId,
});
const boardSession = await this.boardModel.db.startSession();
boardSession.startTransaction();

try {
const { _id, dividedBoards } = await this.deleteBoard(
boardId,
userId,
boardSession,
);

if (!isEmpty(dividedBoards)) {
await this.deleteSubBoards(dividedBoards, boardSession);

await this.deleteBoardUsers(dividedBoards, boardSession, _id);
}

if (result.deletedCount === 1) return true;
await boardSession.commitTransaction();
return true;
} catch (e) {
await boardSession.abortTransaction();
} finally {
await boardSession.endSession();
}

return false;
}
Expand Down

0 comments on commit e72638b

Please sign in to comment.