init
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
import type { DatabaseContext } from "../db/database";
|
||||
import { canCancelOrder } from "./order-status";
|
||||
import { appendAuditEvent, findOrderDetail, updateOrderStatus } from "./order-repository";
|
||||
|
||||
export async function cancelOrder(
|
||||
context: DatabaseContext,
|
||||
input: {
|
||||
orderId: string;
|
||||
actor: string;
|
||||
reason?: string;
|
||||
}
|
||||
) {
|
||||
const order = await findOrderDetail(context, input.orderId);
|
||||
|
||||
if (!order) {
|
||||
return { statusCode: 404, message: "Order not found." } as const;
|
||||
}
|
||||
|
||||
if (!canCancelOrder(order.status)) {
|
||||
return { statusCode: 400, message: "Order cannot be cancelled for this order status." } as const;
|
||||
}
|
||||
|
||||
const updatedAt = new Date().toISOString();
|
||||
await updateOrderStatus(context, input.orderId, "cancelled", updatedAt);
|
||||
await appendAuditEvent(context, {
|
||||
orderId: input.orderId,
|
||||
actor: input.actor,
|
||||
type: "order.cancelled",
|
||||
message: input.reason?.trim() ? `Cancelled order. Reason: ${input.reason.trim()}` : "Cancelled order.",
|
||||
createdAt: updatedAt
|
||||
});
|
||||
|
||||
return {
|
||||
statusCode: 200,
|
||||
order: await findOrderDetail(context, input.orderId)
|
||||
} as const;
|
||||
}
|
||||
Reference in New Issue
Block a user