Compare commits

..
Author SHA1 Message Date
shawn 7dac01acb7 fix: restrict order quantity edits to pending orders
Runner Test / test (push) Successful in 36s
Runner Test / test (pull_request) Successful in 15s
2026-09-29 14:07:12 +10:00
derrick 82ed809f32 update:remove unused check
Runner Test / test (push) Failing after 15s
2026-09-27 22:05:51 +09:00
derrick 0e1e64b6ca add:assignment 3 tests
Runner Test / test (push) Failing after 14s
2026-09-27 22:04:59 +09:00
4 changed files with 97 additions and 3 deletions
-1
View File
@@ -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,91 @@
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
import type { AppWithContext } from "../app";
import { buildApp } from "../app";
import { createUser } from "../users/user-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);
});
test("rejects quantity changes for processing orders", async () => {
const response = await app.inject({
method: "POST",
url: "/api/orders/order_2/items/item_3/quantity",
headers: { cookie: employeeCookie },
payload: { quantity: 2 }
});
expect(response.statusCode).toBe(400);
});
test("rejects quantity changes for shipped orders", async () => {
const response = await app.inject({
method: "POST",
url: "/api/orders/order_3/items/item_4/quantity",
headers: { cookie: employeeCookie },
payload: { quantity: 1 }
});
expect(response.statusCode).toBe(400);
});
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 -1
View File
@@ -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) {
@@ -51,7 +51,11 @@ export function OrderItemsPanel({ order, onUpdateQuantity }: OrderItemsPanelProp
<div className="text-sm text-slate-600">{item.sku}</div> <div className="text-sm text-slate-600">{item.sku}</div>
<div className="text-sm text-slate-500">Available: {item.availableQuantity}</div> <div className="text-sm text-slate-500">Available: {item.availableQuantity}</div>
</div> </div>
{order.status === "pending" ? (
<QuantityForm itemId={item.id} quantity={item.quantity} onSave={onUpdateQuantity} /> <QuantityForm itemId={item.id} quantity={item.quantity} onSave={onUpdateQuantity} />
) : (
<div className="text-sm text-slate-600">Quantity: {item.quantity}</div>
)}
</div> </div>
</div> </div>
))} ))}