diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..94770e3 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,22 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Debug NestJs", + "request": "launch", + "runtimeArgs": [ + "run", + "start:debug" + ], + "runtimeExecutable": "npm", + "skipFiles": [ + "/**" + ], + "type": "node", + "console": "integratedTerminal" + } + ] +} diff --git a/package-lock.json b/package-lock.json index b093142..945e3e1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,7 @@ "dependencies": { "@aws-sdk/client-secrets-manager": "^3.112.0", "@dadosfera/dadosfera-logs": "^1.0.0-beta.3", - "@dadosfera/protospack-v2": "3.10.0", + "@dadosfera/protospack-v2": "^3.11.0-beta.5", "@grpc/grpc-js": "^1.6.7", "@grpc/proto-loader": "^0.6.13", "@nestjs/common": "^8.4.7", @@ -1723,9 +1723,9 @@ } }, "node_modules/@dadosfera/protospack-v2": { - "version": "3.10.0", - "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/@dadosfera/protospack-v2/-/protospack-v2-3.10.0.tgz", - "integrity": "sha512-XPfRh2R2zZtN/MRxAwbC994xDJftRlnl3w2vqPGMt996tJCpZkedzdH1hHdqer68QDIMM5td0OpW/Gs8K1H07A==", + "version": "3.11.0-beta.5", + "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/@dadosfera/protospack-v2/-/protospack-v2-3.11.0-beta.5.tgz", + "integrity": "sha512-rzCUlcGaeTgzW1Bu4t5J1w1o9cQjTrRpYT68HJnnb0NZJJ0QzS22MxCwd3S+WgfP5XnelFiw+H9tgffJy2n6lA==", "dependencies": { "@grpc/grpc-js": "^1.6.7", "rxjs": "^7.5.5", @@ -12287,9 +12287,9 @@ } }, "@dadosfera/protospack-v2": { - "version": "3.10.0", - "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/@dadosfera/protospack-v2/-/protospack-v2-3.10.0.tgz", - "integrity": "sha512-XPfRh2R2zZtN/MRxAwbC994xDJftRlnl3w2vqPGMt996tJCpZkedzdH1hHdqer68QDIMM5td0OpW/Gs8K1H07A==", + "version": "3.11.0-beta.5", + "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/@dadosfera/protospack-v2/-/protospack-v2-3.11.0-beta.5.tgz", + "integrity": "sha512-rzCUlcGaeTgzW1Bu4t5J1w1o9cQjTrRpYT68HJnnb0NZJJ0QzS22MxCwd3S+WgfP5XnelFiw+H9tgffJy2n6lA==", "requires": { "@grpc/grpc-js": "^1.6.7", "rxjs": "^7.5.5", diff --git a/package.json b/package.json index 3b5ed08..9e7de85 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,8 @@ }, "dependencies": { "@aws-sdk/client-secrets-manager": "^3.112.0", + "@dadosfera/dadosfera-logs": "^1.0.0-beta.3", + "@dadosfera/protospack-v2": "3.11.0-beta.5", "@grpc/grpc-js": "^1.6.7", "@grpc/proto-loader": "^0.6.13", "@nestjs/common": "^8.4.7", @@ -38,7 +40,6 @@ "@nestjs/swagger": "^5.2.1", "axios": "^0.25.0", "cron-parser": "^4.4.0", - "@dadosfera/dadosfera-logs": "^1.0.0-beta.3", "dotenv": "^14.3.2", "elastic-apm-node": "^3.36.0", "helmet": "^5.1.0", @@ -51,7 +52,6 @@ "passport-hubspot-oauth2": "^1.0.3", "passport-mailchimp": "^1.1.0", "protospack": "2.5.2", - "@dadosfera/protospack-v2": "3.10.0", "reflect-metadata": "^0.1.13", "rimraf": "^3.0.2", "rxjs": "^7.5.5", diff --git a/src/modules/users/users.controller.ts b/src/modules/users/users.controller.ts index 3177269..089febf 100644 --- a/src/modules/users/users.controller.ts +++ b/src/modules/users/users.controller.ts @@ -11,7 +11,7 @@ import { Patch, Post, Put, - Req, + Query, UseFilters, } from '@nestjs/common'; import { @@ -172,6 +172,18 @@ export class UsersController { return this.userService.resendInvite({ id }); } + @Delete('role') + @RequireAllPermissions(PERMISSIONS.USERS.permissions.ADMIN) + async unassignRoleToUser( + @User() user: RequestUser, + @Query() request: UnassignRoleToUserReq, + @Headers('dadosfera-lang') language, + ) { + this.logger.info('unassignRoleToUser', { user }); + this.userService.setLanguage(language); + return await this.userService.unassignRoleFromUser(request); + } + @Delete(':id') @RequireAllPermissions(PERMISSIONS.USERS.permissions.ADMIN) @ApiOkResponse({ type: DeleteUserRes }) @@ -210,18 +222,6 @@ export class UsersController { return await this.userService.assignRoleToUser(body); } - @Delete('role') - @RequireAllPermissions(PERMISSIONS.USERS.permissions.ADMIN) - async unassignRoleToUser( - @User() user: RequestUser, - @Req() request: UnassignRoleToUserReq, - @Headers('dadosfera-lang') language, - ) { - this.logger.info('unassignRoleToUser', { user }); - this.userService.setLanguage(language); - return await this.userService.unassignRoleToUser(request); - } - @Patch(':id') @RequireAllPermissions(PERMISSIONS.USERS.permissions.ADMIN) @ApiOkResponse({ type: UpdateUserRes }) diff --git a/src/modules/users/users.service.ts b/src/modules/users/users.service.ts index 0fe40de..7d01115 100644 --- a/src/modules/users/users.service.ts +++ b/src/modules/users/users.service.ts @@ -161,7 +161,7 @@ export class UsersService implements OnModuleInit { return lastValueFrom(this.usersClientService.AssignRoleToUser(body)); } - async unassignRoleToUser(body: UnassignRoleFromUserRequest) { + async unassignRoleFromUser(body: UnassignRoleFromUserRequest) { return lastValueFrom(this.usersClientService.UnassignRoleFromUser(body)); } diff --git a/src/utils/ErrorBuilder.ts b/src/utils/ErrorBuilder.ts index e6c0eb8..40cb049 100644 --- a/src/utils/ErrorBuilder.ts +++ b/src/utils/ErrorBuilder.ts @@ -128,13 +128,46 @@ export function EnrichErrorCode(code: string) { code, }; - case ErrorCodes.ROLE.ALREADY_EXIST: + case ErrorCodes.ROLE.ALREADY_EXISTS: return { statusCode: HttpStatus.BAD_REQUEST, error: 'Grupo já existe', message: 'Não é possível criar um grupo com esse nome', code, }; + + case ErrorCodes.ROLE.ALREADY_EXISTS_PUBLIC: + return { + statusCode: HttpStatus.BAD_REQUEST, + error: 'Grupo já existe', + message: 'Nome conflita com grupo público da Dadosfera', + code, + }; + + case ErrorCodes.ROLE.PERMISSION_ALREADY_GRANTED: + return { + statusCode: HttpStatus.BAD_REQUEST, + error: 'Permissão já inclusa', + message: 'Permissão já consta nesse grupo', + code, + }; + + case ErrorCodes.ROLE.PERMISSION_NOT_GRANTED: + return { + statusCode: HttpStatus.BAD_REQUEST, + error: 'Permissão não inclusa', + message: 'Permissão já consta nesse grupo', + code, + }; + + case ErrorCodes.ROLE.PRIVATE: + return { + statusCode: HttpStatus.FORBIDDEN, + error: 'Não é possivel fazer alterações', + message: 'Você não tem permissão para alterar esse grupo', + code, + }; + case ErrorCodes.ROLE.NOT_FOUND: case ErrorCodes.ROLE.FROM_ANOTHER_CUSTOMER: return { @@ -143,6 +176,7 @@ export function EnrichErrorCode(code: string) { message: 'Grupo solicitado não encontrado', code: ErrorCodes.ROLE.NOT_FOUND, }; + case ErrorCodes.ROLE.CANNOT_EDIT_PUBLIC_ROLE: return { statusCode: HttpStatus.FORBIDDEN, @@ -150,6 +184,7 @@ export function EnrichErrorCode(code: string) { message: 'Você não tem permissão para editar esse grupo', code, }; + case ErrorCodes.USER.NOT_FOUND: return { statusCode: HttpStatus.NOT_FOUND, diff --git a/src/utils/errorCodes.ts b/src/utils/errorCodes.ts index b679510..2f7e16d 100644 --- a/src/utils/errorCodes.ts +++ b/src/utils/errorCodes.ts @@ -14,10 +14,15 @@ export const AUTH = { WEAK_NEW_PASSWORD: 'AUTH.WEAK_NEW_PASSWORD', }; export const ROLE = { - ALREADY_EXIST: 'ROLE.ALREADY_EXISTS', + INVALID_ID: 'ROLE.INVALID_ID', NOT_FOUND: 'ROLE.NOT_FOUND', + ALREADY_EXISTS: 'ROLE.ALREADY_EXISTS', + ALREADY_EXISTS_PUBLIC: 'ROLE.ALREADY_EXISTS_PUBLIC', + PERMISSION_ALREADY_GRANTED: 'ROLE.PERMISSION_ALREADY_GRANTED', + PERMISSION_NOT_GRANTED: 'ROLE.PERMISSION_NOT_GRANTED', FROM_ANOTHER_CUSTOMER: 'ROLE.FROM_ANOTHER_CUSTOMER', CANNOT_EDIT_PUBLIC_ROLE: 'ROLE.CANNOT_EDIT_PUBLIC_ROLE', + PRIVATE: 'ROLE.PRIVATE', }; export const USER = {