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

@Entity('allocations')
export class Allocation {
@PrimaryGeneratedColumn({ name: 'allocation_id' })
allocationId: number;

@Column({ name: 'order_id', type: 'int', nullable: false })
@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: 0 additions & 15 deletions apps/backend/src/foodRequests/dtos/order-details.dto.ts

This file was deleted.

77 changes: 8 additions & 69 deletions apps/backend/src/foodRequests/request.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ 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';
import { Order } from '../orders/order.entity';

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

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

describe('GET /all-order-details/:requestId', () => {
it('should call requestsService.getOrderDetails and return all associated orders and their details', async () => {
const mockOrderDetails: OrderDetailsDto[] = [
{
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 @@ -160,7 +107,7 @@ describe('RequestsController', () => {
requestId: 1,
...createBody,
requestedAt: new Date(),
orders: null,
order: null,
};

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

mockOrdersService.updateStatus.mockResolvedValue();

const order = new Order();
order.orderId = 99;

const updatedRequest: Partial<FoodRequest> = {
requestId,
pantryId: 1,
dateReceived: new Date(body.dateReceived),
feedback: body.feedback,
photos: uploadedUrls,
orders: [order],
};

mockRequestsService.updateDeliveryDetails.mockResolvedValue(
Expand All @@ -259,6 +202,8 @@ describe('RequestsController', () => {

expect(mockAWSS3Service.upload).toHaveBeenCalledWith(photos);

expect(mockRequestsService.findOne).toHaveBeenCalledWith(requestId);

expect(mockOrdersService.updateStatus).toHaveBeenCalledWith(
99,
OrderStatus.DELIVERED,
Expand All @@ -285,21 +230,17 @@ describe('RequestsController', () => {
mockRequestsService.findOne.mockResolvedValue({
requestId,
pantryId: 1,
orders: [{ orderId: 100 }],
order: { orderId: 100 },
} as FoodRequest);

mockOrdersService.updateStatus.mockResolvedValue();

const order = new Order();
order.orderId = 100;

const updatedRequest: Partial<FoodRequest> = {
requestId,
pantryId: 1,
dateReceived: new Date(body.dateReceived),
feedback: body.feedback,
photos: [],
orders: [order],
};

mockRequestsService.updateDeliveryDetails.mockResolvedValue(
Expand All @@ -309,6 +250,7 @@ describe('RequestsController', () => {
const result = await controller.confirmDelivery(requestId, body);

expect(mockAWSS3Service.upload).not.toHaveBeenCalled();
expect(mockRequestsService.findOne).toHaveBeenCalledWith(requestId);
expect(mockOrdersService.updateStatus).toHaveBeenCalledWith(
100,
OrderStatus.DELIVERED,
Expand All @@ -333,21 +275,17 @@ describe('RequestsController', () => {
mockRequestsService.findOne.mockResolvedValue({
requestId,
pantryId: 1,
orders: [{ orderId: 101 }],
order: { orderId: 101 },
} as FoodRequest);

mockOrdersService.updateStatus.mockResolvedValue();

const order = new Order();
order.orderId = 101;

const updatedRequest: Partial<FoodRequest> = {
requestId,
pantryId: 1,
dateReceived: new Date(body.dateReceived),
feedback: body.feedback,
photos: [],
orders: [order],
};

mockRequestsService.updateDeliveryDetails.mockResolvedValue(
Expand All @@ -357,6 +295,7 @@ describe('RequestsController', () => {
const result = await controller.confirmDelivery(requestId, body, []);

expect(mockAWSS3Service.upload).not.toHaveBeenCalled();
expect(mockRequestsService.findOne).toHaveBeenCalledWith(requestId);
expect(mockOrdersService.updateStatus).toHaveBeenCalledWith(
101,
OrderStatus.DELIVERED,
Expand Down
37 changes: 8 additions & 29 deletions apps/backend/src/foodRequests/request.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
UploadedFiles,
UseInterceptors,
BadRequestException,
NotFoundException,
} from '@nestjs/common';
import { ApiBody } from '@nestjs/swagger';
import { RequestsService } from './request.service';
Expand All @@ -17,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 @@ -44,13 +43,6 @@ export class RequestsController {
return this.requestsService.find(pantryId);
}

@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 @@ -117,7 +109,6 @@ export class RequestsController {
);
}

//TODO: delete endpoint, here temporarily as a logic reference for order status impl.
@Post('/:requestId/confirm-delivery')
@ApiBody({
description: 'Details for a confirmation form',
Expand Down Expand Up @@ -166,29 +157,17 @@ export class RequestsController {
photos?.length,
);

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

return this.requestsService.updateDeliveryDetails(
requestId,
formattedDate,
body.feedback,
uploadedPhotoUrls,
);

if (!updatedRequest) {
throw new NotFoundException('Invalid request ID');
}

if (!updatedRequest.orders || updatedRequest.orders.length == 0) {
throw new NotFoundException(
'No associated orders found for this request',
);
}

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

return updatedRequest;
}
}
2 changes: 1 addition & 1 deletion apps/backend/src/foodRequests/request.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ export class FoodRequest {
photos: string[];

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