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
5 changes: 5 additions & 0 deletions apps/backend/src/allocations/allocations.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
JoinColumn,
} from 'typeorm';
import { DonationItem } from '../donationItems/donationItems.entity';
import { Order } from '../orders/order.entity';

@Entity('allocations')
export class Allocation {
Expand All @@ -15,6 +16,10 @@ export class Allocation {
@Column({ name: 'order_id', type: 'int' })
orderId: number;

@ManyToOne(() => Order, (order) => order.allocations)
@JoinColumn({ name: 'order_id' })
order: Order;

@Column({ name: 'item_id', type: 'int', nullable: false })
itemId: number;

Expand Down
15 changes: 15 additions & 0 deletions apps/backend/src/foodRequests/dtos/order-details.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { FoodType } from '../../donationItems/types';
import { OrderStatus } from '../../orders/types';

export class OrderItemDetailsDto {
name: string;
quantity: number;
foodType: FoodType;
}

export class OrderDetailsDto {
orderId: number;
status: OrderStatus;
foodManufacturerName: string;
items: OrderItemDetailsDto[];
}
60 changes: 56 additions & 4 deletions apps/backend/src/foodRequests/request.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { Readable } from 'stream';
import { FoodRequest } from './request.entity';
import { RequestSize } from './types';
import { OrderStatus } from '../orders/types';
import { FoodType } from '../donationItems/types';
import { OrderDetailsDto } from './dtos/order-details.dto';

const mockRequestsService = mock<RequestsService>();
const mockOrdersService = mock<OrdersService>();
Expand All @@ -26,6 +28,7 @@ describe('RequestsController', () => {
mockRequestsService.find.mockReset();
mockRequestsService.create.mockReset();
mockRequestsService.updateDeliveryDetails?.mockReset();
mockRequestsService.getOrderDetails.mockReset();
mockAWSS3Service.upload.mockReset();
mockOrdersService.updateStatus.mockReset();

Expand Down Expand Up @@ -91,6 +94,55 @@ describe('RequestsController', () => {
});
});

describe('GET /get-all-order-details/:requestId', () => {
it('should call requestsService.getOrderDetails and return all associated orders and their details', async () => {
const mockOrderDetails = [
{
orderId: 10,
status: OrderStatus.DELIVERED,
foodManufacturerName: 'Test Manufacturer',
items: [
{
name: 'Rice',
quantity: 5,
foodType: FoodType.GRANOLA,
},
{
name: 'Beans',
quantity: 3,
foodType: FoodType.DRIED_BEANS,
},
],
},
{
orderId: 11,
status: OrderStatus.PENDING,
foodManufacturerName: 'Another Manufacturer',
items: [
{
name: 'Milk',
quantity: 2,
foodType: FoodType.DAIRY_FREE_ALTERNATIVES,
},
],
},
];

const requestId = 1;

mockRequestsService.getOrderDetails.mockResolvedValueOnce(
mockOrderDetails as OrderDetailsDto[],
);

const result = await controller.getAllOrderDetailsFromRequest(requestId);

expect(result).toEqual(mockOrderDetails);
expect(mockRequestsService.getOrderDetails).toHaveBeenCalledWith(
requestId,
);
});
});

describe('POST /create', () => {
it('should call requestsService.create and return the created food request', async () => {
const createBody: Partial<FoodRequest> = {
Expand All @@ -107,7 +159,7 @@ describe('RequestsController', () => {
requestId: 1,
...createBody,
requestedAt: new Date(),
order: null,
orders: null,
};

mockRequestsService.create.mockResolvedValueOnce(
Expand Down Expand Up @@ -181,7 +233,7 @@ describe('RequestsController', () => {
mockRequestsService.findOne.mockResolvedValue({
requestId,
pantryId: 1,
order: { orderId: 99 },
orders: [{ orderId: 99 }],
} as FoodRequest);

mockOrdersService.updateStatus.mockResolvedValue();
Expand Down Expand Up @@ -230,7 +282,7 @@ describe('RequestsController', () => {
mockRequestsService.findOne.mockResolvedValue({
requestId,
pantryId: 1,
order: { orderId: 100 },
orders: [{ orderId: 100 }],
} as FoodRequest);

mockOrdersService.updateStatus.mockResolvedValue();
Expand Down Expand Up @@ -275,7 +327,7 @@ describe('RequestsController', () => {
mockRequestsService.findOne.mockResolvedValue({
requestId,
pantryId: 1,
order: { orderId: 101 },
orders: [{ orderId: 101 }],
} as FoodRequest);

mockOrdersService.updateStatus.mockResolvedValue();
Expand Down
17 changes: 13 additions & 4 deletions apps/backend/src/foodRequests/request.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import { AWSS3Service } from '../aws/aws-s3.service';
import { FilesInterceptor } from '@nestjs/platform-express';
import * as multer from 'multer';
import { OrdersService } from '../orders/order.service';
import { Order } from '../orders/order.entity';
import { RequestSize } from './types';
import { OrderStatus } from '../orders/types';
import { OrderDetailsDto } from './dtos/order-details.dto';

@Controller('requests')
// @UseInterceptors()
Expand All @@ -43,6 +43,13 @@ export class RequestsController {
return this.requestsService.find(pantryId);
}

@Get('/get-all-order-details/:requestId')
async getAllOrderDetailsFromRequest(
@Param('requestId', ParseIntPipe) requestId: number,
): Promise<OrderDetailsDto[]> {
return this.requestsService.getOrderDetails(requestId);
}

@Post('/create')
@ApiBody({
description: 'Details for creating a food request',
Expand Down Expand Up @@ -158,9 +165,11 @@ export class RequestsController {
);

const request = await this.requestsService.findOne(requestId);
await this.ordersService.updateStatus(
request.order.orderId,
OrderStatus.DELIVERED,

await Promise.all(
request.orders.map((order) =>
this.ordersService.updateStatus(order.orderId, OrderStatus.DELIVERED),
),
);

return this.requestsService.updateDeliveryDetails(
Expand Down
4 changes: 2 additions & 2 deletions apps/backend/src/foodRequests/request.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ export class FoodRequest {
@Column({ name: 'photos', type: 'text', array: true, nullable: true })
photos: string[];

@OneToMany(() => Order, (order) => order.request, { nullable: true })
order: Order;
@OneToMany(() => Order, (order) => order.request)
orders: Order[];
}
Loading
Loading