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
115 changes: 115 additions & 0 deletions apps/backend/src/donationItems/donationItems.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { Test, TestingModule } from '@nestjs/testing';
import { DonationItemsController } from './donationItems.controller';
import { DonationItemsService } from './donationItems.service';
import { DonationItem } from './donationItems.entity';
import { mock } from 'jest-mock-extended';
import { FoodType } from './types';
import { CreateMultipleDonationItemsDto } from './dtos/create-donation-items.dto';

const mockDonationItemsService = mock<DonationItemsService>();

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

const mockDonationItemsCreateData: Partial<DonationItem>[] = [
{
itemId: 1,
donationId: 1,
itemName: 'Canned Beans',
quantity: 100,
reservedQuantity: 0,
ozPerItem: 15,
estimatedValue: 200,
foodType: FoodType.DAIRY_FREE_ALTERNATIVES,
},
{
itemId: 2,
donationId: 1,
itemName: 'Rice',
quantity: 50,
reservedQuantity: 0,
ozPerItem: 20,
estimatedValue: 150,
foodType: FoodType.GLUTEN_FREE_BAKING_PANCAKE_MIXES,
},
];

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [DonationItemsController],
providers: [
{ provide: DonationItemsService, useValue: mockDonationItemsService },
],
}).compile();

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

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

describe('create', () => {
it('should call donationItemsService.create and return a donationItem', async () => {
const donationItemData = mockDonationItemsCreateData[0];
mockDonationItemsService.create.mockResolvedValue(
donationItemData as DonationItem,
);
const result = await controller.createDonationItem(
donationItemData as DonationItem,
);
expect(result).toEqual(donationItemData as DonationItem);
expect(mockDonationItemsService.create).toHaveBeenCalledWith(
donationItemData.donationId,
donationItemData.itemName,
donationItemData.quantity,
donationItemData.reservedQuantity,
donationItemData.ozPerItem,
donationItemData.estimatedValue,
donationItemData.foodType,
);
});
});

describe('createMultipleDonationItems', () => {
it('should call donationItemsService.createMultipleDonationItems with donationId and items, and return the created donation items', async () => {
const mockBody: CreateMultipleDonationItemsDto = {
donationId: 1,
items: [
{
itemName: 'Rice Noodles',
quantity: 100,
reservedQuantity: 0,
ozPerItem: 5,
estimatedValue: 100,
foodType: FoodType.DAIRY_FREE_ALTERNATIVES,
},
{
itemName: 'Beans',
quantity: 50,
reservedQuantity: 0,
ozPerItem: 10,
estimatedValue: 80,
foodType: FoodType.GLUTEN_FREE_BAKING_PANCAKE_MIXES,
},
],
};

const mockCreatedItems: Partial<DonationItem>[] = [
{ itemId: 1, donationId: 1, ...mockBody.items[0] },
{ itemId: 2, donationId: 1, ...mockBody.items[1] },
];

mockDonationItemsService.createMultipleDonationItems.mockResolvedValue(
mockCreatedItems as DonationItem[],
);

const result = await controller.createMultipleDonationItems(mockBody);

expect(
mockDonationItemsService.createMultipleDonationItems,
).toHaveBeenCalledWith(mockBody.donationId, mockBody.items);
expect(result).toEqual(mockCreatedItems);
});
});
});
41 changes: 41 additions & 0 deletions apps/backend/src/donationItems/donationItems.controller.ts
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since create-multiple takes care of 1+ donation items, when would the create endpoint be used? i see the endpoint associated with it isn't called from the frontend (postDonationItem). was there a plan to delete it later?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a really good point. I personally cannot think of a case as of right now where we would want to just make one donation item, since food manufacturers are the only ones donating food. This could likely be deleted later on, but I think just in case there may be value to keeping it. @sam-schu thoughts?

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { ApiBody } from '@nestjs/swagger';
import { DonationItemsService } from './donationItems.service';
import { DonationItem } from './donationItems.entity';
import { FoodType } from './types';
import { CreateMultipleDonationItemsDto } from './dtos/create-donation-items.dto';

@Controller('donation-items')
//@UseInterceptors()
Expand Down Expand Up @@ -74,6 +75,46 @@ export class DonationItemsController {
);
}

@Post('/create-multiple')
@ApiBody({
description: 'Bulk create donation items for a single donation',
schema: {
type: 'object',
properties: {
donationId: {
type: 'integer',
example: 1,
},
items: {
type: 'array',
items: {
type: 'object',
properties: {
itemName: { type: 'string', example: 'Rice Noodles' },
quantity: { type: 'integer', example: 100 },
reservedQuantity: { type: 'integer', example: 0 },
ozPerItem: { type: 'integer', example: 5 },
estimatedValue: { type: 'integer', example: 100 },
foodType: {
type: 'string',
enum: Object.values(FoodType),
example: FoodType.DAIRY_FREE_ALTERNATIVES,
},
},
},
},
},
},
})
async createMultipleDonationItems(
@Body() body: CreateMultipleDonationItemsDto,
): Promise<DonationItem[]> {
return this.donationItemsService.createMultipleDonationItems(
body.donationId,
body.items,
);
}

@Patch('/update-quantity/:itemId')
async updateDonationItemQuantity(
@Param('itemId', ParseIntPipe) itemId: number,
Expand Down
2 changes: 1 addition & 1 deletion apps/backend/src/donationItems/donationItems.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class DonationItem {
itemId: number;

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

@ManyToOne(() => Donation, { nullable: false })
@JoinColumn({ name: 'donation_id', referencedColumnName: 'donationId' })
Expand Down
31 changes: 31 additions & 0 deletions apps/backend/src/donationItems/donationItems.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,37 @@ export class DonationItemsService {
return this.repo.save(donationItem);
}

async createMultipleDonationItems(
donationId: number,
items: {
itemName: string;
quantity: number;
reservedQuantity: number;
ozPerItem: number;
estimatedValue: number;
foodType: FoodType;
}[],
): Promise<DonationItem[]> {
validateId(donationId, 'Donation');

const donation = await this.donationRepo.findOneBy({ donationId });
if (!donation) throw new NotFoundException('Donation not found');

const donationItems = items.map((item) =>
this.repo.create({
donation,
itemName: item.itemName,
quantity: item.quantity,
reservedQuantity: item.reservedQuantity,
ozPerItem: item.ozPerItem,
estimatedValue: item.estimatedValue,
foodType: item.foodType,
}),
);

return this.repo.save(donationItems);
}

async updateDonationItemQuantity(itemId: number): Promise<DonationItem> {
validateId(itemId, 'Donation Item');

Expand Down
48 changes: 48 additions & 0 deletions apps/backend/src/donationItems/dtos/create-donation-items.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {
IsNumber,
IsString,
IsArray,
ValidateNested,
Min,
IsEnum,
IsNotEmpty,
Length,
} from 'class-validator';
import { Type } from 'class-transformer';
import { FoodType } from '../types';

export class CreateDonationItemDto {
@IsString()
@IsNotEmpty()
@Length(1, 255)
itemName: string;

@IsNumber()
@Min(1)
quantity: number;

@IsNumber()
@Min(0)
reservedQuantity: number;

@IsNumber()
@Min(1)
ozPerItem: number;

@IsNumber()
@Min(1)
estimatedValue: number;

@IsEnum(FoodType)
foodType: FoodType;
}

export class CreateMultipleDonationItemsDto {
@IsNumber()
donationId: number;

@IsArray()
@ValidateNested({ each: true })
@Type(() => CreateDonationItemDto)
items: CreateDonationItemDto[];
}
9 changes: 9 additions & 0 deletions apps/frontend/src/api/apiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
CreateFoodRequestBody,
Pantry,
PantryApplicationDto,
CreateMultipleDonationItemsBody,
OrderSummary,
UserDto,
} from 'types/types';
Expand Down Expand Up @@ -55,6 +56,14 @@ export class ApiClient {
return this.post('/api/requests/create', body) as Promise<FoodRequest>;
}

public async postMultipleDonationItems(
body: CreateMultipleDonationItemsBody,
): Promise<DonationItem[]> {
return this.post('/api/donation-items/create-multiple', body) as Promise<
DonationItem[]
>;
}

private async patch(path: string, body: unknown): Promise<unknown> {
return this.axiosInstance
.patch(path, body)
Expand Down
Loading
Loading