Skip to content
Open
Show file tree
Hide file tree
Changes from 19 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,
},
});
}
}
2 changes: 2 additions & 0 deletions apps/backend/src/config/typeorm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { UpdateFoodRequests1744051370129 } from '../migrations/1744051370129-upd
import { UpdateRequestTable1741571847063 } from '../migrations/1741571847063-updateRequestTable';
import { RemoveOrderIdFromRequests1744133526650 } from '../migrations/1744133526650-removeOrderIdFromRequests.ts';
import { AddOrders1739496585940 } from '../migrations/1739496585940-addOrders';
import { UpdatePantriesTable1742739750279 } from '../migrations/1742739750279-updatePantriesTable';

const config = {
type: 'postgres',
Expand Down Expand Up @@ -45,6 +46,7 @@ const config = {
UpdateRequestTable1741571847063,
UpdateFoodRequests1744051370129,
RemoveOrderIdFromRequests1744133526650,
UpdatePantriesTable1742739750279,
],
};

Expand Down
1 change: 1 addition & 0 deletions apps/backend/src/donations/donations.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class DonationService {

const donation = await this.repo.findOne({
where: { donationId },
relations: ['foodManufacturer'],
});

if (!donation) {
Expand Down
73 changes: 73 additions & 0 deletions apps/backend/src/migrations/1742739750279-updatePantriesTable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class UpdatePantriesTable1742739750279 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE pantries
DROP COLUMN address,
ADD COLUMN address_line_1 varchar(255) NOT NULL DEFAULT 'A',
ADD COLUMN address_line_2 varchar(255),
ADD COLUMN address_city varchar(255) NOT NULL DEFAULT 'A',
ADD COLUMN address_state varchar(255) NOT NULL DEFAULT 'A',
ADD COLUMN address_zip varchar(255) NOT NULL DEFAULT 'A',
ADD COLUMN address_country varchar(255),
ALTER COLUMN reserve_food_for_allergic TYPE varchar(25) USING (
CASE
WHEN reserve_food_for_allergic = true THEN 'Yes'
ELSE 'No'
END
),
ALTER COLUMN reservation_explanation DROP NOT NULL,
ALTER COLUMN client_visit_frequency DROP NOT NULL,
ALTER COLUMN identify_allergens_confidence DROP NOT NULL,
ALTER COLUMN serve_allergic_children DROP NOT NULL,
ALTER COLUMN ssf_representative_id DROP NOT NULL,
ALTER COLUMN activities TYPE varchar(255)[] USING ARRAY[activities];

-- drop temporary defaults
ALTER TABLE pantries
ALTER COLUMN address_line_1 DROP DEFAULT,
ALTER COLUMN address_city DROP DEFAULT,
ALTER COLUMN address_state DROP DEFAULT,
ALTER COLUMN address_zip DROP DEFAULT;

ALTER TABLE pantries
RENAME COLUMN questions TO activities_comments;
`);
}

// Loses address info
// Columns where we are doing SET NOT NULL must not have null values
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE pantries
ADD COLUMN address varchar(255) NOT NULL DEFAULT 'A',
DROP COLUMN address_line_1,
DROP COLUMN address_line_2,
DROP COLUMN address_city,
DROP COLUMN address_state,
DROP COLUMN address_zip,
DROP COLUMN address_country,
ALTER COLUMN reserve_food_for_allergic TYPE boolean USING (
CASE
WHEN reserve_food_for_allergic = 'Yes' THEN true
WHEN reserve_food_for_allergic = 'Some' THEN true
ELSE false
END
),
ALTER COLUMN reservation_explanation SET NOT NULL,
ALTER COLUMN client_visit_frequency SET NOT NULL,
ALTER COLUMN identify_allergens_confidence SET NOT NULL,
ALTER COLUMN serve_allergic_children SET NOT NULL,
ALTER COLUMN ssf_representative_id SET NOT NULL,
ALTER COLUMN activities TYPE text USING array_to_string(activities, ',');

-- drop temporary defaults
ALTER TABLE pantries
ALTER COLUMN address DROP DEFAULT;

ALTER TABLE pantries
RENAME COLUMN activities_comments TO questions;
`);
}
}
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
Loading