Skip to content

Commit f07af7b

Browse files
fix(dashboard,core-flows,types,medusa): Allow editing Order metadata (medusajs#11285)
Resolves SUP-780
1 parent 1185878 commit f07af7b

File tree

11 files changed

+84
-22
lines changed

11 files changed

+84
-22
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@medusajs/dashboard": patch
3+
"@medusajs/core-flows": patch
4+
"@medusajs/types": patch
5+
"@medusajs/medusa": patch
6+
---
7+
8+
fix(dashboard,core-flows,types,medusa): Allow editing Order metadata

packages/admin/dashboard/src/providers/router-provider/route-map.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,10 @@ export const RouteMap: RouteObject[] = [
355355
lazy: () =>
356356
import("../../routes/orders/order-edit-billing-address"),
357357
},
358+
{
359+
path: "metadata/edit",
360+
lazy: () => import("../../routes/orders/order-metadata"),
361+
},
358362
],
359363
},
360364
],

packages/admin/dashboard/src/routes/orders/order-detail/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const DEFAULT_PROPERTIES = [
66
"email",
77
"display_id",
88
"currency_code",
9+
"metadata",
910
// --- TOTALS ---
1011
"total",
1112
"item_total",

packages/admin/dashboard/src/routes/orders/order-detail/order-detail.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export const OrderDetail = () => {
7272
}}
7373
data={order}
7474
showJSON
75+
showMetadata
7576
hasOutlet
7677
>
7778
<TwoColumnPage.Main>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { OrderMetadata as Component } from "./order-metadata"
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { useParams } from "react-router-dom"
2+
import { MetadataForm } from "../../../components/forms/metadata-form/metadata-form"
3+
import { useOrder, useUpdateOrder } from "../../../hooks/api"
4+
5+
export const OrderMetadata = () => {
6+
const { id } = useParams()
7+
8+
const { order, isPending, isError, error } = useOrder(id!, {
9+
fields: "id,metadata",
10+
})
11+
12+
const { mutateAsync, isPending: isMutating } = useUpdateOrder(order?.id!)
13+
14+
if (isError) {
15+
throw error
16+
}
17+
18+
return (
19+
<MetadataForm
20+
metadata={order?.metadata}
21+
hook={mutateAsync}
22+
isPending={isPending}
23+
isMutating={isMutating}
24+
/>
25+
)
26+
}

packages/core/core-flows/src/order/workflows/update-order.ts

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
import { OrderDTO, OrderWorkflow } from "@medusajs/framework/types"
2+
import {
3+
MedusaError,
4+
OrderWorkflowEvents,
5+
validateEmail,
6+
} from "@medusajs/framework/utils"
27
import {
38
WorkflowData,
49
WorkflowResponse,
@@ -11,19 +16,14 @@ import {
1116
RegisterOrderChangeDTO,
1217
UpdateOrderDTO,
1318
} from "@medusajs/types"
14-
import {
15-
MedusaError,
16-
OrderWorkflowEvents,
17-
validateEmail,
18-
} from "@medusajs/framework/utils"
1919

20-
import { throwIfOrderIsCancelled } from "../utils/order-validation"
20+
import { emitEventStep, useQueryGraphStep } from "../../common"
2121
import {
2222
previewOrderChangeStep,
2323
registerOrderChangesStep,
2424
updateOrdersStep,
2525
} from "../steps"
26-
import { emitEventStep, useQueryGraphStep } from "../../common"
26+
import { throwIfOrderIsCancelled } from "../utils/order-validation"
2727

2828
/**
2929
* The data to validate the order update.
@@ -42,14 +42,14 @@ export type UpdateOrderValidationStepInput = {
4242
/**
4343
* This step validates that an order can be updated with provided input. If the order is cancelled,
4444
* the email is invalid, or the country code is being changed in the shipping or billing addresses, the step will throw an error.
45-
*
45+
*
4646
* :::note
47-
*
47+
*
4848
* You can retrieve an order's details using [Query](https://docs.medusajs.com/learn/fundamentals/module-links/query),
4949
* or [useQueryGraphStep](https://docs.medusajs.com/resources/references/medusa-workflows/steps/useQueryGraphStep).
50-
*
50+
*
5151
* :::
52-
*
52+
*
5353
* @example
5454
* const data = updateOrderValidationStep({
5555
* order: {
@@ -64,10 +64,7 @@ export type UpdateOrderValidationStepInput = {
6464
*/
6565
export const updateOrderValidationStep = createStep(
6666
"update-order-validation",
67-
async function ({
68-
order,
69-
input,
70-
}: UpdateOrderValidationStepInput) {
67+
async function ({ order, input }: UpdateOrderValidationStepInput) {
7168
throwIfOrderIsCancelled({ order })
7269

7370
if (
@@ -100,12 +97,12 @@ export const updateOrderValidationStep = createStep(
10097

10198
export const updateOrderWorkflowId = "update-order-workflow"
10299
/**
103-
* This workflow updates an order's general details, such as its email or addresses. It's used by the
100+
* This workflow updates an order's general details, such as its email or addresses. It's used by the
104101
* [Update Order Admin API Route](https://docs.medusajs.com/api/admin#orders_postordersid).
105-
*
102+
*
106103
* You can use this workflow within your customizations or your own custom workflows, allowing you to update an
107104
* order's details in your custom flows.
108-
*
105+
*
109106
* @example
110107
* const { result } = await updateOrderWorkflow(container)
111108
* .run({
@@ -115,9 +112,9 @@ export const updateOrderWorkflowId = "update-order-workflow"
115112
* email: "example@gmail.com",
116113
* }
117114
* })
118-
*
115+
*
119116
* @summary
120-
*
117+
*
121118
* Update an order's details.
122119
*/
123120
export const updateOrderWorkflow = createWorkflow(
@@ -133,6 +130,7 @@ export const updateOrderWorkflow = createWorkflow(
133130
"email",
134131
"shipping_address.*",
135132
"billing_address.*",
133+
"metadata",
136134
],
137135
filters: { id: input.id },
138136
options: { throwIfKeyNotFound: true },
@@ -223,6 +221,20 @@ export const updateOrderWorkflow = createWorkflow(
223221
})
224222
}
225223

224+
if (input.metadata !== undefined) {
225+
changes.push({
226+
change_type: "update_order" as const,
227+
order_id: input.id,
228+
created_by: input.user_id,
229+
confirmed_by: input.user_id,
230+
details: {
231+
type: "metadata",
232+
old: order.metadata,
233+
new: input.metadata,
234+
},
235+
})
236+
}
237+
226238
return changes
227239
}
228240
)

packages/core/types/src/http/order/admin/payload.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ export interface AdminUpdateOrder {
1111
* The order's billing address.
1212
*/
1313
billing_address?: OrderAddress
14+
/**
15+
* The order's metadata.
16+
*/
17+
metadata?: Record<string, unknown> | null
1418
}
1519

1620
export interface AdminCreateOrderFulfillment {

packages/core/types/src/workflow/order/update-order.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ export type UpdateOrderWorkflowInput = {
2626
* The new email of the order.
2727
*/
2828
email?: string
29+
/**
30+
* The new metadata of the order.
31+
*/
32+
metadata?: Record<string, unknown> | null
2933
}
3034

3135
export type UpdateOrderShippingAddressWorkflowInput = {

packages/medusa/src/api/admin/orders/[id]/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import {
77
MedusaResponse,
88
} from "@medusajs/framework/http"
99
import { AdminOrder, HttpTypes } from "@medusajs/framework/types"
10+
import { ContainerRegistrationKeys } from "@medusajs/framework/utils"
1011
import {
1112
AdminGetOrdersOrderParamsType,
1213
AdminUpdateOrderType,
1314
} from "../validators"
14-
import { ContainerRegistrationKeys } from "@medusajs/framework/utils"
1515

1616
export const GET = async (
1717
req: AuthenticatedMedusaRequest<AdminGetOrdersOrderParamsType>,

0 commit comments

Comments
 (0)