From 8f619214f052ccf9b8213459ad55c7d5cc99db43 Mon Sep 17 00:00:00 2001 From: legendcodes1 Date: Tue, 15 Sep 2026 19:38:44 -0500 Subject: [PATCH 1/3] fixed issue with searcing --- apps/api/src/orders/order-repository.ts | 6 +++--- apps/api/src/orders/order-search-assignment-1.test.ts | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/apps/api/src/orders/order-repository.ts b/apps/api/src/orders/order-repository.ts index 5a904e1..20bc84c 100644 --- a/apps/api/src/orders/order-repository.ts +++ b/apps/api/src/orders/order-repository.ts @@ -39,9 +39,9 @@ export async function listOrderSummaries(context: DatabaseContext, query?: strin const filters = search ? or( - sql`instr(${orders.orderNumber}, ${search}) > 0`, - sql`instr(${users.name}, ${search}) > 0`, - sql`instr(${users.email}, ${search}) > 0` + sql`instr(lower(${orders.orderNumber}), lower(${search})) > 0`, + sql`instr(lower(${users.name}), lower(${search})) > 0`, + sql`instr(lower(${users.email}), lower(${search})) > 0` ) : undefined; diff --git a/apps/api/src/orders/order-search-assignment-1.test.ts b/apps/api/src/orders/order-search-assignment-1.test.ts index b3724f5..49cad2f 100644 --- a/apps/api/src/orders/order-search-assignment-1.test.ts +++ b/apps/api/src/orders/order-search-assignment-1.test.ts @@ -7,7 +7,7 @@ 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) { -- 2.54.0 From 4f6c68ad295eb28322207b6398af44a7005001d6 Mon Sep 17 00:00:00 2001 From: derrick Date: Sun, 20 Sep 2026 00:14:31 +0900 Subject: [PATCH 2/3] add:assignment 2 tests --- .../orders/order-notes-assignment-2.test.ts | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 apps/api/src/orders/order-notes-assignment-2.test.ts diff --git a/apps/api/src/orders/order-notes-assignment-2.test.ts b/apps/api/src/orders/order-notes-assignment-2.test.ts new file mode 100644 index 0000000..f74b795 --- /dev/null +++ b/apps/api/src/orders/order-notes-assignment-2.test.ts @@ -0,0 +1,106 @@ +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 = ""; +let customerCookie = ""; + +function firstHeader(value: string | string[] | undefined): string { + 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"); + customerCookie = await login("john-smith"); +}); + +afterEach(async () => { + await app.close(); +}); + +describe("assignment 2 internal order notes", () => { + test("allows employee users to attach notes to an order", async () => { + const response = await app.inject({ + method: "POST", + url: "/api/orders/order_1/notes", + headers: { cookie: employeeCookie }, + payload: { notes: "Follow up with warehouse before shipping." } + }); + + const order = response.json(); + + expect(response.statusCode).toBe(200); + expect(order.orderNumber).toBe("ORD-1001"); + expect(order.notes).toBe("Follow up with warehouse before shipping."); + expect(order.items).toHaveLength(2); + }); + + test("returns persisted notes when the order is viewed later", async () => { + const updateResponse = await app.inject({ + method: "POST", + url: "/api/orders/order_1/notes", + headers: { cookie: employeeCookie }, + payload: { notes: "Customer requested gift receipt." } + }); + + expect(updateResponse.statusCode).toBe(200); + + const detailResponse = await app.inject({ + method: "GET", + url: "/api/orders/order_1", + headers: { cookie: employeeCookie } + }); + + expect(detailResponse.statusCode).toBe(200); + expect(detailResponse.json().notes).toBe("Customer requested gift receipt."); + }); + + test("rejects customer users when attaching notes", async () => { + const response = await app.inject({ + method: "POST", + url: "/api/orders/order_1/notes", + headers: { cookie: customerCookie }, + payload: { notes: "Customer should not be able to add this." } + }); + + expect(response.statusCode).toBe(403); + }); + + test("rejects empty notes", async () => { + const response = await app.inject({ + method: "POST", + url: "/api/orders/order_1/notes", + headers: { cookie: employeeCookie }, + payload: { notes: " " } + }); + + expect(response.statusCode).toBe(400); + }); + + test("rejects notes beyond the accepted length limit", async () => { + const response = await app.inject({ + method: "POST", + url: "/api/orders/order_1/notes", + headers: { cookie: employeeCookie }, + payload: { notes: "x".repeat(1001) } + }); + + expect(response.statusCode).toBe(400); + }); +}); -- 2.54.0 From 98801089e5440a9a491e562bfcfa801a40b59645 Mon Sep 17 00:00:00 2001 From: legendcodes1 Date: Thu, 24 Sep 2026 16:43:54 -0500 Subject: [PATCH 3/3] assignment 2 ui changes --- apps/api/src/db/init-db.ts | 5 ++ apps/api/src/db/schema/orders.ts | 1 + apps/api/src/orders/order-repository.ts | 5 ++ apps/api/src/orders/order-routes.ts | 30 ++++++++++++ apps/api/src/orders/update-notes.ts | 44 +++++++++++++++++ apps/web/src/orders/order-api.ts | 7 +++ apps/web/src/orders/order-detail-page.tsx | 4 ++ apps/web/src/orders/order-notes.tsx | 60 +++++++++++++++++++++++ apps/web/src/orders/order-page.tsx | 3 +- packages/shared/src/order-detail.ts | 1 + 10 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 apps/api/src/orders/update-notes.ts create mode 100644 apps/web/src/orders/order-notes.tsx 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 (
+