Feat added internal notes
This commit is contained in:
@@ -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,7 @@ export function initializeDatabase(sqlite: Database) {
|
||||
created_at TEXT NOT NULL
|
||||
);
|
||||
`);
|
||||
if (!columnExists(sqlite, "orders", "notes")) {
|
||||
sqlite.exec("ALTER TABLE orders ADD COLUMN notes TEXT");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
});
|
||||
|
||||
@@ -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: {
|
||||
@@ -127,6 +128,10 @@ export async function updateOrderTimestamp(context: DatabaseContext, orderId: st
|
||||
await context.db.update(orders).set({ 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 updateOrderStatus(context: DatabaseContext, orderId: string, status: OrderDetail["status"], updatedAt: string) {
|
||||
await context.db.update(orders).set({ status, updatedAt }).where(eq(orders.id, orderId));
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
@@ -69,6 +70,29 @@ export const registerOrderRoutes: FastifyPluginAsync<OrderRouteOptions> = async
|
||||
|
||||
app.get<{ Params: IdParams }>("/orders/:orderId/audit-events", async (request) => listAuditEvents(options.db, request.params.orderId));
|
||||
|
||||
app.post<{ Params: IdParams; Body: { notes: string } }>("/orders/:orderId/notes", {
|
||||
schema: {
|
||||
body: {
|
||||
type: "object",
|
||||
required: ["notes"],
|
||||
properties: {
|
||||
notes: { type: "string", minLength: 1, maxLength: 1000 }
|
||||
}
|
||||
}
|
||||
}
|
||||
}, async (request, reply) => {
|
||||
const result = await changeOrderNotes(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;
|
||||
});
|
||||
|
||||
app.post<{ Params: OrderItemParams; Body: QuantityBody }>("/orders/:orderId/items/:itemId/quantity", {
|
||||
schema: {
|
||||
body: {
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
import type { DatabaseContext } from "../db/database";
|
||||
import { findOrderDetail, updateOrderNotes } from "./order-repository";
|
||||
|
||||
export async function changeOrderNotes(context: DatabaseContext, input: { orderId: string; notes: string }) {
|
||||
const notes = input.notes.trim();
|
||||
|
||||
if (!notes || Array.from(input.notes).length > 1000) {
|
||||
return { statusCode: 400, message: "Internal notes must contain between 1 and 1000 characters." } as const;
|
||||
}
|
||||
|
||||
const order = await findOrderDetail(context, input.orderId);
|
||||
if (!order) {
|
||||
return { statusCode: 404, message: "Order not found." } as const;
|
||||
}
|
||||
|
||||
await updateOrderNotes(context, input.orderId, notes, new Date().toISOString());
|
||||
|
||||
return { statusCode: 200, order: await findOrderDetail(context, input.orderId) } as const;
|
||||
}
|
||||
Reference in New Issue
Block a user