Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/little-geese-wave.md
Original file line number Diff line number Diff line change
@@ -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.
43 changes: 32 additions & 11 deletions packages/medusa/src/api/admin/draft-orders/route.ts
Original file line number Diff line number Diff line change
@@ -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<HttpTypes.AdminOrderFilters>,
Expand Down Expand Up @@ -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: {
Expand All @@ -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: {
Expand All @@ -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({
Expand Down
6 changes: 3 additions & 3 deletions packages/medusa/src/api/admin/draft-orders/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading