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

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Abdulaziz Salim Said AL-Gharbi
2026-09-24 20:24:10 +04:00
co-authored by Claude Opus 5.5
parent 79b485db56
commit bfe60631a5
11 changed files with 146 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,
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL
);
@@ -119,4 +120,9 @@ export function initializeDatabase(sqlite: Database) {
created_at TEXT NOT NULL
);
`);
// Databases created before internal order notes existed keep their orders table, so add the column in place.
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()
});
+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;
}
@@ -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));
+30
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 { changeOrderNotes } 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) => {
@@ -134,6 +139,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" }
}
}
}
}, 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: CancelBody }>("/orders/:orderId/cancel", {
schema: {
body: {
+43
View File
@@ -0,0 +1,43 @@
import { maxOrderNotesLength } from "@orderops/shared";
import type { DatabaseContext } from "../db/database";
import { appendAuditEvent, findOrderDetail, updateOrderNotes } from "./order-repository";
export async function changeOrderNotes(
context: DatabaseContext,
input: {
orderId: string;
notes: string;
actor: string;
}
) {
const notes = input.notes.trim();
if (!notes) {
return { statusCode: 400, message: "Notes cannot be empty." } as const;
}
if (notes.length > maxOrderNotesLength) {
return { statusCode: 400, message: `Notes cannot be longer than ${maxOrderNotesLength} characters.` } as const;
}
const order = await findOrderDetail(context, input.orderId);
if (!order) {
return { statusCode: 404, message: "Order not found." } 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;
}