add:internal order notes
Runner Test / test (push) Successful in 17s
Runner Test / test (pull_request) Successful in 16s

This commit is contained in:
shawn
2026-09-21 19:15:10 +10:00
parent 79b485db56
commit 2c3353db19
11 changed files with 154 additions and 3 deletions
+6
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 NOT NULL DEFAULT '',
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL
);
@@ -119,4 +120,9 @@ export function initializeDatabase(sqlite: Database) {
created_at TEXT NOT NULL
);
`);
// Existing local databases predate internal notes, so add the column in place.
if (!columnExists(sqlite, "orders", "notes")) {
sqlite.exec(`ALTER TABLE orders ADD COLUMN notes TEXT NOT NULL DEFAULT ''`);
}
}
+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").notNull().default(""),
createdAt: text("created_at").notNull(),
updatedAt: text("updated_at").notNull()
});
+6 -1
View File
@@ -104,7 +104,8 @@ export async function findOrderDetail(context: DatabaseContext, orderId: string)
email: header.customer.email
},
shippingAddress: toAddress(header.address),
items: itemRows.map(toOrderItemDetail)
items: itemRows.map(toOrderItemDetail),
notes: header.order.notes
} satisfies OrderDetail;
}
@@ -174,6 +175,10 @@ export async function appendAuditEvent(
});
}
export async function updateOrderNotes(context: DatabaseContext, orderId: string, notes: string) {
await context.db.update(orders).set({ notes }).where(eq(orders.id, orderId));
}
async function findCustomerSummaryByOrderId(context: DatabaseContext, orderId: string) {
const [row] = await context.db
.select({ customer: users })
+29
View File
@@ -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 { saveOrderNotes } from "./update-order-notes";
import { changeShippingAddress } from "./update-shipping-address";
interface OrderRouteOptions {
@@ -33,6 +34,10 @@ interface CancelBody {
reason?: string;
}
interface NotesBody {
notes: string;
}
type AuthenticatedRequest = FastifyRequest & { currentUser: AuthUser };
export const registerOrderRoutes: FastifyPluginAsync<OrderRouteOptions> = async (app, options) => {
@@ -157,4 +162,28 @@ 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: {
// Empty and length rules are measured on the trimmed notes in saveOrderNotes.
notes: { type: "string" }
}
}
}
}, async (request, reply) => {
const result = await saveOrderNotes(options.db, {
orderId: request.params.orderId,
notes: request.body.notes
});
if (result.statusCode !== 200) {
return reply.code(result.statusCode).send({ message: result.message });
}
return result.order;
});
};
+38
View File
@@ -0,0 +1,38 @@
import { orderNotesMaxLength } from "@orderops/shared";
import type { DatabaseContext } from "../db/database";
import { findOrderDetail, updateOrderNotes, updateOrderTimestamp } from "./order-repository";
// Notes are allowed for every order status: follow-up work often happens
// after an order has shipped, been cancelled, or been refunded.
export async function saveOrderNotes(
context: DatabaseContext,
input: {
orderId: string;
notes: 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 > orderNotesMaxLength) {
return { statusCode: 400, message: `Notes must be ${orderNotesMaxLength} characters or fewer.` } as const;
}
const updatedAt = new Date().toISOString();
await updateOrderNotes(context, input.orderId, notes);
await updateOrderTimestamp(context, input.orderId, updatedAt);
return {
statusCode: 200,
order: await findOrderDetail(context, input.orderId)
} as const;
}