Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
45 lines
1.3 KiB
TypeScript
45 lines
1.3 KiB
TypeScript
import type { Address } from "@orderops/shared/address";
|
|
import { canEditShippingAddress, shippingAddressLockedMessage } from "@orderops/shared/order-status";
|
|
import type { DatabaseContext } from "../db/database";
|
|
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: shippingAddressLockedMessage } 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;
|
|
}
|