Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8297a2dd9d |
@@ -1,108 +0,0 @@
|
|||||||
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);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -1,8 +1,6 @@
|
|||||||
import type { OrderStatus } from "@orderops/shared/order-status";
|
import type { OrderStatus } from "@orderops/shared/order-status";
|
||||||
|
|
||||||
export function canEditOrderItems(status: OrderStatus) {
|
export { canEditOrderItems } from "@orderops/shared/order-status";
|
||||||
return status === "pending";
|
|
||||||
}
|
|
||||||
|
|
||||||
export function canEditShippingAddress(status: OrderStatus) {
|
export function canEditShippingAddress(status: OrderStatus) {
|
||||||
return status === "pending" || status === "processing";
|
return status === "pending" || status === "processing";
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import type { OrderDetail } from "@orderops/shared";
|
import type { OrderDetail } from "@orderops/shared";
|
||||||
|
import { canEditOrderItems } from "@orderops/shared/order-status";
|
||||||
|
|
||||||
interface OrderItemsPanelProps {
|
interface OrderItemsPanelProps {
|
||||||
order: OrderDetail;
|
order: OrderDetail;
|
||||||
@@ -39,9 +40,14 @@ function QuantityForm({
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function OrderItemsPanel({ order, onUpdateQuantity }: OrderItemsPanelProps) {
|
export function OrderItemsPanel({ order, onUpdateQuantity }: OrderItemsPanelProps) {
|
||||||
|
const canEditQuantity = canEditOrderItems(order.status);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<section className="rounded-xl border border-slate-200 bg-white p-4 shadow-sm">
|
<section className="rounded-xl border border-slate-200 bg-white p-4 shadow-sm">
|
||||||
<h3 className="mb-3 text-sm font-semibold text-slate-700">Items</h3>
|
<h3 className="mb-3 text-sm font-semibold text-slate-700">Items</h3>
|
||||||
|
{!canEditQuantity ? (
|
||||||
|
<p className="mb-3 text-sm text-slate-600">Item quantities can only be changed while an order is pending.</p>
|
||||||
|
) : null}
|
||||||
<div className="space-y-3">
|
<div className="space-y-3">
|
||||||
{order.items.map((item) => (
|
{order.items.map((item) => (
|
||||||
<div key={item.id} className="rounded-lg border border-slate-200 p-3">
|
<div key={item.id} className="rounded-lg border border-slate-200 p-3">
|
||||||
@@ -51,7 +57,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>
|
||||||
|
{canEditQuantity ? (
|
||||||
<QuantityForm itemId={item.id} quantity={item.quantity} onSave={onUpdateQuantity} />
|
<QuantityForm itemId={item.id} quantity={item.quantity} onSave={onUpdateQuantity} />
|
||||||
|
) : (
|
||||||
|
<div className="text-sm text-slate-700">Quantity: {item.quantity}</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
|
|||||||
@@ -72,19 +72,6 @@ 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">
|
||||||
@@ -97,7 +84,7 @@ export function OrderPage() {
|
|||||||
<OrderDetailPage
|
<OrderDetailPage
|
||||||
order={order}
|
order={order}
|
||||||
auditEvents={auditEvents}
|
auditEvents={auditEvents}
|
||||||
onUpdateQuantity={handleUpdateQuantity}
|
onUpdateQuantity={(itemId, quantity) => runAction(() => updateOrderItemQuantity(order.id, itemId, quantity), "Quantity saved.")}
|
||||||
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.")}
|
||||||
|
|||||||
@@ -1,3 +1,7 @@
|
|||||||
export const orderStatuses = ["pending", "processing", "shipped", "cancelled", "refunded"] as const;
|
export const orderStatuses = ["pending", "processing", "shipped", "cancelled", "refunded"] as const;
|
||||||
|
|
||||||
export type OrderStatus = (typeof orderStatuses)[number];
|
export type OrderStatus = (typeof orderStatuses)[number];
|
||||||
|
|
||||||
|
export function canEditOrderItems(status: OrderStatus) {
|
||||||
|
return status === "pending";
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user