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
1 change: 1 addition & 0 deletions apps/backend/src/foodRequests/dtos/order-details.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ export class OrderDetailsDto {
orderId: number;
status: OrderStatus;
foodManufacturerName: string;
trackingLink: string | null;
items: OrderItemDetailsDto[];
}
4 changes: 3 additions & 1 deletion apps/backend/src/foodRequests/request.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,14 @@ describe('RequestsController', () => {
});
});

describe('GET /all-order-details/:requestId', () => {
describe('GET /:requestId/order-details', () => {
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',
trackingLink: 'examplelink.com',
items: [
{
name: 'Rice',
Expand All @@ -119,6 +120,7 @@ describe('RequestsController', () => {
orderId: 11,
status: OrderStatus.PENDING,
foodManufacturerName: 'Another Manufacturer',
trackingLink: 'examplelink.com',
items: [
{
name: 'Milk',
Expand Down
1 change: 1 addition & 0 deletions apps/backend/src/foodRequests/request.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export class RequestsService {
orderId: order.orderId,
status: order.status,
foodManufacturerName: order.foodManufacturer.foodManufacturerName,
trackingLink: order.trackingLink,
items: order.allocations.map((allocation) => ({
name: allocation.item.itemName,
quantity: allocation.allocatedQuantity,
Expand Down
19 changes: 19 additions & 0 deletions apps/backend/src/orders/dtos/food-request-summary.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { RequestSize } from '../../foodRequests/types';

export class FoodRequestSummaryDto {
requestId!: number;
pantryId!: number;

pantryName!: string;

requestedSize!: RequestSize;
requestedItems!: string[];

additionalInformation!: string | null;

requestedAt!: Date;
dateReceived!: Date | null;

feedback!: string | null;
photos!: string[] | null;
}
65 changes: 39 additions & 26 deletions apps/backend/src/orders/order.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import { OrderStatus } from './types';
import { FoodRequest } from '../foodRequests/request.entity';
import { Pantry } from '../pantries/pantries.entity';
import { TrackingCostDto } from './dtos/tracking-cost.dto';
import { OrderDetailsDto } from '../foodRequests/dtos/order-details.dto';
import { FoodType } from '../donationItems/types';
import { BadRequestException, NotFoundException } from '@nestjs/common';
import { FoodManufacturer } from '../foodManufacturers/manufacturers.entity';
import { FoodRequestSummaryDto } from './dtos/food-request-summary.dto';

const mockOrdersService = mock<OrdersService>();
const mockAllocationsService = mock<AllocationsService>();
Expand All @@ -30,6 +33,11 @@ describe('OrdersController', () => {
{ requestId: 3, pantry: mockPantries[2] as Pantry },
];

const mockRequestSummary: Partial<FoodRequestSummaryDto> = {
requestId: 4,
pantryName: 'Example Pantry',
};

const mockFoodManufacturer: Partial<FoodManufacturer> = {
foodManufacturerId: 1,
foodManufacturerName: 'Test FM',
Expand Down Expand Up @@ -62,6 +70,20 @@ describe('OrdersController', () => {
{ allocationId: 3, orderId: 2 },
];

const mockOrderDetails: Partial<OrderDetailsDto> = {
orderId: 1,
status: OrderStatus.DELIVERED,
foodManufacturerName: 'food manufacturer 1',
trackingLink: 'example-link.com',
items: [
{
name: 'item1',
quantity: 10,
foodType: FoodType.DAIRY_FREE_ALTERNATIVES,
},
],
};

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [OrdersController],
Expand All @@ -78,6 +100,21 @@ describe('OrdersController', () => {
expect(controller).toBeDefined();
});

describe('getOrder', () => {
it('should call ordersService.findOrderDetails and return order details', async () => {
mockOrdersService.findOrderDetails.mockResolvedValueOnce(
mockOrderDetails as OrderDetailsDto,
);

const orderId = 1;

const result = await controller.getOrder(orderId);

expect(result).toEqual(mockOrderDetails as OrderDetailsDto);
expect(mockOrdersService.findOrderDetails).toHaveBeenCalledWith(orderId);
});
});

describe('getAllOrders', () => {
it('should call ordersService.getAll and return orders', async () => {
const status = 'pending';
Expand Down Expand Up @@ -155,12 +192,12 @@ describe('OrdersController', () => {
it('should call ordersService.findOrderFoodRequest and return food request', async () => {
const orderId = 1;
mockOrdersService.findOrderFoodRequest.mockResolvedValueOnce(
mockRequests[0] as FoodRequest,
mockRequestSummary as FoodRequestSummaryDto,
);

const result = await controller.getRequestFromOrder(orderId);

expect(result).toEqual(mockRequests[0] as Pantry);
expect(result).toEqual(mockRequestSummary as FoodRequestSummaryDto);
expect(mockOrdersService.findOrderFoodRequest).toHaveBeenCalledWith(
orderId,
);
Expand Down Expand Up @@ -211,30 +248,6 @@ describe('OrdersController', () => {
});
});

describe('getOrder', () => {
it('should call ordersService.findOne and return order', async () => {
const orderId = 1;
mockOrdersService.findOne.mockResolvedValueOnce(mockOrders[0] as Order);

const result = await controller.getOrder(orderId);

expect(result).toEqual(mockOrders[0] as Order);
expect(mockOrdersService.findOne).toHaveBeenCalledWith(orderId);
});

it('should propagate NotFoundException when order not found', async () => {
const orderId = 999;
mockOrdersService.findOne.mockRejectedValueOnce(
new NotFoundException(`Order ${orderId} not found`),
);

const promise = controller.getOrder(orderId);
await expect(promise).rejects.toBeInstanceOf(NotFoundException);
await expect(promise).rejects.toThrow(`Order ${orderId} not found`);
expect(mockOrdersService.findOne).toHaveBeenCalledWith(orderId);
});
});

describe('getOrderByRequestId', () => {
it('should call ordersService.findOrderByRequest and return order', async () => {
const requestId = 1;
Expand Down
8 changes: 5 additions & 3 deletions apps/backend/src/orders/order.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { FoodRequest } from '../foodRequests/request.entity';
import { AllocationsService } from '../allocations/allocations.service';
import { OrderStatus } from './types';
import { TrackingCostDto } from './dtos/tracking-cost.dto';
import { OrderDetailsDto } from '../foodRequests/dtos/order-details.dto';
import { FoodRequestSummaryDto } from './dtos/food-request-summary.dto';

@Controller('orders')
export class OrdersController {
Expand Down Expand Up @@ -59,7 +61,7 @@ export class OrdersController {
@Get('/:orderId/request')
async getRequestFromOrder(
@Param('orderId', ParseIntPipe) orderId: number,
): Promise<FoodRequest> {
): Promise<FoodRequestSummaryDto> {
return this.ordersService.findOrderFoodRequest(orderId);
}

Expand All @@ -73,8 +75,8 @@ export class OrdersController {
@Get('/:orderId')
async getOrder(
@Param('orderId', ParseIntPipe) orderId: number,
): Promise<Order> {
return this.ordersService.findOne(orderId);
): Promise<OrderDetailsDto> {
return this.ordersService.findOrderDetails(orderId);
}

@Get('/order/:requestId')
Expand Down
55 changes: 55 additions & 0 deletions apps/backend/src/orders/order.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import { Order } from './order.entity';
import { testDataSource } from '../config/typeormTestDataSource';
import { OrderStatus } from './types';
import { Pantry } from '../pantries/pantries.entity';
import { OrderDetailsDto } from '../foodRequests/dtos/order-details.dto';
import { BadRequestException, NotFoundException } from '@nestjs/common';
import { TrackingCostDto } from './dtos/tracking-cost.dto';
import { FoodType } from '../donationItems/types';

// Set 1 minute timeout for async DB operations
jest.setTimeout(60000);
Expand Down Expand Up @@ -124,6 +126,59 @@ describe('OrdersService', () => {
});
});

describe('findOrderDetails', () => {
it('returns mapped OrderDetailsDto including allocations and manufacturer', async () => {
const orderId = 1;

const result = await service.findOrderDetails(orderId);

const expected: OrderDetailsDto = {
orderId: 1,
status: OrderStatus.DELIVERED,
foodManufacturerName: 'FoodCorp Industries',
trackingLink: 'www.samplelink/samplelink',
items: [
{
foodType: FoodType.SEED_BUTTERS,
name: 'Peanut Butter (16oz)',
quantity: 10,
},
{
foodType: FoodType.REFRIGERATED_MEALS,
name: 'Canned Green Beans',
quantity: 5,
},
{
foodType: FoodType.GLUTEN_FREE_BREAD,
name: 'Whole Wheat Bread',
quantity: 25,
},
],
};

expect(result).toMatchObject({
orderId: expected.orderId,
status: expected.status,
foodManufacturerName: expected.foodManufacturerName,
trackingLink: expected.trackingLink,
});

expect(result.items).toHaveLength(expected.items.length);
expect(result.items).toEqual(expect.arrayContaining(expected.items));
});

it('throws NotFoundException when order does not exist', async () => {
const missingOrderId = 99999999;

await expect(service.findOrderDetails(missingOrderId)).rejects.toThrow(
NotFoundException,
);
await expect(service.findOrderDetails(missingOrderId)).rejects.toThrow(
`Order ${missingOrderId} not found`,
);
});
});

describe('getCurrentOrders', () => {
it(`returns only orders with status 'pending' or 'shipped'`, async () => {
const orders = await service.getCurrentOrders();
Expand Down
74 changes: 71 additions & 3 deletions apps/backend/src/orders/order.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { FoodRequest } from '../foodRequests/request.entity';
import { validateId } from '../utils/validation.utils';
import { OrderStatus } from './types';
import { TrackingCostDto } from './dtos/tracking-cost.dto';
import { OrderDetailsDto } from '../foodRequests/dtos/order-details.dto';
import { FoodRequestSummaryDto } from './dtos/food-request-summary.dto';

@Injectable()
export class OrdersService {
Expand Down Expand Up @@ -77,6 +79,36 @@ export class OrdersService {
return order;
}

async findOrderDetails(orderId: number): Promise<OrderDetailsDto> {
validateId(orderId, 'Order');

const order = await this.repo.findOne({
where: { orderId },
relations: {
allocations: {
item: true,
},
foodManufacturer: true,
},
});

if (!order) {
throw new NotFoundException(`Order ${orderId} not found`);
}

return {
orderId: order.orderId,
status: order.status,
foodManufacturerName: order.foodManufacturer.foodManufacturerName,
trackingLink: order.trackingLink,
items: order.allocations.map((allocation) => ({
name: allocation.item.itemName,
quantity: allocation.allocatedQuantity,
foodType: allocation.item.foodType,
})),
};
}

async findOrderByRequest(requestId: number): Promise<Order> {
validateId(requestId, 'Request');

Expand Down Expand Up @@ -106,18 +138,54 @@ export class OrdersService {
return pantry;
}

async findOrderFoodRequest(orderId: number): Promise<FoodRequest> {
async findOrderFoodRequest(orderId: number): Promise<FoodRequestSummaryDto> {
validateId(orderId, 'Order');

const order = await this.repo.findOne({
where: { orderId },
relations: ['request'],
relations: {
request: {
pantry: true,
},
},
select: {
request: {
requestId: true,
pantryId: true,
requestedSize: true,
requestedItems: true,
additionalInformation: true,
requestedAt: true,
dateReceived: true,
feedback: true,
photos: true,
pantry: {
pantryName: true,
},
},
},
});

if (!order) {
throw new NotFoundException(`Order ${orderId} not found`);
}
return order.request;

return {
requestId: order.request.requestId,
pantryId: order.request.pantryId,
pantryName: order.request.pantry.pantryName,

requestedSize: order.request.requestedSize,
requestedItems: order.request.requestedItems,

additionalInformation: order.request.additionalInformation ?? null,

requestedAt: order.request.requestedAt,
dateReceived: order.request.dateReceived ?? null,

feedback: order.request.feedback ?? null,
photos: order.request.photos ?? null,
};
}

async findOrderFoodManufacturer(orderId: number): Promise<FoodManufacturer> {
Expand Down
Loading