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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- AlterTable
ALTER TABLE "fundraisers" ADD COLUMN "expenses" MONEY;

-- AlterTable
ALTER TABLE "items" ADD COLUMN "unit_cost" MONEY;
4 changes: 4 additions & 0 deletions backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ model Fundraiser {
description String
published Boolean @default(false)
goalAmount Decimal? @map("goal_amount") @db.Money
// Seller-entered fixed costs (booth fees, bulk ingredients, ...). Null = not entered.
expenses Decimal? @db.Money
imageUrls String[] @map("image_urls")
buyingStartsAt DateTime @map("buying_starts_at")
buyingEndsAt DateTime @map("buying_ends_at")
Expand Down Expand Up @@ -123,6 +125,8 @@ model Item {
offsale Boolean @default(false)
limit Int?
price Decimal @db.Money
// Seller-entered cost to make/buy one unit. Null = not entered.
unitCost Decimal? @map("unit_cost") @db.Money
category ItemCategory @default(OTHER)
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
Expand Down
126 changes: 126 additions & 0 deletions backend/src/api/fundraiser/__tests__/fundraiser.handlers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import { Decimal } from "decimal.js";
import {
mockResponse,
createMockOrganization,
createMockUser,
} from "../../../__tests__";
import {
getFundraiserCostsHandler,
getFundraiserItemsHandler,
updateFundraiserItemHandler,
} from "../fundraiser.handlers";
import * as fundraiserServices from "../fundraiser.services";

jest.mock("../fundraiser.services");
jest.mock("../../../utils/email", () => ({
sendAnnouncementEmail: jest.fn(),
sendVenmoSetupEmail: jest.fn(),
}));

const adminId = "123e4567-e89b-12d3-a456-426614174000";
const strangerId = "123e4567-e89b-12d3-a456-426614174009";
const fundraiserId = "423e4567-e89b-12d3-a456-426614174000";
const itemId = "523e4567-e89b-12d3-a456-426614174000";

const fundraiser = {
id: fundraiserId,
published: true,
organization: {
...createMockOrganization(),
admins: [createMockUser({ id: adminId })],
},
};

const dbItem = {
id: itemId,
name: "Cookie",
description: "Chocolate chip",
price: new Decimal(3),
unitCost: new Decimal(1.25),
imageUrl: null,
offsale: false,
limit: null,
category: "DESSERTS",
fundraiserId,
};

describe("Fundraiser cost handlers", () => {
beforeEach(() => {
jest.clearAllMocks();
(fundraiserServices.getFundraiser as jest.Mock).mockResolvedValue(fundraiser);
});

describe("getFundraiserCostsHandler", () => {
const req: any = { params: { id: fundraiserId } };

it("returns costs to org admins", async () => {
(fundraiserServices.getFundraiserCosts as jest.Mock).mockResolvedValue({
expenses: new Decimal(40),
items: [{ id: itemId, unitCost: new Decimal(1.25) }],
});
const res = mockResponse();
res.locals.user = { id: adminId };

await getFundraiserCostsHandler(req, res);

expect(res.status).toHaveBeenCalledWith(200);
const { data } = res.json.mock.calls[0][0];
expect(Number(data.expenses)).toBe(40);
expect(Number(data.items[0].unitCost)).toBe(1.25);
});

it("rejects non-admins", async () => {
const res = mockResponse();
res.locals.user = { id: strangerId };

await getFundraiserCostsHandler(req, res);

expect(res.status).toHaveBeenCalledWith(403);
expect(fundraiserServices.getFundraiserCosts).not.toHaveBeenCalled();
});
});

it("never exposes unit cost on the public items endpoint", async () => {
(fundraiserServices.getFundraiserItems as jest.Mock).mockResolvedValue([dbItem]);
const res = mockResponse();

await getFundraiserItemsHandler({ params: { id: fundraiserId } } as any, res);

expect(res.status).toHaveBeenCalledWith(200);
expect(res.json.mock.calls[0][0].data[0]).not.toHaveProperty("unitCost");
});

it("allows editing unit cost on a published fundraiser and invalidates analytics", async () => {
(fundraiserServices.getFundraiserItemForFundraiser as jest.Mock).mockResolvedValue(dbItem);
(fundraiserServices.validatePublishedFundraiserItemUpdate as jest.Mock).mockReturnValue({
valid: true,
});
(fundraiserServices.updateFundraiserItem as jest.Mock).mockResolvedValue({
...dbItem,
unitCost: new Decimal(2),
});
const res = mockResponse();
res.locals.user = { id: adminId };

await updateFundraiserItemHandler(
{
params: { fundraiserId, itemId },
body: {
name: "Cookie",
description: "Chocolate chip",
price: new Decimal(3),
offsale: false,
unitCost: new Decimal(2),
category: "DESSERTS",
},
} as any,
res
);

expect(res.status).toHaveBeenCalledWith(200);
expect(Number(res.json.mock.calls[0][0].data.unitCost)).toBe(2);
expect(fundraiserServices.invalidateFundraiserAnalyticsCache).toHaveBeenCalledWith(
fundraiserId
);
});
});
Loading
Loading