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

feat: update team role #625

Merged
merged 16 commits into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions backend/src/modules/teams/applications/update.team.application.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Inject, Injectable } from '@nestjs/common';

import TeamUserDto from '../dto/team.user.dto';
import { UpdateTeamApplicationInterface } from '../interfaces/applications/update.team.application.interface';
import { UpdateTeamServiceInterface } from '../interfaces/services/update.team.service.interface';
import { TYPES } from '../interfaces/types';

@Injectable()
export class UpdateTeamApplication implements UpdateTeamApplicationInterface {
constructor(
@Inject(TYPES.services.UpdateTeamService)
private updateTeamService: UpdateTeamServiceInterface
) {}

updateTeamUser(userId: string, teamId: string, teamData: TeamUserDto) {
return this.updateTeamService.updateTeamUser(userId, teamId, teamData);
}
}
52 changes: 50 additions & 2 deletions backend/src/modules/teams/controller/team.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ import {
import {
ApiBadRequestResponse,
ApiBearerAuth,
ApiBody,
ApiCreatedResponse,
ApiForbiddenResponse,
ApiInternalServerErrorResponse,
ApiNotFoundResponse,
ApiOkResponse,
ApiOperation,
ApiParam,
Expand All @@ -29,13 +32,16 @@ import {
import { TeamParams } from 'libs/dto/param/team.params';
import { TeamQueryParams } from 'libs/dto/param/team.query.params';
import { TeamRoles } from 'libs/enum/team.roles';
import { INSERT_FAILED } from 'libs/exceptions/messages';
import { INSERT_FAILED, UPDATE_FAILED } from 'libs/exceptions/messages';
import JwtAuthenticationGuard from 'libs/guards/jwtAuth.guard';
import RequestWithUser from 'libs/interfaces/requestWithUser.interface';
import { BadRequestResponse } from 'libs/swagger/errors/bad-request.swagger';
import { InternalServerErrorResponse } from 'libs/swagger/errors/internal-server-error.swagger';
import { UnauthorizedResponse } from 'libs/swagger/errors/unauthorized.swagger';

import { ForbiddenResponse } from '../../../libs/swagger/errors/forbidden.swagger';
import { NotFoundResponse } from '../../../libs/swagger/errors/not-found.swagger';
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';
Expand All @@ -52,7 +58,9 @@ export default class TeamsController {
@Inject(TYPES.applications.CreateTeamApplication)
private createTeamApp: CreateTeamApplicationInterface,
@Inject(TYPES.applications.GetTeamApplication)
private getTeamApp: GetTeamApplicationInterface
private getTeamApp: GetTeamApplicationInterface,
@Inject(TYPES.applications.UpdateTeamApplication)
private updateTeamApp: UpdateTeamApplication
) {}

@ApiOperation({ summary: 'Create a new team' })
Expand Down Expand Up @@ -170,4 +178,44 @@ export default class TeamsController {
getTeam(@Param() { teamId }: TeamParams, @Query() teamQueryParams?: TeamQueryParams) {
return this.getTeamApp.getTeam(teamId, teamQueryParams);
}

@ApiOperation({ summary: 'Update a specific team member' })
@ApiParam({ type: String, name: 'teamId', required: true })
@ApiBody({ type: TeamUserDto })
@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
})
@Put(':teamId')
async updateTeamUser(
@Req() request: RequestWithUser,
@Param() { teamId }: TeamParams,
@Body() teamData: TeamUserDto
) {
const teamUser = await this.updateTeamApp.updateTeamUser(request.user._id, teamId, teamData);

if (!teamUser) throw new BadRequestException(UPDATE_FAILED);

return teamUser;
}
}
1 change: 0 additions & 1 deletion backend/src/modules/teams/dto/team.user.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,5 @@ export default class TeamUserDto {
@ApiPropertyOptional()
@IsOptional()
@IsBoolean()
@IsMongoId()
isNewJoiner?: boolean;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { LeanDocument } from 'mongoose';

import TeamUserDto from '../../dto/team.user.dto';
import { TeamUserDocument } from '../../schemas/team.user.schema';

export interface UpdateTeamApplicationInterface {
updateTeamUser(
userId: string,
teamId: string,
teamData: TeamUserDto
): Promise<LeanDocument<TeamUserDocument> | null>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface GetTeamServiceInterface {

getUsersOfTeam(teamId: string): Promise<LeanDocument<TeamUserDocument>[]>;

getTeamUser(userId: string, boardId: string): Promise<LeanDocument<TeamUserDocument> | null>;
getTeamUser(userId: string, teamId: string): Promise<LeanDocument<TeamUserDocument> | null>;

getAllTeams(): Promise<LeanDocument<TeamDocument>[]>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { LeanDocument } from 'mongoose';

import TeamUserDto from '../../dto/team.user.dto';
import { TeamUserDocument } from '../../schemas/team.user.schema';

export interface UpdateTeamServiceInterface {
updateTeamUser(
userId: string,
teamId: string,
teamData: TeamUserDto
): Promise<LeanDocument<TeamUserDocument> | null>;
}
7 changes: 4 additions & 3 deletions backend/src/modules/teams/interfaces/types.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
export const TYPES = {
services: {
CreateTeamService: 'CreateTeamService',
GetTeamService: 'GetTeamService'
GetTeamService: 'GetTeamService',
UpdateTeamService: 'UpdateTeamService'
// DeleteTeamService: 'DeleteTeamService',
// UpdateTeamService: 'UpdateTeamService',
},
applications: {
CreateTeamApplication: 'CreateTeamApplication',
GetTeamApplication: 'GetTeamApplication'
GetTeamApplication: 'GetTeamApplication',
UpdateTeamApplication: 'UpdateTeamApplication'
// DeleteBoardApplication: 'DeleteBoardApplication',
// UpdateBoardApplication: 'UpdateBoardApplication',
}
Expand Down
12 changes: 12 additions & 0 deletions backend/src/modules/teams/providers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { CreateTeamApplication } from './applications/create.team.application';
import { GetTeamApplication } from './applications/get.team.application';
import { UpdateTeamApplication } from './applications/update.team.application';
import { TYPES } from './interfaces/types';
import CreateTeamService from './services/create.team.service';
import GetTeamService from './services/get.team.service';
import UpdateTeamService from './services/update.team.service';

export const createTeamService = {
provide: TYPES.services.CreateTeamService,
Expand All @@ -23,3 +25,13 @@ export const getTeamApplication = {
provide: TYPES.applications.GetTeamApplication,
useClass: GetTeamApplication
};

export const updateTeamService = {
provide: TYPES.services.UpdateTeamService,
useClass: UpdateTeamService
};

export const updateTeamApplication = {
provide: TYPES.applications.UpdateTeamApplication,
useClass: UpdateTeamApplication
};
47 changes: 47 additions & 0 deletions backend/src/modules/teams/services/update.team.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Inject, Injectable, NotFoundException } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { LeanDocument, Model } from 'mongoose';

import TeamUserDto from '../dto/team.user.dto';
import { GetTeamServiceInterface } from '../interfaces/services/get.team.service.interface';
import { UpdateTeamServiceInterface } from '../interfaces/services/update.team.service.interface';
import { TYPES } from '../interfaces/types';
import TeamUser, { TeamUserDocument } from '../schemas/team.user.schema';
import Team, { TeamDocument } from '../schemas/teams.schema';

@Injectable()
export default class UpdateTeamService implements UpdateTeamServiceInterface {
constructor(
@InjectModel(Team.name) private teamModel: Model<TeamDocument>,
@InjectModel(TeamUser.name) private teamUserModel: Model<TeamUserDocument>,
@Inject(TYPES.services.GetTeamService)
private getTeamService: GetTeamServiceInterface
) {}

async updateTeamUser(
userId: string,
teamId: string,
teamData: TeamUserDto
): Promise<LeanDocument<TeamUserDocument> | null> {
const team = await this.teamModel.findById(teamId).exec();

const teamUser = await this.getTeamService.getTeamUser(teamData.user, teamId);

if (!team) {
throw new NotFoundException('Team not found!');
}

if (!teamUser) {
throw new NotFoundException('Team member not found!');
}
CatiaAntunes96 marked this conversation as resolved.
Show resolved Hide resolved

return this.teamUserModel
.findOneAndUpdate(
{ user: teamData.user, team: teamId },
{ $set: { role: teamData.role, isNewJoiner: teamData.isNewJoiner } },
{ new: true }
)
.lean()
.exec();
}
}
15 changes: 12 additions & 3 deletions backend/src/modules/teams/teams.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,22 @@ import {
createTeamApplication,
createTeamService,
getTeamApplication,
getTeamService
getTeamService,
updateTeamApplication,
updateTeamService
} from './providers';

@Module({
imports: [mongooseTeamModule, mongooseTeamUserModule, forwardRef(() => BoardsModule)],
providers: [createTeamService, createTeamApplication, getTeamService, getTeamApplication],
providers: [
createTeamService,
createTeamApplication,
getTeamService,
getTeamApplication,
updateTeamService,
updateTeamApplication
],
controllers: [TeamsController],
exports: [getTeamApplication, getTeamService, createTeamService]
exports: [getTeamApplication, getTeamService, createTeamService, updateTeamService]
})
export default class TeamsModule {}
5 changes: 5 additions & 0 deletions frontend/src/api/teamService.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { GetServerSidePropsContext } from 'next';

import { CreateTeamDto, Team } from 'types/team/team';
import fetchData from 'utils/fetchData';
import { TeamUserUpdate } from '../types/team/team.user';

export const getAllTeams = (context?: GetServerSidePropsContext): Promise<Team[]> => {
return fetchData(`/teams`, { context, serverSide: !!context });
Expand All @@ -18,3 +19,7 @@ export const createTeamRequest = (newTeam: CreateTeamDto): Promise<Team> => {
export const getTeamRequest = (id: string, context?: GetServerSidePropsContext): Promise<Team> => {
return fetchData(`/teams/${id}`, { context, serverSide: !!context });
};

export const updateTeamUserRequest = (team: TeamUserUpdate): Promise<TeamUserUpdate> => {
return fetchData(`/teams/${team.team}`, { method: 'PUT', data: team });
};
Loading