-
Notifications
You must be signed in to change notification settings - Fork 0
SSF 97 pantry management backend #76
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
62ca4fb
8907ee8
08d4047
d2aef46
63fa43a
265bad6
1c948a7
ab18d04
4256a27
fb5ac86
3489633
72fc25d
ddda63f
ba519d2
1681f42
fe9f0fd
b1fffbb
0f9df9c
af4fea6
66a7677
85a317f
0094616
842ccf0
8f25130
ae4f126
31cb4e5
20bd058
a8a3289
dc53864
7fe16c0
6aa3025
18776f3
b0591a0
f4e3893
d29748f
7a66455
7f6edf9
cf9f2b6
82ea854
e88b4bd
303c5c2
b9e4491
934e9db
9f5e2e5
ff491af
97fbb5d
994bf06
0a78425
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 |
|---|---|---|
|
|
@@ -33,8 +33,7 @@ npm-debug.log | |
| yarn-error.log | ||
| testem.log | ||
| /typings | ||
| .nx | ||
|
|
||
|
|
||
| # System Files | ||
| .DS_Store | ||
| Thumbs.db | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ import { mock } from 'jest-mock-extended'; | |
| import { PantryApplicationDto } from './dtos/pantry-application.dto'; | ||
| import { OrdersService } from '../orders/order.service'; | ||
| import { Order } from '../orders/order.entity'; | ||
| import { ApprovedPantryResponse } from './types'; | ||
|
Collaborator
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. lets add this to the list of imports below! |
||
| import { | ||
| Activity, | ||
| AllergensConfidence, | ||
|
|
@@ -253,6 +254,95 @@ describe('PantriesController', () => { | |
| }); | ||
| }); | ||
|
|
||
| describe('getApprovedPantries', () => { | ||
| it('should return approved pantries with volunteers', async () => { | ||
| const mockApprovedPantries: ApprovedPantryResponse[] = [ | ||
swarkewalia marked this conversation as resolved.
Show resolved
Hide resolved
swarkewalia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| pantryId: 1, | ||
| pantryName: 'Community Food Pantry', | ||
|
|
||
| contactFirstName: 'John', | ||
| contactLastName: 'Smith', | ||
| contactEmail: 'john.smith@example.com', | ||
| contactPhone: '(508) 508-6789', | ||
|
|
||
| shipmentAddressLine1: '123 Main Street', | ||
| shipmentAddressCity: 'Boston', | ||
| shipmentAddressZip: '02101', | ||
Yurika-Kan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| shipmentAddressState: 'MA', | ||
|
Collaborator
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 you make it so state comes before zip? |
||
| shipmentAddressCountry: 'United States', | ||
|
|
||
| allergenClients: '10 to 20', | ||
| restrictions: ['Peanuts', 'Dairy'], | ||
|
|
||
| refrigeratedDonation: RefrigeratedDonation.YES, | ||
| reserveFoodForAllergic: ReserveFoodForAllergic.YES, | ||
| reservationExplanation: | ||
| 'We regularly serve clients with severe allergies.', | ||
|
|
||
| dedicatedAllergyFriendly: true, | ||
|
|
||
| clientVisitFrequency: ClientVisitFrequency.FEW_TIMES_A_MONTH, | ||
| identifyAllergensConfidence: AllergensConfidence.VERY_CONFIDENT, | ||
| serveAllergicChildren: ServeAllergicChildren.YES_MANY, | ||
|
|
||
| activities: [ | ||
| Activity.POST_RESOURCE_FLYERS, | ||
| Activity.CREATE_LABELED_SHELF, | ||
| ], | ||
| activitiesComments: 'Weekly food distribution events', | ||
|
|
||
| itemsInStock: 'Canned goods, rice, pasta', | ||
| needMoreOptions: 'Gluten-free and nut-free items', | ||
|
|
||
| newsletterSubscription: true, | ||
| volunteers: [ | ||
| { | ||
| userId: 10, | ||
| name: 'Alice Johnson', | ||
| email: 'alice.johnson@example.com', | ||
| phone: '(617) 555-0100', | ||
| role: 'Volunteer Coordinator', | ||
| }, | ||
| { | ||
| userId: 11, | ||
| name: 'Bob Williams', | ||
| email: 'bob.williams@example.com', | ||
| phone: '(617) 555-0101', | ||
| role: 'Food Distributor', | ||
| }, | ||
| ], | ||
| }, | ||
| ]; | ||
|
|
||
| mockPantriesService.getApprovedPantriesWithVolunteers.mockResolvedValue( | ||
| mockApprovedPantries, | ||
| ); | ||
|
|
||
| const result = await controller.getApprovedPantries(); | ||
|
|
||
| expect(result).toEqual(mockApprovedPantries); | ||
| expect( | ||
| mockPantriesService.getApprovedPantriesWithVolunteers, | ||
| ).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); | ||
|
|
||
| describe('updatePantryVolunteers', () => { | ||
| it('should overwrite the set of volunteers assigned to a pantry', async () => { | ||
| const pantryId = 1; | ||
| const volunteerIds = [10, 11, 12]; | ||
|
|
||
| mockPantriesService.updatePantryVolunteers.mockResolvedValue(undefined); | ||
|
|
||
| await controller.updatePantryVolunteers(pantryId, volunteerIds); | ||
|
|
||
| expect(mockPantriesService.updatePantryVolunteers).toHaveBeenCalledWith( | ||
| pantryId, | ||
| volunteerIds, | ||
| ); | ||
| }); | ||
| }); | ||
| describe('getCurrentUserPantryId', () => { | ||
| it('returns pantryId when req.currentUser is present', async () => { | ||
| const req = { user: { id: 1 } }; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ import { | |
| Param, | ||
| ParseIntPipe, | ||
| Patch, | ||
| Put, | ||
| Post, | ||
| Req, | ||
| UnauthorizedException, | ||
|
|
@@ -16,6 +17,7 @@ import { Roles } from '../auth/roles.decorator'; | |
| import { ValidationPipe } from '@nestjs/common'; | ||
|
Collaborator
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 add this to the list of imports above in the first lines? |
||
| import { PantryApplicationDto } from './dtos/pantry-application.dto'; | ||
| import { ApiBody } from '@nestjs/swagger'; | ||
| import { ApprovedPantryResponse } from './types'; | ||
|
Collaborator
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. also here add this import into the list of imports below so that we aren't unnecessarily calling multiple imports of the same file |
||
| import { | ||
| Activity, | ||
| AllergensConfidence, | ||
|
|
@@ -56,6 +58,11 @@ export class PantriesController { | |
| return this.pantriesService.getPendingPantries(); | ||
| } | ||
|
|
||
|
Collaborator
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. since we began auth gating endpoints by role, let's continue that here by making this only accessible to admin role - aligned with the feature implications of this backend ticket |
||
| @Get('/approved') | ||
|
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 should write a test for this as well. |
||
| async getApprovedPantries(): Promise<ApprovedPantryResponse[]> { | ||
| return this.pantriesService.getApprovedPantriesWithVolunteers(); | ||
| } | ||
|
|
||
| @Roles(Role.PANTRY, Role.ADMIN) | ||
| @Get('/:pantryId') | ||
| async getPantry( | ||
|
|
@@ -343,4 +350,12 @@ export class PantriesController { | |
| attachments, | ||
| ); | ||
| } | ||
|
|
||
|
Collaborator
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. same here with annotating this to be accessible only for admin |
||
| @Put('/:pantryId/volunteers') | ||
| async updatePantryVolunteers( | ||
| @Param('pantryId', ParseIntPipe) pantryId: number, | ||
| @Body('volunteerIds') volunteerIds: number[], | ||
| ): Promise<void> { | ||
| return this.pantriesService.updatePantryVolunteers(pantryId, volunteerIds); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,8 +15,10 @@ import { | |
| AllergensConfidence, | ||
| } from './types'; | ||
| import { ApplicationStatus } from '../shared/types'; | ||
| import { User } from '../users/user.entity'; | ||
|
|
||
| const mockRepository = mock<Repository<Pantry>>(); | ||
| const mockUserRepository = mock<Repository<User>>(); | ||
|
|
||
| describe('PantriesService', () => { | ||
| let service: PantriesService; | ||
|
|
@@ -79,6 +81,10 @@ describe('PantriesService', () => { | |
| provide: getRepositoryToken(Pantry), | ||
| useValue: mockRepository, | ||
| }, | ||
| { | ||
| provide: getRepositoryToken(User), | ||
| useValue: mockUserRepository, | ||
| }, | ||
| ], | ||
| }).compile(); | ||
|
|
||
|
Collaborator
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. missing tests for getApprovedPantriesWithVolunteers & updatePantryVolunteers |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,8 @@ | ||
| import { Injectable, NotFoundException } from '@nestjs/common'; | ||
| import { | ||
| Injectable, | ||
| NotFoundException, | ||
| BadRequestException, | ||
| } from '@nestjs/common'; | ||
| import { InjectRepository } from '@nestjs/typeorm'; | ||
| import { In, Repository } from 'typeorm'; | ||
| import { Pantry } from './pantries.entity'; | ||
|
|
@@ -7,10 +11,14 @@ import { validateId } from '../utils/validation.utils'; | |
| import { ApplicationStatus } from '../shared/types'; | ||
| import { PantryApplicationDto } from './dtos/pantry-application.dto'; | ||
| import { Role } from '../users/types'; | ||
| import { ApprovedPantryResponse } from './types'; | ||
|
|
||
| @Injectable() | ||
| export class PantriesService { | ||
| constructor(@InjectRepository(Pantry) private repo: Repository<Pantry>) {} | ||
| constructor( | ||
| @InjectRepository(Pantry) private repo: Repository<Pantry>, | ||
| @InjectRepository(User) private userRepo: Repository<User>, | ||
| ) {} | ||
|
|
||
| async findOne(pantryId: number): Promise<Pantry> { | ||
| validateId(pantryId, 'Pantry'); | ||
|
|
@@ -113,6 +121,84 @@ export class PantriesService { | |
| await this.repo.update(id, { status: ApplicationStatus.DENIED }); | ||
| } | ||
|
|
||
| async getApprovedPantriesWithVolunteers(): Promise<ApprovedPantryResponse[]> { | ||
|
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. See comment in the type.ts file, this will need to change too. |
||
| const pantries = await this.repo.find({ | ||
| where: { status: ApplicationStatus.APPROVED }, | ||
| relations: ['volunteers', 'pantryUser'], | ||
| }); | ||
|
|
||
| return pantries.map((pantry) => ({ | ||
|
Collaborator
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 you take a look at what the return from the above call to TypeORM looks like? That should likely have all the information we need, it doesn't look like this map is doing much other than copying all the fields from one object to another. If we do go with the current approach rather than just returning |
||
| pantryId: pantry.pantryId, | ||
| pantryName: pantry.pantryName, | ||
| contactFirstName: pantry.pantryUser.firstName, | ||
| contactLastName: pantry.pantryUser.lastName, | ||
| contactEmail: pantry.pantryUser.email, | ||
| contactPhone: pantry.pantryUser.phone, | ||
| shipmentAddressLine1: pantry.shipmentAddressLine1, | ||
| shipmentAddressCity: pantry.shipmentAddressCity, | ||
| shipmentAddressZip: pantry.shipmentAddressZip, | ||
|
Collaborator
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. shipmentAddressState is also missing here!
Collaborator
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. for best order, can you put zip address after state & country |
||
| shipmentAddressState: pantry.shipmentAddressState, | ||
| shipmentAddressCountry: pantry.shipmentAddressCountry, | ||
| allergenClients: pantry.allergenClients, | ||
| restrictions: pantry.restrictions, | ||
| refrigeratedDonation: pantry.refrigeratedDonation, | ||
| reserveFoodForAllergic: pantry.reserveFoodForAllergic, | ||
| reservationExplanation: pantry.reservationExplanation, | ||
| dedicatedAllergyFriendly: pantry.dedicatedAllergyFriendly, | ||
| clientVisitFrequency: pantry.clientVisitFrequency, | ||
| identifyAllergensConfidence: pantry.identifyAllergensConfidence, | ||
| serveAllergicChildren: pantry.serveAllergicChildren, | ||
| activities: pantry.activities, | ||
| activitiesComments: pantry.activitiesComments, | ||
| itemsInStock: pantry.itemsInStock, | ||
| needMoreOptions: pantry.needMoreOptions, | ||
| newsletterSubscription: pantry.newsletterSubscription ?? false, | ||
| volunteers: (pantry.volunteers || []).map((volunteer) => ({ | ||
| userId: volunteer.id, | ||
| name: `${volunteer.firstName} ${volunteer.lastName}`, | ||
|
Collaborator
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 should not be combining the first and last names into one field here, this will make things more difficult for the frontend which needs to show the user's initials |
||
| email: volunteer.email, | ||
| phone: volunteer.phone, | ||
| role: volunteer.role, | ||
| })), | ||
| })); | ||
| } | ||
|
|
||
| async updatePantryVolunteers( | ||
| pantryId: number, | ||
| volunteerIds: number[], | ||
| ): Promise<void> { | ||
| validateId(pantryId, 'Pantry'); | ||
| volunteerIds.forEach((id) => validateId(id, 'Volunteer')); | ||
|
|
||
| const pantry = await this.repo.findOne({ | ||
| where: { pantryId }, | ||
| relations: ['volunteers'], | ||
| }); | ||
|
|
||
| if (!pantry) { | ||
| throw new NotFoundException(`Pantry with ID ${pantryId} not found`); | ||
| } | ||
|
|
||
dburkhart07 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const users = await this.userRepo.findBy({ id: In(volunteerIds) }); | ||
|
|
||
| if (users.length !== volunteerIds.length) { | ||
| throw new NotFoundException('One or more users not found'); | ||
| } | ||
|
|
||
| const nonVolunteers = users.filter((user) => user.role !== Role.VOLUNTEER); | ||
|
|
||
| if (nonVolunteers.length > 0) { | ||
| throw new BadRequestException( | ||
| `Users ${nonVolunteers | ||
| .map((user) => user.id) | ||
| .join(', ')} are not volunteers`, | ||
| ); | ||
| } | ||
|
|
||
| pantry.volunteers = users; | ||
| await this.repo.save(pantry); | ||
| } | ||
|
|
||
| async findByIds(pantryIds: number[]): Promise<Pantry[]> { | ||
| pantryIds.forEach((id) => validateId(id, 'Pantry')); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,40 @@ | ||
| export interface ApprovedPantryResponse { | ||
|
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. I think we are going to need more info than this for our approved pantry response. For one, all these pantries are approved so we shouldnt need the status. Additionally, check out the Figma for the frontend: https://www.figma.com/design/brc5luMhizIFp893XIutYe/SP26---SSF-Designs?node-id=756-11085&t=3UiKr0MdYxCcdUUK-0 The modal that pops up with Pantry Details should tell you what information we need.
Collaborator
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. Sorry, there may have been some confusion here. It's fine if we have to make another API call to get the full details about a pantry when the user navigates to the "pantry details" page - we just need the info for the main table (as well as the IDs necessary to be able to navigate to the other pages)
Collaborator
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. Both of these interfaces look very similar to the |
||
| pantryId: number; | ||
| pantryName: string; | ||
| contactFirstName: string; | ||
| contactLastName: string; | ||
| contactEmail: string; | ||
| contactPhone: string; | ||
| shipmentAddressLine1: string; | ||
| shipmentAddressCity: string; | ||
|
Collaborator
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.
Collaborator
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 zip be after state & country? |
||
| shipmentAddressZip: string; | ||
| shipmentAddressState: string; | ||
| shipmentAddressCountry?: string; | ||
| allergenClients: string; | ||
| restrictions: string[]; | ||
| refrigeratedDonation: RefrigeratedDonation; | ||
| reserveFoodForAllergic: ReserveFoodForAllergic; | ||
| reservationExplanation?: string; | ||
| dedicatedAllergyFriendly: boolean; | ||
| clientVisitFrequency?: ClientVisitFrequency; | ||
| identifyAllergensConfidence?: AllergensConfidence; | ||
| serveAllergicChildren?: ServeAllergicChildren; | ||
| activities: Activity[]; | ||
| activitiesComments?: string; | ||
| itemsInStock: string; | ||
| needMoreOptions: string; | ||
| newsletterSubscription: boolean; | ||
| volunteers: AssignedVolunteer[]; | ||
| } | ||
|
|
||
| export interface AssignedVolunteer { | ||
| userId: number; | ||
| name: string; | ||
| email: string; | ||
| phone: string; | ||
| role: string; | ||
|
Collaborator
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 have a user enum that we should use instead of string here to determine role!
Collaborator
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. i don't think we need to save role of the volunteer here - they should always be volunteer - could you delete this everywhere in this implementation? @sam-schu lmk thoughts
Collaborator
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. I'm fine either way - this type seems to only be used as part of the return of the endpoint to get info about the approved pantries, where I agree it's not strictly necessary that we return the role of every assigned volunteer but it also doesn't hurt anything |
||
| } | ||
|
|
||
| export enum RefrigeratedDonation { | ||
| YES = 'Yes, always', | ||
| NO = 'No', | ||
|
|
||
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.
Why is this here? Can we get rid of it?