-
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
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 |
|---|---|---|
|
|
@@ -218,4 +218,4 @@ export class PantryApplicationDto { | |
| @IsOptional() | ||
| @IsBoolean() | ||
| newsletterSubscription?: boolean; | ||
| } | ||
| } | ||
| 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'; | ||
| import { | ||
| Activity, | ||
| AllergensConfidence, | ||
|
|
@@ -247,6 +248,94 @@ 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', | ||
|
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 include shipmentAddressState! |
||
| 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'; | ||
| import { PantryApplicationDto } from './dtos/pantry-application.dto'; | ||
| import { ApiBody } from '@nestjs/swagger'; | ||
| import { ApprovedPantryResponse } from './types'; | ||
| import { | ||
| Activity, | ||
| AllergensConfidence, | ||
|
|
@@ -53,6 +55,11 @@ export class PantriesController { | |
| return this.pantriesService.getPendingPantries(); | ||
| } | ||
|
|
||
| @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( | ||
|
|
@@ -328,4 +335,12 @@ export class PantriesController { | |
| ): Promise<void> { | ||
| return this.pantriesService.deny(pantryId); | ||
| } | ||
|
|
||
| @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 |
|---|---|---|
| @@ -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,83 @@ 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) => ({ | ||
| 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! |
||
| 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}`, | ||
| 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,39 @@ | ||
| export interface ApprovedPantryResponse { | ||
dburkhart07 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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.
|
||
| shipmentAddressZip: 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; | ||
| } | ||
|
|
||
| export enum RefrigeratedDonation { | ||
| YES = 'Yes, always', | ||
| NO = 'No', | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -69,4 +69,4 @@ export const USPhoneInput: React.FC<USPhoneInputProps> = ({ | |
| {...inputProps} | ||
| /> | ||
| ); | ||
| }; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,4 +14,4 @@ root.render( | |
| <App /> | ||
| </ChakraProvider> | ||
| </StrictMode>, | ||
| ); | ||
| ); | ||
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?