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
3 changes: 3 additions & 0 deletions apps/backend/src/donationItems/donationItems.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export class DonationItem {
@PrimaryGeneratedColumn({ name: 'item_id' })
itemId: number;

@Column({ name: 'donation_id', type: 'int' })
donation_id: number;

@ManyToOne(() => Donation, { nullable: false })
@JoinColumn({ name: 'donation_id', referencedColumnName: 'donationId' })
donation: Donation;
Expand Down
9 changes: 0 additions & 9 deletions apps/backend/src/orders/order.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import {
CreateDateColumn,
ManyToOne,
JoinColumn,
OneToOne,
} from 'typeorm';
import { FoodRequest } from '../foodRequests/request.entity';
import { Pantry } from '../pantries/pantries.entity';
import { FoodManufacturer } from '../foodManufacturers/manufacturer.entity';
import { Donation } from '../donations/donations.entity';
import { OrderStatus } from './types';

@Entity('orders')
Expand Down Expand Up @@ -45,13 +43,6 @@ export class Order {
@Column({ name: 'shipped_by', nullable: true })
shippedBy: number;

@ManyToOne(() => Donation, { nullable: false })
@JoinColumn({
name: 'donation_id',
referencedColumnName: 'donationId',
})
donation: Donation;

@Column({
name: 'status',
type: 'enum',
Expand Down
1 change: 1 addition & 0 deletions apps/backend/src/orders/order.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ import { AllocationModule } from '../allocations/allocations.module';
imports: [TypeOrmModule.forFeature([Order]), AllocationModule],
controllers: [OrdersController],
providers: [OrdersService, AuthService, JwtStrategy],
exports: [OrdersService],
})
export class OrdersModule {}
32 changes: 12 additions & 20 deletions apps/backend/src/orders/order.service.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import {
BadRequestException,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, In } from 'typeorm';
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 { validateId } from '../utils/validation.utils';
import { OrderStatus } from './types';

Expand Down Expand Up @@ -121,20 +116,6 @@ export class OrdersService {
return order.foodManufacturer;
}

async findOrderDonation(orderId: number): Promise<Donation> {
validateId(orderId, 'Order');

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

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

async updateStatus(orderId: number, newStatus: OrderStatus) {
validateId(orderId, 'Order');

Expand All @@ -151,4 +132,15 @@ export class OrdersService {
.where('order_id = :orderId', { orderId })
.execute();
}

async getOrdersByPantry(pantryId: number): Promise<Order[]> {
validateId(pantryId, 'Pantry');

const orders = await this.repo.find({
where: { pantry: { pantryId } },
relations: ['request'],
});

return orders;
}
}
62 changes: 62 additions & 0 deletions apps/backend/src/pantries/pantries.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Test, TestingModule } from '@nestjs/testing';
import { mock } from 'jest-mock-extended';
import { PantriesController } from './pantries.controller';
import { PantriesService } from './pantries.service';
import { OrdersService } from '../orders/order.service';
import { Order } from '../orders/order.entity';

const mockPantriesService = mock<PantriesService>();
const mockOrdersService = mock<OrdersService>();

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

beforeEach(async () => {
jest.clearAllMocks();

const module: TestingModule = await Test.createTestingModule({
controllers: [PantriesController],
providers: [
{ provide: PantriesService, useValue: mockPantriesService },
{ provide: OrdersService, useValue: mockOrdersService },
],
}).compile();

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

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

describe('getOrders', () => {
it('should return orders for a pantry', async () => {
const pantryId = 24;

const mockOrders: Partial<Order>[] = [
{
orderId: 26,
requestId: 26,
shippedBy: 32,
},
{
orderId: 27,
requestId: 27,
shippedBy: 33,
},
];

mockOrdersService.getOrdersByPantry.mockResolvedValue(
mockOrders as Order[],
);

const result = await controller.getOrders(pantryId);

expect(result).toEqual(mockOrders);
expect(result).toHaveLength(2);
expect(result[0].orderId).toBe(26);
expect(result[1].orderId).toBe(27);
expect(mockOrdersService.getOrdersByPantry).toHaveBeenCalledWith(24);
});
});
});
34 changes: 25 additions & 9 deletions apps/backend/src/pantries/pantries.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,29 @@ import {
Param,
ParseIntPipe,
Post,
ValidationPipe
ValidationPipe,
} from '@nestjs/common';
import { Pantry } from './pantries.entity';
import { PantriesService } from './pantries.service';
import { PantryApplicationDto } from './dtos/pantry-application.dto';
import { ApiBody } from '@nestjs/swagger';
import { Activity, AllergensConfidence, ClientVisitFrequency, RefrigeratedDonation, ReserveFoodForAllergic, ServeAllergicChildren } from './types';
import {
Activity,
AllergensConfidence,
ClientVisitFrequency,
RefrigeratedDonation,
ReserveFoodForAllergic,
ServeAllergicChildren,
} from './types';
import { Order } from '../orders/order.entity';
import { OrdersService } from '../orders/order.service';

@Controller('pantries')
export class PantriesController {
constructor(private pantriesService: PantriesService) {}
constructor(
private pantriesService: PantriesService,
private ordersService: OrdersService,
) {}

@Get('/pending')
async getPendingPantries(): Promise<Pantry[]> {
Expand All @@ -29,6 +41,13 @@ export class PantriesController {
return this.pantriesService.findOne(pantryId);
}

@Get('/:pantryId/orders')
async getOrders(
@Param('pantryId', ParseIntPipe) pantryId: number,
): Promise<Order[]> {
return this.ordersService.getOrdersByPantry(pantryId);
}

@ApiBody({
description: 'Details for submitting a pantry application',
schema: {
Expand Down Expand Up @@ -142,12 +161,9 @@ export class PantriesController {
type: 'array',
items: {
type: 'string',
enum: Object.values(Activity)
enum: Object.values(Activity),
},
example: [
Activity.COLLECT_FEEDBACK,
Activity.CREATE_LABELED_SHELF,
],
example: [Activity.COLLECT_FEEDBACK, Activity.CREATE_LABELED_SHELF],
},
activitiesComments: {
type: 'string',
Expand Down Expand Up @@ -190,7 +206,7 @@ export class PantriesController {
})
@Post()
async submitPantryApplication(
@Body(new ValidationPipe())
@Body(new ValidationPipe())
pantryData: PantryApplicationDto,
): Promise<void> {
return this.pantriesService.addPantry(pantryData);
Expand Down
24 changes: 15 additions & 9 deletions apps/backend/src/pantries/pantries.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ export class Pantry {
})
refrigeratedDonation: RefrigeratedDonation;

@Column({ name: 'reserve_food_for_allergic',
type: 'enum',
enum: ReserveFoodForAllergic,
enumName: 'reserve_food_for_allergic_enum'
@Column({
name: 'reserve_food_for_allergic',
type: 'enum',
enum: ReserveFoodForAllergic,
enumName: 'reserve_food_for_allergic_enum',
})
reserveFoodForAllergic: string;

Expand Down Expand Up @@ -114,7 +115,11 @@ export class Pantry {
// cascade: ['insert'] means that when we create a new
// pantry, the pantry user will automatically be added
// to the User table
@OneToOne(() => User, { nullable: false, cascade: ['insert'], onDelete: 'CASCADE' })
@OneToOne(() => User, {
nullable: false,
cascade: ['insert'],
onDelete: 'CASCADE',
})
@JoinColumn({
name: 'pantry_user_id',
referencedColumnName: 'id',
Expand All @@ -136,12 +141,13 @@ export class Pantry {
})
dateApplied: Date;

@Column({
name: 'activities',
@Column({
name: 'activities',
type: 'enum',
enum: Activity,
enum: Activity,
enumName: 'activity_enum',
array: true })
array: true,
})
activities: Activity[];

@Column({ name: 'activities_comments', type: 'text', nullable: true })
Expand Down
3 changes: 2 additions & 1 deletion apps/backend/src/pantries/pantries.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import { PantriesController } from './pantries.controller';
import { JwtStrategy } from '../auth/jwt.strategy';
import { AuthService } from '../auth/auth.service';
import { Pantry } from './pantries.entity';
import { OrdersModule } from '../orders/order.module';

@Module({
imports: [TypeOrmModule.forFeature([Pantry, User])],
imports: [TypeOrmModule.forFeature([Pantry, User]), OrdersModule],
controllers: [PantriesController],
providers: [PantriesService, AuthService, JwtStrategy],
})
Expand Down
Loading