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

Add Delete Role API #56

Open
3 of 10 tasks
sanjaysah101 opened this issue Sep 18, 2024 · 10 comments
Open
3 of 10 tasks

Add Delete Role API #56

sanjaysah101 opened this issue Sep 18, 2024 · 10 comments

Comments

@sanjaysah101
Copy link
Collaborator

sanjaysah101 commented Sep 18, 2024

Description:

We need to add functionality for deleting roles in the user-service. This API should follow specific constraints to ensure that system-defined roles (default roles) are not deleted, while roles created by users can be deleted under the right conditions.

Key Requirements

1. Role Deletion Constraints

  • Default roles cannot be deleted.
  • Only user-created roles should be deletable.
  • Add a boolean flag canBeDeleted in the Role model to mark which roles are deletable.

2. Authentication & Authorization:

  • Use the existing validateAccessToken middleware to authenticate the user.
  • Use checkPermission(['delete-roles']) to authorize users with the right permissions to delete roles.

3. API Endpoint:

  • Create a new DELETE /roles/:roleId route to handle role deletion.

4. Controller Layer:

  • Create a deleteRole method in the controller to handle the incoming request for role deletion.

5. Service Layer:

  • Implement business logic in the service layer through a new deleteRole method.
  • Ensure that deletion is allowed only if the canBeDeleted flag is true.

6. Data Access Layer (DAL):

  • Add a deleteRole method in the DAL to perform the actual deletion of the role in the database.

7. Validation:

  • Update the role.validation.ts file to handle any necessary validation for role deletion, including checking the canBeDeleted flag.

8. Existing Code Reference:

The new route and functionality should be consistent with existing routes such as:

router.delete('/roles', validateAccessToken, checkPermission(['delete-roles']), deleteRole);

9. Tests:

Write unit tests to cover:

Tasks:

  • Add a canBeDeleted flag in the Role model.
  • Create a new DELETE /roles/:roleId endpoint in the router.
  • Add deleteRole method in the controller.
  • Add deleteRole method in the service.
  • Add deleteRole method in the DAL.
  • Ensure that only roles with canBeDeleted = true can be deleted.
  • Use validateAccessToken to authenticate the user.
  • Use checkPermission(['delete-roles']) to authorize the user.
  • Update role.validation.ts with any necessary validation logic.
  • Write unit tests for this new functionality.

Acceptance Criteria:

  • Default roles are protected from deletion.
  • User-created roles can be deleted if canBeDeleted is true.
  • API is secure with proper authentication and authorization in place.
  • All tests pass.
@Code-With-Abhishek-Kumar

I am Interested to code this

@sanjaysah101
Copy link
Collaborator Author

I am Interested to code this

Yes please

@Code-With-Abhishek-Kumar
Copy link

Code-With-Abhishek-Kumar commented Jan 14, 2025

I am Interested to code this

By default the The Value of canBeDeleted Deleted is false . If user Is proper authenticated then you want to Update the canBeDeleted = true . If i am not Correct then Please Describe me In detail .

@Code-With-Abhishek-Kumar

/attempt 1

@sanjaysah101
Copy link
Collaborator Author

Hi @Code-With-Abhishek-Kumar, As you can see in role.model.ts file there is isSystemRole. Instead of adding extra flag, we can use isSystemRole flag.

Edge Case:

  • If isSystemRole is true then do not allow delete
  • Add SYSTEM_ROLE_CANNOT_BE_DELETED in error-types.constant.ts file

let me know if you have any doubt.

@Code-With-Abhishek-Kumar

Hi @Code-With-Abhishek-Kumar, As you can see in role.model.ts file there is isSystemRole. Instead of adding extra flag, we can use isSystemRole flag.

Edge Case:

* If isSystemRole is true then do not allow delete

* Add `SYSTEM_ROLE_CANNOT_BE_DELETED` in `error-types.constant.ts` file

let me know if you have any doubt.

Okay Got it

@Code-With-Abhishek-Kumar

Hi @Code-With-Abhishek-Kumar, As you can see in role.model.ts file there is isSystemRole. Instead of adding extra flag, we can use isSystemRole flag.

Edge Case:

* If isSystemRole is true then do not allow delete

* Add `SYSTEM_ROLE_CANNOT_BE_DELETED` in `error-types.constant.ts` file

let me know if you have any doubt.

How can i pass These Credential so that i can test application in my local

Screenshot at 2025-01-14 12-22-28

@sanjaysah101
Copy link
Collaborator Author

sanjaysah101 commented Jan 14, 2025

Hi @Code-With-Abhishek-Kumar,

Please follow these steps to set up the project:

  1. Pull the latest changes from the repository.
  2. Copy the content from env.example to .env.
  3. Install the required packages.
  4. Run the command pnpm generate-keys to generate RSA keys.
  5. Copy both the public and private keys to the .env file.
  6. Execute pnpm test to run the test suite.

Once all the test cases pass, the project setup is complete.

@Code-With-Abhishek-Kumar

Hi @sanjaysah101
,
I am Getting Error When I Try To run pnpm test

Screenshot at 2025-01-15 09-48-35

Screenshot at 2025-01-15 09-49-12

@sanjaysah101
Copy link
Collaborator Author

This means the env constant has not been set correctly. Please refer Readme.md file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants