-
Notifications
You must be signed in to change notification settings - Fork 0
[SSF-171] update volunteers endpoint #138
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 |
|---|---|---|
|
|
@@ -332,7 +332,7 @@ export class PantriesService { | |
| pantryMessage.subject, | ||
| pantryMessage.bodyHTML, | ||
| ); | ||
| } catch (error) { | ||
| } catch { | ||
| throw new InternalServerErrorException( | ||
| 'Failed to send pantry application submitted confirmation email to representative', | ||
| ); | ||
|
|
@@ -345,7 +345,7 @@ export class PantriesService { | |
| adminMessage.subject, | ||
| adminMessage.bodyHTML, | ||
| ); | ||
| } catch (error) { | ||
| } catch { | ||
| throw new InternalServerErrorException( | ||
| 'Failed to send new pantry application notification email to SSF', | ||
| ); | ||
|
|
@@ -419,7 +419,7 @@ export class PantriesService { | |
| message.subject, | ||
| message.bodyHTML, | ||
| ); | ||
| } catch (error) { | ||
| } catch { | ||
| throw new InternalServerErrorException( | ||
| 'Failed to send pantry account approved notification email to representative', | ||
| ); | ||
|
|
@@ -465,10 +465,25 @@ export class PantriesService { | |
|
|
||
| async updatePantryVolunteers( | ||
| pantryId: number, | ||
| volunteerIds: number[], | ||
| addVolunteerIds: number[], | ||
| removeVolunteerIds: number[], | ||
|
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. While we are on the topic of sets, on conversion, we should validate at the start that there are no duplicates within our respective lists (cant remove a volunteer twice and cant add one twice), and add a test for this. |
||
| ): Promise<void> { | ||
| validateId(pantryId, 'Pantry'); | ||
| volunteerIds.forEach((id) => validateId(id, 'Volunteer')); | ||
|
|
||
| const overlap = addVolunteerIds.filter((id) => | ||
| removeVolunteerIds.includes(id), | ||
|
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. Can we make removeVolunteerIds also a set for easier iteration? Both of these arrays are looped through a ton, so I think they should immediately be converted to sets, and have that used for every other check (this will reduce many computations below from O(n) -> O(1)). Hooray for Leetcode!! |
||
| ); | ||
| if (overlap.length > 0) { | ||
| throw new BadRequestException( | ||
| `The following ID(s) appear in both the add and remove lists: ${overlap.join( | ||
| ', ', | ||
| )}`, | ||
| ); | ||
| } | ||
|
|
||
| const uniqueAddIds = [...new Set(addVolunteerIds)]; | ||
| const allVolunteerIds = [...uniqueAddIds, ...removeVolunteerIds]; | ||
| allVolunteerIds.forEach((id) => validateId(id, 'Volunteer')); | ||
|
|
||
| const pantry = await this.repo.findOne({ | ||
| where: { pantryId }, | ||
|
|
@@ -480,24 +495,32 @@ export class PantriesService { | |
| } | ||
|
|
||
| const users = await Promise.all( | ||
|
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. Can we actually change this to use |
||
| volunteerIds.map((id) => this.usersService.findOne(id)), | ||
| allVolunteerIds.map((id) => this.usersService.findOne(id)), | ||
| ); | ||
|
|
||
| if (users.length !== volunteerIds.length) { | ||
|
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. Why was this check removed? |
||
| throw new NotFoundException('One or more users not found'); | ||
| } | ||
|
|
||
| const nonVolunteers = users.filter((user) => user.role !== Role.VOLUNTEER); | ||
|
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. We don't need to check that the volunteer users being removed are volunteers (we already know they are), so we can just check the ones being added. |
||
|
|
||
| if (nonVolunteers.length > 0) { | ||
| throw new BadRequestException( | ||
| `Users ${nonVolunteers | ||
| `User(s) ${nonVolunteers | ||
| .map((user) => user.id) | ||
| .join(', ')} are not volunteers`, | ||
| ); | ||
| } | ||
|
|
||
| pantry.volunteers = users; | ||
| const usersToAdd = users.filter((u) => uniqueAddIds.includes(u.id)); | ||
|
|
||
| const currentVolunteers = pantry.volunteers ?? []; | ||
| const filteredVolunteers = currentVolunteers.filter( | ||
| (v) => !removeVolunteerIds.includes(v.id), | ||
| ); | ||
|
|
||
| const existingVolunteerIds = new Set(filteredVolunteers.map((v) => v.id)); | ||
| const volunteersToAdd = usersToAdd.filter( | ||
| (u) => !existingVolunteerIds.has(u.id), | ||
| ); | ||
|
|
||
| pantry.volunteers = [...filteredVolunteers, ...volunteersToAdd]; | ||
| await this.repo.save(pantry); | ||
| } | ||
|
|
||
|
|
||
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.
Should these both be required? Thinking we only need 1 to execute this. Should adjust tests accordingly.