Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/backend-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ jobs:
node-version: 20
- run: yarn install --frozen-lockfile
- run: yarn list strip-ansi string-width string-length
- run: npx jest
- run: yarn test
8 changes: 4 additions & 4 deletions apps/backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ We have a few environment variables that we utilize to access several AWS servic
- There, you can validate the information in `auth/aws_exports.ts` (the `userPoolClientId`), as well as copy the client secret into your env file

5. Creating a new user within AWS Cognito
There are 2 ways you can create a new user in AWS Cognito. The simplest, is through loading the up, going to the landing page, and creating a new account there. If you choose to do it alternatively through the console, follow these steps:
There are 2 ways you can create a new user in AWS Cognito. The simplest, is through loading the app, going to the signup page, and creating a new account there. If you choose to do it alternatively through the console, follow these steps:
- Navigate to AWS Cognito
- Make sure you are on "United States (N. Virginia) as your region
- Make sure you are on "United States (N. Virginia)" as your region
- Go into User pools and click on the one that says "ssf"
- Go to Users
- If you do not already see your email there, create a new User, setting an email in password (this will be what you login with on the frontend)
- If you do not already see your email there, create a new User, setting an email and password (this will be what you login with on the frontend)
- Click 'Create User'
- Load up the app, and go to the landing page
- Load up the app, and go to the login page
- Verify you are able to login with these new credentials you created

### Running backend tests
Expand Down
6 changes: 0 additions & 6 deletions apps/backend/src/allocations/allocations.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,4 @@ export class Allocation {

@Column({ name: 'allocated_quantity', type: 'int' })
allocatedQuantity: number;

@Column({ name: 'reserved_at', type: 'timestamp' })
reservedAt: Date;

@Column({ name: 'fulfilled_at', type: 'timestamp' })
fulfilledAt: Date;
}
2 changes: 0 additions & 2 deletions apps/backend/src/allocations/allocations.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ export class AllocationsService {
select: {
allocationId: true,
allocatedQuantity: true,
reservedAt: true,
fulfilledAt: true,
},
});
}
Expand Down
2 changes: 2 additions & 0 deletions apps/backend/src/config/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { UpdateManufacturerEntity1768680807820 } from '../migrations/17686808078
import { AddUserPoolId1769189327767 } from '../migrations/1769189327767-AddUserPoolId';
import { UpdateOrderEntity1769990652833 } from '../migrations/1769990652833-UpdateOrderEntity';
import { RenameDonationMatchingStatus1771260403657 } from '../migrations/1771260403657-RenameDonationMatchingStatus';
import { CleanupRequestsAndAllocations1771821377918 } from '../migrations/1771821377918-CleanupRequestsAndAllocations';

const schemaMigrations = [
User1725726359198,
Expand Down Expand Up @@ -66,6 +67,7 @@ const schemaMigrations = [
AddUserPoolId1769189327767,
UpdateOrderEntity1769990652833,
RenameDonationMatchingStatus1771260403657,
CleanupRequestsAndAllocations1771821377918,
];

export default schemaMigrations;
19 changes: 19 additions & 0 deletions apps/backend/src/foodRequests/dtos/matching.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { FoodType } from '../../donationItems/types';
import { FoodManufacturer } from '../../foodManufacturers/manufacturers.entity';

export class MatchingManufacturersDto {
matchingManufacturers: FoodManufacturer[];
nonMatchingManufacturers: FoodManufacturer[];
}

export class MatchingItemsDto {
matchingItems: DonationItemDetailsDto[];
nonMatchingItems: DonationItemDetailsDto[];
}

export class DonationItemDetailsDto {
itemId: number;
itemName: string;
foodType: FoodType;
availableQuantity: number;
}
92 changes: 90 additions & 2 deletions apps/backend/src/foodRequests/request.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ import { OrderStatus } from '../orders/types';
import { FoodType } from '../donationItems/types';
import { OrderDetailsDto } from './dtos/order-details.dto';
import { Order } from '../orders/order.entity';
import {
DonationItemDetailsDto,
MatchingItemsDto,
MatchingManufacturersDto,
} from './dtos/matching.dto';
import { FoodManufacturer } from '../foodManufacturers/manufacturers.entity';

const mockRequestsService = mock<RequestsService>();
const mockOrdersService = mock<OrdersService>();
Expand Down Expand Up @@ -149,7 +155,10 @@ describe('RequestsController', () => {
const createBody: Partial<FoodRequest> = {
pantryId: 1,
requestedSize: RequestSize.MEDIUM,
requestedItems: ['Test item 1', 'Test item 2'],
requestedFoodTypes: [
FoodType.DAIRY_FREE_ALTERNATIVES,
FoodType.DRIED_BEANS,
],
additionalInformation: 'Test information.',
dateReceived: null,
feedback: null,
Expand All @@ -173,7 +182,7 @@ describe('RequestsController', () => {
expect(mockRequestsService.create).toHaveBeenCalledWith(
createBody.pantryId,
createBody.requestedSize,
createBody.requestedItems,
createBody.requestedFoodTypes,
createBody.additionalInformation,
createBody.dateReceived,
createBody.feedback,
Expand Down Expand Up @@ -379,4 +388,83 @@ describe('RequestsController', () => {
).rejects.toThrow('Invalid date format for deliveryDate');
});
});

describe('GET /:requestId/matching-manufacturers', () => {
it('should call requestsService.getMatchingManufacturers and return grouped manufacturers', async () => {
const requestId = 1;

const mockResult: MatchingManufacturersDto = {
matchingManufacturers: [
{
foodManufacturerId: 1,
foodManufacturerName: 'Test Manufacturer 1',
} as FoodManufacturer,
{
foodManufacturerId: 2,
foodManufacturerName: 'Test Manufacturer 2',
} as FoodManufacturer,
],
nonMatchingManufacturers: [
{
foodManufacturerId: 3,
foodManufacturerName: 'Non-Matching Manufacturer',
} as FoodManufacturer,
],
};
mockRequestsService.getMatchingManufacturers.mockResolvedValueOnce(
mockResult,
);

const result = await controller.getMatchingManufacturers(requestId);

expect(result).toEqual(mockResult);
expect(mockRequestsService.getMatchingManufacturers).toHaveBeenCalledWith(
requestId,
);
});
});

describe('GET /:requestId/matching-manufacturers/:foodManufacturerId/available-items', () => {
it('should call requestsService.getAvailableItems and return grouped items', async () => {
const requestId = 1;
const foodManufacturerId = 1;

const mockResult: MatchingItemsDto = {
matchingItems: [
{
itemId: 1,
itemName: 'Granola',
foodType: FoodType.GRANOLA,
availableQuantity: 10,
} as DonationItemDetailsDto,
{
itemId: 2,
itemName: 'Dried Beans',
foodType: FoodType.DRIED_BEANS,
availableQuantity: 5,
} as DonationItemDetailsDto,
],
nonMatchingItems: [
{
itemId: 3,
itemName: 'Dairy Free Alternatives',
foodType: FoodType.DAIRY_FREE_ALTERNATIVES,
availableQuantity: 8,
} as DonationItemDetailsDto,
],
};
mockRequestsService.getAvailableItems.mockResolvedValueOnce(mockResult);

const result = await controller.getAvailableItemsForManufacturer(
requestId,
foodManufacturerId,
);

expect(result).toEqual(mockResult);
expect(mockRequestsService.getAvailableItems).toHaveBeenCalledWith(
requestId,
foodManufacturerId,
);
});
});
});
32 changes: 27 additions & 5 deletions apps/backend/src/foodRequests/request.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ import { OrdersService } from '../orders/order.service';
import { RequestSize } from './types';
import { OrderStatus } from '../orders/types';
import { OrderDetailsDto } from './dtos/order-details.dto';
import { FoodType } from '../donationItems/types';
import {
MatchingItemsDto,
MatchingManufacturersDto,
} from './dtos/matching.dto';

@Controller('requests')
export class RequestsController {
Expand Down Expand Up @@ -54,6 +59,23 @@ export class RequestsController {
return this.requestsService.getOrderDetails(requestId);
}

@Roles(Role.ADMIN, Role.VOLUNTEER)
@Get('/:requestId/matching-manufacturers')
async getMatchingManufacturers(
@Param('requestId', ParseIntPipe) requestId: number,
): Promise<MatchingManufacturersDto> {
return this.requestsService.getMatchingManufacturers(requestId);
}

@Roles(Role.ADMIN, Role.VOLUNTEER)
@Get('/:requestId/matching-manufacturers/:manufacturerId/available-items')
async getAvailableItemsForManufacturer(
@Param('requestId', ParseIntPipe) requestId: number,
@Param('manufacturerId', ParseIntPipe) manufacturerId: number,
): Promise<MatchingItemsDto> {
return this.requestsService.getAvailableItems(requestId, manufacturerId);
}

@Post('/create')
@ApiBody({
description: 'Details for creating a food request',
Expand All @@ -66,10 +88,10 @@ export class RequestsController {
enum: Object.values(RequestSize),
example: RequestSize.LARGE,
},
requestedItems: {
requestedFoodTypes: {
type: 'array',
items: { type: 'string' },
example: ['Rice Noodles', 'Quinoa'],
items: { type: 'string', enum: Object.values(FoodType) },
example: [FoodType.DAIRY_FREE_ALTERNATIVES, FoodType.DRIED_BEANS],
},
additionalInformation: {
type: 'string',
Expand Down Expand Up @@ -97,7 +119,7 @@ export class RequestsController {
body: {
pantryId: number;
requestedSize: RequestSize;
requestedItems: string[];
requestedFoodTypes: FoodType[];
additionalInformation: string;
dateReceived: Date;
feedback: string;
Expand All @@ -112,7 +134,7 @@ export class RequestsController {
return this.requestsService.create(
body.pantryId,
body.requestedSize,
body.requestedItems,
body.requestedFoodTypes,
body.additionalInformation,
body.dateReceived,
body.feedback,
Expand Down
19 changes: 17 additions & 2 deletions apps/backend/src/foodRequests/request.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { Order } from '../orders/order.entity';
import { RequestSize } from './types';
import { Pantry } from '../pantries/pantries.entity';
import { FoodType } from '../donationItems/types';

@Entity('food_requests')
export class FoodRequest {
Expand All @@ -31,8 +32,22 @@ export class FoodRequest {
})
requestedSize: RequestSize;

@Column({ name: 'requested_items', type: 'text', array: true })
requestedItems: string[];
@Column({
name: 'requested_food_types',
type: 'text',
array: true,
transformer: {
to: (value: FoodType[]) => value,
from: (value: string | string[]) =>
Array.isArray(value)
? value
: value
.slice(1, -1)
.match(/("([^"]*)")|([^,]+)/g)
?.map((v) => v.replace(/(^"|"$)/g, '').trim()) || [],
},
})
requestedFoodTypes: FoodType[];

@Column({ name: 'additional_information', type: 'text', nullable: true })
additionalInformation: string;
Expand Down
10 changes: 9 additions & 1 deletion apps/backend/src/foodRequests/request.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@ import { AuthModule } from '../auth/auth.module';
import { OrdersService } from '../orders/order.service';
import { Order } from '../orders/order.entity';
import { Pantry } from '../pantries/pantries.entity';
import { FoodManufacturer } from '../foodManufacturers/manufacturers.entity';
import { DonationItem } from '../donationItems/donationItems.entity';

@Module({
imports: [
AWSS3Module,
MulterModule.register({ dest: './uploads' }),
TypeOrmModule.forFeature([FoodRequest, Order, Pantry]),
TypeOrmModule.forFeature([
FoodRequest,
Order,
Pantry,
FoodManufacturer,
DonationItem,
]),
AuthModule,
],
controllers: [RequestsController],
Expand Down
Loading