diff --git a/apps/api/src/db/init-db.ts b/apps/api/src/db/init-db.ts index 05f934c..334b999 100644 --- a/apps/api/src/db/init-db.ts +++ b/apps/api/src/db/init-db.ts @@ -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 NOT NULL DEFAULT '', 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 (tableExists(sqlite, "orders") && !columnExists(sqlite, "orders", "notes")) { + sqlite.exec(`ALTER TABLE orders ADD COLUMN notes TEXT NOT NULL DEFAULT ''`); + } } diff --git a/apps/api/src/db/schema/orders.ts b/apps/api/src/db/schema/orders.ts index 2c31f07..2752a15 100644 --- a/apps/api/src/db/schema/orders.ts +++ b/apps/api/src/db/schema/orders.ts @@ -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").notNull().default(""), createdAt: text("created_at").notNull(), updatedAt: text("updated_at").notNull() }); diff --git a/apps/api/src/orders/order-repository.ts b/apps/api/src/orders/order-repository.ts index 20bc84c..4cec37d 100644 --- a/apps/api/src/orders/order-repository.ts +++ b/apps/api/src/orders/order-repository.ts @@ -96,6 +96,7 @@ export async function findOrderDetail(context: DatabaseContext, orderId: string) id: header.order.id, orderNumber: header.order.orderNumber, status: header.order.status as OrderDetail["status"], + notes: header.order.notes, createdAt: header.order.createdAt, updatedAt: header.order.updatedAt, customer: { @@ -131,6 +132,10 @@ export async function updateOrderStatus(context: DatabaseContext, orderId: strin await context.db.update(orders).set({ status, updatedAt }).where(eq(orders.id, orderId)); } +export async function updateOrderNotes(context: DatabaseContext, orderId: string, notes: string, updatedAt: string) { + await context.db.update(orders).set({ notes, updatedAt }).where(eq(orders.id, orderId)); +} + export async function updateOrderAddress(context: DatabaseContext, orderId: string, address: Address) { const [row] = await context.db.select({ shippingAddressId: orders.shippingAddressId }).from(orders).where(eq(orders.id, orderId)); diff --git a/apps/api/src/orders/order-routes.ts b/apps/api/src/orders/order-routes.ts index ecb3b32..a1e7ff2 100644 --- a/apps/api/src/orders/order-routes.ts +++ b/apps/api/src/orders/order-routes.ts @@ -7,6 +7,7 @@ import { getOrder } from "./get-order"; import { listAuditEvents } from "./list-audit-events"; import { listOrders } from "./list-orders"; import { changeOrderItemQuantity } from "./update-order-item"; +import { changeOrderNotes } from "./update-notes"; import { changeShippingAddress } from "./update-shipping-address"; interface OrderRouteOptions { @@ -29,6 +30,10 @@ interface QuantityBody { quantity: number; } +interface NotesBody { + notes: string; +} + interface CancelBody { reason?: string; } @@ -95,6 +100,31 @@ export const registerOrderRoutes: FastifyPluginAsync = async return result.order; }); + app.post<{ Params: IdParams; Body: NotesBody }>("/orders/:orderId/notes", { + schema: { + body: { + type: "object", + required: ["notes"], + properties: { + notes: { type: "string" } + } + } + } + }, async (request, reply) => { + const currentUser = (request as AuthenticatedRequest).currentUser; + const result = await changeOrderNotes(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: { diff --git a/apps/api/src/orders/update-notes.ts b/apps/api/src/orders/update-notes.ts new file mode 100644 index 0000000..5061328 --- /dev/null +++ b/apps/api/src/orders/update-notes.ts @@ -0,0 +1,44 @@ +import type { DatabaseContext } from "../db/database"; +import { appendAuditEvent, findOrderDetail, updateOrderNotes } from "./order-repository"; + +export const NOTES_MAX_LENGTH = 1000; + +export async function changeOrderNotes( + 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.length === 0) { + return { statusCode: 400, message: "Notes cannot be empty." } as const; + } + + if (notes.length > NOTES_MAX_LENGTH) { + return { statusCode: 400, message: `Notes cannot exceed ${NOTES_MAX_LENGTH} characters.` } as const; + } + + const updatedAt = new Date().toISOString(); + await updateOrderNotes(context, input.orderId, notes, updatedAt); + await appendAuditEvent(context, { + orderId: input.orderId, + actor: input.actor, + type: "order.notes_updated", + message: "Updated internal notes.", + createdAt: updatedAt + }); + + return { + statusCode: 200, + order: await findOrderDetail(context, input.orderId) + } as const; +} \ No newline at end of file diff --git a/apps/web/src/orders/order-api.ts b/apps/web/src/orders/order-api.ts index 957c8ef..838b637 100644 --- a/apps/web/src/orders/order-api.ts +++ b/apps/web/src/orders/order-api.ts @@ -28,6 +28,13 @@ export function updateShippingAddress(orderId: string, address: Address) { }); } +export function updateOrderNotes(orderId: string, notes: string) { + return apiRequest(`/api/orders/${orderId}/notes`, { + method: "POST", + body: JSON.stringify({ notes }) + }); +} + export function cancelOrder(orderId: string, reason?: string) { return apiRequest(`/api/orders/${orderId}/cancel`, { method: "POST", diff --git a/apps/web/src/orders/order-detail-page.tsx b/apps/web/src/orders/order-detail-page.tsx index 7387ded..92110a1 100644 --- a/apps/web/src/orders/order-detail-page.tsx +++ b/apps/web/src/orders/order-detail-page.tsx @@ -1,6 +1,7 @@ import type { Address, AuditEvent, OrderDetail } from "@orderops/shared"; import { AuditTimeline } from "./audit-timeline"; import { OrderItemsPanel } from "./order-items-panel"; +import { OrderNotesPanel } 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; onSaveAddress: (address: Address) => Promise; + onSaveNotes: (notes: string) => Promise; onCancel: () => Promise; } @@ -17,11 +19,13 @@ export function OrderDetailPage({ auditEvents, onUpdateQuantity, onSaveAddress, + onSaveNotes, onCancel }: OrderDetailPageProps) { return (
+