fix(medusa): Fix draft order validator, and endpoint (#11398)

Resolves CMRC-925, CMRC-924
This commit is contained in:
Kasper Fabricius Kristensen authored and GitHub committed 2025-02-13 08:06:11 +00:00
1 parent 2726bb3759
commit 681121bb19
3 files changed
+40 -14

No files matched your search

+5
View File
@@ -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.
@@ -1,21 +1,21 @@
import { createOrderWorkflow } from "@medusajs/core-flows" import { createOrderWorkflow } from "@medusajs/core-flows"
import {
ContainerRegistrationKeys,
OrderStatus,
remoteQueryObjectFromString,
} from "@medusajs/framework/utils"
import { import {
AuthenticatedMedusaRequest, AuthenticatedMedusaRequest,
MedusaRequest, MedusaRequest,
MedusaResponse, MedusaResponse,
} from "@medusajs/framework/http" } from "@medusajs/framework/http"
import { AdminCreateDraftOrderType } from "./validators"
import { refetchOrder } from "./helpers"
import { import {
AdditionalData, AdditionalData,
CreateOrderDTO, CreateOrderDTO,
HttpTypes, HttpTypes,
} from "@medusajs/framework/types" } from "@medusajs/framework/types"
import {
ContainerRegistrationKeys,
OrderStatus,
remoteQueryObjectFromString,
} from "@medusajs/framework/utils"
import { refetchOrder } from "./helpers"
import { AdminCreateDraftOrderType } from "./validators"
export const GET = async ( export const GET = async (
req: MedusaRequest<HttpTypes.AdminOrderFilters>, req: MedusaRequest<HttpTypes.AdminOrderFilters>,
@@ -59,7 +59,10 @@ export const POST = async (
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY) 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({ const queryObject = remoteQueryObjectFromString({
entryPoint: "region", entryPoint: "region",
variables: { variables: {
@@ -68,10 +71,13 @@ export const POST = async (
fields: ["currency_code"], fields: ["currency_code"],
}) })
const [region] = await remoteQuery(queryObject) 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({ const queryObject = remoteQueryObjectFromString({
entryPoint: "customer", entryPoint: "customer",
variables: { variables: {
@@ -80,7 +86,22 @@ export const POST = async (
fields: ["email"], fields: ["email"],
}) })
const [customer] = await remoteQuery(queryObject) 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({ const { result } = await createOrderWorkflow(req.scope).run({
@@ -64,14 +64,14 @@ const CreateDraftOrder = z
sales_channel_id: z.string().nullish(), sales_channel_id: z.string().nullish(),
email: z.string().nullish(), email: z.string().nullish(),
customer_id: z.string().nullish(), customer_id: z.string().nullish(),
billing_address: AddressPayload.optional(), billing_address: z.union([AddressPayload, z.string()]).optional(),
shipping_address: AddressPayload.optional(), shipping_address: z.union([AddressPayload, z.string()]).optional(),
items: z.array(Item).optional(), items: z.array(Item).optional(),
region_id: z.string(), region_id: z.string(),
promo_codes: z.array(z.string()).optional(), promo_codes: z.array(z.string()).optional(),
currency_code: z.string().nullish(), currency_code: z.string().nullish(),
no_notification_order: z.boolean().optional(), no_notification_order: z.boolean().optional(),
shipping_methods: z.array(ShippingMethod), shipping_methods: z.array(ShippingMethod).optional(),
metadata: z.record(z.unknown()).nullish(), metadata: z.record(z.unknown()).nullish(),
}) })
.strict() .strict()