init
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
import type { Address } from "@orderops/shared/address";
|
||||
import type { DatabaseContext } from "../db/database";
|
||||
import { canEditShippingAddress } from "./order-status";
|
||||
import { appendAuditEvent, findOrderDetail, updateOrderAddress, updateOrderTimestamp } from "./order-repository";
|
||||
|
||||
export async function changeShippingAddress(
|
||||
context: DatabaseContext,
|
||||
input: {
|
||||
orderId: string;
|
||||
address: Address;
|
||||
actor: string;
|
||||
}
|
||||
) {
|
||||
const order = await findOrderDetail(context, input.orderId);
|
||||
|
||||
if (!order) {
|
||||
return { statusCode: 404, message: "Order not found." } as const;
|
||||
}
|
||||
|
||||
if (!canEditShippingAddress(order.status)) {
|
||||
return { statusCode: 400, message: "Shipping address cannot be changed for this order status." } as const;
|
||||
}
|
||||
|
||||
const updatedAt = new Date().toISOString();
|
||||
const updated = await updateOrderAddress(context, input.orderId, input.address);
|
||||
|
||||
if (!updated) {
|
||||
return { statusCode: 404, message: "Order not found." } as const;
|
||||
}
|
||||
|
||||
await updateOrderTimestamp(context, input.orderId, updatedAt);
|
||||
await appendAuditEvent(context, {
|
||||
orderId: input.orderId,
|
||||
actor: input.actor,
|
||||
type: "order.shipping_address_changed",
|
||||
message: "Updated shipping address.",
|
||||
createdAt: updatedAt
|
||||
});
|
||||
|
||||
return {
|
||||
statusCode: 200,
|
||||
order: await findOrderDetail(context, input.orderId)
|
||||
} as const;
|
||||
}
|
||||
Reference in New Issue
Block a user