ADDED: new add notes feature for orders backend and frontend
Runner Test / test (push) Failing after 17s
Runner Test / test (pull_request) Failing after 16s

This commit is contained in:
Lena Deriso
2026-09-22 23:06:01 -04:00
parent e493d1592b
commit c5215daedb
12 changed files with 425 additions and 8 deletions
+45 -3
View File
@@ -1,7 +1,7 @@
import type { Address, AuditEvent, OrderDetail, OrderItemDetail, OrderSummary } from "@orderops/shared";
import type { Address, AuditEvent, Note, OrderDetail, OrderItemDetail, OrderSummary } from "@orderops/shared";
import { and, asc, desc, eq, inArray, or, sql } from "drizzle-orm";
import type { DatabaseContext } from "../db/database";
import { addresses, auditEvents, inventory, orderItems, orders, products, users } from "../db/schema";
import { addresses, auditEvents, inventory, orderItems, orderNotes, orders, products, users } from "../db/schema";
function toAddress(row: typeof addresses.$inferSelect): Address {
return {
@@ -92,6 +92,20 @@ export async function findOrderDetail(context: DatabaseContext, orderId: string)
.where(eq(orderItems.orderId, orderId))
.orderBy(asc(orderItems.id));
const notes = await context.db
.select({
id: orderNotes.id,
orderId: orderNotes.orderId,
body: orderNotes.body,
authorUserId: orderNotes.authorUserId,
authorName: users.name,
createdAt: orderNotes.createdAt
})
.from(orderNotes)
.innerJoin(users, eq(orderNotes.authorUserId, users.id))
.where(eq(orderNotes.orderId, orderId))
.orderBy(desc(orderNotes.createdAt));
return {
id: header.order.id,
orderNumber: header.order.orderNumber,
@@ -104,10 +118,38 @@ 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
} satisfies OrderDetail;
}
export async function insertOrderNote(
context: DatabaseContext,
input: {
orderId: string;
authorUserId: string;
body: string;
}
) {
const [author] = await context.db.select({ name: users.name }).from(users).where(eq(users.id, input.authorUserId));
if (!author) {
return null;
}
const note = {
id: crypto.randomUUID(),
orderId: input.orderId,
body: input.body,
authorUserId: input.authorUserId,
createdAt: new Date().toISOString()
} satisfies typeof orderNotes.$inferInsert;
await context.db.insert(orderNotes).values(note);
return { ...note, authorName: author.name } satisfies Note;
}
export async function findOrderItemRecord(context: DatabaseContext, orderId: string, itemId: string) {
const [row] = await context.db
.select({ order: orders, item: orderItems, inventory })