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
2 changes: 2 additions & 0 deletions apps/backend/src/config/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { AddDonationItemConfirmation1774140453305 } from '../migrations/17741404
import { DonationItemsOnDeleteCascade1774214910101 } from '../migrations/1774214910101-DonationItemsOnDeleteCascade';
import { OrdersVolunteerActions1774883880543 } from '../migrations/1774883880543-OrdersVolunteerActions';
import { AddUserActiveField1780531200000 } from '../migrations/1780531200000-AddUserActiveField';
import { MakeDonationItemDetailsRequired1782000000000 } from '../migrations/1782000000000-MakeDonationItemDetailsRequired';

const schemaMigrations = [
User1725726359198,
Expand Down Expand Up @@ -88,6 +89,7 @@ const schemaMigrations = [
DonationItemsOnDeleteCascade1774214910101,
OrdersVolunteerActions1774883880543,
AddUserActiveField1780531200000,
MakeDonationItemDetailsRequired1782000000000,
];

export default schemaMigrations;
8 changes: 4 additions & 4 deletions apps/backend/src/donationItems/donationItems.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ export class DonationItem {
@Column({ name: 'reserved_quantity', type: 'int', default: 0 })
reservedQuantity!: number;

@Column({ name: 'oz_per_item', type: 'numeric', nullable: true })
ozPerItem!: number | null;
@Column({ name: 'oz_per_item', type: 'numeric' })
ozPerItem!: number;

@Column({ name: 'estimated_value', type: 'numeric', nullable: true })
estimatedValue!: number | null;
@Column({ name: 'estimated_value', type: 'numeric' })
estimatedValue!: number;

@Column({
name: 'food_type',
Expand Down
124 changes: 19 additions & 105 deletions apps/backend/src/donationItems/donationItems.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,71 +229,17 @@ describe('DonationItemsService', () => {
expect(rice.detailsConfirmed).toEqual(true);
});

it('creates items with optional fields omitted', async () => {
it('sets detailsConfirmed to true since ozPerItem and estimatedValue are required', async () => {
const donation = await getSeedDonation();
const transactionManager = testDataSource.createEntityManager();

const minimalItems: CreateDonationItemDto[] = [
{
itemName: 'Plain Item',
quantity: 3,
foodType: FoodType.DRIED_BEANS,
foodRescue: true,
},
];

const result = await service.createMultiple(
donation,
minimalItems,
transactionManager,
);

expect(result).toHaveLength(1);
expect(result[0].itemId).toBeDefined();
expect(result[0].ozPerItem).toBeNull();
expect(result[0].estimatedValue).toBeNull();
expect(result[0].detailsConfirmed).toEqual(false);
});

it('sets detailsConfirmed to true only when both ozPerItem and estimatedValue are provided', async () => {
const donation = await getSeedDonation();
const transactionManager = testDataSource.createEntityManager();

const mixedItems: CreateDonationItemDto[] = [
{
itemName: 'Both Fields',
quantity: 4,
ozPerItem: 12,
estimatedValue: 3.5,
foodType: FoodType.DRIED_BEANS,
foodRescue: false,
},
{
itemName: 'Missing Estimated Value',
quantity: 2,
ozPerItem: 8,
foodType: FoodType.DRIED_BEANS,
foodRescue: false,
},
{
itemName: 'Missing Oz Per Item',
quantity: 6,
estimatedValue: 1.99,
foodType: FoodType.DRIED_BEANS,
foodRescue: false,
},
];

const result = await service.createMultiple(
donation,
mixedItems,
validItems,
transactionManager,
);

const byName = Object.fromEntries(result.map((i) => [i.itemName, i]));
expect(byName['Both Fields'].detailsConfirmed).toEqual(true);
expect(byName['Missing Estimated Value'].detailsConfirmed).toEqual(false);
expect(byName['Missing Oz Per Item'].detailsConfirmed).toEqual(false);
expect(result.every((item) => item.detailsConfirmed)).toBe(true);
});

it('rolls back all items when one fails within a transaction', async () => {
Expand All @@ -309,6 +255,8 @@ describe('DonationItemsService', () => {
{
itemName: 'a'.repeat(1000),
quantity: 5,
ozPerItem: 10,
estimatedValue: 2.5,
foodType: FoodType.DRIED_BEANS,
foodRescue: false,
},
Expand Down Expand Up @@ -372,8 +320,8 @@ describe('DonationItemsService', () => {
): Promise<number> {
const result = await testDataSource.query(
`INSERT INTO donation_items
(donation_id, item_name, quantity, reserved_quantity, food_type, details_confirmed)
VALUES ($1, 'Test Item', $2, $3, 'Granola', false)
(donation_id, item_name, quantity, reserved_quantity, oz_per_item, estimated_value, food_type, details_confirmed)
VALUES ($1, 'Test Item', $2, $3, 3.4, 3.4, 'Granola', false)
RETURNING item_id`,
[donationId, qty, reserved],
);
Expand Down Expand Up @@ -494,51 +442,6 @@ describe('DonationItemsService', () => {
.getRepository(DonationItem)
.findOneBy({ itemId });
expect(item?.detailsConfirmed).toBe(false);
expect(item?.ozPerItem).toBeNull();
});

it('returns false and does not confirm when only some fields are provided', async () => {
const donationId = await insertMatchedDonation();
const itemId = await insertDonationItem(donationId, 10, 5);

const result = await testDataSource.transaction((tm) =>
service.updateItemDetails(donationId, [{ itemId, ozPerItem: 8.5 }], tm),
);

expect(result).toBe(false);
const item = await testDataSource
.getRepository(DonationItem)
.findOneBy({ itemId });
expect(Number(item?.ozPerItem)).toBe(8.5);
expect(item?.estimatedValue).toBeNull();
expect(item?.detailsConfirmed).toBe(false);
});

it('confirms item on a second call that supplies the remaining fields', async () => {
const donationId = await insertMatchedDonation();
const itemId = await insertDonationItem(donationId, 10, 5);

const firstResult = await testDataSource.transaction((tm) =>
service.updateItemDetails(donationId, [{ itemId, ozPerItem: 8.5 }], tm),
);
expect(firstResult).toBe(false);

const secondResult = await testDataSource.transaction((tm) =>
service.updateItemDetails(
donationId,
[{ itemId, estimatedValue: 12.0, foodRescue: true }],
tm,
),
);
expect(secondResult).toBe(true);

const item = await testDataSource
.getRepository(DonationItem)
.findOneBy({ itemId });
expect(Number(item?.ozPerItem)).toBe(8.5);
expect(Number(item?.estimatedValue)).toBe(12.0);
expect(item?.foodRescue).toBe(true);
expect(item?.detailsConfirmed).toBe(true);
});

it('allows updating an already-confirmed item without throwing', async () => {
Expand All @@ -552,7 +455,18 @@ describe('DonationItemsService', () => {
);

const result = await testDataSource.transaction((tm) =>
service.updateItemDetails(donationId, [{ itemId, ozPerItem: 9.0 }], tm),
service.updateItemDetails(
donationId,
[
{
itemId,
ozPerItem: 9.0,
estimatedValue: 10.0,
foodRescue: true,
},
],
tm,
),
);

expect(result).toBe(true);
Expand Down
31 changes: 10 additions & 21 deletions apps/backend/src/donationItems/donationItems.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,26 +105,15 @@ export class DonationItemsService {
);
}

const updateData: Partial<DonationItem> = {};
if (dto.ozPerItem !== undefined) updateData.ozPerItem = dto.ozPerItem;
if (dto.estimatedValue !== undefined)
updateData.estimatedValue = dto.estimatedValue;
if (dto.foodRescue !== undefined) updateData.foodRescue = dto.foodRescue;

// If included in DTO, keep it, otherwise use whatever is in the DB (could be null)
const resultingOzPerItem =
updateData.ozPerItem !== undefined
? updateData.ozPerItem
: item.ozPerItem;
const resultingEstimatedValue =
updateData.estimatedValue !== undefined
? updateData.estimatedValue
: item.estimatedValue;

if (resultingOzPerItem != null && resultingEstimatedValue != null) {
updateData.detailsConfirmed = true;
confirmedDetailsForAnItem = true;
}
// ozPerItem, estimatedValue, and foodRescue are required on the DTO, so an
// update always supplies the full set of details and confirms the item.
const updateData: Partial<DonationItem> = {
ozPerItem: dto.ozPerItem,
estimatedValue: dto.estimatedValue,
foodRescue: dto.foodRescue,
detailsConfirmed: true,
};
confirmedDetailsForAnItem = true;

await donationItemTransactionRepo.update(dto.itemId, updateData);
}
Expand Down Expand Up @@ -207,7 +196,7 @@ export class DonationItemsService {
estimatedValue: item.estimatedValue,
foodType: item.foodType,
foodRescue: item.foodRescue,
detailsConfirmed: item.ozPerItem != null && item.estimatedValue != null,
detailsConfirmed: true,
}),
);
return transactionRepo.save(donationItems);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
IsEnum,
IsNotEmpty,
Length,
IsOptional,
IsInt,
IsBoolean,
} from 'class-validator';
Expand All @@ -26,16 +25,14 @@ export class CreateDonationItemDto {
{ message: 'ozPerItem must have at most 2 decimal places' },
)
@Min(0.01)
@IsOptional()
ozPerItem?: number;
ozPerItem!: number;

@IsNumber(
{ maxDecimalPlaces: 2 },
{ message: 'estimatedValue must have at most 2 decimal places' },
)
@Min(0.01)
@IsOptional()
estimatedValue?: number;
estimatedValue!: number;

@IsEnum(FoodType)
foodType!: FoodType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
import { IsNumber, Min, IsBoolean, IsInt, IsOptional } from 'class-validator';
import { IsNumber, Min, IsBoolean, IsInt } from 'class-validator';

export class UpdateDonationItemDetailsDto {
@IsInt()
@Min(1)
itemId!: number;

@IsOptional()
@IsNumber(
{ maxDecimalPlaces: 2 },
{ message: 'Oz per item must have at most 2 decimal places' },
)
@Min(0.01, { message: 'Oz per item must be at least 0.01' })
ozPerItem?: number;
ozPerItem!: number;

@IsOptional()
@IsNumber(
{ maxDecimalPlaces: 2 },
{ message: 'Estimated value must have at most 2 decimal places' },
)
@Min(0.01, { message: 'Estimated value must be at least 0.01' })
estimatedValue?: number;
estimatedValue!: number;

@IsOptional()
@IsBoolean()
foodRescue?: boolean;
foodRescue!: boolean;
}
4 changes: 2 additions & 2 deletions apps/backend/src/donations/donations.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ export class DonationsController {
properties: {
itemName: { type: 'string', example: 'Canned Beans' },
quantity: { type: 'integer', example: 1 },
ozPerItem: { type: 'number', example: 0.01, nullable: true },
estimatedValue: { type: 'number', example: 0.01, nullable: true },
ozPerItem: { type: 'number', example: 0.01 },
estimatedValue: { type: 'number', example: 0.01 },
foodType: {
type: 'enum',
enum: Object.values(FoodType),
Expand Down
25 changes: 6 additions & 19 deletions apps/backend/src/donations/donations.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ async function insertDonationItem(
): Promise<number> {
const result = await testDataSource.query(
`INSERT INTO donation_items
(donation_id, item_name, quantity, reserved_quantity, food_type, details_confirmed)
VALUES ($1, 'Test Item', $2, $3, 'Granola', false)
(donation_id, item_name, quantity, reserved_quantity, oz_per_item, estimated_value, food_type, details_confirmed)
VALUES ($1, 'Test Item', $2, $3, 3.4, 3.4, 'Granola', false)
RETURNING item_id`,
[donationId, qty, reserved],
);
Expand Down Expand Up @@ -1256,6 +1256,8 @@ describe('DonationService', () => {
quantity: 5,
foodType: FoodType.DAIRY_FREE_ALTERNATIVES,
foodRescue: false,
ozPerItem: 3.4,
estimatedValue: 3.4,
},
],
},
Expand Down Expand Up @@ -1398,21 +1400,6 @@ describe('DonationService', () => {

expect(spy).toHaveBeenCalled();
});

it('does not call checkAndFulfillDonation when no items are fully confirmed', async () => {
const donationId = await insertMatchedDonation();
const itemId = await insertDonationItem(donationId, 10, 5);

const spy = jest.spyOn(service, 'checkAndFulfillDonation');

await service.updateDonationItemDetails(donationId, [
{ itemId, ozPerItem: 5.0 },
]);

const dbDonation = await service.findOne(donationId);
expect(dbDonation.status).toBe(DonationStatus.MATCHED);
expect(spy).not.toHaveBeenCalled();
});
});

describe('checkAndFulfillDonation', () => {
Expand All @@ -1423,8 +1410,8 @@ describe('DonationService', () => {
): Promise<number> {
const result = await testDataSource.query(
`INSERT INTO donation_items
(donation_id, item_name, quantity, reserved_quantity, food_type, details_confirmed)
VALUES ($1, 'Test Item', $2, $3, 'Granola', true)
(donation_id, item_name, quantity, reserved_quantity, oz_per_item, estimated_value, food_type, details_confirmed)
VALUES ($1, 'Test Item', $2, $3, 3.4, 3.4, 'Granola', true)
RETURNING item_id`,
[donationId, qty, reserved],
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export class DonationItemWithAllocatedQuantityDto {
itemName!: string;
foodType!: FoodType;
allocatedQuantity!: number;
ozPerItem?: number;
estimatedValue?: number;
ozPerItem!: number;
estimatedValue!: number;
foodRescue!: boolean;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ describe('FoodManufacturersController', () => {
foodType: FoodType.DAIRY_FREE_ALTERNATIVES,
allocatedQuantity: 10,
foodRescue: false,
estimatedValue: 3.4,
ozPerItem: 3.4,
},
],
},
Expand Down
4 changes: 2 additions & 2 deletions apps/backend/src/foodManufacturers/manufacturers.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ export class FoodManufacturersService {
(sum, a) => sum + a.allocatedQuantity,
0,
),
ozPerItem: item.ozPerItem ?? undefined,
estimatedValue: item.estimatedValue ?? undefined,
ozPerItem: item.ozPerItem,
estimatedValue: item.estimatedValue,
foodRescue: item.foodRescue,
});

Expand Down
Loading
Loading