Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cb3020a73c | ||
|
|
82ed809f32 | ||
|
|
0e1e64b6ca |
@@ -28,7 +28,6 @@ bun run dev:web
|
|||||||
Common checks:
|
Common checks:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
bun run check
|
|
||||||
bun run test
|
bun run test
|
||||||
bun run build:web
|
bun run build:web
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -0,0 +1,98 @@
|
|||||||
|
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
|
||||||
|
import type { AppWithContext } from "../app";
|
||||||
|
import { buildApp } from "../app";
|
||||||
|
import { createUser } from "../users/user-repository";
|
||||||
|
import { findOrderDetail, listOrderAuditEvents, updateOrderStatus } from "./order-repository";
|
||||||
|
|
||||||
|
let app: AppWithContext;
|
||||||
|
let employeeCookie = "";
|
||||||
|
|
||||||
|
function firstHeader(value: string | string[] | undefined) {
|
||||||
|
return (Array.isArray(value) ? value[0] : value) ?? "";
|
||||||
|
}
|
||||||
|
|
||||||
|
async function login(username: string) {
|
||||||
|
const response = await app.inject({ method: "POST", url: "/api/login", payload: { username } });
|
||||||
|
return firstHeader(response.headers["set-cookie"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
app = await buildApp(":memory:");
|
||||||
|
|
||||||
|
await createUser(app.dbContext, {
|
||||||
|
username: "dev-employee",
|
||||||
|
name: "Dev Employee",
|
||||||
|
email: "dev.employee@example.com",
|
||||||
|
role: "employee"
|
||||||
|
});
|
||||||
|
|
||||||
|
employeeCookie = await login("dev-employee");
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
await app.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("assignment 3 quantity validation", () => {
|
||||||
|
test("allows quantity changes for pending orders", async () => {
|
||||||
|
const response = await app.inject({
|
||||||
|
method: "POST",
|
||||||
|
url: "/api/orders/order_1/items/item_1/quantity",
|
||||||
|
headers: { cookie: employeeCookie },
|
||||||
|
payload: { quantity: 5 }
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.statusCode).toBe(200);
|
||||||
|
expect(response.json().items.find((item: { id: string }) => item.id === "item_1")?.quantity).toBe(5);
|
||||||
|
const order = await findOrderDetail(app.dbContext, "order_1");
|
||||||
|
expect(order?.items.find((item) => item.id === "item_1")?.quantity).toBe(5);
|
||||||
|
const events = await listOrderAuditEvents(app.dbContext, "order_1");
|
||||||
|
expect(events.filter((event) => event.type === "order.item.quantity_changed")).toEqual([
|
||||||
|
expect.objectContaining({ actor: "dev-employee", message: "Updated item item_1 quantity to 5." })
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
for (const status of ["processing", "shipped", "cancelled", "refunded"] as const) {
|
||||||
|
test(`rejects quantity changes for ${status} orders without side effects`, async () => {
|
||||||
|
await updateOrderStatus(app.dbContext, "order_1", status, "2025-01-16T10:00:00.000Z");
|
||||||
|
const beforeOrder = await findOrderDetail(app.dbContext, "order_1");
|
||||||
|
const beforeEvents = await listOrderAuditEvents(app.dbContext, "order_1");
|
||||||
|
|
||||||
|
for (const payload of [{ quantity: 5 }, { quantity: 5, status: "pending" }]) {
|
||||||
|
const response = await app.inject({
|
||||||
|
method: "POST",
|
||||||
|
url: "/api/orders/order_1/items/item_1/quantity",
|
||||||
|
headers: { cookie: employeeCookie },
|
||||||
|
payload
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.statusCode).toBe(400);
|
||||||
|
expect(response.json<{ message: string }>()).toEqual({ message: "Order items cannot be changed for this order status." });
|
||||||
|
expect(await findOrderDetail(app.dbContext, "order_1")).toEqual(beforeOrder);
|
||||||
|
expect(await listOrderAuditEvents(app.dbContext, "order_1")).toEqual(beforeEvents);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
test("rejects quantity outside the allowed range", async () => {
|
||||||
|
const response = await app.inject({
|
||||||
|
method: "POST",
|
||||||
|
url: "/api/orders/order_1/items/item_1/quantity",
|
||||||
|
headers: { cookie: employeeCookie },
|
||||||
|
payload: { quantity: 0 }
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.statusCode).toBe(400);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("rejects quantity above effective inventory", async () => {
|
||||||
|
const response = await app.inject({
|
||||||
|
method: "POST",
|
||||||
|
url: "/api/orders/order_1/items/item_1/quantity",
|
||||||
|
headers: { cookie: employeeCookie },
|
||||||
|
payload: { quantity: 9 }
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.statusCode).toBe(400);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import type { OrderStatus } from "@orderops/shared/order-status";
|
import type { OrderStatus } from "@orderops/shared/order-status";
|
||||||
|
|
||||||
export function canEditOrderItems(status: OrderStatus) {
|
export function canEditOrderItems(status: OrderStatus) {
|
||||||
return status === "pending" || status === "processing";
|
return status === "pending";
|
||||||
}
|
}
|
||||||
|
|
||||||
export function canEditShippingAddress(status: OrderStatus) {
|
export function canEditShippingAddress(status: OrderStatus) {
|
||||||
|
|||||||
Reference in New Issue
Block a user