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
18 changes: 0 additions & 18 deletions apps/backend-e2e/.eslintrc.json

This file was deleted.

19 changes: 0 additions & 19 deletions apps/backend-e2e/jest.config.ts

This file was deleted.

23 changes: 0 additions & 23 deletions apps/backend-e2e/project.json

This file was deleted.

10 changes: 0 additions & 10 deletions apps/backend-e2e/src/apps/backend/apps/backend.spec.ts

This file was deleted.

10 changes: 0 additions & 10 deletions apps/backend-e2e/src/support/global-setup.ts

This file was deleted.

7 changes: 0 additions & 7 deletions apps/backend-e2e/src/support/global-teardown.ts

This file was deleted.

10 changes: 0 additions & 10 deletions apps/backend-e2e/src/support/test-setup.ts

This file was deleted.

13 changes: 0 additions & 13 deletions apps/backend-e2e/tsconfig.json

This file was deleted.

9 changes: 0 additions & 9 deletions apps/backend-e2e/tsconfig.spec.json

This file was deleted.

7 changes: 0 additions & 7 deletions apps/backend/src/allocations/allocations.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,4 @@ import { Allocation } from './allocations.entity';
@Controller('allocations')
export class AllocationsController {
constructor(private allocationsService: AllocationsService) {}

@Get(':orderId/get-all-allocations')
async getAllAllocationsByOrder(
@Param('orderId', ParseIntPipe) orderId: number,
): Promise<Allocation[] | null> {
return this.allocationsService.getAllAllocationsByOrder(orderId);
}
}
1 change: 1 addition & 0 deletions apps/backend/src/allocations/allocations.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ import { JwtStrategy } from '../auth/jwt.strategy';
imports: [TypeOrmModule.forFeature([Allocation])],
controllers: [AllocationsController],
providers: [AllocationsService, AuthService, JwtStrategy],
exports: [AllocationsService],
})
export class AllocationModule {}
11 changes: 9 additions & 2 deletions apps/backend/src/allocations/allocations.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,17 @@ export class AllocationsService {

async getAllAllocationsByOrder(
orderId: number,
): Promise<Allocation[] | null> {
): Promise<Partial<Allocation>[]> {
return this.repo.find({
where: { orderId: orderId },
where: { orderId },
relations: ['item'],
select: {
allocationId: true,
allocatedQuantity: true,
reservedAt: true,
fulfilledAt: true,
status: true,
},
});
}
}
75 changes: 75 additions & 0 deletions apps/backend/src/orders/order.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { Test, TestingModule } from '@nestjs/testing';
import { OrdersController } from './order.controller';
import { OrdersService } from './order.service';
import { AllocationsService } from '../allocations/allocations.service';
import { Order } from './order.entity';
import { Allocation } from '../allocations/allocations.entity';
import { mock } from 'jest-mock-extended';

const mockOrdersService = mock<OrdersService>();
const mockAllocationsService = mock<AllocationsService>();

describe('OrdersController', () => {
let controller: OrdersController;

const mockOrders: Partial<Order>[] = [
{ orderId: 1, status: 'pending' },
{ orderId: 2, status: 'delivered' },
];

const mockAllocations: Partial<Allocation>[] = [
{ allocationId: 1, orderId: 1 },
{ allocationId: 2, orderId: 1 },
{ allocationId: 3, orderId: 2 },
];

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [OrdersController],
providers: [
{ provide: OrdersService, useValue: mockOrdersService },
{ provide: AllocationsService, useValue: mockAllocationsService },
],
}).compile();

controller = module.get<OrdersController>(OrdersController);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});

describe('getAllOrders', () => {
it('should call ordersService.getAll and return orders', async () => {
const status = 'pending';
const pantryNames = ['Test Pantry', 'Test Pantry 2'];
mockOrdersService.getAll.mockResolvedValueOnce([
mockOrders[0],
] as Order[]);

const result = await controller.getAllOrders(status, pantryNames);

expect(result).toEqual([mockOrders[0]] as Order[]);
expect(mockOrdersService.getAll).toHaveBeenCalledWith({
status,
pantryNames,
});
});
});

describe('getAllAllocationsByOrder', () => {
it('should call allocationsService.getAllAllocationsByOrder and return allocations', async () => {
const orderId = 1;
mockAllocationsService.getAllAllocationsByOrder.mockResolvedValueOnce(
mockAllocations.slice(0, 2) as Allocation[],
);

const result = await controller.getAllAllocationsByOrder(orderId);

expect(result).toEqual(mockAllocations.slice(0, 2) as Allocation[]);
expect(
mockAllocationsService.getAllAllocationsByOrder,
).toHaveBeenCalledWith(orderId);
});
});
});
28 changes: 24 additions & 4 deletions apps/backend/src/orders/order.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,34 @@ import {
Param,
ParseIntPipe,
Body,
Query,
} from '@nestjs/common';
import { OrdersService } from './order.service';
import { Order } from './order.entity';
import { Pantry } from '../pantries/pantries.entity';
import { FoodManufacturer } from '../foodManufacturers/manufacturer.entity';
import { FoodRequest } from '../foodRequests/request.entity';
import { Donation } from '../donations/donations.entity';
import { AllocationsService } from '../allocations/allocations.service';

@Controller('orders')
export class OrdersController {
constructor(private ordersService: OrdersService) {}
constructor(
private readonly ordersService: OrdersService,
private readonly allocationsService: AllocationsService,
) {}

@Get('/get-all-orders')
async getAllOrders(): Promise<Order[]> {
return this.ordersService.getAll();
// Called like: /?status=pending&pantryName=Test%20Pantry&pantryName=Test%20Pantry%2
// %20 is the URL encoded space character
@Get('/')
async getAllOrders(
@Query('status') status?: string,
@Query('pantryName') pantryNames?: string | string[],
): Promise<Order[]> {
if (typeof pantryNames === 'string') {
pantryNames = [pantryNames];
}
return this.ordersService.getAll({ status, pantryNames });
}

@Get('/get-current-orders')
Expand Down Expand Up @@ -74,6 +87,13 @@ export class OrdersController {
return this.ordersService.findOrderByRequest(requestId);
}

@Get(':orderId/allocations')
async getAllAllocationsByOrder(
@Param('orderId', ParseIntPipe) orderId: number,
) {
return this.allocationsService.getAllAllocationsByOrder(orderId);
}

@Patch('/update-status/:orderId')
async updateStatus(
@Param('orderId', ParseIntPipe) orderId: number,
Expand Down
3 changes: 2 additions & 1 deletion apps/backend/src/orders/order.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import { Order } from './order.entity';
import { OrdersService } from './order.service';
import { JwtStrategy } from '../auth/jwt.strategy';
import { AuthService } from '../auth/auth.service';
import { AllocationModule } from '../allocations/allocations.module';

@Module({
imports: [TypeOrmModule.forFeature([Order])],
imports: [TypeOrmModule.forFeature([Order]), AllocationModule],
controllers: [OrdersController],
providers: [OrdersService, AuthService, JwtStrategy],
})
Expand Down
Loading