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 5 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
26 changes: 26 additions & 0 deletions backend/src/libs/guards/teamRoles.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';

import TeamUser, { TeamUserDocument } from '../../modules/teams/schemas/team.user.schema';

@Injectable()
export class TeamUserGuard implements CanActivate {
constructor(
private readonly reflector: Reflector,
@InjectModel(TeamUser.name) private teamUserModel: Model<TeamUserDocument>
) {}

async canActivate(context: ExecutionContext) {
const permission = this.reflector.get<string>('permission', context.getHandler());
const request = context.switchToHttp().getRequest();

const user = request.user;
const team: string = request.params.teamId;

const userFound = await this.teamUserModel.findOne({ user: user._id, teamId: team }).exec();

return user.isSAdmin || permission === userFound?.role;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class UpdateTeamApplication implements UpdateTeamApplicationInterface {
private updateTeamService: UpdateTeamServiceInterface
) {}

updateTeamUser(userId: string, teamId: string, teamData: TeamUserDto) {
return this.updateTeamService.updateTeamUser(userId, teamId, teamData);
updateTeamUser(teamData: TeamUserDto) {
return this.updateTeamService.updateTeamUser(teamData);
}
}
14 changes: 8 additions & 6 deletions backend/src/modules/teams/controller/team.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Put,
Query,
Req,
SetMetadata,
UseGuards,
UsePipes,
ValidationPipe
Expand Down Expand Up @@ -39,6 +40,7 @@ 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 { TeamUserGuard } from '../../../libs/guards/teamRoles.guard';
import { ForbiddenResponse } from '../../../libs/swagger/errors/forbidden.swagger';
import { NotFoundResponse } from '../../../libs/swagger/errors/not-found.swagger';
import { UpdateTeamApplication } from '../applications/update.team.application';
Expand All @@ -49,6 +51,8 @@ import { CreateTeamApplicationInterface } from '../interfaces/applications/creat
import { GetTeamApplicationInterface } from '../interfaces/applications/get.team.application.interface';
import { TYPES } from '../interfaces/types';

const TeamUser = (permission: string) => SetMetadata('permission', permission);

@ApiBearerAuth('access-token')
@ApiTags('Teams')
@UseGuards(JwtAuthenticationGuard)
Expand Down Expand Up @@ -206,13 +210,11 @@ export default class TeamsController {
description: 'Internal Server Error',
type: InternalServerErrorResponse
})
@TeamUser('admin')
CatiaAntunes96 marked this conversation as resolved.
Show resolved Hide resolved
@UseGuards(TeamUserGuard)
@Put(':teamId')
async updateTeamUser(
@Req() request: RequestWithUser,
@Param() { teamId }: TeamParams,
@Body() teamData: TeamUserDto
) {
const teamUser = await this.updateTeamApp.updateTeamUser(request.user._id, teamId, teamData);
async updateTeamUser(@Body() teamData: TeamUserDto) {
const teamUser = await this.updateTeamApp.updateTeamUser(teamData);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,5 @@ 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>;
updateTeamUser(teamData: TeamUserDto): Promise<LeanDocument<TeamUserDocument> | null>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,5 @@ 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>;
updateTeamUser(teamData: TeamUserDto): Promise<LeanDocument<TeamUserDocument> | null>;
}
32 changes: 4 additions & 28 deletions backend/src/modules/teams/services/update.team.service.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,19 @@
import { Inject, Injectable, NotFoundException } from '@nestjs/common';
import { Injectable } 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!');
}
constructor(@InjectModel(TeamUser.name) private teamUserModel: Model<TeamUserDocument>) {}

updateTeamUser(teamData: TeamUserDto): Promise<LeanDocument<TeamUserDocument> | null> {
return this.teamUserModel
.findOneAndUpdate(
{ user: teamData.user, team: teamId },
{ user: teamData.user, team: teamData.team },
{ $set: { role: teamData.role, isNewJoiner: teamData.isNewJoiner } },
{ new: true }
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ const PopoverRoleSettings: React.FC<PopoverRoleSettingsProps> = React.memo(
}
};

const handleSelectFunction = (role: TeamUserRoles) => {
return isTeamPage ? updateUserRole(role) : selectRole(role);
};

return (
<Popover>
<PopoverTriggerStyled
Expand All @@ -70,8 +74,7 @@ const PopoverRoleSettings: React.FC<PopoverRoleSettingsProps> = React.memo(
align="end"
direction="column"
onClick={() => {
if (isTeamPage) updateUserRole(TeamUserRoles.MEMBER);
else selectRole(TeamUserRoles.MEMBER);
handleSelectFunction(TeamUserRoles.MEMBER);
}}
>
<Text css={{ textAlign: 'end' }} size="sm" weight="medium">
Expand All @@ -88,8 +91,7 @@ const PopoverRoleSettings: React.FC<PopoverRoleSettingsProps> = React.memo(
align="end"
direction="column"
onClick={() => {
if (isTeamPage) updateUserRole(TeamUserRoles.ADMIN);
else selectRole(TeamUserRoles.ADMIN);
handleSelectFunction(TeamUserRoles.ADMIN);
}}
>
<Text css={{ textAlign: 'end' }} size="sm" weight="medium">
Expand All @@ -106,8 +108,7 @@ const PopoverRoleSettings: React.FC<PopoverRoleSettingsProps> = React.memo(
align="end"
direction="column"
onClick={() => {
if (isTeamPage) updateUserRole(TeamUserRoles.STAKEHOLDER);
else selectRole(TeamUserRoles.STAKEHOLDER);
handleSelectFunction(TeamUserRoles.STAKEHOLDER);
}}
>
<Text css={{ textAlign: 'end' }} size="sm" weight="medium">
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/components/Teams/CreateTeam/CardMember/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const CardMember = React.memo<CardBodyProps>(
</StyledMemberTitle>
</Flex>
</Flex>
{isTeamMemberOrStakeholder ? (
{isTeamMemberOrStakeholder && isNewJoiner && (
<Flex align="center" css={{ width: '35%' }} gap="8" justify="end">
<Text size="sm" weight="medium">
New Joiner
Expand All @@ -97,7 +97,8 @@ const CardMember = React.memo<CardBodyProps>(
</IconButton>
</Tooltip>
</Flex>
) : (
)}
{!isTeamMemberOrStakeholder && (
<Flex align="center" css={{ width: '23%' }} gap="8" justify="center">
<ConfigurationSettings
handleCheckedChange={handleIsNewJoiner}
Expand Down