feat: add Request.purchaseOrderId (nullable FK)
This commit is contained in:
+100
-2
@@ -50,12 +50,44 @@ function createTestEmployee(overrides: { name?: string; department?: string } =
|
||||
});
|
||||
}
|
||||
|
||||
// Request.purchaseOrderId is a real FK to PurchaseOrder (see schema.prisma /
|
||||
// src/lib/requests.ts) — tests that need a valid purchase_order_id create a
|
||||
// real PurchaseOrder row (and the Vendor it requires) first and use its id.
|
||||
// Mirrors tests/licenses.test.ts's createTestVendor()/createTestPurchaseOrder()
|
||||
// helpers.
|
||||
function createTestVendor(overrides: { name?: string } = {}) {
|
||||
return prisma.vendor.create({
|
||||
data: {
|
||||
name: overrides.name ?? "Test Vendor",
|
||||
contactEmail: "[email protected]",
|
||||
contactPhone: "0281234567",
|
||||
address: "123 Test Street",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function createTestPurchaseOrder(overrides: { vendorId?: string } = {}) {
|
||||
return prisma.purchaseOrder.create({
|
||||
data: {
|
||||
vendorId: overrides.vendorId as string,
|
||||
orderDate: new Date("2026-08-01"),
|
||||
expectedDate: new Date("2026-08-10"),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
beforeEach(async () => {
|
||||
// Each test starts from empty tables so listing/order assertions are
|
||||
// deterministic. requests must be cleared before employees since
|
||||
// requests.employee_id has an FK (ON DELETE RESTRICT) to employees.
|
||||
// deterministic. requests must be cleared before employees/purchase_orders
|
||||
// since requests.employee_id has an FK (ON DELETE RESTRICT) to employees
|
||||
// and requests.purchase_order_id has an FK (ON DELETE SET NULL) to
|
||||
// purchase_orders. purchase_orders must be cleared before employees/vendors
|
||||
// (ON DELETE RESTRICT to vendors, mirrors tests/licenses.test.ts) — order:
|
||||
// requests -> purchase_orders -> employees -> vendors.
|
||||
await prisma.request.deleteMany();
|
||||
await prisma.purchaseOrder.deleteMany();
|
||||
await prisma.employee.deleteMany();
|
||||
await prisma.vendor.deleteMany();
|
||||
// Default to a logged-in session — the tests below exercise request
|
||||
// business logic, not auth gating (that's covered by the "auth gating"
|
||||
// describe block further down).
|
||||
@@ -141,6 +173,72 @@ describe("POST /api/requests", () => {
|
||||
const remaining = await prisma.request.findMany();
|
||||
expect(remaining).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("creates a request with a purchase_order_id that references a real purchase order", async () => {
|
||||
const employee = await createTestEmployee();
|
||||
const vendor = await createTestVendor();
|
||||
const purchaseOrder = await createTestPurchaseOrder({ vendorId: vendor.id });
|
||||
const res = await POST(
|
||||
postRequest({
|
||||
employee_id: employee.id,
|
||||
type: "asset",
|
||||
reason: "New laptop for onboarding",
|
||||
purchase_order_id: purchaseOrder.id,
|
||||
})
|
||||
);
|
||||
|
||||
expect(res.status).toBe(201);
|
||||
const body = await res.json();
|
||||
expect(body.purchase_order_id).toBe(purchaseOrder.id);
|
||||
});
|
||||
|
||||
it("creates a request with no purchase_order_id (stays null)", async () => {
|
||||
const employee = await createTestEmployee();
|
||||
const res = await POST(
|
||||
postRequest({ employee_id: employee.id, type: "asset", reason: "New laptop for onboarding" })
|
||||
);
|
||||
|
||||
expect(res.status).toBe(201);
|
||||
const body = await res.json();
|
||||
expect(body.purchase_order_id).toBeNull();
|
||||
});
|
||||
|
||||
it("rejects a purchase_order_id that is a well-formed UUID but doesn't reference an existing purchase order", async () => {
|
||||
const employee = await createTestEmployee();
|
||||
const res = await POST(
|
||||
postRequest({
|
||||
employee_id: employee.id,
|
||||
type: "asset",
|
||||
reason: "New laptop for onboarding",
|
||||
purchase_order_id: "00000000-0000-0000-0000-000000000000",
|
||||
})
|
||||
);
|
||||
|
||||
expect(res.status).toBe(400);
|
||||
const body = await res.json();
|
||||
expect(body.error).toMatch(/purchase_order_id/i);
|
||||
expect(body.error).toMatch(/does not reference an existing purchase order/i);
|
||||
|
||||
// And it must not have created anything.
|
||||
const remaining = await prisma.request.findMany();
|
||||
expect(remaining).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("rejects a purchase_order_id that is not a valid UUID", async () => {
|
||||
const employee = await createTestEmployee();
|
||||
const res = await POST(
|
||||
postRequest({
|
||||
employee_id: employee.id,
|
||||
type: "asset",
|
||||
reason: "New laptop for onboarding",
|
||||
purchase_order_id: "not-a-uuid",
|
||||
})
|
||||
);
|
||||
|
||||
expect(res.status).toBe(400);
|
||||
const body = await res.json();
|
||||
expect(body.error).toMatch(/purchase_order_id/i);
|
||||
});
|
||||
});
|
||||
|
||||
describe("GET /api/requests", () => {
|
||||
|
||||
Reference in New Issue
Block a user