Compare commits
7
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f0c2c11f8e | ||
|
|
31b367e3a3 | ||
|
|
877eb9e09a | ||
|
|
6d3570909a | ||
|
|
82ed809f32 | ||
|
|
0e1e64b6ca | ||
|
|
8235ceb61f |
@@ -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);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,108 @@
|
|||||||
|
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 4 order lifecycle rules", () => {
|
||||||
|
test("keeps quantity editing blocked 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("allows address editing for processing orders", async () => {
|
||||||
|
const response = await app.inject({
|
||||||
|
method: "POST",
|
||||||
|
url: "/api/orders/order_2/shipping-address",
|
||||||
|
headers: { cookie: employeeCookie },
|
||||||
|
payload: {
|
||||||
|
fullName: "Jane Doe",
|
||||||
|
line1: "50 New Street",
|
||||||
|
line2: null,
|
||||||
|
city: "Oakland",
|
||||||
|
state: "CA",
|
||||||
|
postalCode: "94607",
|
||||||
|
country: "US"
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.statusCode).toBe(200);
|
||||||
|
expect(response.json().shippingAddress.city).toBe("Oakland");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("allows cancellation for processing orders", async () => {
|
||||||
|
const response = await app.inject({
|
||||||
|
method: "POST",
|
||||||
|
url: "/api/orders/order_2/cancel",
|
||||||
|
headers: { cookie: employeeCookie },
|
||||||
|
payload: { reason: "Customer request" }
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.statusCode).toBe(200);
|
||||||
|
expect(response.json().status).toBe("cancelled");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("blocks address editing for shipped orders", async () => {
|
||||||
|
const response = await app.inject({
|
||||||
|
method: "POST",
|
||||||
|
url: "/api/orders/order_3/shipping-address",
|
||||||
|
headers: { cookie: employeeCookie },
|
||||||
|
payload: {
|
||||||
|
fullName: "John Smith",
|
||||||
|
line1: "77 Final Mile Road",
|
||||||
|
line2: null,
|
||||||
|
city: "Denver",
|
||||||
|
state: "CO",
|
||||||
|
postalCode: "80202",
|
||||||
|
country: "US"
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.statusCode).toBe(400);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("blocks cancellation for shipped orders", async () => {
|
||||||
|
const response = await app.inject({
|
||||||
|
method: "POST",
|
||||||
|
url: "/api/orders/order_3/cancel",
|
||||||
|
headers: { cookie: employeeCookie },
|
||||||
|
payload: { reason: "Late request" }
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.statusCode).toBe(400);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
import { describe, expect, test } from "bun:test";
|
||||||
|
import { canCancelOrder, canEditOrderItems, canEditShippingAddress } from "./order-status";
|
||||||
|
|
||||||
|
describe("order status rules", () => {
|
||||||
|
const cases = [
|
||||||
|
{ status: "pending", editItems: true, editShipping: true, cancel: true },
|
||||||
|
{ status: "processing", editItems: false, editShipping: true, cancel: true },
|
||||||
|
{ status: "shipped", editItems: false, editShipping: false, cancel: false },
|
||||||
|
{ status: "cancelled", editItems: false, editShipping: false, cancel: false },
|
||||||
|
{ status: "refunded", editItems: false, editShipping: false, cancel: false }
|
||||||
|
] as const;
|
||||||
|
|
||||||
|
for (const { status, editItems, editShipping, cancel } of cases) {
|
||||||
|
test(`item editing is ${editItems ? "allowed" : "denied"} for ${status} orders`, () => {
|
||||||
|
expect(canEditOrderItems(status)).toBe(editItems);
|
||||||
|
});
|
||||||
|
|
||||||
|
test(`shipping address editing is ${editShipping ? "allowed" : "denied"} for ${status} orders`, () => {
|
||||||
|
expect(canEditShippingAddress(status)).toBe(editShipping);
|
||||||
|
});
|
||||||
|
|
||||||
|
test(`cancellation is ${cancel ? "allowed" : "denied"} for ${status} orders`, () => {
|
||||||
|
expect(canCancelOrder(status)).toBe(cancel);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
@@ -1,13 +1,17 @@
|
|||||||
import type { OrderStatus } from "@orderops/shared/order-status";
|
import type { OrderStatus } from "@orderops/shared/order-status";
|
||||||
|
|
||||||
export function canEditOrderItems(status: OrderStatus) {
|
function isPendingOrProcessing(status: OrderStatus) {
|
||||||
return status === "pending" || status === "processing";
|
return status === "pending" || status === "processing";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function canEditOrderItems(status: OrderStatus) {
|
||||||
|
return status === "pending";
|
||||||
|
}
|
||||||
|
|
||||||
export function canEditShippingAddress(status: OrderStatus) {
|
export function canEditShippingAddress(status: OrderStatus) {
|
||||||
return status === "pending" || status === "processing";
|
return isPendingOrProcessing(status);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function canCancelOrder(status: OrderStatus) {
|
export function canCancelOrder(status: OrderStatus) {
|
||||||
return status === "pending" || status === "processing";
|
return isPendingOrProcessing(status);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -72,6 +72,19 @@ export function OrderPage() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function handleUpdateQuantity(itemId: string, quantity: number) {
|
||||||
|
if (!order) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (order.status !== "pending") {
|
||||||
|
setToast({ message: "Order items cannot be changed for this order status.", variant: "error" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await runAction(() => updateOrderItemQuantity(order.id, itemId, quantity), "Quantity saved.");
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="space-y-4">
|
<div className="space-y-4">
|
||||||
<Link className="inline-flex text-sm font-medium text-blue-600 hover:text-blue-700" to="/orders">
|
<Link className="inline-flex text-sm font-medium text-blue-600 hover:text-blue-700" to="/orders">
|
||||||
@@ -84,7 +97,7 @@ export function OrderPage() {
|
|||||||
<OrderDetailPage
|
<OrderDetailPage
|
||||||
order={order}
|
order={order}
|
||||||
auditEvents={auditEvents}
|
auditEvents={auditEvents}
|
||||||
onUpdateQuantity={(itemId, quantity) => runAction(() => updateOrderItemQuantity(order.id, itemId, quantity), "Quantity saved.")}
|
onUpdateQuantity={handleUpdateQuantity}
|
||||||
onSaveAddress={(address: Address) => runAction(() => updateShippingAddress(order.id, address), "Shipping address saved.")}
|
onSaveAddress={(address: Address) => runAction(() => updateShippingAddress(order.id, address), "Shipping address saved.")}
|
||||||
onSaveNotes={(notes) => runAction(() => updateOrderNotes(order.id, notes), "Notes saved.")}
|
onSaveNotes={(notes) => runAction(() => updateOrderNotes(order.id, notes), "Notes saved.")}
|
||||||
onCancel={() => runAction(() => cancelOrder(order.id), "Order cancelled.")}
|
onCancel={() => runAction(() => cancelOrder(order.id), "Order cancelled.")}
|
||||||
|
|||||||
Reference in New Issue
Block a user