[SSF-211] promote volunteer to admin#187
Conversation
|
Also backend tests are passing for me so not sure why you're having issues. |
dburkhart07
left a comment
There was a problem hiding this comment.
few questions pending yurika's clarification, but still some good things to work on. make sure all these updates you make you make to the tests too!
| @Roles(Role.ADMIN) | ||
| async promoteToAdmin( | ||
| @Param('id', ParseIntPipe) id: number, | ||
| @Body() dto: UpdateUserRoleDto, |
There was a problem hiding this comment.
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?
| return this.usersService.update(id, dto); | ||
| } | ||
|
|
||
| @Patch('/:id/role') |
There was a problem hiding this comment.
nit: Can we rename this to something more descriptive, perhaps @Patch('/:id/promote-volunteer)
There was a problem hiding this comment.
renamed to promote-volunteer
| async promoteToAdmin( | ||
| @Param('id', ParseIntPipe) id: number, | ||
| @Body() dto: UpdateUserRoleDto, | ||
| ): Promise<User> { |
There was a problem hiding this comment.
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.
| [userId], | ||
| ); | ||
|
|
||
| if (user.userCognitoSub) { |
There was a problem hiding this comment.
This is a required field, it will aways exist
| ); | ||
| } | ||
|
|
||
| const queryRunner = this.dataSource.createQueryRunner(); |
There was a problem hiding this comment.
I don't think we need any of this queryRunner, and we should be able to just set the volunteers to [] and save that. I think that may have been missed in the ticket, so can you do that? Should just be able to do this.repo.save instead (like all our other service functions).
| if (user.userCognitoSub) { | ||
| await this.authService.addUserToGroup(user.email, 'admin'); | ||
| await this.authService.removeUserFromGroup(user.email, 'volunteer'); | ||
| } |
There was a problem hiding this comment.
Can we wrap all 3 service calls in one single transaction? Look at many of our other service functions (one example would be the create function in the donations service) to understand how we go about using a transactionManager. The boilerplate code should be identical to that.
| AdminCreateUserCommand, | ||
| } from '@aws-sdk/client-cognito-identity-provider'; | ||
|
|
||
| import { |
There was a problem hiding this comment.
nit: include these imports in the same one as above
ae986a3 to
d932f0a
Compare
…the same as figma designs
| expect(mockUserService.promoteVolunteerToAdmin).toHaveBeenCalledWith(1); | ||
| }); | ||
|
|
||
| it('should throw NotFoundException from service when user not found', async () => { |
There was a problem hiding this comment.
we generally havent been testing for the sad cases in the controller, since primarily the service will handle bad input logic. controller tests for us are primarily just to make sure that the service is called once, and with the right parameters.
cuz of that, can we delete this test and the one below?
| describe('promoteVolunteerToAdmin', () => { | ||
| it('should promote volunteer to admin successfully', async () => { | ||
| const userRepo = testDataSource.getRepository(User); | ||
| const volunteers = await userRepo.find({ |
There was a problem hiding this comment.
in our dummy data, user id 7 is a volunteer i believe (from line 553). can we just use that for all of our promote checks here instead of finding all volunteers?
| await userRepo.save(user); | ||
|
|
||
| await this.authService.addUserToGroup(user.email, 'admin'); | ||
| await this.authService.removeUserFromGroup(user.email, 'volunteer'); |
There was a problem hiding this comment.
if this removeUserFromGroup call fails, then our database will be inconsistent with Cognito (we will have an admin user in Cognito, but this will rollback and give us a volunteer in the database. For this second call, can we do a try catch and, in the catch, run await this.authService.removeUserFromGroup(user.email, 'admin'); so that we remove the just added admin, and then throw an error so that everything rolls back? can we also add a specific test for this?
| where: { role: Role.VOLUNTEER }, | ||
| relations: ['pantries'], | ||
| }); | ||
| expect(volunteer).toBeDefined(); |
There was a problem hiding this comment.
can we make sure hat the volunteer had pantries to begin with before, to show that the call removes them?
| ); | ||
| }); | ||
|
|
||
| it('should throw BadRequestException when user is pantry', async () => { |
There was a problem hiding this comment.
nit: can we rephrase this to "when user is not a volunteer" since thats really what we want to test (any other role will work though).
| }); | ||
|
|
||
| it('should throw BadRequestException when user is already admin', async () => { | ||
| const admin = await testDataSource.getRepository(User).findOne({ |
There was a problem hiding this comment.
same thing here, user id 1 should be an admin
| }); | ||
|
|
||
| it('should call Cognito addUserToGroup and removeUserFromGroup', async () => { | ||
| const volunteer = await testDataSource.getRepository(User).findOne({ |
There was a problem hiding this comment.
see above comment about using id instead
|
|
||
| it('should rollback if Cognito fails', async () => { | ||
| const userRepo = testDataSource.getRepository(User); | ||
| const volunteer = await userRepo.findOne({ |
| return ( | ||
| <Dialog.Root | ||
| open={isOpen} | ||
| onOpenChange={(e: { open: boolean }) => !e.open && onClose()} |
There was a problem hiding this comment.
nit: can we change this to if (!e.open) onClose(); like all our other modals?
| } | ||
| } | ||
|
|
||
| async addUserToGroup(username: string, groupName: string): Promise<void> { |
| return hmac.digest('base64'); | ||
| } | ||
|
|
||
| async adminCreateUser({ |
There was a problem hiding this comment.
i know this isnt part of your ticket, but can we also make this function take in the users role, so that they can be added to the appropriate group?

ℹ️ Issue
Closes [SSF-211]
📝 Description
✔️ Verification
used frontend to test promoting a volunteer to admin and checked the network tab to see user role updated to admin
🏕️ (Optional) Future Work / Notes
my yarn tests are failing currently but im not sure how to fix it yet.