fix(dashboard,core-flows,types,medusa): Allow editing Order metadata (#11285)

Resolves SUP-780
This commit is contained in:
Kasper Fabricius Kristensen
2025-02-04 11:08:20 +01:00
committed by GitHub
parent 1185878ecd
commit f07af7b93c
11 changed files with 84 additions and 22 deletions

View File

@@ -0,0 +1,8 @@
---
"@medusajs/dashboard": patch
"@medusajs/core-flows": patch
"@medusajs/types": patch
"@medusajs/medusa": patch
---
fix(dashboard,core-flows,types,medusa): Allow editing Order metadata

View File

@@ -355,6 +355,10 @@ export const RouteMap: RouteObject[] = [
lazy: () =>
import("../../routes/orders/order-edit-billing-address"),
},
{
path: "metadata/edit",
lazy: () => import("../../routes/orders/order-metadata"),
},
],
},
],

View File

@@ -6,6 +6,7 @@ const DEFAULT_PROPERTIES = [
"email",
"display_id",
"currency_code",
"metadata",
// --- TOTALS ---
"total",
"item_total",

View File

@@ -72,6 +72,7 @@ export const OrderDetail = () => {
}}
data={order}
showJSON
showMetadata
hasOutlet
>
<TwoColumnPage.Main>

View File

@@ -0,0 +1 @@
export { OrderMetadata as Component } from "./order-metadata"

View File

@@ -0,0 +1,26 @@
import { useParams } from "react-router-dom"
import { MetadataForm } from "../../../components/forms/metadata-form/metadata-form"
import { useOrder, useUpdateOrder } from "../../../hooks/api"
export const OrderMetadata = () => {
const { id } = useParams()
const { order, isPending, isError, error } = useOrder(id!, {
fields: "id,metadata",
})
const { mutateAsync, isPending: isMutating } = useUpdateOrder(order?.id!)
if (isError) {
throw error
}
return (
<MetadataForm
metadata={order?.metadata}
hook={mutateAsync}
isPending={isPending}
isMutating={isMutating}
/>
)
}

View File

@@ -1,4 +1,9 @@
import { OrderDTO, OrderWorkflow } from "@medusajs/framework/types"
import {
MedusaError,
OrderWorkflowEvents,
validateEmail,
} from "@medusajs/framework/utils"
import {
WorkflowData,
WorkflowResponse,
@@ -11,19 +16,14 @@ import {
RegisterOrderChangeDTO,
UpdateOrderDTO,
} from "@medusajs/types"
import {
MedusaError,
OrderWorkflowEvents,
validateEmail,
} from "@medusajs/framework/utils"
import { throwIfOrderIsCancelled } from "../utils/order-validation"
import { emitEventStep, useQueryGraphStep } from "../../common"
import {
previewOrderChangeStep,
registerOrderChangesStep,
updateOrdersStep,
} from "../steps"
import { emitEventStep, useQueryGraphStep } from "../../common"
import { throwIfOrderIsCancelled } from "../utils/order-validation"
/**
* The data to validate the order update.
@@ -42,14 +42,14 @@ export type UpdateOrderValidationStepInput = {
/**
* This step validates that an order can be updated with provided input. If the order is cancelled,
* the email is invalid, or the country code is being changed in the shipping or billing addresses, the step will throw an error.
*
*
* :::note
*
*
* You can retrieve an order's details using [Query](https://docs.medusajs.com/learn/fundamentals/module-links/query),
* or [useQueryGraphStep](https://docs.medusajs.com/resources/references/medusa-workflows/steps/useQueryGraphStep).
*
*
* :::
*
*
* @example
* const data = updateOrderValidationStep({
* order: {
@@ -64,10 +64,7 @@ export type UpdateOrderValidationStepInput = {
*/
export const updateOrderValidationStep = createStep(
"update-order-validation",
async function ({
order,
input,
}: UpdateOrderValidationStepInput) {
async function ({ order, input }: UpdateOrderValidationStepInput) {
throwIfOrderIsCancelled({ order })
if (
@@ -100,12 +97,12 @@ export const updateOrderValidationStep = createStep(
export const updateOrderWorkflowId = "update-order-workflow"
/**
* This workflow updates an order's general details, such as its email or addresses. It's used by the
* This workflow updates an order's general details, such as its email or addresses. It's used by the
* [Update Order Admin API Route](https://docs.medusajs.com/api/admin#orders_postordersid).
*
*
* You can use this workflow within your customizations or your own custom workflows, allowing you to update an
* order's details in your custom flows.
*
*
* @example
* const { result } = await updateOrderWorkflow(container)
* .run({
@@ -115,9 +112,9 @@ export const updateOrderWorkflowId = "update-order-workflow"
* email: "example@gmail.com",
* }
* })
*
*
* @summary
*
*
* Update an order's details.
*/
export const updateOrderWorkflow = createWorkflow(
@@ -133,6 +130,7 @@ export const updateOrderWorkflow = createWorkflow(
"email",
"shipping_address.*",
"billing_address.*",
"metadata",
],
filters: { id: input.id },
options: { throwIfKeyNotFound: true },
@@ -223,6 +221,20 @@ export const updateOrderWorkflow = createWorkflow(
})
}
if (input.metadata !== undefined) {
changes.push({
change_type: "update_order" as const,
order_id: input.id,
created_by: input.user_id,
confirmed_by: input.user_id,
details: {
type: "metadata",
old: order.metadata,
new: input.metadata,
},
})
}
return changes
}
)

View File

@@ -11,6 +11,10 @@ export interface AdminUpdateOrder {
* The order's billing address.
*/
billing_address?: OrderAddress
/**
* The order's metadata.
*/
metadata?: Record<string, unknown> | null
}
export interface AdminCreateOrderFulfillment {

View File

@@ -26,6 +26,10 @@ export type UpdateOrderWorkflowInput = {
* The new email of the order.
*/
email?: string
/**
* The new metadata of the order.
*/
metadata?: Record<string, unknown> | null
}
export type UpdateOrderShippingAddressWorkflowInput = {

View File

@@ -7,11 +7,11 @@ import {
MedusaResponse,
} from "@medusajs/framework/http"
import { AdminOrder, HttpTypes } from "@medusajs/framework/types"
import { ContainerRegistrationKeys } from "@medusajs/framework/utils"
import {
AdminGetOrdersOrderParamsType,
AdminUpdateOrderType,
} from "../validators"
import { ContainerRegistrationKeys } from "@medusajs/framework/utils"
export const GET = async (
req: AuthenticatedMedusaRequest<AdminGetOrdersOrderParamsType>,

View File

@@ -1,11 +1,11 @@
import { z } from "zod"
import { AddressPayload } from "../../utils/common-validators"
import {
createFindParams,
createOperatorMap,
createSelectParams,
WithAdditionalData,
} from "../../utils/validators"
import { AddressPayload } from "../../utils/common-validators"
export const AdminGetOrdersOrderParams = createSelectParams().merge(
z.object({
@@ -144,4 +144,5 @@ export const AdminUpdateOrder = z.object({
email: z.string().optional(),
shipping_address: AddressPayload.optional(),
billing_address: AddressPayload.optional(),
metadata: z.record(z.unknown()).nullish(),
})