assignment 2 ui changes
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 NOT NULL DEFAULT '',
|
||||
created_at TEXT NOT NULL,
|
||||
updated_at TEXT NOT NULL
|
||||
);
|
||||
@@ -119,4 +120,8 @@ export function initializeDatabase(sqlite: Database) {
|
||||
created_at TEXT NOT NULL
|
||||
);
|
||||
`);
|
||||
|
||||
if (tableExists(sqlite, "orders") && !columnExists(sqlite, "orders", "notes")) {
|
||||
sqlite.exec(`ALTER TABLE orders ADD COLUMN notes TEXT NOT NULL DEFAULT ''`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
});
|
||||
|
||||
@@ -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: {
|
||||
@@ -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));
|
||||
|
||||
|
||||
@@ -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-notes";
|
||||
import { changeShippingAddress } from "./update-shipping-address";
|
||||
|
||||
interface OrderRouteOptions {
|
||||
@@ -29,6 +30,10 @@ interface QuantityBody {
|
||||
quantity: number;
|
||||
}
|
||||
|
||||
interface NotesBody {
|
||||
notes: string;
|
||||
}
|
||||
|
||||
interface CancelBody {
|
||||
reason?: string;
|
||||
}
|
||||
@@ -95,6 +100,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: Address }>("/orders/:orderId/shipping-address", {
|
||||
schema: {
|
||||
body: {
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
import type { DatabaseContext } from "../db/database";
|
||||
import { appendAuditEvent, findOrderDetail, updateOrderNotes } from "./order-repository";
|
||||
|
||||
export const NOTES_MAX_LENGTH = 1000;
|
||||
|
||||
export async function changeOrderNotes(
|
||||
context: DatabaseContext,
|
||||
input: {
|
||||
orderId: string;
|
||||
notes: string;
|
||||
actor: 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.length === 0) {
|
||||
return { statusCode: 400, message: "Notes cannot be empty." } as const;
|
||||
}
|
||||
|
||||
if (notes.length > NOTES_MAX_LENGTH) {
|
||||
return { statusCode: 400, message: `Notes cannot exceed ${NOTES_MAX_LENGTH} characters.` } 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;
|
||||
}
|
||||
Reference in New Issue
Block a user