From 477fb3ebd566051e8c4e1bfcd52c5c9cb6314cdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patr=C3=ADcia=20Dias?= Date: Fri, 9 Dec 2022 18:44:26 +0000 Subject: [PATCH 1/3] feat: backend removes and adds team members --- .../applications/update.team.application.ts | 3 ++ .../teams/controller/team.controller.ts | 35 +++++++++++++++ .../modules/teams/dto/update.team.user.dto.ts | 13 ++++++ .../update.team.application.interface.ts | 1 + .../services/update.team.service.interface.ts | 1 + .../teams/services/update.team.service.ts | 43 ++++++++++++++++++- 6 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 backend/src/modules/teams/dto/update.team.user.dto.ts diff --git a/backend/src/modules/teams/applications/update.team.application.ts b/backend/src/modules/teams/applications/update.team.application.ts index 305da87c8..0e59722ef 100644 --- a/backend/src/modules/teams/applications/update.team.application.ts +++ b/backend/src/modules/teams/applications/update.team.application.ts @@ -14,4 +14,7 @@ export class UpdateTeamApplication implements UpdateTeamApplicationInterface { updateTeamUser(teamData: TeamUserDto) { return this.updateTeamService.updateTeamUser(teamData); } + addAndRemoveTeamUsers(addUsers: TeamUserDto[], removeUsers: string[]) { + return this.updateTeamService.addAndRemoveTeamUsers(addUsers, removeUsers); + } } diff --git a/backend/src/modules/teams/controller/team.controller.ts b/backend/src/modules/teams/controller/team.controller.ts index a2e405af7..9b1cf0f1a 100644 --- a/backend/src/modules/teams/controller/team.controller.ts +++ b/backend/src/modules/teams/controller/team.controller.ts @@ -47,6 +47,7 @@ import { UpdateTeamApplication } from '../applications/update.team.application'; import { CreateTeamDto } from '../dto/crate-team.dto'; import TeamDto from '../dto/team.dto'; import TeamUserDto from '../dto/team.user.dto'; +import UpdateTeamUserDto from '../dto/update.team.user.dto'; import { CreateTeamApplicationInterface } from '../interfaces/applications/create.team.application.interface'; import { GetTeamApplicationInterface } from '../interfaces/applications/get.team.application.interface'; import { TYPES } from '../interfaces/types'; @@ -225,6 +226,40 @@ export default class TeamsController { return teamUser; } + @ApiOperation({ summary: 'Add and remove team members' }) + @ApiParam({ type: String, name: 'teamId', required: true }) + @ApiBody({ type: UpdateTeamUserDto }) + @ApiOkResponse({ + type: TeamUserDto, + description: 'Team member updated successfully!' + }) + @ApiBadRequestResponse({ + description: 'Bad Request', + type: BadRequestResponse + }) + @ApiUnauthorizedResponse({ + description: 'Unauthorized', + type: UnauthorizedResponse + }) + @ApiNotFoundResponse({ + type: NotFoundResponse, + description: 'Not found!' + }) + @ApiForbiddenResponse({ + description: 'Forbidden', + type: ForbiddenResponse + }) + @ApiInternalServerErrorResponse({ + description: 'Internal Server Error', + type: InternalServerErrorResponse + }) + @TeamUser([TeamRoles.ADMIN, TeamRoles.STAKEHOLDER]) + @UseGuards(TeamUserGuard) + @Put('/:teamId/addAndRemove') + addAndRemoveTeamUsers(@Body() users: UpdateTeamUserDto) { + return this.updateTeamApp.addAndRemoveTeamUsers(users.addUsers, users.removeUsers); + } + @ApiOperation({ summary: 'Delete a specific team' }) @ApiParam({ type: String, name: 'teamId', required: true }) @ApiOkResponse({ type: Boolean, description: 'Team successfully deleted!' }) diff --git a/backend/src/modules/teams/dto/update.team.user.dto.ts b/backend/src/modules/teams/dto/update.team.user.dto.ts new file mode 100644 index 000000000..95560b75b --- /dev/null +++ b/backend/src/modules/teams/dto/update.team.user.dto.ts @@ -0,0 +1,13 @@ +import TeamUserDto from 'src/modules/teams/dto/team.user.dto'; +import { ApiProperty } from '@nestjs/swagger'; +import { IsArray } from 'class-validator'; + +export default class UpdateTeamUserDto { + @ApiProperty({ description: 'List of users to add on team' }) + @IsArray() + addUsers!: TeamUserDto[]; + + @ApiProperty({ description: 'List of users ids to remove from team' }) + @IsArray() + removeUsers!: string[]; +} diff --git a/backend/src/modules/teams/interfaces/applications/update.team.application.interface.ts b/backend/src/modules/teams/interfaces/applications/update.team.application.interface.ts index 4219c06b7..e8d68c1b3 100644 --- a/backend/src/modules/teams/interfaces/applications/update.team.application.interface.ts +++ b/backend/src/modules/teams/interfaces/applications/update.team.application.interface.ts @@ -4,4 +4,5 @@ import { TeamUserDocument } from '../../schemas/team.user.schema'; export interface UpdateTeamApplicationInterface { updateTeamUser(teamData: TeamUserDto): Promise | null>; + addAndRemoveTeamUsers(addUsers: TeamUserDto[], removeUsers: string[]): Promise; } diff --git a/backend/src/modules/teams/interfaces/services/update.team.service.interface.ts b/backend/src/modules/teams/interfaces/services/update.team.service.interface.ts index be73657cd..d2c3e1093 100644 --- a/backend/src/modules/teams/interfaces/services/update.team.service.interface.ts +++ b/backend/src/modules/teams/interfaces/services/update.team.service.interface.ts @@ -4,4 +4,5 @@ import { TeamUserDocument } from '../../schemas/team.user.schema'; export interface UpdateTeamServiceInterface { updateTeamUser(teamData: TeamUserDto): Promise | null>; + addAndRemoveTeamUsers(addUsers: TeamUserDto[], removeUsers: string[]): Promise; } diff --git a/backend/src/modules/teams/services/update.team.service.ts b/backend/src/modules/teams/services/update.team.service.ts index eb2b94f04..fda8210ce 100644 --- a/backend/src/modules/teams/services/update.team.service.ts +++ b/backend/src/modules/teams/services/update.team.service.ts @@ -1,6 +1,7 @@ -import { Injectable } from '@nestjs/common'; +import { BadRequestException, Injectable } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; -import { LeanDocument, Model } from 'mongoose'; +import { ClientSession, LeanDocument, Model } from 'mongoose'; +import { DELETE_FAILED, INSERT_FAILED, UPDATE_FAILED } from 'src/libs/exceptions/messages'; import TeamUserDto from '../dto/team.user.dto'; import { UpdateTeamServiceInterface } from '../interfaces/services/update.team.service.interface'; import TeamUser, { TeamUserDocument } from '../schemas/team.user.schema'; @@ -19,4 +20,42 @@ export default class UpdateTeamService implements UpdateTeamServiceInterface { .lean() .exec(); } + + async addAndRemoveTeamUsers(addUsers: TeamUserDto[], removeUsers: string[]) { + const teamUserSession = await this.teamUserModel.db.startSession(); + teamUserSession.startTransaction(); + try { + if (addUsers.length > 0) await this.addTeamUsers(addUsers); + + if (removeUsers.length > 0) await this.deleteTeamUsers(removeUsers, teamUserSession); + + await teamUserSession.commitTransaction(); + + return true; + } catch (error) { + await teamUserSession.abortTransaction(); + } finally { + await teamUserSession.endSession(); + } + throw new BadRequestException(UPDATE_FAILED); + } + + async addTeamUsers(teamUsers: TeamUserDto[]) { + const { length } = await this.teamUserModel.insertMany(teamUsers); + + if (length < 1) throw new Error(INSERT_FAILED); + } + + async deleteTeamUsers(teamUsers: string[], teamUserSession: ClientSession) { + const { deletedCount } = await this.teamUserModel + .deleteMany( + { + _id: teamUsers + }, + { session: teamUserSession } + ) + .exec(); + + if (deletedCount <= 0) throw new Error(DELETE_FAILED); + } } From 3911373b0ea644f8ab5d10673fcf3e61949796d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patr=C3=ADcia=20Dias?= Date: Mon, 12 Dec 2022 10:01:47 +0000 Subject: [PATCH 2/3] feat: endpoint retrieves list of created team users --- .../update.team.application.interface.ts | 5 ++++- .../services/update.team.service.interface.ts | 5 ++++- .../modules/teams/services/update.team.service.ts | 12 ++++++++---- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/backend/src/modules/teams/interfaces/applications/update.team.application.interface.ts b/backend/src/modules/teams/interfaces/applications/update.team.application.interface.ts index e8d68c1b3..628addb3d 100644 --- a/backend/src/modules/teams/interfaces/applications/update.team.application.interface.ts +++ b/backend/src/modules/teams/interfaces/applications/update.team.application.interface.ts @@ -4,5 +4,8 @@ import { TeamUserDocument } from '../../schemas/team.user.schema'; export interface UpdateTeamApplicationInterface { updateTeamUser(teamData: TeamUserDto): Promise | null>; - addAndRemoveTeamUsers(addUsers: TeamUserDto[], removeUsers: string[]): Promise; + addAndRemoveTeamUsers( + addUsers: TeamUserDto[], + removeUsers: string[] + ): Promise>; } diff --git a/backend/src/modules/teams/interfaces/services/update.team.service.interface.ts b/backend/src/modules/teams/interfaces/services/update.team.service.interface.ts index d2c3e1093..0d2e753c0 100644 --- a/backend/src/modules/teams/interfaces/services/update.team.service.interface.ts +++ b/backend/src/modules/teams/interfaces/services/update.team.service.interface.ts @@ -4,5 +4,8 @@ import { TeamUserDocument } from '../../schemas/team.user.schema'; export interface UpdateTeamServiceInterface { updateTeamUser(teamData: TeamUserDto): Promise | null>; - addAndRemoveTeamUsers(addUsers: TeamUserDto[], removeUsers: string[]): Promise; + addAndRemoveTeamUsers( + addUsers: TeamUserDto[], + removeUsers: string[] + ): Promise>; } diff --git a/backend/src/modules/teams/services/update.team.service.ts b/backend/src/modules/teams/services/update.team.service.ts index fda8210ce..57b2c5320 100644 --- a/backend/src/modules/teams/services/update.team.service.ts +++ b/backend/src/modules/teams/services/update.team.service.ts @@ -25,13 +25,15 @@ export default class UpdateTeamService implements UpdateTeamServiceInterface { const teamUserSession = await this.teamUserModel.db.startSession(); teamUserSession.startTransaction(); try { - if (addUsers.length > 0) await this.addTeamUsers(addUsers); + let createdTeamUsers: TeamUserDocument[] = []; + + if (addUsers.length > 0) createdTeamUsers = await this.addTeamUsers(addUsers); if (removeUsers.length > 0) await this.deleteTeamUsers(removeUsers, teamUserSession); await teamUserSession.commitTransaction(); - return true; + return createdTeamUsers; } catch (error) { await teamUserSession.abortTransaction(); } finally { @@ -41,9 +43,11 @@ export default class UpdateTeamService implements UpdateTeamServiceInterface { } async addTeamUsers(teamUsers: TeamUserDto[]) { - const { length } = await this.teamUserModel.insertMany(teamUsers); + const createdTeamUsers = await this.teamUserModel.insertMany(teamUsers); + + if (createdTeamUsers.length < 1) throw new Error(INSERT_FAILED); - if (length < 1) throw new Error(INSERT_FAILED); + return createdTeamUsers; } async deleteTeamUsers(teamUsers: string[], teamUserSession: ClientSession) { From b5e1d2ff5277c679ffc5bad156883285eff0dee9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patr=C3=ADcia=20Dias?= Date: Mon, 12 Dec 2022 16:20:07 +0000 Subject: [PATCH 3/3] fix: removed unused function --- .../repositories/mongo/mongo-generic.repository.ts | 13 ------------- .../modules/teams/services/update.team.service.ts | 5 ++--- 2 files changed, 2 insertions(+), 16 deletions(-) diff --git a/backend/src/libs/repositories/mongo/mongo-generic.repository.ts b/backend/src/libs/repositories/mongo/mongo-generic.repository.ts index 0d70ee29b..d7e52ea7d 100644 --- a/backend/src/libs/repositories/mongo/mongo-generic.repository.ts +++ b/backend/src/libs/repositories/mongo/mongo-generic.repository.ts @@ -86,19 +86,6 @@ export class MongoGenericRepository implements BaseInterfaceRepository { return deletedCount; } - async deleteManyByListOfIds(teamUserIds: string[], withSession: boolean): Promise { - const { deletedCount } = await this._repository - .deleteMany( - { - _id: teamUserIds - }, - { session: withSession ? this._session : undefined } - ) - .exec(); - - return deletedCount; - } - async startTransaction() { this._session = await this._repository.db.startSession(); this._session.startTransaction(); diff --git a/backend/src/modules/teams/services/update.team.service.ts b/backend/src/modules/teams/services/update.team.service.ts index ab4133bd7..2749c9fda 100644 --- a/backend/src/modules/teams/services/update.team.service.ts +++ b/backend/src/modules/teams/services/update.team.service.ts @@ -21,10 +21,9 @@ export default class UpdateTeamService implements UpdateTeamServiceInterface { try { let createdTeamUsers: TeamUser[] = []; - if (addUsers.length > 0) - createdTeamUsers = await this.teamUserRepository.insertMany(addUsers); + if (addUsers.length > 0) createdTeamUsers = await this.addTeamUsers(addUsers); - if (removeUsers.length > 0) await this.teamUserRepository.deleteMany(removeUsers, true); + if (removeUsers.length > 0) await this.deleteTeamUsers(removeUsers, true); return createdTeamUsers; } catch (error) {