From 681121bb192145669b500ce3a01ad7d06356a396 Mon Sep 17 00:00:00 2001 From: Kasper Fabricius Kristensen <45367945+kasperkristensen@users.noreply.github.com> Date: Thu, 13 Feb 2025 09:06:11 +0100 Subject: [PATCH] fix(medusa): Fix draft order validator, and endpoint (#11398) Resolves CMRC-925, CMRC-924 --- .changeset/little-geese-wave.md | 5 +++ .../src/api/admin/draft-orders/route.ts | 43 ++++++++++++++----- .../src/api/admin/draft-orders/validators.ts | 6 +-- 3 files changed, 40 insertions(+), 14 deletions(-) create mode 100644 .changeset/little-geese-wave.md diff --git a/.changeset/little-geese-wave.md b/.changeset/little-geese-wave.md new file mode 100644 index 0000000000..cc96337137 --- /dev/null +++ b/.changeset/little-geese-wave.md @@ -0,0 +1,5 @@ +--- +"@medusajs/medusa": patch +--- + +fix(medusa): Make Shipping Methods optional on create draft orders endpoint, and allow passing a address ID for both billing and shipping address. diff --git a/packages/medusa/src/api/admin/draft-orders/route.ts b/packages/medusa/src/api/admin/draft-orders/route.ts index d29958be28..9d4d3cc48c 100644 --- a/packages/medusa/src/api/admin/draft-orders/route.ts +++ b/packages/medusa/src/api/admin/draft-orders/route.ts @@ -1,21 +1,21 @@ import { createOrderWorkflow } from "@medusajs/core-flows" -import { - ContainerRegistrationKeys, - OrderStatus, - remoteQueryObjectFromString, -} from "@medusajs/framework/utils" import { AuthenticatedMedusaRequest, MedusaRequest, MedusaResponse, } from "@medusajs/framework/http" -import { AdminCreateDraftOrderType } from "./validators" -import { refetchOrder } from "./helpers" import { AdditionalData, CreateOrderDTO, HttpTypes, } from "@medusajs/framework/types" +import { + ContainerRegistrationKeys, + OrderStatus, + remoteQueryObjectFromString, +} from "@medusajs/framework/utils" +import { refetchOrder } from "./helpers" +import { AdminCreateDraftOrderType } from "./validators" export const GET = async ( req: MedusaRequest, @@ -59,7 +59,10 @@ export const POST = async ( const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY) - if (!input.currency_code) { + /** + * If the currency code is not provided, we fetch the region and use the currency code from there. + */ + if (!workflowInput.currency_code) { const queryObject = remoteQueryObjectFromString({ entryPoint: "region", variables: { @@ -68,10 +71,13 @@ export const POST = async ( fields: ["currency_code"], }) const [region] = await remoteQuery(queryObject) - input.currency_code = region?.currency_code + workflowInput.currency_code = region?.currency_code } - if (!input.email) { + /** + * If the email is not provided, we fetch the customer and use the email from there. + */ + if (!workflowInput.email) { const queryObject = remoteQueryObjectFromString({ entryPoint: "customer", variables: { @@ -80,7 +86,22 @@ export const POST = async ( fields: ["email"], }) const [customer] = await remoteQuery(queryObject) - input.email = customer?.email + workflowInput.email = customer?.email + } + + /** + * We accept either a ID or a payload for both billing and shipping addresses. + * If either field was received as a string, we assume it's an ID and + * then ensure that it is passed along correctly to the workflow. + */ + if (typeof input.billing_address === "string") { + workflowInput.billing_address_id = input.billing_address + delete workflowInput.billing_address + } + + if (typeof input.shipping_address === "string") { + workflowInput.shipping_address_id = input.shipping_address + delete workflowInput.shipping_address } const { result } = await createOrderWorkflow(req.scope).run({ diff --git a/packages/medusa/src/api/admin/draft-orders/validators.ts b/packages/medusa/src/api/admin/draft-orders/validators.ts index 800f157912..bade7c840c 100644 --- a/packages/medusa/src/api/admin/draft-orders/validators.ts +++ b/packages/medusa/src/api/admin/draft-orders/validators.ts @@ -64,14 +64,14 @@ const CreateDraftOrder = z sales_channel_id: z.string().nullish(), email: z.string().nullish(), customer_id: z.string().nullish(), - billing_address: AddressPayload.optional(), - shipping_address: AddressPayload.optional(), + billing_address: z.union([AddressPayload, z.string()]).optional(), + shipping_address: z.union([AddressPayload, z.string()]).optional(), items: z.array(Item).optional(), region_id: z.string(), promo_codes: z.array(z.string()).optional(), currency_code: z.string().nullish(), no_notification_order: z.boolean().optional(), - shipping_methods: z.array(ShippingMethod), + shipping_methods: z.array(ShippingMethod).optional(), metadata: z.record(z.unknown()).nullish(), }) .strict()