-
Notifications
You must be signed in to change notification settings - Fork 0
[SSF-211] promote volunteer to admin #187
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,11 @@ import { | |
| AdminCreateUserCommand, | ||
| } from '@aws-sdk/client-cognito-identity-provider'; | ||
|
|
||
| import { | ||
| AdminAddUserToGroupCommand, | ||
| AdminRemoveUserFromGroupCommand, | ||
| } from '@aws-sdk/client-cognito-identity-provider'; | ||
|
|
||
| import CognitoAuthConfig from './aws-exports'; | ||
| import { SignUpDto } from './dtos/sign-up.dto'; | ||
| import { createHmac } from 'crypto'; | ||
|
|
@@ -70,4 +75,39 @@ export class AuthService { | |
| } | ||
| } | ||
| } | ||
|
|
||
| async addUserToGroup(username: string, groupName: string): Promise<void> { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Yurika-Kan now that we have this function, should we be calling it for when we create a new volunteer as well? what about pantry and fm? |
||
| const command = new AdminAddUserToGroupCommand({ | ||
| UserPoolId: CognitoAuthConfig.userPoolId, | ||
| Username: username, | ||
| GroupName: groupName, | ||
| }); | ||
|
|
||
| try { | ||
| await this.providerClient.send(command); | ||
| } catch (error) { | ||
| throw new InternalServerErrorException( | ||
| `Failed to add user to group ${groupName}`, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| async removeUserFromGroup( | ||
| username: string, | ||
| groupName: string, | ||
| ): Promise<void> { | ||
| const command = new AdminRemoveUserFromGroupCommand({ | ||
| UserPoolId: CognitoAuthConfig.userPoolId, | ||
| Username: username, | ||
| GroupName: groupName, | ||
| }); | ||
|
|
||
| try { | ||
| await this.providerClient.send(command); | ||
| } catch (error) { | ||
| throw new InternalServerErrorException( | ||
| `Failed to remove user from group ${groupName}`, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { IsEnum, IsNotEmpty } from 'class-validator'; | ||
| import { Role } from '../types'; | ||
|
|
||
| export class UpdateUserRoleDto { | ||
| @IsEnum(Role) | ||
| @IsNotEmpty() | ||
| role!: Role; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import { | ||
| BadRequestException, | ||
| Controller, | ||
| Delete, | ||
| Get, | ||
|
|
@@ -14,6 +15,7 @@ import { UsersService } from './users.service'; | |
| import { User } from './users.entity'; | ||
| import { userSchemaDto } from './dtos/userSchema.dto'; | ||
| import { UpdateUserInfoDto } from './dtos/update-user-info.dto'; | ||
| import { UpdateUserRoleDto } from './dtos/update-user-role.dto'; | ||
| import { PendingApplication, Role } from './types'; | ||
| import { AuthenticatedRequest } from '../auth/authenticated-request'; | ||
| import { JwtAuthGuard } from '../auth/jwt-auth.guard'; | ||
|
|
@@ -53,6 +55,18 @@ export class UsersController { | |
| return this.usersService.update(id, dto); | ||
| } | ||
|
|
||
| @Patch('/:id/role') | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Can we rename this to something more descriptive, perhaps |
||
| @Roles(Role.ADMIN) | ||
| async promoteToAdmin( | ||
| @Param('id', ParseIntPipe) id: number, | ||
| @Body() dto: UpdateUserRoleDto, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This may be a very rare case where we do not need a body. We know we are already promoting the volunteer to admin, so we shouldn't need to verify any other data. Can we delete this and the dto, and the check in the controller? |
||
| ): Promise<User> { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most of our patch api calls will return void you'll see, let's do the same here and update the service function and all tests. |
||
| if (dto.role !== Role.ADMIN) { | ||
| throw new BadRequestException('Only promotion to admin is supported'); | ||
| } | ||
| return this.usersService.promoteVolunteerToAdmin(id); | ||
| } | ||
|
|
||
| // Keeping these two as functionality seems useful | ||
| @Post('/') | ||
| async createUser(@Body() createUserDto: userSchemaDto): Promise<User> { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: include these imports in the same one as above