Skip to content

Commit 1f3ab84

Browse files
committed
centralise fields
1 parent 3a81ebd commit 1f3ab84

9 files changed

Lines changed: 144 additions & 99 deletions

File tree

packages/core/core-flows/src/cart/utils/fields.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ export const completeCartFields = [
118118
"items.variant.inventory_items.inventory.location_levels.reserved_quantity",
119119
"items.variant.inventory_items.inventory.location_levels.raw_stocked_quantity",
120120
"items.variant.inventory_items.inventory.location_levels.raw_reserved_quantity",
121+
"items.variant.inventory_items.inventory.location_levels.location_id",
121122
"items.variant.inventory_items.inventory.location_levels.stock_locations.id",
122123
"items.variant.inventory_items.inventory.location_levels.stock_locations.name",
123124
"items.variant.inventory_items.inventory.location_levels.stock_locations.sales_channels.id",

packages/core/core-flows/src/cart/utils/prepare-confirm-inventory-input.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,43 @@ import {
99
deepFlatMap,
1010
} from "@medusajs/framework/utils"
1111

12+
export const requiredOrderFieldsForInventoryConfirmation = [
13+
"id",
14+
"version",
15+
"canceled_at",
16+
"sales_channel_id",
17+
"items.*",
18+
"items.variant.manage_inventory",
19+
"items.variant.allow_backorder",
20+
"items.variant.inventory_items.inventory_item_id",
21+
"items.variant.inventory_items.required_quantity",
22+
"items.variant.inventory_items.inventory.location_levels.stocked_quantity",
23+
"items.variant.inventory_items.inventory.location_levels.reserved_quantity",
24+
"items.variant.inventory_items.inventory.location_levels.raw_stocked_quantity",
25+
"items.variant.inventory_items.inventory.location_levels.raw_reserved_quantity",
26+
"items.variant.inventory_items.inventory.location_levels.location_id",
27+
"items.variant.inventory_items.inventory.location_levels.stock_locations.id",
28+
"items.variant.inventory_items.inventory.location_levels.stock_locations.name",
29+
"items.variant.inventory_items.inventory.location_levels.stock_locations.sales_channels.id",
30+
"items.variant.inventory_items.inventory.location_levels.stock_locations.sales_channels.name",
31+
]
32+
33+
export const requiredVariantFieldsForInventoryConfirmation = [
34+
"manage_inventory",
35+
"allow_backorder",
36+
"inventory_items.inventory_item_id",
37+
"inventory_items.required_quantity",
38+
"inventory_items.inventory.location_levels.stocked_quantity",
39+
"inventory_items.inventory.location_levels.reserved_quantity",
40+
"inventory_items.inventory.location_levels.raw_stocked_quantity",
41+
"inventory_items.inventory.location_levels.raw_reserved_quantity",
42+
"inventory_items.inventory.location_levels.location_id",
43+
"inventory_items.inventory.location_levels.stock_locations.id",
44+
"inventory_items.inventory.location_levels.stock_locations.name",
45+
"inventory_items.inventory.location_levels.stock_locations.sales_channels.id",
46+
"inventory_items.inventory.location_levels.stock_locations.sales_channels.name",
47+
]
48+
1249
interface ConfirmInventoryPreparationInput {
1350
product_variant_inventory_items: {
1451
variant_id: string

packages/core/core-flows/src/cart/workflows/add-to-cart.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import {
33
AddToCartWorkflowInputDTO,
44
ConfirmVariantInventoryWorkflowInputDTO,
55
} from "@medusajs/framework/types"
6-
import { CartWorkflowEvents, isDefined } from "@medusajs/framework/utils"
6+
import {
7+
CartWorkflowEvents,
8+
deduplicate,
9+
isDefined,
10+
} from "@medusajs/framework/utils"
711
import {
812
createHook,
913
createWorkflow,
@@ -28,13 +32,14 @@ import {
2832
cartFieldsForPricingContext,
2933
productVariantsFields,
3034
} from "../utils/fields"
35+
import { requiredVariantFieldsForInventoryConfirmation } from "../utils/prepare-confirm-inventory-input"
3136
import {
3237
prepareLineItemData,
3338
PrepareLineItemDataInput,
3439
} from "../utils/prepare-line-item-data"
40+
import { pricingContextResult } from "../utils/schemas"
3541
import { confirmVariantInventoryWorkflow } from "./confirm-variant-inventory"
3642
import { refreshCartItemsWorkflow } from "./refresh-cart-items"
37-
import { pricingContextResult } from "../utils/schemas"
3843

3944
const cartFields = ["completed_at"].concat(cartFieldsForPricingContext)
4045

@@ -71,23 +76,23 @@ export const addToCartWorkflowId = "add-to-cart"
7176
*
7277
* @property hooks.validate - This hook is executed before all operations. You can consume this hook to perform any custom validation. If validation fails, you can throw an error to stop the workflow execution.
7378
* @property hooks.setPricingContext - This hook is executed after the cart is retrieved and before the line items are created. You can consume this hook to return any custom context useful for the prices retrieval of the variants to be added to the cart.
74-
*
79+
*
7580
* For example, assuming you have the following custom pricing rule:
76-
*
81+
*
7782
* ```json
7883
* {
7984
* "attribute": "location_id",
8085
* "operator": "eq",
8186
* "value": "sloc_123",
8287
* }
8388
* ```
84-
*
89+
*
8590
* You can consume the `setPricingContext` hook to add the `location_id` context to the prices calculation:
86-
*
91+
*
8792
* ```ts
8893
* import { addToCartWorkflow } from "@medusajs/medusa/core-flows";
8994
* import { StepResponse } from "@medusajs/workflows-sdk";
90-
*
95+
*
9196
* addToCartWorkflow.hooks.setPricingContext((
9297
* { cart, variantIds, items, additional_data }, { container }
9398
* ) => {
@@ -96,13 +101,13 @@ export const addToCartWorkflowId = "add-to-cart"
96101
* });
97102
* });
98103
* ```
99-
*
104+
*
100105
* The variants' prices will now be retrieved using the context you return.
101-
*
106+
*
102107
* :::note
103-
*
108+
*
104109
* Learn more about prices calculation context in the [Prices Calculation](https://docs.medusajs.com/resources/commerce-modules/pricing/price-calculation) documentation.
105-
*
110+
*
106111
* :::
107112
*/
108113
export const addToCartWorkflow = createWorkflow(
@@ -163,7 +168,10 @@ export const addToCartWorkflow = createWorkflow(
163168
}).then(() => {
164169
return useRemoteQueryStep({
165170
entry_point: "variants",
166-
fields: productVariantsFields,
171+
fields: deduplicate([
172+
...productVariantsFields,
173+
...requiredVariantFieldsForInventoryConfirmation,
174+
]),
167175
variables: {
168176
id: variantIds,
169177
calculated_price: {

packages/core/core-flows/src/cart/workflows/create-carts.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
} from "@medusajs/framework/types"
55
import {
66
CartWorkflowEvents,
7+
deduplicate,
78
isDefined,
89
MedusaError,
910
} from "@medusajs/framework/utils"
@@ -25,18 +26,19 @@ import {
2526
findSalesChannelStep,
2627
} from "../steps"
2728
import { validateLineItemPricesStep } from "../steps/validate-line-item-prices"
29+
import { validateSalesChannelStep } from "../steps/validate-sales-channel"
2830
import { validateVariantPricesStep } from "../steps/validate-variant-prices"
2931
import { productVariantsFields } from "../utils/fields"
32+
import { requiredVariantFieldsForInventoryConfirmation } from "../utils/prepare-confirm-inventory-input"
3033
import {
3134
prepareLineItemData,
3235
PrepareLineItemDataInput,
3336
} from "../utils/prepare-line-item-data"
37+
import { pricingContextResult } from "../utils/schemas"
3438
import { confirmVariantInventoryWorkflow } from "./confirm-variant-inventory"
3539
import { refreshPaymentCollectionForCartWorkflow } from "./refresh-payment-collection"
3640
import { updateCartPromotionsWorkflow } from "./update-cart-promotions"
3741
import { updateTaxLinesWorkflow } from "./update-tax-lines"
38-
import { validateSalesChannelStep } from "../steps/validate-sales-channel"
39-
import { pricingContextResult } from "../utils/schemas"
4042

4143
/**
4244
* The data to create the cart, along with custom data that's passed to the workflow's hooks.
@@ -78,23 +80,23 @@ export const createCartWorkflowId = "create-cart"
7880
* @property hooks.validate - This hook is executed before all operations. You can consume this hook to perform any custom validation. If validation fails, you can throw an error to stop the workflow execution.
7981
* @property hooks.cartCreated - This hook is executed after a cart is created. You can consume this hook to perform custom actions on the created cart.
8082
* @property hooks.setPricingContext - This hook is executed after the cart is retrieved and before the line items are created. You can consume this hook to return any custom context useful for the prices retrieval of the variants to be added to the cart.
81-
*
83+
*
8284
* For example, assuming you have the following custom pricing rule:
83-
*
85+
*
8486
* ```json
8587
* {
8688
* "attribute": "location_id",
8789
* "operator": "eq",
8890
* "value": "sloc_123",
8991
* }
9092
* ```
91-
*
93+
*
9294
* You can consume the `setPricingContext` hook to add the `location_id` context to the prices calculation:
93-
*
95+
*
9496
* ```ts
9597
* import { createCartWorkflow } from "@medusajs/medusa/core-flows";
9698
* import { StepResponse } from "@medusajs/workflows-sdk";
97-
*
99+
*
98100
* createCartWorkflow.hooks.setPricingContext((
99101
* { region, variantIds, salesChannel, customerData, additional_data }, { container }
100102
* ) => {
@@ -103,13 +105,13 @@ export const createCartWorkflowId = "create-cart"
103105
* });
104106
* });
105107
* ```
106-
*
108+
*
107109
* The variants' prices will now be retrieved using the context you return.
108-
*
110+
*
109111
* :::note
110-
*
112+
*
111113
* Learn more about prices calculation context in the [Prices Calculation](https://docs.medusajs.com/resources/commerce-modules/pricing/price-calculation) documentation.
112-
*
114+
*
113115
* :::
114116
*/
115117
export const createCartWorkflow = createWorkflow(
@@ -170,7 +172,10 @@ export const createCartWorkflow = createWorkflow(
170172
}).then(() => {
171173
return useRemoteQueryStep({
172174
entry_point: "variants",
173-
fields: productVariantsFields,
175+
fields: deduplicate([
176+
...productVariantsFields,
177+
...requiredVariantFieldsForInventoryConfirmation,
178+
]),
174179
variables: {
175180
id: variantIds,
176181
calculated_price: {

packages/core/core-flows/src/cart/workflows/update-line-item-in-cart.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import {
22
AdditionalData,
33
UpdateLineItemInCartWorkflowInputDTO,
44
} from "@medusajs/framework/types"
5-
import { CartWorkflowEvents, isDefined, MedusaError } from "@medusajs/framework/utils"
5+
import {
6+
CartWorkflowEvents,
7+
deduplicate,
8+
isDefined,
9+
MedusaError,
10+
} from "@medusajs/framework/utils"
611
import {
712
createHook,
813
createWorkflow,
@@ -21,9 +26,10 @@ import {
2126
cartFieldsForPricingContext,
2227
productVariantsFields,
2328
} from "../utils/fields"
29+
import { requiredVariantFieldsForInventoryConfirmation } from "../utils/prepare-confirm-inventory-input"
30+
import { pricingContextResult } from "../utils/schemas"
2431
import { confirmVariantInventoryWorkflow } from "./confirm-variant-inventory"
2532
import { refreshCartItemsWorkflow } from "./refresh-cart-items"
26-
import { pricingContextResult } from "../utils/schemas"
2733

2834
const cartFields = cartFieldsForPricingContext.concat(["items.*"])
2935

@@ -52,23 +58,23 @@ export const updateLineItemInCartWorkflowId = "update-line-item-in-cart"
5258
*
5359
* @property hooks.validate - This hook is executed before all operations. You can consume this hook to perform any custom validation. If validation fails, you can throw an error to stop the workflow execution.
5460
* @property hooks.setPricingContext - This hook is executed before the cart is updated. You can consume this hook to return any custom context useful for the prices retrieval of the line item's variant.
55-
*
61+
*
5662
* For example, assuming you have the following custom pricing rule:
57-
*
63+
*
5864
* ```json
5965
* {
6066
* "attribute": "location_id",
6167
* "operator": "eq",
6268
* "value": "sloc_123",
6369
* }
6470
* ```
65-
*
71+
*
6672
* You can consume the `setPricingContext` hook to add the `location_id` context to the prices calculation:
67-
*
73+
*
6874
* ```ts
6975
* import { addToCartWorkflow } from "@medusajs/medusa/core-flows";
7076
* import { StepResponse } from "@medusajs/workflows-sdk";
71-
*
77+
*
7278
* addToCartWorkflow.hooks.setPricingContext((
7379
* { cart, variantIds, items, additional_data }, { container }
7480
* ) => {
@@ -77,13 +83,13 @@ export const updateLineItemInCartWorkflowId = "update-line-item-in-cart"
7783
* });
7884
* });
7985
* ```
80-
*
86+
*
8187
* The variant's prices will now be retrieved using the context you return.
82-
*
88+
*
8389
* :::note
84-
*
90+
*
8591
* Learn more about prices calculation context in the [Prices Calculation](https://docs.medusajs.com/resources/commerce-modules/pricing/price-calculation) documentation.
86-
*
92+
*
8793
* :::
8894
*/
8995
export const updateLineItemInCartWorkflow = createWorkflow(
@@ -148,7 +154,10 @@ export const updateLineItemInCartWorkflow = createWorkflow(
148154
}).then(() => {
149155
return useRemoteQueryStep({
150156
entry_point: "variants",
151-
fields: productVariantsFields,
157+
fields: deduplicate([
158+
...productVariantsFields,
159+
...requiredVariantFieldsForInventoryConfirmation,
160+
]),
152161
variables: {
153162
id: variantIds,
154163
calculated_price: {

packages/core/core-flows/src/draft-order/workflows/confirm-draft-order-edit.ts

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import {
1010
} from "@medusajs/framework/workflows-sdk"
1111
import { BigNumberInput, OrderChangeDTO, OrderDTO } from "@medusajs/types"
1212
import { reserveInventoryStep } from "../../cart"
13-
import { prepareConfirmInventoryInput } from "../../cart/utils/prepare-confirm-inventory-input"
13+
import {
14+
prepareConfirmInventoryInput,
15+
requiredOrderFieldsForInventoryConfirmation,
16+
} from "../../cart/utils/prepare-confirm-inventory-input"
1417
import { useRemoteQueryStep } from "../../common"
1518
import {
1619
createOrUpdateOrderPaymentCollectionWorkflow,
@@ -114,24 +117,7 @@ export const confirmDraftOrderEditWorkflow = createWorkflow(
114117

115118
const orderItems = useRemoteQueryStep({
116119
entry_point: "order",
117-
fields: [
118-
"id",
119-
"version",
120-
"canceled_at",
121-
"sales_channel_id",
122-
"items.*",
123-
"items.variant.manage_inventory",
124-
"items.variant.allow_backorder",
125-
"items.variant.inventory_items.inventory_item_id",
126-
"items.variant.inventory_items.required_quantity",
127-
"items.variant.inventory_items.inventory.location_levels.stocked_quantity",
128-
"items.variant.inventory_items.inventory.location_levels.reserved_quantity",
129-
"items.variant.inventory_items.inventory.location_levels.location_id",
130-
"items.variant.inventory_items.inventory.location_levels.stock_locations.id",
131-
"items.variant.inventory_items.inventory.location_levels.stock_locations.name",
132-
"items.variant.inventory_items.inventory.location_levels.stock_locations.sales_channels.id",
133-
"items.variant.inventory_items.inventory.location_levels.stock_locations.sales_channels.name",
134-
],
120+
fields: requiredOrderFieldsForInventoryConfirmation,
135121
variables: { id: input.order_id },
136122
list: false,
137123
throw_if_key_not_found: true,

0 commit comments

Comments
 (0)