Compare commits

..
Author SHA1 Message Date
derrick 698a49ae82 add:order notes ui component
Runner Test / test (push) Successful in 17s
Runner Test / test (pull_request) Successful in 16s
2026-09-24 22:35:55 +09:00
derrick 97438aaa56 add:order notes crud 2026-09-24 21:07:19 +09:00
derrick 2e2694354d add:notes column to orders 2026-09-24 18:09:20 +09:00
10 changed files with 134 additions and 3 deletions
+5
View File
@@ -90,6 +90,7 @@ export function initializeDatabase(sqlite: Database) {
customer_id TEXT NOT NULL REFERENCES users(id),
shipping_address_id TEXT NOT NULL REFERENCES addresses(id),
status TEXT NOT NULL,
notes TEXT,
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL
);
@@ -119,4 +120,8 @@ export function initializeDatabase(sqlite: Database) {
created_at TEXT NOT NULL
);
`);
if (!columnExists(sqlite, "orders", "notes")) {
sqlite.exec("ALTER TABLE orders ADD COLUMN notes TEXT");
}
}
+1
View File
@@ -9,6 +9,7 @@ export const orders = sqliteTable("orders", {
customerId: text("customer_id").notNull().references(() => users.id),
shippingAddressId: text("shipping_address_id").notNull().references(() => addresses.id),
status: text("status").notNull(),
notes: text("notes"),
createdAt: text("created_at").notNull(),
updatedAt: text("updated_at").notNull()
});
+5
View File
@@ -98,6 +98,7 @@ export async function findOrderDetail(context: DatabaseContext, orderId: string)
status: header.order.status as OrderDetail["status"],
createdAt: header.order.createdAt,
updatedAt: header.order.updatedAt,
notes: header.order.notes,
customer: {
id: header.customer.id,
name: header.customer.name,
@@ -123,6 +124,10 @@ export async function updateOrderItemQuantity(context: DatabaseContext, itemId:
await context.db.update(orderItems).set({ quantity }).where(eq(orderItems.id, itemId));
}
export async function updateOrderNotes(context: DatabaseContext, orderId: string, notes: string) {
await context.db.update(orders).set({ notes }).where(eq(orders.id, orderId));
}
export async function updateOrderTimestamp(context: DatabaseContext, orderId: string, updatedAt: string) {
await context.db.update(orders).set({ updatedAt }).where(eq(orders.id, orderId));
}
+30 -1
View File
@@ -6,7 +6,7 @@ import { cancelOrder } from "./cancel-order";
import { getOrder } from "./get-order";
import { listAuditEvents } from "./list-audit-events";
import { listOrders } from "./list-orders";
import { changeOrderItemQuantity } from "./update-order-item";
import { changeOrderItemQuantity, updateOrderNotes } from "./update-order-item";
import { changeShippingAddress } from "./update-shipping-address";
interface OrderRouteOptions {
@@ -33,6 +33,10 @@ interface CancelBody {
reason?: string;
}
interface NotesBody {
notes: string;
}
type AuthenticatedRequest = FastifyRequest & { currentUser: AuthUser };
export const registerOrderRoutes: FastifyPluginAsync<OrderRouteOptions> = async (app, options) => {
@@ -95,6 +99,31 @@ export const registerOrderRoutes: FastifyPluginAsync<OrderRouteOptions> = async
return result.order;
});
app.post<{ Params: IdParams; Body: NotesBody }>("/orders/:orderId/notes", {
schema: {
body: {
type: "object",
required: ["notes"],
properties: {
notes: { type: "string", maxLength: 1000 }
}
}
}
}, async (request, reply) => {
const currentUser = (request as AuthenticatedRequest).currentUser;
const result = await updateOrderNotes(options.db, {
orderId: request.params.orderId,
notes: request.body.notes,
actor: currentUser.username
});
if (result.statusCode !== 200) {
return reply.code(result.statusCode).send({ message: result.message });
}
return result.order;
});
app.post<{ Params: IdParams; Body: Address }>("/orders/:orderId/shipping-address", {
schema: {
body: {
+42 -1
View File
@@ -1,6 +1,6 @@
import type { DatabaseContext } from "../db/database";
import { canEditOrderItems } from "./order-status";
import { appendAuditEvent, findOrderDetail, findOrderItemRecord, updateOrderItemQuantity, updateOrderTimestamp } from "./order-repository";
import { appendAuditEvent, findOrderDetail, findOrderItemRecord, updateOrderItemQuantity, updateOrderNotes as saveOrderNotes, updateOrderTimestamp } from "./order-repository";
export async function changeOrderItemQuantity(
context: DatabaseContext,
@@ -47,3 +47,44 @@ export async function changeOrderItemQuantity(
order: await findOrderDetail(context, input.orderId)
} as const;
}
export async function updateOrderNotes(
context: DatabaseContext,
input: {
orderId: string;
notes: string;
actor: string;
}
) {
const order = await findOrderDetail(context, input.orderId);
if (!order) {
return { statusCode: 404, message: "Order not found." } as const;
}
const notes = input.notes.trim();
if (!notes) {
return { statusCode: 400, message: "Notes cannot be empty." } as const;
}
if (notes.length > 1000) {
return { statusCode: 400, message: "Notes must be 1000 characters or fewer." } as const;
}
const updatedAt = new Date().toISOString();
await saveOrderNotes(context, input.orderId, notes);
await updateOrderTimestamp(context, input.orderId, updatedAt);
await appendAuditEvent(context, {
orderId: input.orderId,
actor: input.actor,
type: "order.notes_updated",
message: "Updated internal order notes.",
createdAt: updatedAt
});
return {
statusCode: 200,
order: await findOrderDetail(context, input.orderId)
} as const;
}
+7
View File
@@ -21,6 +21,13 @@ export function updateOrderItemQuantity(orderId: string, itemId: string, quantit
});
}
export function updateOrderNotes(orderId: string, notes: string) {
return apiRequest<OrderDetail>(`/api/orders/${orderId}/notes`, {
method: "POST",
body: JSON.stringify({ notes })
});
}
export function updateShippingAddress(orderId: string, address: Address) {
return apiRequest<OrderDetail>(`/api/orders/${orderId}/shipping-address`, {
method: "POST",
@@ -1,6 +1,7 @@
import type { Address, AuditEvent, OrderDetail } from "@orderops/shared";
import { AuditTimeline } from "./audit-timeline";
import { OrderItemsPanel } from "./order-items-panel";
import { OrderNotes } from "./order-notes";
import { OrderSummaryPanel } from "./order-summary";
import { ShippingAddressForm } from "./shipping-address-form";
@@ -9,6 +10,7 @@ interface OrderDetailPageProps {
auditEvents: AuditEvent[];
onUpdateQuantity: (itemId: string, quantity: number) => Promise<void>;
onSaveAddress: (address: Address) => Promise<void>;
onSaveNotes: (notes: string) => Promise<void>;
onCancel: () => Promise<void>;
}
@@ -17,6 +19,7 @@ export function OrderDetailPage({
auditEvents,
onUpdateQuantity,
onSaveAddress,
onSaveNotes,
onCancel
}: OrderDetailPageProps) {
return (
@@ -29,6 +32,7 @@ export function OrderDetailPage({
</div>
<OrderItemsPanel order={order} onUpdateQuantity={onUpdateQuantity} />
<ShippingAddressForm order={order} onSave={onSaveAddress} />
<OrderNotes order={order} onSave={onSaveNotes} />
<AuditTimeline events={auditEvents} />
</div>
);
+37
View File
@@ -0,0 +1,37 @@
import { useState } from "react";
import type { OrderDetail } from "@orderops/shared";
interface OrderNotesProps {
order: OrderDetail;
onSave: (notes: string) => Promise<void>;
}
export function OrderNotes({ order, onSave }: OrderNotesProps) {
const [notes, setNotes] = useState(order.notes ?? "");
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">Internal notes</h3>
<form
className="space-y-3"
onSubmit={async (event) => {
event.preventDefault();
await onSave(notes);
}}
>
<label className="block text-sm text-slate-700">
<span className="mb-1 block">Notes</span>
<textarea
className="w-full resize-none rounded-lg border border-slate-300 px-3 py-2"
rows={5}
value={notes}
onChange={(event) => setNotes(event.target.value)}
/>
</label>
<button className="rounded-md bg-slate-900 px-4 py-2 text-sm font-medium text-white" type="submit">
Save notes
</button>
</form>
</section>
);
}
+2 -1
View File
@@ -2,7 +2,7 @@ import { useEffect, useState } from "react";
import type { Address, AuditEvent, OrderDetail } from "@orderops/shared";
import { Link, useParams } from "react-router-dom";
import { Toast } from "../ui/toast";
import { cancelOrder, getOrder, listAuditEvents, updateOrderItemQuantity, updateShippingAddress } from "./order-api";
import { cancelOrder, getOrder, listAuditEvents, updateOrderItemQuantity, updateOrderNotes, updateShippingAddress } from "./order-api";
import { OrderDetailPage } from "./order-detail-page";
interface ToastState {
@@ -86,6 +86,7 @@ export function OrderPage() {
auditEvents={auditEvents}
onUpdateQuantity={(itemId, quantity) => runAction(() => updateOrderItemQuantity(order.id, itemId, quantity), "Quantity saved.")}
onSaveAddress={(address: Address) => runAction(() => updateShippingAddress(order.id, address), "Shipping address saved.")}
onSaveNotes={(notes) => runAction(() => updateOrderNotes(order.id, notes), "Notes saved.")}
onCancel={() => runAction(() => cancelOrder(order.id), "Order cancelled.")}
/>
) : null}
+1
View File
@@ -19,6 +19,7 @@ export interface OrderDetail {
status: OrderStatus;
createdAt: string;
updatedAt: string;
notes: string | null;
customer: CustomerSummary;
shippingAddress: Address;
items: OrderItemDetail[];