ADDED: new add notes feature for orders backend and frontend
This commit is contained in:
@@ -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 })
|
||||
|
||||
Reference in New Issue
Block a user