Skip to content

Commit

Permalink
Merge pull request #138 from apurvaubade/DeleteRole_new
Browse files Browse the repository at this point in the history
Role:Delete Role API
  • Loading branch information
vaivk369 authored Apr 26, 2024
2 parents e7a1c7a + 1f5588c commit fca810d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
35 changes: 33 additions & 2 deletions src/adapters/postgres/rbac/role-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ErrorResponseTypeOrm } from "src/error-response-typeorm";
import { RoleSearchDto } from "../../../rbac/role/dto/role-search.dto";
import { UserRoleMapping } from "src/rbac/assign-role/entities/assign-role.entity";
import { Privilege } from "src/rbac/privilege/entities/privilege.entity";
import { isUUID } from "class-validator";

@Injectable()
export class PostgresRoleService {
Expand Down Expand Up @@ -228,7 +229,37 @@ export class PostgresRoleService {

public async deleteRole(roleId: string) {
try {
let response = await this.roleRepository.delete(roleId);
if (!isUUID(roleId)) {
return new ErrorResponseTypeOrm({
statusCode: HttpStatus.BAD_REQUEST,
errorMessage: "Please Enter valid (UUID)",
});
}

const roleToDelete = await this.roleRepository.findOne({
where: { roleId: roleId },
});

if (!roleToDelete) {
return new ErrorResponseTypeOrm({
statusCode: HttpStatus.NOT_FOUND,
errorMessage: "Role not found",
});
}
// Delete the role
const response = await this.roleRepository.delete(roleId);

// Delete entries from RolePrivilegesMapping table associated with the roleId
const rolePrivilegesDeleteResponse =
await this.roleprivilegeMappingRepository.delete({
roleId: roleId,
});

const userRoleDeleteResponse =
await this.userRoleMappingRepository.delete({
roleId: roleId,
});

return new SuccessResponse({
statusCode: HttpStatus.OK,
message: "Role deleted successfully.",
Expand All @@ -239,7 +270,7 @@ export class PostgresRoleService {
} catch (e) {
return new ErrorResponseTypeOrm({
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
errorMessage: e,
errorMessage: "Internal server error", // Access the error message
});
}
}
Expand Down
12 changes: 8 additions & 4 deletions src/rbac/role/role.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {
ApiCreatedResponse,
ApiBasicAuth,
ApiHeader,
ApiBadRequestResponse,
ApiNotFoundResponse,
} from "@nestjs/swagger";
import { Request } from "@nestjs/common";
import { CreateRolesDto, RoleDto } from "./dto/role.dto";
Expand Down Expand Up @@ -108,12 +110,14 @@ export class RoleController {
}

//delete role
@Delete("/:id")
@Delete("/:roleId")
@ApiBasicAuth("access-token")
@ApiCreatedResponse({ description: "Role deleted successfully." })
@ApiForbiddenResponse({ description: "Forbidden" })
@ApiHeader({ name: "tenantid" })
@ApiOkResponse({ description: "Role deleted successfully." })
@ApiNotFoundResponse({ description: "Data not found" })
@ApiBadRequestResponse({ description: "Bad request" })
public async deleteRole(
@Param("id") roleId: string,
@Param("roleId") roleId: string,
@Res() response: Response
) {
const result = await this.roleAdapter.buildRbacAdapter().deleteRole(roleId);
Expand Down

0 comments on commit fca810d

Please sign in to comment.