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 e7f8fcd
Showing
17 changed files
with
816 additions
and
1,058 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 * as mongoose 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 & mongoose.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: mongoose.Schema.Types.ObjectId, ref: 'User', nullable: false }) | ||
user!: User | mongoose.Schema.Types.ObjectId; | ||
|
||
@Prop({ type: mongoose.Schema.Types.ObjectId, ref: 'Board', nullable: false }) | ||
board!: mongoose.Schema.Types.ObjectId; | ||
|
||
@Prop({ nullable: false }) | ||
votesCount!: number; | ||
} | ||
|
||
export const BoardUserSchema = SchemaFactory.createForClass(BoardUser); |
61 changes: 58 additions & 3 deletions
61
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,79 @@ | ||
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 { 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 createDividedBoards(boards: BoardDto[], userId: string) { | ||
const newBoardsIds: string[] = []; | ||
|
||
await boards.reduce(async (previous, current) => { | ||
await previous; | ||
const newBoard = await this.boardModel.create({ | ||
...current, | ||
userId, | ||
isSubBoard: true, | ||
}); | ||
const { users } = current; | ||
newBoardsIds.push(newBoard._id); | ||
|
||
if (!isEmpty(users)) { | ||
await users.reduce(async (prevUser, currentUser) => { | ||
await prevUser; | ||
await this.boardUserModel.create({ | ||
...currentUser, | ||
board: newBoard._id, | ||
}); | ||
}, Promise.resolve()); | ||
} | ||
}, Promise.resolve()); | ||
return newBoardsIds; | ||
} | ||
|
||
async create(boardData: BoardDto, userId: string) { | ||
if (boardData.password) { | ||
boardData.password = await encrypt(boardData.password); | ||
const { password, dividedBoards, users } = boardData; | ||
if (password) { | ||
boardData.password = await encrypt(password); | ||
} | ||
return this.boardModel.create({ | ||
|
||
const newBoard = await this.boardModel.create({ | ||
...boardData, | ||
createdBy: userId, | ||
dividedBoards: await this.createDividedBoards( | ||
dividedBoards ?? [], | ||
userId, | ||
), | ||
}); | ||
|
||
const newUsers = [...users]; | ||
newUsers.push({ | ||
user: userId.toString(), | ||
role: BoardRoles.OWNER, | ||
}); | ||
|
||
if (!isEmpty(newUsers)) { | ||
await newUsers.reduce(async (prevUser, currentUser) => { | ||
await prevUser; | ||
await this.boardUserModel.create({ | ||
...currentUser, | ||
board: newBoard._id, | ||
}); | ||
}, Promise.resolve()); | ||
} | ||
|
||
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.