Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cb3020a73c |
@@ -2,12 +2,13 @@ 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 ?? "";
|
||||
return (Array.isArray(value) ? value[0] : value) ?? "";
|
||||
}
|
||||
|
||||
async function login(username: string) {
|
||||
@@ -43,29 +44,35 @@ describe("assignment 3 quantity validation", () => {
|
||||
|
||||
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." })
|
||||
]);
|
||||
});
|
||||
|
||||
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 }
|
||||
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);
|
||||
}
|
||||
});
|
||||
|
||||
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({
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import type { OrderStatus } from "@orderops/shared/order-status";
|
||||
|
||||
export { canEditOrderItems } from "@orderops/shared/order-status";
|
||||
export function canEditOrderItems(status: OrderStatus) {
|
||||
return status === "pending";
|
||||
}
|
||||
|
||||
export function canEditShippingAddress(status: OrderStatus) {
|
||||
return status === "pending" || status === "processing";
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -1,5 +1,5 @@
|
||||
import { useState } from "react";
|
||||
import { canEditOrderItems, type OrderDetail } from "@orderops/shared";
|
||||
import type { OrderDetail } from "@orderops/shared";
|
||||
|
||||
interface OrderItemsPanelProps {
|
||||
order: OrderDetail;
|
||||
@@ -9,12 +9,10 @@ interface OrderItemsPanelProps {
|
||||
function QuantityForm({
|
||||
itemId,
|
||||
quantity,
|
||||
isEditable,
|
||||
onSave
|
||||
}: {
|
||||
itemId: string;
|
||||
quantity: number;
|
||||
isEditable: boolean;
|
||||
onSave: (itemId: string, quantity: number) => Promise<void>;
|
||||
}) {
|
||||
const [value, setValue] = useState(String(quantity));
|
||||
@@ -28,17 +26,12 @@ function QuantityForm({
|
||||
}}
|
||||
>
|
||||
<input
|
||||
className="w-16 rounded-md border border-slate-300 px-2 py-1 text-sm disabled:bg-slate-100 disabled:text-slate-500"
|
||||
disabled={!isEditable}
|
||||
className="w-16 rounded-md border border-slate-300 px-2 py-1 text-sm"
|
||||
inputMode="numeric"
|
||||
value={value}
|
||||
onChange={(event) => setValue(event.target.value)}
|
||||
/>
|
||||
<button
|
||||
className="rounded-md bg-blue-600 px-3 py-1 text-sm font-medium text-white disabled:cursor-not-allowed disabled:bg-slate-300"
|
||||
disabled={!isEditable}
|
||||
type="submit"
|
||||
>
|
||||
<button className="rounded-md bg-blue-600 px-3 py-1 text-sm font-medium text-white" type="submit">
|
||||
Save
|
||||
</button>
|
||||
</form>
|
||||
@@ -46,12 +39,9 @@ function QuantityForm({
|
||||
}
|
||||
|
||||
export function OrderItemsPanel({ order, onUpdateQuantity }: OrderItemsPanelProps) {
|
||||
const isEditable = canEditOrderItems(order.status);
|
||||
|
||||
return (
|
||||
<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>
|
||||
{isEditable ? null : <p className="mb-3 text-sm text-slate-500">Quantities can only be changed while the order is pending.</p>}
|
||||
<div className="space-y-3">
|
||||
{order.items.map((item) => (
|
||||
<div key={item.id} className="rounded-lg border border-slate-200 p-3">
|
||||
@@ -61,7 +51,7 @@ export function OrderItemsPanel({ order, onUpdateQuantity }: OrderItemsPanelProp
|
||||
<div className="text-sm text-slate-600">{item.sku}</div>
|
||||
<div className="text-sm text-slate-500">Available: {item.availableQuantity}</div>
|
||||
</div>
|
||||
<QuantityForm itemId={item.id} quantity={item.quantity} isEditable={isEditable} onSave={onUpdateQuantity} />
|
||||
<QuantityForm itemId={item.id} quantity={item.quantity} onSave={onUpdateQuantity} />
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
|
||||
@@ -1,9 +1,3 @@
|
||||
export const orderStatuses = ["pending", "processing", "shipped", "cancelled", "refunded"] as const;
|
||||
|
||||
export type OrderStatus = (typeof orderStatuses)[number];
|
||||
|
||||
// Item quantities can only be adjusted before fulfillment starts. Shared so the UI
|
||||
// matches the API rule; the API stays the authoritative check.
|
||||
export function canEditOrderItems(status: OrderStatus) {
|
||||
return status === "pending";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user