generated from xgeekshq/oss-template
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4315497
commit 5b930b2
Showing
17 changed files
with
824 additions
and
1,062 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,18 @@ | ||
import { MongooseModule } from '@nestjs/mongoose'; | ||
import User, { UserSchema } from '../../modules/users/schemas/user.schema'; | ||
import Board, { BoardSchema } from '../../modules/boards/schemas/board.schema'; | ||
import BoardUser, { | ||
BoardUserSchema, | ||
} from '../../modules/boards/schemas/board.user.schema'; | ||
|
||
export const mongooseBoardModule = MongooseModule.forFeature([ | ||
{ name: Board.name, schema: BoardSchema }, | ||
]); | ||
|
||
export const mongooseBoardUserModule = MongooseModule.forFeature([ | ||
{ name: BoardUser.name, schema: BoardUserSchema }, | ||
]); | ||
|
||
export const mongooseUserModule = MongooseModule.forFeature([ | ||
{ name: User.name, schema: UserSchema }, | ||
]); |
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,5 @@ | ||
export enum BoardRoles { | ||
OWNER = 'owner', | ||
MEMBER = 'member', | ||
RESPONSIBLE = 'responsible', | ||
} |
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,21 @@ | ||
import { | ||
ValidatorConstraint, | ||
ValidatorConstraintInterface, | ||
} from 'class-validator'; | ||
import BoardUserDto from '../../modules/boards/dto/board.user.dto'; | ||
|
||
@ValidatorConstraint({ name: 'checkUniqueUsers', async: false }) | ||
export class CheckUniqueUsers implements ValidatorConstraintInterface { | ||
validate(users: BoardUserDto[]) { | ||
const usersIds = users.map((user) => user.user); | ||
if (usersIds.length === new Set(usersIds).size) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
defaultMessage() { | ||
return 'Duplicate users are not allowed'; | ||
} | ||
} |
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
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,24 @@ | ||
import { | ||
IsString, | ||
IsNotEmpty, | ||
IsOptional, | ||
IsMongoId, | ||
IsEnum, | ||
} from 'class-validator'; | ||
import { BoardRoles } from '../../../libs/enum/board.roles'; | ||
|
||
export default class BoardUserDto { | ||
@IsOptional() | ||
@IsMongoId() | ||
_id?: string; | ||
|
||
@IsString() | ||
@IsNotEmpty() | ||
@IsEnum(BoardRoles, { each: true }) | ||
role!: string; | ||
|
||
@IsMongoId() | ||
@IsString() | ||
@IsNotEmpty() | ||
user!: string; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { ObjectId, SchemaTypes, Document } from 'mongoose'; | ||
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; | ||
import { BoardRoles } from '../../../libs/enum/board.roles'; | ||
import User from '../../users/schemas/user.schema'; | ||
|
||
export type BoardUserDocument = BoardUser & Document; | ||
|
||
@Schema({ | ||
toJSON: { | ||
virtuals: true, | ||
}, | ||
}) | ||
export default class BoardUser { | ||
@Prop({ | ||
nullable: false, | ||
type: String, | ||
enum: [BoardRoles.RESPONSIBLE, BoardRoles.MEMBER, BoardRoles.OWNER], | ||
}) | ||
role!: string; | ||
|
||
@Prop({ type: SchemaTypes.ObjectId, ref: 'User', nullable: false }) | ||
user!: User | ObjectId; | ||
|
||
@Prop({ type: SchemaTypes.ObjectId, ref: 'Board', nullable: false }) | ||
board!: ObjectId; | ||
|
||
@Prop({ nullable: false }) | ||
votesCount!: number; | ||
} | ||
|
||
export const BoardUserSchema = SchemaFactory.createForClass(BoardUser); |
67 changes: 63 additions & 4 deletions
67
backend/src/modules/boards/services/create.board.service.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 |
---|---|---|
@@ -1,24 +1,83 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { InjectModel } from '@nestjs/mongoose'; | ||
import { Model } from 'mongoose'; | ||
import { BoardRoles } from '../../../libs/enum/board.roles'; | ||
import { encrypt } from '../../../libs/utils/bcrypt'; | ||
import isEmpty from '../../../libs/utils/isEmpty'; | ||
import BoardDto from '../dto/board.dto'; | ||
import BoardUserDto from '../dto/board.user.dto'; | ||
import { CreateBoardService } from '../interfaces/services/create.board.service.interface'; | ||
import Board, { BoardDocument } from '../schemas/board.schema'; | ||
import BoardUser, { BoardUserDocument } from '../schemas/board.user.schema'; | ||
|
||
@Injectable() | ||
export default class CreateBoardServiceImpl implements CreateBoardService { | ||
constructor( | ||
@InjectModel(Board.name) private boardModel: Model<BoardDocument>, | ||
@InjectModel(BoardUser.name) | ||
private boardUserModel: Model<BoardUserDocument>, | ||
) {} | ||
|
||
async create(boardData: BoardDto, userId: string) { | ||
if (boardData.password) { | ||
boardData.password = await encrypt(boardData.password); | ||
} | ||
saveBoardUsers(newUsers: BoardUserDto[], newBoardId: string) { | ||
Promise.all( | ||
newUsers.map((user) => | ||
this.boardUserModel.create({ ...user, board: newBoardId }), | ||
), | ||
); | ||
} | ||
|
||
async createDividedBoards(boards: BoardDto[], userId: string) { | ||
const newBoardsIds = await Promise.allSettled( | ||
boards.map(async (board) => { | ||
const { users } = board; | ||
const { _id } = await this.create(board, userId); | ||
if (!isEmpty(users)) { | ||
this.saveBoardUsers(users, _id); | ||
} | ||
return _id; | ||
}), | ||
); | ||
return newBoardsIds.flatMap((result) => | ||
result.status === 'fulfilled' ? [result.value] : [], | ||
); | ||
} | ||
|
||
async createBoard(boardData: BoardDto, userId: string) { | ||
const { dividedBoards } = boardData; | ||
return this.boardModel.create({ | ||
...boardData, | ||
createdBy: userId, | ||
dividedBoards: await this.createDividedBoards( | ||
dividedBoards ?? [], | ||
userId, | ||
), | ||
}); | ||
} | ||
|
||
addOwner(users: BoardUserDto[], userId: string) { | ||
return [ | ||
...users, | ||
{ | ||
user: userId.toString(), | ||
role: BoardRoles.OWNER, | ||
}, | ||
]; | ||
} | ||
|
||
async create(boardData: BoardDto, userId: string) { | ||
const { password, users } = boardData; | ||
if (password) { | ||
boardData.password = await encrypt(password); | ||
} | ||
|
||
const newBoard = await this.createBoard(boardData, userId); | ||
|
||
const newUsers = this.addOwner(users, userId); | ||
|
||
if (!isEmpty(newUsers)) { | ||
this.saveBoardUsers(newUsers, newBoard._id); | ||
} | ||
|
||
return newBoard; | ||
} | ||
} |
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
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.