Skip to content

Commit 924564b

Browse files
authored
fix(core-flows): customer id filter not working in getOrderDetails (medusajs#13695)
As discussed, fixed the get order detail and also changed the remotequery to graph a bunch of workflows
1 parent 82f3b04 commit 924564b

17 files changed

+258
-183
lines changed

.changeset/little-falcons-count.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@medusajs/core-flows": patch
3+
---
4+
5+
fix(core-flows): customer id filter not working in getOrderDetails
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { getOrderDetailWorkflow } from "@medusajs/core-flows"
2+
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
3+
import { OrderDTO } from "@medusajs/types"
4+
import { createOrderFixture, prepareDataFixtures } from "./__fixtures__"
5+
6+
jest.setTimeout(50000)
7+
8+
medusaIntegrationTestRunner({
9+
env: {},
10+
testSuite: ({ getContainer }) => {
11+
let container
12+
13+
beforeAll(() => {
14+
container = getContainer()
15+
})
16+
17+
describe("Get order detail workflow", () => {
18+
let order: OrderDTO
19+
20+
describe("createOrderChangeWorkflow", () => {
21+
beforeEach(async () => {
22+
const fixtures = await prepareDataFixtures({
23+
container,
24+
})
25+
26+
order = await createOrderFixture({
27+
container,
28+
product: fixtures.product,
29+
location: fixtures.location,
30+
inventoryItem: fixtures.inventoryItem,
31+
})
32+
})
33+
34+
it("should get an order based on filters", async () => {
35+
const response = await getOrderDetailWorkflow(container).run({
36+
input: {
37+
fields: [],
38+
filters: {
39+
customer_id: order.customer_id ?? "",
40+
},
41+
order_id: order.id,
42+
},
43+
throwOnError: false,
44+
})
45+
46+
expect(response).toBeDefined()
47+
})
48+
49+
it("should throw an error when getting order if none is found with the provided customer id", async () => {
50+
const {
51+
errors: [error],
52+
} = await getOrderDetailWorkflow(container).run({
53+
input: {
54+
fields: [],
55+
filters: {
56+
customer_id: "wrong-id",
57+
},
58+
order_id: order.id,
59+
},
60+
throwOnError: false,
61+
})
62+
63+
expect(error.error).toEqual(
64+
expect.objectContaining({
65+
message: `Order id not found: ${order.id}`,
66+
})
67+
)
68+
})
69+
})
70+
})
71+
},
72+
})

packages/core/core-flows/src/order/workflows/add-line-items.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { requiredVariantFieldsForInventoryConfirmation } from "../../cart/utils/
2121
import { pricingContextResult } from "../../cart/utils/schemas"
2222
import { confirmVariantInventoryWorkflow } from "../../cart/workflows/confirm-variant-inventory"
2323
import { getVariantsAndItemsWithPrices } from "../../cart/workflows/get-variants-and-items-with-prices"
24-
import { useRemoteQueryStep } from "../../common"
24+
import { useQueryGraphStep } from "../../common"
2525
import { createOrderLineItemsStep } from "../steps"
2626
import { productVariantsFields } from "../utils/fields"
2727

@@ -98,8 +98,9 @@ export const addOrderLineItemsWorkflow = createWorkflow(
9898
OrderWorkflow.OrderAddLineItemWorkflowInput & AdditionalData
9999
>
100100
) => {
101-
const order = useRemoteQueryStep({
102-
entry_point: "orders",
101+
const { data: order } = useQueryGraphStep({
102+
entity: "order",
103+
filters: { id: input.order_id },
103104
fields: [
104105
"id",
105106
"sales_channel_id",
@@ -108,9 +109,7 @@ export const addOrderLineItemsWorkflow = createWorkflow(
108109
"email",
109110
"currency_code",
110111
],
111-
variables: { id: input.order_id },
112-
list: false,
113-
throw_if_key_not_found: true,
112+
options: { throwIfKeyNotFound: true, isList: false },
114113
}).config({ name: "order-query" })
115114

116115
const variantIds = transform({ input }, (data) => {

packages/core/core-flows/src/order/workflows/cancel-order-fulfillment.ts

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,30 @@ import {
1616
OrderWorkflowEvents,
1717
} from "@medusajs/framework/utils"
1818
import {
19-
WorkflowData,
20-
WorkflowResponse,
2119
createHook,
2220
createStep,
2321
createWorkflow,
2422
parallelize,
2523
transform,
24+
WorkflowData,
25+
WorkflowResponse,
2626
} from "@medusajs/framework/workflows-sdk"
27-
import { emitEventStep, useRemoteQueryStep } from "../../common"
27+
import {
28+
emitEventStep,
29+
useQueryGraphStep,
30+
useRemoteQueryStep,
31+
} from "../../common"
2832
import { cancelFulfillmentWorkflow } from "../../fulfillment"
2933
import { adjustInventoryLevelsStep } from "../../inventory"
3034
import { cancelOrderFulfillmentStep } from "../steps/cancel-fulfillment"
3135
import {
3236
throwIfItemsDoesNotExistsInOrder,
3337
throwIfOrderIsCancelled,
3438
} from "../utils/order-validation"
35-
import { createReservationsStep } from "../../reservation"
36-
import { updateReservationsStep } from "../../reservation"
39+
import {
40+
createReservationsStep,
41+
updateReservationsStep,
42+
} from "../../reservation"
3743

3844
type OrderItemWithVariantDTO = OrderLineItemDTO & {
3945
variant?: ProductVariantDTO & {
@@ -307,31 +313,29 @@ export const cancelOrderFulfillmentWorkflowId = "cancel-order-fulfillment"
307313
export const cancelOrderFulfillmentWorkflow = createWorkflow(
308314
cancelOrderFulfillmentWorkflowId,
309315
(input: WorkflowData<CancelOrderFulfillmentWorkflowInput>) => {
310-
const order: OrderDTO & { fulfillments: FulfillmentDTO[] } =
311-
useRemoteQueryStep({
312-
entry_point: "orders",
313-
fields: [
314-
"id",
315-
"status",
316-
"items.id",
317-
"items.quantity",
318-
"items.variant.allow_backorder",
319-
"items.variant.manage_inventory",
320-
"items.variant.inventory_items.inventory.id",
321-
"items.variant.inventory_items.required_quantity",
322-
"fulfillments.id",
323-
"fulfillments.canceled_at",
324-
"fulfillments.shipped_at",
325-
"fulfillments.location_id",
326-
"fulfillments.items.id",
327-
"fulfillments.items.quantity",
328-
"fulfillments.items.line_item_id",
329-
"fulfillments.items.inventory_item_id",
330-
],
331-
variables: { id: input.order_id },
332-
list: false,
333-
throw_if_key_not_found: true,
334-
})
316+
const { data: order } = useQueryGraphStep({
317+
entity: "order",
318+
filters: { id: input.order_id },
319+
fields: [
320+
"id",
321+
"status",
322+
"items.id",
323+
"items.quantity",
324+
"items.variant.allow_backorder",
325+
"items.variant.manage_inventory",
326+
"items.variant.inventory_items.inventory.id",
327+
"items.variant.inventory_items.required_quantity",
328+
"fulfillments.id",
329+
"fulfillments.canceled_at",
330+
"fulfillments.shipped_at",
331+
"fulfillments.location_id",
332+
"fulfillments.items.id",
333+
"fulfillments.items.quantity",
334+
"fulfillments.items.line_item_id",
335+
"fulfillments.items.inventory_item_id",
336+
],
337+
options: { throwIfKeyNotFound: true, isList: false },
338+
}).config({ name: "get-order" })
335339

336340
cancelOrderFulfillmentValidateOrder({ order, input })
337341

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

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@ import {
55
PaymentCollectionDTO,
66
} from "@medusajs/framework/types"
77
import {
8+
deepFlatMap,
89
MathBN,
910
MedusaError,
1011
OrderWorkflowEvents,
1112
PaymentCollectionStatus,
12-
deepFlatMap,
1313
} from "@medusajs/framework/utils"
1414
import {
15-
WorkflowData,
16-
WorkflowResponse,
1715
createHook,
1816
createStep,
1917
createWorkflow,
2018
parallelize,
2119
transform,
2220
when,
21+
WorkflowData,
22+
WorkflowResponse,
2323
} from "@medusajs/framework/workflows-sdk"
2424
import { emitEventStep, useQueryGraphStep } from "../../common"
2525
import { updatePaymentCollectionStep } from "../../payment-collection"
@@ -47,14 +47,14 @@ export type CancelValidateOrderStepInput = {
4747
/**
4848
* This step validates that an order can be canceled. If the order has fulfillments that
4949
* aren't canceled, or the order was canceled previously, the step throws an error.
50-
*
50+
*
5151
* :::note
52-
*
52+
*
5353
* You can retrieve an order's details using [Query](https://docs.medusajs.com/learn/fundamentals/module-links/query),
5454
* or [useQueryGraphStep](https://docs.medusajs.com/resources/references/medusa-workflows/steps/useQueryGraphStep).
55-
*
55+
*
5656
* :::
57-
*
57+
*
5858
* @example
5959
* const data = cancelValidateOrder({
6060
* order: {
@@ -68,9 +68,7 @@ export type CancelValidateOrderStepInput = {
6868
*/
6969
export const cancelValidateOrder = createStep(
7070
"cancel-validate-order",
71-
({
72-
order,
73-
}: CancelValidateOrderStepInput) => {
71+
({ order }: CancelValidateOrderStepInput) => {
7472
const order_ = order as OrderDTO & {
7573
payment_collections: PaymentCollectionDTO[]
7674
fulfillments: FulfillmentDTO[]
@@ -102,33 +100,33 @@ export const cancelOrderWorkflowId = "cancel-order"
102100
* This workflow cancels an order. An order can only be canceled if it doesn't have
103101
* any fulfillments, or if all fulfillments are canceled. The workflow will also cancel
104102
* any uncaptured payments, and refund any captured payments.
105-
*
103+
*
106104
* This workflow is used by the [Cancel Order Admin API Route](https://docs.medusajs.com/api/admin#orders_postordersidcancel).
107-
*
108-
* This workflow has a hook that allows you to perform custom actions on the canceled order. For example, you can
105+
*
106+
* This workflow has a hook that allows you to perform custom actions on the canceled order. For example, you can
109107
* make changes to custom models linked to the order.
110-
*
108+
*
111109
* You can also use this workflow within your customizations or your own custom workflows, allowing you to wrap custom logic around canceling an order.
112-
*
110+
*
113111
* @example
114112
* const { result } = await cancelOrderWorkflow(container)
115113
* .run({
116114
* input: {
117115
* order_id: "order_123",
118116
* }
119117
* })
120-
*
118+
*
121119
* @summary
122-
*
120+
*
123121
* Cancel an order.
124-
*
122+
*
125123
* @property hooks.orderCanceled - This hook is executed after the order is canceled. You can consume this hook to perform custom actions on the canceled order.
126124
*/
127125
export const cancelOrderWorkflow = createWorkflow(
128126
cancelOrderWorkflowId,
129127
(input: WorkflowData<OrderWorkflow.CancelOrderWorkflowInput>) => {
130128
const orderQuery = useQueryGraphStep({
131-
entity: "orders",
129+
entity: "order",
132130
fields: [
133131
"id",
134132
"status",
@@ -143,7 +141,7 @@ export const cancelOrderWorkflow = createWorkflow(
143141
],
144142
filters: { id: input.order_id },
145143
options: { throwIfKeyNotFound: true },
146-
}).config({ name: "get-cart" })
144+
}).config({ name: "get-order" })
147145

148146
const order = transform(
149147
{ orderQuery },

packages/core/core-flows/src/order/workflows/create-fulfillment.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,18 @@ import {
1818
OrderWorkflowEvents,
1919
} from "@medusajs/framework/utils"
2020
import {
21-
WorkflowData,
22-
WorkflowResponse,
2321
createHook,
2422
createStep,
2523
createWorkflow,
2624
parallelize,
2725
transform,
26+
WorkflowData,
27+
WorkflowResponse,
2828
} from "@medusajs/framework/workflows-sdk"
2929
import {
3030
createRemoteLinkStep,
3131
emitEventStep,
32+
useQueryGraphStep,
3233
useRemoteQueryStep,
3334
} from "../../common"
3435
import { createFulfillmentWorkflow } from "../../fulfillment"
@@ -393,8 +394,9 @@ export const createOrderFulfillmentWorkflowId = "create-order-fulfillment"
393394
export const createOrderFulfillmentWorkflow = createWorkflow(
394395
createOrderFulfillmentWorkflowId,
395396
(input: WorkflowData<CreateOrderFulfillmentWorkflowInput>) => {
396-
const order: OrderDTO = useRemoteQueryStep({
397-
entry_point: "orders",
397+
const { data: order } = useQueryGraphStep({
398+
entity: "order",
399+
filters: { id: input.order_id },
398400
fields: [
399401
"id",
400402
"display_id",
@@ -444,10 +446,8 @@ export const createOrderFulfillmentWorkflow = createWorkflow(
444446
"shipping_methods.data",
445447
"shipping_methods.amount",
446448
],
447-
variables: { id: input.order_id },
448-
list: false,
449-
throw_if_key_not_found: true,
450-
})
449+
options: { throwIfKeyNotFound: true, isList: false },
450+
}).config({ name: "get-order" })
451451

452452
createFulfillmentValidateOrder({ order, inputItems: input.items })
453453

0 commit comments

Comments
 (0)