Skip to content

Commit

Permalink
refactor: applied suggested changes on PR and and added new validatio…
Browse files Browse the repository at this point in the history
…n to updateSuperAdmin endpoint
  • Loading branch information
RafaelSBatista97 committed Dec 9, 2022
1 parent 0f23379 commit 807e4cf
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Inject, Injectable } from '@nestjs/common';
import RequestWithUser from 'src/libs/interfaces/requestWithUser.interface';
import UpdateUserDto from '../dto/update.user.dto';
import { UpdateUserApplication } from '../interfaces/applications/update.user.service.interface';
import { UpdateUserService } from '../interfaces/services/update.user.service.interface';
Expand All @@ -23,7 +24,7 @@ export class UpdateUserApplicationImpl implements UpdateUserApplication {
return this.updateUserService.checkEmail(token);
}

updateSuperAdmin(user: UpdateUserDto) {
return this.updateUserService.updateSuperAdmin(user);
updateSuperAdmin(user: UpdateUserDto, requestUser: RequestWithUser) {
return this.updateUserService.updateSuperAdmin(user, requestUser);
}
}
7 changes: 4 additions & 3 deletions backend/src/modules/users/controller/users.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Body, Controller, Get, Inject, Put, UseGuards } from '@nestjs/common';
import { Body, Controller, Get, Inject, Put, Req, UseGuards } from '@nestjs/common';
import {
ApiBadRequestResponse,
ApiBearerAuth,
Expand All @@ -25,6 +25,7 @@ import { SuperAdminGuard } from 'src/libs/guards/superAdmin.guard';
import { ForbiddenResponse } from '../../../libs/swagger/errors/forbidden.swagger';
import { NotFoundResponse } from '../../../libs/swagger/errors/not-found.swagger';
import { UpdateSuperAdminSwagger } from '../swagger/update.superadmin.swagger';
import RequestWithUser from 'src/libs/interfaces/requestWithUser.interface';

@ApiBearerAuth('access-token')
@ApiTags('Users')
Expand Down Expand Up @@ -112,7 +113,7 @@ export default class UsersController {
})
@UseGuards(SuperAdminGuard)
@Put('/sadmin')
updateUserSuperAdmin(@Body() userData: UpdateUserDto) {
return this.updateUserApp.updateSuperAdmin(userData);
updateUserSuperAdmin(@Req() request: RequestWithUser, @Body() userData: UpdateUserDto) {
return this.updateUserApp.updateSuperAdmin(userData, request);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { LeanDocument } from 'mongoose';
import UpdateUserDto from '../../dto/update.user.dto';
import User, { UserDocument } from '../../entities/user.schema';
import RequestWithUser from 'src/libs/interfaces/requestWithUser.interface';

export interface UpdateUserApplication {
setCurrentRefreshToken(
Expand All @@ -16,5 +17,8 @@ export interface UpdateUserApplication {

checkEmail(token: string): Promise<string>;

updateSuperAdmin(user: UpdateUserDto): Promise<LeanDocument<UserDocument>>;
updateSuperAdmin(
user: UpdateUserDto,
requestUser: RequestWithUser
): Promise<LeanDocument<UserDocument>>;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { LeanDocument } from 'mongoose';
import UpdateUserDto from '../../dto/update.user.dto';
import User, { UserDocument } from '../../entities/user.schema';
import RequestWithUser from 'src/libs/interfaces/requestWithUser.interface';

export interface UpdateUserService {
setCurrentRefreshToken(refreshToken: string, userId: string): Promise<User | null>;
Expand All @@ -13,5 +14,8 @@ export interface UpdateUserService {

checkEmail(token: string): Promise<string>;

updateSuperAdmin(user: UpdateUserDto): Promise<LeanDocument<UserDocument>>;
updateSuperAdmin(
user: UpdateUserDto,
requestUser: RequestWithUser
): Promise<LeanDocument<UserDocument>>;
}
14 changes: 10 additions & 4 deletions backend/src/modules/users/services/update.user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { TYPES } from '../interfaces/types';
import { UserRepositoryInterface } from '../repository/user.repository.interface';
import User, { UserDocument } from '../entities/user.schema';
import { UPDATE_FAILED } from 'src/libs/exceptions/messages';
import RequestWithUser from 'src/libs/interfaces/requestWithUser.interface';

@Injectable()
export default class updateUserServiceImpl implements UpdateUserService {
Expand Down Expand Up @@ -60,11 +61,16 @@ export default class updateUserServiceImpl implements UpdateUserService {
}
}

async updateSuperAdmin(user: UpdateUserDto) {
const userToUpdate = await this.userRepository.updateSuperAdmin(user._id, user.isSAdmin);
async updateSuperAdmin(user: UpdateUserDto, requestUser: RequestWithUser) {
if (requestUser.user._id.toString() === user._id) {
throw new BadRequestException(UPDATE_FAILED);
}
const userUpdated = await this.userRepository.updateSuperAdmin(user._id, user.isSAdmin);

if (!userToUpdate) throw new BadRequestException(UPDATE_FAILED);
if (!userUpdated) {
throw new BadRequestException(UPDATE_FAILED);
}

return userToUpdate;
return userUpdated;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,7 @@ const CardBody = React.memo<CardBodyProps>(({ userWithTeams }) => {
return 'no teams';
};

let teamsSeparatedByComma: string[] = [];
if (teamsNames) {
teamsSeparatedByComma = teamsNames?.map((team, index) => {
if (index !== teamsNames.length - 1) {
return team.concat(', ');
}
return team;
});
}
const teamsSeparatedByComma = teamsNames?.join(', ') || '';

return (
<Flex css={{ flex: '1 1 1', marginBottom: '$10' }} direction="column" gap="12">
Expand Down

0 comments on commit 807e4cf

Please sign in to comment.