feat: Set pricing context hook (#11891)
This commit is contained in:
1 parent
350b6b9a71
commit
b1b3b48474
18 files changed
+1451
-62
No files matched your search
@@ -0,0 +1,7 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/core-flows": patch
|
||||||
|
"@medusajs/workflows-sdk": patch
|
||||||
|
"integration-tests-modules": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
feat: Set pricing context hook
|
||||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,2 @@
|
|||||||
|
import z from "zod"
|
||||||
|
export const pricingContextResult = z.record(z.string(), z.any()).optional()
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
|
AdditionalData,
|
||||||
AddToCartWorkflowInputDTO,
|
AddToCartWorkflowInputDTO,
|
||||||
ConfirmVariantInventoryWorkflowInputDTO,
|
ConfirmVariantInventoryWorkflowInputDTO,
|
||||||
} from "@medusajs/framework/types"
|
} from "@medusajs/framework/types"
|
||||||
@@ -33,6 +34,7 @@ import {
|
|||||||
} from "../utils/prepare-line-item-data"
|
} from "../utils/prepare-line-item-data"
|
||||||
import { confirmVariantInventoryWorkflow } from "./confirm-variant-inventory"
|
import { confirmVariantInventoryWorkflow } from "./confirm-variant-inventory"
|
||||||
import { refreshCartItemsWorkflow } from "./refresh-cart-items"
|
import { refreshCartItemsWorkflow } from "./refresh-cart-items"
|
||||||
|
import { pricingContextResult } from "../utils/schemas"
|
||||||
|
|
||||||
const cartFields = ["completed_at"].concat(cartFieldsForPricingContext)
|
const cartFields = ["completed_at"].concat(cartFieldsForPricingContext)
|
||||||
|
|
||||||
@@ -71,7 +73,7 @@ export const addToCartWorkflowId = "add-to-cart"
|
|||||||
*/
|
*/
|
||||||
export const addToCartWorkflow = createWorkflow(
|
export const addToCartWorkflow = createWorkflow(
|
||||||
addToCartWorkflowId,
|
addToCartWorkflowId,
|
||||||
(input: WorkflowData<AddToCartWorkflowInputDTO>) => {
|
(input: WorkflowData<AddToCartWorkflowInputDTO & AdditionalData>) => {
|
||||||
const cartQuery = useQueryGraphStep({
|
const cartQuery = useQueryGraphStep({
|
||||||
entity: "cart",
|
entity: "cart",
|
||||||
filters: { id: input.cart_id },
|
filters: { id: input.cart_id },
|
||||||
@@ -93,6 +95,35 @@ export const addToCartWorkflow = createWorkflow(
|
|||||||
return (data.input.items ?? []).map((i) => i.variant_id).filter(Boolean)
|
return (data.input.items ?? []).map((i) => i.variant_id).filter(Boolean)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const setPricingContext = createHook(
|
||||||
|
"setPricingContext",
|
||||||
|
{
|
||||||
|
cart,
|
||||||
|
variantIds,
|
||||||
|
items: input.items,
|
||||||
|
additional_data: input.additional_data,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
resultValidator: pricingContextResult,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
const setPricingContextResult = setPricingContext.getResult()
|
||||||
|
const pricingContext = transform(
|
||||||
|
{ cart, setPricingContextResult },
|
||||||
|
(data) => {
|
||||||
|
return {
|
||||||
|
...data.cart,
|
||||||
|
...(data.setPricingContextResult ? data.setPricingContextResult : {}),
|
||||||
|
currency_code: data.cart.currency_code,
|
||||||
|
region_id: data.cart.region_id,
|
||||||
|
region: data.cart.region,
|
||||||
|
customer_id: data.cart.customer_id,
|
||||||
|
customer: data.cart.customer,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
const variants = when({ variantIds }, ({ variantIds }) => {
|
const variants = when({ variantIds }, ({ variantIds }) => {
|
||||||
return !!variantIds.length
|
return !!variantIds.length
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
@@ -102,7 +133,7 @@ export const addToCartWorkflow = createWorkflow(
|
|||||||
variables: {
|
variables: {
|
||||||
id: variantIds,
|
id: variantIds,
|
||||||
calculated_price: {
|
calculated_price: {
|
||||||
context: cart,
|
context: pricingContext,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@@ -201,7 +232,7 @@ export const addToCartWorkflow = createWorkflow(
|
|||||||
})
|
})
|
||||||
|
|
||||||
return new WorkflowResponse(void 0, {
|
return new WorkflowResponse(void 0, {
|
||||||
hooks: [validate],
|
hooks: [validate, setPricingContext] as const,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@@ -36,6 +36,7 @@ import { refreshPaymentCollectionForCartWorkflow } from "./refresh-payment-colle
|
|||||||
import { updateCartPromotionsWorkflow } from "./update-cart-promotions"
|
import { updateCartPromotionsWorkflow } from "./update-cart-promotions"
|
||||||
import { updateTaxLinesWorkflow } from "./update-tax-lines"
|
import { updateTaxLinesWorkflow } from "./update-tax-lines"
|
||||||
import { validateSalesChannelStep } from "../steps/validate-sales-channel"
|
import { validateSalesChannelStep } from "../steps/validate-sales-channel"
|
||||||
|
import { pricingContextResult } from "../utils/schemas"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The data to create the cart, along with custom data that's passed to the workflow's hooks.
|
* The data to create the cart, along with custom data that's passed to the workflow's hooks.
|
||||||
@@ -98,16 +99,31 @@ export const createCartWorkflow = createWorkflow(
|
|||||||
)
|
)
|
||||||
|
|
||||||
validateSalesChannelStep({ salesChannel })
|
validateSalesChannelStep({ salesChannel })
|
||||||
|
const setPricingContext = createHook(
|
||||||
|
"setPricingContext",
|
||||||
|
{
|
||||||
|
region,
|
||||||
|
variantIds,
|
||||||
|
salesChannel,
|
||||||
|
customerData,
|
||||||
|
additional_data: input.additional_data,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
resultValidator: pricingContextResult,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
const setPricingContextResult = setPricingContext.getResult()
|
||||||
|
|
||||||
// TODO: This is on par with the context used in v1.*, but we can be more flexible.
|
// TODO: This is on par with the context used in v1.*, but we can be more flexible.
|
||||||
const pricingContext = transform(
|
const pricingContext = transform(
|
||||||
{ input, region, customerData },
|
{ input, region, customerData, setPricingContextResult },
|
||||||
(data) => {
|
(data) => {
|
||||||
if (!data.region) {
|
if (!data.region) {
|
||||||
throw new MedusaError(MedusaError.Types.NOT_FOUND, "No regions found")
|
throw new MedusaError(MedusaError.Types.NOT_FOUND, "No regions found")
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
...(data.setPricingContextResult ? data.setPricingContextResult : {}),
|
||||||
currency_code: data.input.currency_code ?? data.region.currency_code,
|
currency_code: data.input.currency_code ?? data.region.currency_code,
|
||||||
region_id: data.region.id,
|
region_id: data.region.id,
|
||||||
customer_id: data.customerData.customer?.id,
|
customer_id: data.customerData.customer?.id,
|
||||||
@@ -248,7 +264,7 @@ export const createCartWorkflow = createWorkflow(
|
|||||||
})
|
})
|
||||||
|
|
||||||
return new WorkflowResponse(cart, {
|
return new WorkflowResponse(cart, {
|
||||||
hooks: [validate, cartCreated],
|
hooks: [validate, cartCreated, setPricingContext] as const,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
|
createHook,
|
||||||
createWorkflow,
|
createWorkflow,
|
||||||
transform,
|
transform,
|
||||||
WorkflowData,
|
WorkflowData,
|
||||||
@@ -7,8 +8,12 @@ import {
|
|||||||
import { useQueryGraphStep, validatePresenceOfStep } from "../../common"
|
import { useQueryGraphStep, validatePresenceOfStep } from "../../common"
|
||||||
import { useRemoteQueryStep } from "../../common/steps/use-remote-query"
|
import { useRemoteQueryStep } from "../../common/steps/use-remote-query"
|
||||||
import { cartFieldsForPricingContext } from "../utils/fields"
|
import { cartFieldsForPricingContext } from "../utils/fields"
|
||||||
import { ListShippingOptionsForCartWorkflowInput } from "@medusajs/types"
|
import {
|
||||||
|
AdditionalData,
|
||||||
|
ListShippingOptionsForCartWorkflowInput,
|
||||||
|
} from "@medusajs/types"
|
||||||
import { isDefined } from "@medusajs/framework/utils"
|
import { isDefined } from "@medusajs/framework/utils"
|
||||||
|
import { pricingContextResult } from "../utils/schemas"
|
||||||
|
|
||||||
export const listShippingOptionsForCartWorkflowId =
|
export const listShippingOptionsForCartWorkflowId =
|
||||||
"list-shipping-options-for-cart"
|
"list-shipping-options-for-cart"
|
||||||
@@ -41,7 +46,11 @@ export const listShippingOptionsForCartWorkflowId =
|
|||||||
*/
|
*/
|
||||||
export const listShippingOptionsForCartWorkflow = createWorkflow(
|
export const listShippingOptionsForCartWorkflow = createWorkflow(
|
||||||
listShippingOptionsForCartWorkflowId,
|
listShippingOptionsForCartWorkflowId,
|
||||||
(input: WorkflowData<ListShippingOptionsForCartWorkflowInput>) => {
|
(
|
||||||
|
input: WorkflowData<
|
||||||
|
ListShippingOptionsForCartWorkflowInput & AdditionalData
|
||||||
|
>
|
||||||
|
) => {
|
||||||
const cartQuery = useQueryGraphStep({
|
const cartQuery = useQueryGraphStep({
|
||||||
entity: "cart",
|
entity: "cart",
|
||||||
filters: { id: input.cart_id },
|
filters: { id: input.cart_id },
|
||||||
@@ -96,9 +105,22 @@ export const listShippingOptionsForCartWorkflow = createWorkflow(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const setPricingContext = createHook(
|
||||||
|
"setPricingContext",
|
||||||
|
{
|
||||||
|
cart: cart,
|
||||||
|
fulfillmentSetIds,
|
||||||
|
additional_data: input.additional_data,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
resultValidator: pricingContextResult,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
const setPricingContextResult = setPricingContext.getResult()
|
||||||
|
|
||||||
const queryVariables = transform(
|
const queryVariables = transform(
|
||||||
{ input, fulfillmentSetIds, cart },
|
{ input, fulfillmentSetIds, cart, setPricingContextResult },
|
||||||
({ input, fulfillmentSetIds, cart }) => {
|
({ input, fulfillmentSetIds, cart, setPricingContextResult }) => {
|
||||||
return {
|
return {
|
||||||
id: input.option_ids,
|
id: input.option_ids,
|
||||||
|
|
||||||
@@ -122,7 +144,17 @@ export const listShippingOptionsForCartWorkflow = createWorkflow(
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
calculated_price: { context: cart },
|
calculated_price: {
|
||||||
|
context: {
|
||||||
|
...cart,
|
||||||
|
...(setPricingContextResult ? setPricingContextResult : {}),
|
||||||
|
currency_code: cart.currency_code,
|
||||||
|
region_id: cart.region_id,
|
||||||
|
region: cart.region,
|
||||||
|
customer_id: cart.customer_id,
|
||||||
|
customer: cart.customer,
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@@ -202,6 +234,8 @@ export const listShippingOptionsForCartWorkflow = createWorkflow(
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
return new WorkflowResponse(shippingOptionsWithPrice)
|
return new WorkflowResponse(shippingOptionsWithPrice, {
|
||||||
|
hooks: [setPricingContext] as const,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@@ -28,6 +28,8 @@ import { refreshPaymentCollectionForCartWorkflow } from "./refresh-payment-colle
|
|||||||
import { updateCartPromotionsWorkflow } from "./update-cart-promotions"
|
import { updateCartPromotionsWorkflow } from "./update-cart-promotions"
|
||||||
import { updateTaxLinesWorkflow } from "./update-tax-lines"
|
import { updateTaxLinesWorkflow } from "./update-tax-lines"
|
||||||
import { upsertTaxLinesWorkflow } from "./upsert-tax-lines"
|
import { upsertTaxLinesWorkflow } from "./upsert-tax-lines"
|
||||||
|
import { AdditionalData } from "@medusajs/types"
|
||||||
|
import { pricingContextResult } from "../utils/schemas"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The details of the cart to refresh.
|
* The details of the cart to refresh.
|
||||||
@@ -91,7 +93,20 @@ export const refreshCartItemsWorkflowId = "refresh-cart-items"
|
|||||||
*/
|
*/
|
||||||
export const refreshCartItemsWorkflow = createWorkflow(
|
export const refreshCartItemsWorkflow = createWorkflow(
|
||||||
refreshCartItemsWorkflowId,
|
refreshCartItemsWorkflowId,
|
||||||
(input: WorkflowData<RefreshCartItemsWorkflowInput>) => {
|
(input: WorkflowData<RefreshCartItemsWorkflowInput & AdditionalData>) => {
|
||||||
|
const setPricingContext = createHook(
|
||||||
|
"setPricingContext",
|
||||||
|
{
|
||||||
|
cart_id: input.cart_id,
|
||||||
|
items: input.items,
|
||||||
|
additional_data: input.additional_data,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
resultValidator: pricingContextResult,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
const setPricingContextResult = setPricingContext.getResult()
|
||||||
|
|
||||||
when({ input }, ({ input }) => {
|
when({ input }, ({ input }) => {
|
||||||
return !!input.force_refresh
|
return !!input.force_refresh
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
@@ -106,9 +121,22 @@ export const refreshCartItemsWorkflow = createWorkflow(
|
|||||||
return (data.cart.items ?? []).map((i) => i.variant_id).filter(Boolean)
|
return (data.cart.items ?? []).map((i) => i.variant_id).filter(Boolean)
|
||||||
})
|
})
|
||||||
|
|
||||||
const cartPricingContext = transform({ cart }, ({ cart }) => {
|
const cartPricingContext = transform(
|
||||||
return filterObjectByKeys(cart, cartFieldsForPricingContext)
|
{ cart, setPricingContextResult },
|
||||||
})
|
(data) => {
|
||||||
|
return {
|
||||||
|
...filterObjectByKeys(data.cart, cartFieldsForPricingContext),
|
||||||
|
...(data.setPricingContextResult
|
||||||
|
? data.setPricingContextResult
|
||||||
|
: {}),
|
||||||
|
currency_code: data.cart.currency_code,
|
||||||
|
region_id: data.cart.region_id,
|
||||||
|
region: data.cart.region,
|
||||||
|
customer_id: data.cart.customer_id,
|
||||||
|
customer: data.cart.customer,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
const variants = useRemoteQueryStep({
|
const variants = useRemoteQueryStep({
|
||||||
entry_point: "variants",
|
entry_point: "variants",
|
||||||
@@ -235,6 +263,8 @@ export const refreshCartItemsWorkflow = createWorkflow(
|
|||||||
input: { cart_id: input.cart_id },
|
input: { cart_id: input.cart_id },
|
||||||
})
|
})
|
||||||
|
|
||||||
return new WorkflowResponse(refetchedCart)
|
return new WorkflowResponse(refetchedCart, {
|
||||||
|
hooks: [setPricingContext] as const,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@@ -1,4 +1,7 @@
|
|||||||
import { UpdateLineItemInCartWorkflowInputDTO } from "@medusajs/framework/types"
|
import {
|
||||||
|
AdditionalData,
|
||||||
|
UpdateLineItemInCartWorkflowInputDTO,
|
||||||
|
} from "@medusajs/framework/types"
|
||||||
import { isDefined, MedusaError } from "@medusajs/framework/utils"
|
import { isDefined, MedusaError } from "@medusajs/framework/utils"
|
||||||
import {
|
import {
|
||||||
createHook,
|
createHook,
|
||||||
@@ -19,6 +22,7 @@ import {
|
|||||||
} from "../utils/fields"
|
} from "../utils/fields"
|
||||||
import { confirmVariantInventoryWorkflow } from "./confirm-variant-inventory"
|
import { confirmVariantInventoryWorkflow } from "./confirm-variant-inventory"
|
||||||
import { refreshCartItemsWorkflow } from "./refresh-cart-items"
|
import { refreshCartItemsWorkflow } from "./refresh-cart-items"
|
||||||
|
import { pricingContextResult } from "../utils/schemas"
|
||||||
|
|
||||||
const cartFields = cartFieldsForPricingContext.concat(["items.*"])
|
const cartFields = cartFieldsForPricingContext.concat(["items.*"])
|
||||||
|
|
||||||
@@ -49,7 +53,9 @@ export const updateLineItemInCartWorkflowId = "update-line-item-in-cart"
|
|||||||
*/
|
*/
|
||||||
export const updateLineItemInCartWorkflow = createWorkflow(
|
export const updateLineItemInCartWorkflow = createWorkflow(
|
||||||
updateLineItemInCartWorkflowId,
|
updateLineItemInCartWorkflowId,
|
||||||
(input: WorkflowData<UpdateLineItemInCartWorkflowInputDTO>) => {
|
(
|
||||||
|
input: WorkflowData<UpdateLineItemInCartWorkflowInputDTO & AdditionalData>
|
||||||
|
) => {
|
||||||
const cartQuery = useQueryGraphStep({
|
const cartQuery = useQueryGraphStep({
|
||||||
entity: "cart",
|
entity: "cart",
|
||||||
filters: { id: input.cart_id },
|
filters: { id: input.cart_id },
|
||||||
@@ -73,6 +79,35 @@ export const updateLineItemInCartWorkflow = createWorkflow(
|
|||||||
return [item.variant_id].filter(Boolean)
|
return [item.variant_id].filter(Boolean)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const setPricingContext = createHook(
|
||||||
|
"setPricingContext",
|
||||||
|
{
|
||||||
|
cart,
|
||||||
|
item,
|
||||||
|
variantIds,
|
||||||
|
additional_data: input.additional_data,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
resultValidator: pricingContextResult,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
const setPricingContextResult = setPricingContext.getResult()
|
||||||
|
const pricingContext = transform(
|
||||||
|
{ cart, setPricingContextResult },
|
||||||
|
(data) => {
|
||||||
|
return {
|
||||||
|
...data.cart,
|
||||||
|
...(data.setPricingContextResult ? data.setPricingContextResult : {}),
|
||||||
|
currency_code: data.cart.currency_code,
|
||||||
|
region_id: data.cart.region_id,
|
||||||
|
region: data.cart.region,
|
||||||
|
customer_id: data.cart.customer_id,
|
||||||
|
customer: data.cart.customer,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
const variants = when({ variantIds }, ({ variantIds }) => {
|
const variants = when({ variantIds }, ({ variantIds }) => {
|
||||||
return !!variantIds.length
|
return !!variantIds.length
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
@@ -82,7 +117,7 @@ export const updateLineItemInCartWorkflow = createWorkflow(
|
|||||||
variables: {
|
variables: {
|
||||||
id: variantIds,
|
id: variantIds,
|
||||||
calculated_price: {
|
calculated_price: {
|
||||||
context: cart,
|
context: pricingContext,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}).config({ name: "fetch-variants" })
|
}).config({ name: "fetch-variants" })
|
||||||
@@ -98,7 +133,7 @@ export const updateLineItemInCartWorkflow = createWorkflow(
|
|||||||
|
|
||||||
confirmVariantInventoryWorkflow.runAsStep({
|
confirmVariantInventoryWorkflow.runAsStep({
|
||||||
input: {
|
input: {
|
||||||
sales_channel_id: cart.sales_channel_id,
|
sales_channel_id: pricingContext.sales_channel_id,
|
||||||
variants,
|
variants,
|
||||||
items,
|
items,
|
||||||
},
|
},
|
||||||
@@ -147,7 +182,7 @@ export const updateLineItemInCartWorkflow = createWorkflow(
|
|||||||
})
|
})
|
||||||
|
|
||||||
return new WorkflowResponse(void 0, {
|
return new WorkflowResponse(void 0, {
|
||||||
hooks: [validate],
|
hooks: [validate, setPricingContext] as const,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
|
AdditionalData,
|
||||||
BigNumberInput,
|
BigNumberInput,
|
||||||
CalculatedRMAShippingContext,
|
CalculatedRMAShippingContext,
|
||||||
CalculateShippingOptionPriceDTO,
|
CalculateShippingOptionPriceDTO,
|
||||||
@@ -6,6 +7,7 @@ import {
|
|||||||
} from "@medusajs/framework/types"
|
} from "@medusajs/framework/types"
|
||||||
import {
|
import {
|
||||||
WorkflowResponse,
|
WorkflowResponse,
|
||||||
|
createHook,
|
||||||
createWorkflow,
|
createWorkflow,
|
||||||
transform,
|
transform,
|
||||||
when,
|
when,
|
||||||
@@ -13,6 +15,7 @@ import {
|
|||||||
import { BigNumber, ShippingOptionPriceType } from "@medusajs/framework/utils"
|
import { BigNumber, ShippingOptionPriceType } from "@medusajs/framework/utils"
|
||||||
import { calculateShippingOptionsPricesStep } from "../../fulfillment/steps"
|
import { calculateShippingOptionsPricesStep } from "../../fulfillment/steps"
|
||||||
import { useRemoteQueryStep } from "../../common"
|
import { useRemoteQueryStep } from "../../common"
|
||||||
|
import { pricingContextResult } from "../../cart/utils/schemas"
|
||||||
|
|
||||||
const COMMON_OPTIONS_FIELDS = [
|
const COMMON_OPTIONS_FIELDS = [
|
||||||
"id",
|
"id",
|
||||||
@@ -90,9 +93,7 @@ export const createOrderEditShippingMethodWorkflowId = "fetch-shipping-option"
|
|||||||
*/
|
*/
|
||||||
export const fetchShippingOptionForOrderWorkflow = createWorkflow(
|
export const fetchShippingOptionForOrderWorkflow = createWorkflow(
|
||||||
createOrderEditShippingMethodWorkflowId,
|
createOrderEditShippingMethodWorkflowId,
|
||||||
function (
|
function (input: FetchShippingOptionForOrderWorkflowInput & AdditionalData) {
|
||||||
input: FetchShippingOptionForOrderWorkflowInput
|
|
||||||
): WorkflowResponse<FetchShippingOptionForOrderWorkflowOutput> {
|
|
||||||
const initialOption = useRemoteQueryStep({
|
const initialOption = useRemoteQueryStep({
|
||||||
entry_point: "shipping_option",
|
entry_point: "shipping_option",
|
||||||
variables: { id: input.shipping_option_id },
|
variables: { id: input.shipping_option_id },
|
||||||
@@ -170,6 +171,20 @@ export const fetchShippingOptionForOrderWorkflow = createWorkflow(
|
|||||||
return shippingOptionsWithPrice
|
return shippingOptionsWithPrice
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const setPricingContext = createHook("setPricingContext", input, {
|
||||||
|
resultValidator: pricingContextResult,
|
||||||
|
})
|
||||||
|
const setPricingContextResult = setPricingContext.getResult()
|
||||||
|
const pricingContext = transform(
|
||||||
|
{ input, setPricingContextResult },
|
||||||
|
(data) => {
|
||||||
|
return {
|
||||||
|
...(data.setPricingContextResult ? data.setPricingContextResult : {}),
|
||||||
|
currency_code: data.input.currency_code,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
const flatRateShippingOption = when(
|
const flatRateShippingOption = when(
|
||||||
"option-flat",
|
"option-flat",
|
||||||
{ isCalculatedPriceShippingOption },
|
{ isCalculatedPriceShippingOption },
|
||||||
@@ -186,7 +201,7 @@ export const fetchShippingOptionForOrderWorkflow = createWorkflow(
|
|||||||
variables: {
|
variables: {
|
||||||
id: input.shipping_option_id,
|
id: input.shipping_option_id,
|
||||||
calculated_price: {
|
calculated_price: {
|
||||||
context: { currency_code: input.currency_code },
|
context: pricingContext,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
list: false,
|
list: false,
|
||||||
@@ -195,7 +210,7 @@ export const fetchShippingOptionForOrderWorkflow = createWorkflow(
|
|||||||
return shippingOption
|
return shippingOption
|
||||||
})
|
})
|
||||||
|
|
||||||
const result = transform(
|
const result: FetchShippingOptionForOrderWorkflowOutput = transform(
|
||||||
{
|
{
|
||||||
calculatedPriceShippingOption,
|
calculatedPriceShippingOption,
|
||||||
flatRateShippingOption,
|
flatRateShippingOption,
|
||||||
@@ -205,6 +220,6 @@ export const fetchShippingOptionForOrderWorkflow = createWorkflow(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
return new WorkflowResponse(result)
|
return new WorkflowResponse(result, { hooks: [setPricingContext] as const })
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@@ -1,6 +1,11 @@
|
|||||||
import { OrderLineItemDTO, OrderWorkflow } from "@medusajs/framework/types"
|
import {
|
||||||
|
AdditionalData,
|
||||||
|
OrderLineItemDTO,
|
||||||
|
OrderWorkflow,
|
||||||
|
} from "@medusajs/framework/types"
|
||||||
import { isDefined, MedusaError } from "@medusajs/framework/utils"
|
import { isDefined, MedusaError } from "@medusajs/framework/utils"
|
||||||
import {
|
import {
|
||||||
|
createHook,
|
||||||
createWorkflow,
|
createWorkflow,
|
||||||
parallelize,
|
parallelize,
|
||||||
transform,
|
transform,
|
||||||
@@ -21,6 +26,7 @@ import { confirmVariantInventoryWorkflow } from "../../cart/workflows/confirm-va
|
|||||||
import { useRemoteQueryStep } from "../../common"
|
import { useRemoteQueryStep } from "../../common"
|
||||||
import { createOrderLineItemsStep } from "../steps"
|
import { createOrderLineItemsStep } from "../steps"
|
||||||
import { productVariantsFields } from "../utils/fields"
|
import { productVariantsFields } from "../utils/fields"
|
||||||
|
import { pricingContextResult } from "../../cart/utils/schemas"
|
||||||
|
|
||||||
function prepareLineItems(data) {
|
function prepareLineItems(data) {
|
||||||
const items = (data.input.items ?? []).map((item) => {
|
const items = (data.input.items ?? []).map((item) => {
|
||||||
@@ -82,8 +88,10 @@ export const addOrderLineItemsWorkflowId = "order-add-line-items"
|
|||||||
export const addOrderLineItemsWorkflow = createWorkflow(
|
export const addOrderLineItemsWorkflow = createWorkflow(
|
||||||
addOrderLineItemsWorkflowId,
|
addOrderLineItemsWorkflowId,
|
||||||
(
|
(
|
||||||
input: WorkflowData<OrderWorkflow.OrderAddLineItemWorkflowInput>
|
input: WorkflowData<
|
||||||
): WorkflowResponse<OrderAddLineItemWorkflowOutput> => {
|
OrderWorkflow.OrderAddLineItemWorkflowInput & AdditionalData
|
||||||
|
>
|
||||||
|
) => {
|
||||||
const order = useRemoteQueryStep({
|
const order = useRemoteQueryStep({
|
||||||
entry_point: "orders",
|
entry_point: "orders",
|
||||||
fields: [
|
fields: [
|
||||||
@@ -118,14 +126,30 @@ export const addOrderLineItemsWorkflow = createWorkflow(
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const setPricingContext = createHook(
|
||||||
|
"setPricingContext",
|
||||||
|
{
|
||||||
|
order,
|
||||||
|
variantIds,
|
||||||
|
region,
|
||||||
|
customerData,
|
||||||
|
additional_data: input.additional_data,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
resultValidator: pricingContextResult,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
const setPricingContextResult = setPricingContext.getResult()
|
||||||
|
|
||||||
const pricingContext = transform(
|
const pricingContext = transform(
|
||||||
{ input, region, customerData, order },
|
{ input, region, customerData, order, setPricingContextResult },
|
||||||
(data) => {
|
(data) => {
|
||||||
if (!data.region) {
|
if (!data.region) {
|
||||||
throw new MedusaError(MedusaError.Types.NOT_FOUND, "Region not found")
|
throw new MedusaError(MedusaError.Types.NOT_FOUND, "Region not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
...(data.setPricingContextResult ? data.setPricingContextResult : {}),
|
||||||
currency_code: data.order.currency_code ?? data.region.currency_code,
|
currency_code: data.order.currency_code ?? data.region.currency_code,
|
||||||
region_id: data.region.id,
|
region_id: data.region.id,
|
||||||
customer_id: data.customerData.customer?.id,
|
customer_id: data.customerData.customer?.id,
|
||||||
@@ -165,7 +189,10 @@ export const addOrderLineItemsWorkflow = createWorkflow(
|
|||||||
return new WorkflowResponse(
|
return new WorkflowResponse(
|
||||||
createOrderLineItemsStep({
|
createOrderLineItemsStep({
|
||||||
items: lineItems,
|
items: lineItems,
|
||||||
})
|
}) satisfies OrderAddLineItemWorkflowOutput,
|
||||||
|
{
|
||||||
|
hooks: [setPricingContext] as const,
|
||||||
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
|
AdditionalData,
|
||||||
OrderChangeActionDTO,
|
OrderChangeActionDTO,
|
||||||
OrderChangeDTO,
|
OrderChangeDTO,
|
||||||
OrderClaimDTO,
|
OrderClaimDTO,
|
||||||
@@ -9,6 +10,7 @@ import { ChangeActionType, OrderChangeStatus } from "@medusajs/framework/utils"
|
|||||||
import {
|
import {
|
||||||
WorkflowData,
|
WorkflowData,
|
||||||
WorkflowResponse,
|
WorkflowResponse,
|
||||||
|
createHook,
|
||||||
createStep,
|
createStep,
|
||||||
createWorkflow,
|
createWorkflow,
|
||||||
parallelize,
|
parallelize,
|
||||||
@@ -26,6 +28,7 @@ import {
|
|||||||
throwIfOrderChangeIsNotActive,
|
throwIfOrderChangeIsNotActive,
|
||||||
} from "../../utils/order-validation"
|
} from "../../utils/order-validation"
|
||||||
import { prepareShippingMethodUpdate } from "../../utils/prepare-shipping-method"
|
import { prepareShippingMethodUpdate } from "../../utils/prepare-shipping-method"
|
||||||
|
import { pricingContextResult } from "../../../cart/utils/schemas"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The data to validate that a claim's shipping method can be updated.
|
* The data to validate that a claim's shipping method can be updated.
|
||||||
@@ -42,7 +45,10 @@ export type UpdateClaimShippingMethodValidationStepInput = {
|
|||||||
/**
|
/**
|
||||||
* The details of updating the shipping method.
|
* The details of updating the shipping method.
|
||||||
*/
|
*/
|
||||||
input: Pick<OrderWorkflow.UpdateClaimShippingMethodWorkflowInput, "claim_id" | "action_id">
|
input: Pick<
|
||||||
|
OrderWorkflow.UpdateClaimShippingMethodWorkflowInput,
|
||||||
|
"claim_id" | "action_id"
|
||||||
|
>
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -128,8 +134,10 @@ export const updateClaimShippingMethodWorkflowId =
|
|||||||
export const updateClaimShippingMethodWorkflow = createWorkflow(
|
export const updateClaimShippingMethodWorkflow = createWorkflow(
|
||||||
updateClaimShippingMethodWorkflowId,
|
updateClaimShippingMethodWorkflowId,
|
||||||
function (
|
function (
|
||||||
input: WorkflowData<OrderWorkflow.UpdateClaimShippingMethodWorkflowInput>
|
input: WorkflowData<
|
||||||
): WorkflowResponse<OrderPreviewDTO> {
|
OrderWorkflow.UpdateClaimShippingMethodWorkflowInput & AdditionalData
|
||||||
|
>
|
||||||
|
) {
|
||||||
const orderClaim: OrderClaimDTO = useRemoteQueryStep({
|
const orderClaim: OrderClaimDTO = useRemoteQueryStep({
|
||||||
entry_point: "order_claim",
|
entry_point: "order_claim",
|
||||||
fields: [
|
fields: [
|
||||||
@@ -157,6 +165,19 @@ export const updateClaimShippingMethodWorkflow = createWorkflow(
|
|||||||
list: false,
|
list: false,
|
||||||
}).config({ name: "order-change-query" })
|
}).config({ name: "order-change-query" })
|
||||||
|
|
||||||
|
const setPricingContext = createHook(
|
||||||
|
"setPricingContext",
|
||||||
|
{
|
||||||
|
order_claim: orderClaim,
|
||||||
|
order_change: orderChange,
|
||||||
|
additional_data: input.additional_data,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
resultValidator: pricingContextResult,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
const setPricingContextResult = setPricingContext.getResult()
|
||||||
|
|
||||||
const shippingOptions = when({ input }, ({ input }) => {
|
const shippingOptions = when({ input }, ({ input }) => {
|
||||||
return input.data?.custom_amount === null
|
return input.data?.custom_amount === null
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
@@ -174,6 +195,18 @@ export const updateClaimShippingMethodWorkflow = createWorkflow(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const pricingContext = transform(
|
||||||
|
{ action, setPricingContextResult },
|
||||||
|
(data) => {
|
||||||
|
return {
|
||||||
|
...(data.setPricingContextResult
|
||||||
|
? data.setPricingContextResult
|
||||||
|
: {}),
|
||||||
|
currency_code: data.action.currency_code,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
const shippingMethod = useRemoteQueryStep({
|
const shippingMethod = useRemoteQueryStep({
|
||||||
entry_point: "order_shipping_method",
|
entry_point: "order_shipping_method",
|
||||||
fields: ["id", "shipping_option_id"],
|
fields: ["id", "shipping_option_id"],
|
||||||
@@ -194,7 +227,7 @@ export const updateClaimShippingMethodWorkflow = createWorkflow(
|
|||||||
variables: {
|
variables: {
|
||||||
id: shippingMethod.shipping_option_id,
|
id: shippingMethod.shipping_option_id,
|
||||||
calculated_price: {
|
calculated_price: {
|
||||||
context: { currency_code: action.currency_code },
|
context: pricingContext,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}).config({ name: "fetch-shipping-option" })
|
}).config({ name: "fetch-shipping-option" })
|
||||||
@@ -212,6 +245,11 @@ export const updateClaimShippingMethodWorkflow = createWorkflow(
|
|||||||
updateOrderShippingMethodsStep([updateData.shippingMethod!])
|
updateOrderShippingMethodsStep([updateData.shippingMethod!])
|
||||||
)
|
)
|
||||||
|
|
||||||
return new WorkflowResponse(previewOrderChangeStep(orderClaim.order_id))
|
return new WorkflowResponse(
|
||||||
|
previewOrderChangeStep(orderClaim.order_id) as OrderPreviewDTO,
|
||||||
|
{
|
||||||
|
hooks: [setPricingContext] as const,
|
||||||
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@@ -23,6 +23,7 @@ import { useRemoteQueryStep } from "../../common"
|
|||||||
import { createOrdersStep } from "../steps"
|
import { createOrdersStep } from "../steps"
|
||||||
import { productVariantsFields } from "../utils/fields"
|
import { productVariantsFields } from "../utils/fields"
|
||||||
import { updateOrderTaxLinesWorkflow } from "./update-tax-lines"
|
import { updateOrderTaxLinesWorkflow } from "./update-tax-lines"
|
||||||
|
import { pricingContextResult } from "../../cart/utils/schemas"
|
||||||
|
|
||||||
function prepareLineItems(data) {
|
function prepareLineItems(data) {
|
||||||
const items = (data.input.items ?? []).map((item) => {
|
const items = (data.input.items ?? []).map((item) => {
|
||||||
@@ -150,15 +151,30 @@ export const createOrderWorkflow = createWorkflow(
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const setPricingContext = createHook(
|
||||||
|
"setPricingContext",
|
||||||
|
{
|
||||||
|
variantIds,
|
||||||
|
region,
|
||||||
|
customerData,
|
||||||
|
additional_data: input.additional_data,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
resultValidator: pricingContextResult,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
const setPricingContextResult = setPricingContext.getResult()
|
||||||
|
|
||||||
// TODO: This is on par with the context used in v1.*, but we can be more flexible.
|
// TODO: This is on par with the context used in v1.*, but we can be more flexible.
|
||||||
const pricingContext = transform(
|
const pricingContext = transform(
|
||||||
{ input, region, customerData },
|
{ input, region, customerData, setPricingContextResult },
|
||||||
(data) => {
|
(data) => {
|
||||||
if (!data.region) {
|
if (!data.region) {
|
||||||
throw new MedusaError(MedusaError.Types.NOT_FOUND, "Region not found")
|
throw new MedusaError(MedusaError.Types.NOT_FOUND, "Region not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
...(data.setPricingContextResult ? data.setPricingContextResult : {}),
|
||||||
currency_code: data.input.currency_code ?? data.region.currency_code,
|
currency_code: data.input.currency_code ?? data.region.currency_code,
|
||||||
region_id: data.region.id,
|
region_id: data.region.id,
|
||||||
customer_id: data.customerData.customer?.id,
|
customer_id: data.customerData.customer?.id,
|
||||||
@@ -222,7 +238,7 @@ export const createOrderWorkflow = createWorkflow(
|
|||||||
})
|
})
|
||||||
|
|
||||||
return new WorkflowResponse(order, {
|
return new WorkflowResponse(order, {
|
||||||
hooks: [orderCreated],
|
hooks: [orderCreated, setPricingContext] as const,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|||||||
+43
-5
@@ -1,4 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
|
AdditionalData,
|
||||||
OrderChangeActionDTO,
|
OrderChangeActionDTO,
|
||||||
OrderChangeDTO,
|
OrderChangeDTO,
|
||||||
OrderExchangeDTO,
|
OrderExchangeDTO,
|
||||||
@@ -9,6 +10,7 @@ import { ChangeActionType, OrderChangeStatus } from "@medusajs/framework/utils"
|
|||||||
import {
|
import {
|
||||||
WorkflowData,
|
WorkflowData,
|
||||||
WorkflowResponse,
|
WorkflowResponse,
|
||||||
|
createHook,
|
||||||
createStep,
|
createStep,
|
||||||
createWorkflow,
|
createWorkflow,
|
||||||
parallelize,
|
parallelize,
|
||||||
@@ -26,6 +28,7 @@ import {
|
|||||||
throwIfOrderChangeIsNotActive,
|
throwIfOrderChangeIsNotActive,
|
||||||
} from "../../utils/order-validation"
|
} from "../../utils/order-validation"
|
||||||
import { prepareShippingMethodUpdate } from "../../utils/prepare-shipping-method"
|
import { prepareShippingMethodUpdate } from "../../utils/prepare-shipping-method"
|
||||||
|
import { pricingContextResult } from "../../../cart/utils/schemas"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The data to validate that an exchange's shipping method can be updated.
|
* The data to validate that an exchange's shipping method can be updated.
|
||||||
@@ -42,7 +45,10 @@ export type UpdateExchangeShippingMethodValidationStepInput = {
|
|||||||
/**
|
/**
|
||||||
* The details of the shipping method update.
|
* The details of the shipping method update.
|
||||||
*/
|
*/
|
||||||
input: Pick<OrderWorkflow.UpdateExchangeShippingMethodWorkflowInput, "exchange_id" | "action_id">
|
input: Pick<
|
||||||
|
OrderWorkflow.UpdateExchangeShippingMethodWorkflowInput,
|
||||||
|
"exchange_id" | "action_id"
|
||||||
|
>
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -132,8 +138,10 @@ export const updateExchangeShippingMethodWorkflowId =
|
|||||||
export const updateExchangeShippingMethodWorkflow = createWorkflow(
|
export const updateExchangeShippingMethodWorkflow = createWorkflow(
|
||||||
updateExchangeShippingMethodWorkflowId,
|
updateExchangeShippingMethodWorkflowId,
|
||||||
function (
|
function (
|
||||||
input: WorkflowData<OrderWorkflow.UpdateExchangeShippingMethodWorkflowInput>
|
input: WorkflowData<
|
||||||
): WorkflowResponse<OrderPreviewDTO> {
|
OrderWorkflow.UpdateExchangeShippingMethodWorkflowInput & AdditionalData
|
||||||
|
>
|
||||||
|
) {
|
||||||
const orderExchange: OrderExchangeDTO = useRemoteQueryStep({
|
const orderExchange: OrderExchangeDTO = useRemoteQueryStep({
|
||||||
entry_point: "order_exchange",
|
entry_point: "order_exchange",
|
||||||
fields: [
|
fields: [
|
||||||
@@ -161,6 +169,19 @@ export const updateExchangeShippingMethodWorkflow = createWorkflow(
|
|||||||
list: false,
|
list: false,
|
||||||
}).config({ name: "order-change-query" })
|
}).config({ name: "order-change-query" })
|
||||||
|
|
||||||
|
const setPricingContext = createHook(
|
||||||
|
"setPricingContext",
|
||||||
|
{
|
||||||
|
order_exchange: orderExchange,
|
||||||
|
order_change: orderChange,
|
||||||
|
additional_data: input.additional_data,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
resultValidator: pricingContextResult,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
const setPricingContextResult = setPricingContext.getResult()
|
||||||
|
|
||||||
const shippingOptions = when({ input }, ({ input }) => {
|
const shippingOptions = when({ input }, ({ input }) => {
|
||||||
return input.data?.custom_amount === null
|
return input.data?.custom_amount === null
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
@@ -178,6 +199,18 @@ export const updateExchangeShippingMethodWorkflow = createWorkflow(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const pricingContext = transform(
|
||||||
|
{ action, setPricingContextResult },
|
||||||
|
(data) => {
|
||||||
|
return {
|
||||||
|
...(data.setPricingContextResult
|
||||||
|
? data.setPricingContextResult
|
||||||
|
: {}),
|
||||||
|
currency_code: data.action.currency_code,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
const shippingMethod = useRemoteQueryStep({
|
const shippingMethod = useRemoteQueryStep({
|
||||||
entry_point: "order_shipping_method",
|
entry_point: "order_shipping_method",
|
||||||
fields: ["id", "shipping_option_id"],
|
fields: ["id", "shipping_option_id"],
|
||||||
@@ -198,7 +231,7 @@ export const updateExchangeShippingMethodWorkflow = createWorkflow(
|
|||||||
variables: {
|
variables: {
|
||||||
id: shippingMethod.shipping_option_id,
|
id: shippingMethod.shipping_option_id,
|
||||||
calculated_price: {
|
calculated_price: {
|
||||||
context: { currency_code: action.currency_code },
|
context: pricingContext,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}).config({ name: "fetch-shipping-option" })
|
}).config({ name: "fetch-shipping-option" })
|
||||||
@@ -220,6 +253,11 @@ export const updateExchangeShippingMethodWorkflow = createWorkflow(
|
|||||||
updateOrderShippingMethodsStep([updateData.shippingMethod!])
|
updateOrderShippingMethodsStep([updateData.shippingMethod!])
|
||||||
)
|
)
|
||||||
|
|
||||||
return new WorkflowResponse(previewOrderChangeStep(orderExchange.order_id))
|
return new WorkflowResponse(
|
||||||
|
previewOrderChangeStep(orderExchange.order_id) as OrderPreviewDTO,
|
||||||
|
{
|
||||||
|
hooks: [setPricingContext] as const,
|
||||||
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
+35
-4
@@ -1,4 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
|
AdditionalData,
|
||||||
BigNumberInput,
|
BigNumberInput,
|
||||||
OrderChangeDTO,
|
OrderChangeDTO,
|
||||||
OrderDTO,
|
OrderDTO,
|
||||||
@@ -7,6 +8,7 @@ import {
|
|||||||
import { ChangeActionType, OrderChangeStatus } from "@medusajs/framework/utils"
|
import { ChangeActionType, OrderChangeStatus } from "@medusajs/framework/utils"
|
||||||
import {
|
import {
|
||||||
WorkflowResponse,
|
WorkflowResponse,
|
||||||
|
createHook,
|
||||||
createStep,
|
createStep,
|
||||||
createWorkflow,
|
createWorkflow,
|
||||||
transform,
|
transform,
|
||||||
@@ -21,6 +23,7 @@ import {
|
|||||||
import { prepareShippingMethod } from "../../utils/prepare-shipping-method"
|
import { prepareShippingMethod } from "../../utils/prepare-shipping-method"
|
||||||
import { createOrderChangeActionsWorkflow } from "../create-order-change-actions"
|
import { createOrderChangeActionsWorkflow } from "../create-order-change-actions"
|
||||||
import { updateOrderTaxLinesWorkflow } from "../update-tax-lines"
|
import { updateOrderTaxLinesWorkflow } from "../update-tax-lines"
|
||||||
|
import { pricingContextResult } from "../../../cart/utils/schemas"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The data to validate that a shipping method can be created for an order edit.
|
* The data to validate that a shipping method can be created for an order edit.
|
||||||
@@ -114,8 +117,8 @@ export const createOrderEditShippingMethodWorkflowId =
|
|||||||
export const createOrderEditShippingMethodWorkflow = createWorkflow(
|
export const createOrderEditShippingMethodWorkflow = createWorkflow(
|
||||||
createOrderEditShippingMethodWorkflowId,
|
createOrderEditShippingMethodWorkflowId,
|
||||||
function (
|
function (
|
||||||
input: CreateOrderEditShippingMethodWorkflowInput
|
input: CreateOrderEditShippingMethodWorkflowInput & AdditionalData
|
||||||
): WorkflowResponse<OrderPreviewDTO> {
|
) {
|
||||||
const order: OrderDTO = useRemoteQueryStep({
|
const order: OrderDTO = useRemoteQueryStep({
|
||||||
entry_point: "orders",
|
entry_point: "orders",
|
||||||
fields: ["id", "status", "currency_code", "canceled_at"],
|
fields: ["id", "status", "currency_code", "canceled_at"],
|
||||||
@@ -124,6 +127,29 @@ export const createOrderEditShippingMethodWorkflow = createWorkflow(
|
|||||||
throw_if_key_not_found: true,
|
throw_if_key_not_found: true,
|
||||||
}).config({ name: "order-query" })
|
}).config({ name: "order-query" })
|
||||||
|
|
||||||
|
const setPricingContext = createHook(
|
||||||
|
"setPricingContext",
|
||||||
|
{
|
||||||
|
order,
|
||||||
|
shipping_option_id: input.shipping_option_id,
|
||||||
|
additional_data: input.additional_data,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
resultValidator: pricingContextResult,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
const setPricingContextResult = setPricingContext.getResult()
|
||||||
|
|
||||||
|
const pricingContext = transform(
|
||||||
|
{ order, setPricingContextResult },
|
||||||
|
(data) => {
|
||||||
|
return {
|
||||||
|
...(data.setPricingContextResult ? data.setPricingContextResult : {}),
|
||||||
|
currency_code: data.order.currency_code,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
const shippingOptions = useRemoteQueryStep({
|
const shippingOptions = useRemoteQueryStep({
|
||||||
entry_point: "shipping_option",
|
entry_point: "shipping_option",
|
||||||
fields: [
|
fields: [
|
||||||
@@ -135,7 +161,7 @@ export const createOrderEditShippingMethodWorkflow = createWorkflow(
|
|||||||
variables: {
|
variables: {
|
||||||
id: input.shipping_option_id,
|
id: input.shipping_option_id,
|
||||||
calculated_price: {
|
calculated_price: {
|
||||||
context: { currency_code: order.currency_code },
|
context: pricingContext,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}).config({ name: "fetch-shipping-option" })
|
}).config({ name: "fetch-shipping-option" })
|
||||||
@@ -213,6 +239,11 @@ export const createOrderEditShippingMethodWorkflow = createWorkflow(
|
|||||||
input: [orderChangeActionInput],
|
input: [orderChangeActionInput],
|
||||||
})
|
})
|
||||||
|
|
||||||
return new WorkflowResponse(previewOrderChangeStep(order.id))
|
return new WorkflowResponse(
|
||||||
|
previewOrderChangeStep(order.id) as OrderPreviewDTO,
|
||||||
|
{
|
||||||
|
hooks: [setPricingContext] as const,
|
||||||
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
+43
-5
@@ -1,4 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
|
AdditionalData,
|
||||||
OrderChangeActionDTO,
|
OrderChangeActionDTO,
|
||||||
OrderChangeDTO,
|
OrderChangeDTO,
|
||||||
OrderDTO,
|
OrderDTO,
|
||||||
@@ -9,6 +10,7 @@ import { ChangeActionType, OrderChangeStatus } from "@medusajs/framework/utils"
|
|||||||
import {
|
import {
|
||||||
WorkflowData,
|
WorkflowData,
|
||||||
WorkflowResponse,
|
WorkflowResponse,
|
||||||
|
createHook,
|
||||||
createStep,
|
createStep,
|
||||||
createWorkflow,
|
createWorkflow,
|
||||||
parallelize,
|
parallelize,
|
||||||
@@ -23,6 +25,7 @@ import {
|
|||||||
import { previewOrderChangeStep } from "../../steps/preview-order-change"
|
import { previewOrderChangeStep } from "../../steps/preview-order-change"
|
||||||
import { throwIfOrderChangeIsNotActive } from "../../utils/order-validation"
|
import { throwIfOrderChangeIsNotActive } from "../../utils/order-validation"
|
||||||
import { prepareShippingMethodUpdate } from "../../utils/prepare-shipping-method"
|
import { prepareShippingMethodUpdate } from "../../utils/prepare-shipping-method"
|
||||||
|
import { pricingContextResult } from "../../../cart/utils/schemas"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The data to validate that an order edit's shipping method can be updated.
|
* The data to validate that an order edit's shipping method can be updated.
|
||||||
@@ -35,7 +38,10 @@ export type UpdateOrderEditShippingMethodValidationStepInput = {
|
|||||||
/**
|
/**
|
||||||
* The details of the shipping method to be updated.
|
* The details of the shipping method to be updated.
|
||||||
*/
|
*/
|
||||||
input: Pick<OrderWorkflow.UpdateOrderEditShippingMethodWorkflowInput, "order_id" | "action_id">
|
input: Pick<
|
||||||
|
OrderWorkflow.UpdateOrderEditShippingMethodWorkflowInput,
|
||||||
|
"order_id" | "action_id"
|
||||||
|
>
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -117,8 +123,10 @@ export const updateOrderEditShippingMethodWorkflowId =
|
|||||||
export const updateOrderEditShippingMethodWorkflow = createWorkflow(
|
export const updateOrderEditShippingMethodWorkflow = createWorkflow(
|
||||||
updateOrderEditShippingMethodWorkflowId,
|
updateOrderEditShippingMethodWorkflowId,
|
||||||
function (
|
function (
|
||||||
input: WorkflowData<OrderWorkflow.UpdateOrderEditShippingMethodWorkflowInput>
|
input: WorkflowData<
|
||||||
): WorkflowResponse<OrderPreviewDTO> {
|
OrderWorkflow.UpdateOrderEditShippingMethodWorkflowInput & AdditionalData
|
||||||
|
>
|
||||||
|
) {
|
||||||
const order: OrderDTO = useRemoteQueryStep({
|
const order: OrderDTO = useRemoteQueryStep({
|
||||||
entry_point: "order_claim",
|
entry_point: "order_claim",
|
||||||
fields: ["id", "currency_code"],
|
fields: ["id", "currency_code"],
|
||||||
@@ -139,6 +147,19 @@ export const updateOrderEditShippingMethodWorkflow = createWorkflow(
|
|||||||
list: false,
|
list: false,
|
||||||
}).config({ name: "order-change-query" })
|
}).config({ name: "order-change-query" })
|
||||||
|
|
||||||
|
const setPricingContext = createHook(
|
||||||
|
"setPricingContext",
|
||||||
|
{
|
||||||
|
order: order,
|
||||||
|
order_change: orderChange,
|
||||||
|
additional_data: input.additional_data,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
resultValidator: pricingContextResult,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
const setPricingContextResult = setPricingContext.getResult()
|
||||||
|
|
||||||
const shippingOptions = when({ input }, ({ input }) => {
|
const shippingOptions = when({ input }, ({ input }) => {
|
||||||
return input.data?.custom_amount === null
|
return input.data?.custom_amount === null
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
@@ -156,6 +177,18 @@ export const updateOrderEditShippingMethodWorkflow = createWorkflow(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const pricingContext = transform(
|
||||||
|
{ action, setPricingContextResult },
|
||||||
|
(data) => {
|
||||||
|
return {
|
||||||
|
...(data.setPricingContextResult
|
||||||
|
? data.setPricingContextResult
|
||||||
|
: {}),
|
||||||
|
currency_code: data.action.currency_code,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
const shippingMethod = useRemoteQueryStep({
|
const shippingMethod = useRemoteQueryStep({
|
||||||
entry_point: "order_shipping_method",
|
entry_point: "order_shipping_method",
|
||||||
fields: ["id", "shipping_option_id"],
|
fields: ["id", "shipping_option_id"],
|
||||||
@@ -176,7 +209,7 @@ export const updateOrderEditShippingMethodWorkflow = createWorkflow(
|
|||||||
variables: {
|
variables: {
|
||||||
id: shippingMethod.shipping_option_id,
|
id: shippingMethod.shipping_option_id,
|
||||||
calculated_price: {
|
calculated_price: {
|
||||||
context: { currency_code: action.currency_code },
|
context: pricingContext,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}).config({ name: "fetch-shipping-option" })
|
}).config({ name: "fetch-shipping-option" })
|
||||||
@@ -197,6 +230,11 @@ export const updateOrderEditShippingMethodWorkflow = createWorkflow(
|
|||||||
updateOrderShippingMethodsStep([updateData.shippingMethod!])
|
updateOrderShippingMethodsStep([updateData.shippingMethod!])
|
||||||
)
|
)
|
||||||
|
|
||||||
return new WorkflowResponse(previewOrderChangeStep(input.order_id))
|
return new WorkflowResponse(
|
||||||
|
previewOrderChangeStep(input.order_id) as OrderPreviewDTO,
|
||||||
|
{
|
||||||
|
hooks: [setPricingContext] as const,
|
||||||
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@@ -7,6 +7,7 @@ import {
|
|||||||
OrderWorkflow,
|
OrderWorkflow,
|
||||||
ShippingOptionDTO,
|
ShippingOptionDTO,
|
||||||
WithCalculatedPrice,
|
WithCalculatedPrice,
|
||||||
|
AdditionalData,
|
||||||
} from "@medusajs/framework/types"
|
} from "@medusajs/framework/types"
|
||||||
import {
|
import {
|
||||||
MathBN,
|
MathBN,
|
||||||
@@ -18,6 +19,7 @@ import {
|
|||||||
import {
|
import {
|
||||||
WorkflowData,
|
WorkflowData,
|
||||||
WorkflowResponse,
|
WorkflowResponse,
|
||||||
|
createHook,
|
||||||
createStep,
|
createStep,
|
||||||
createWorkflow,
|
createWorkflow,
|
||||||
parallelize,
|
parallelize,
|
||||||
@@ -36,6 +38,7 @@ import {
|
|||||||
throwIfOrderIsCancelled,
|
throwIfOrderIsCancelled,
|
||||||
} from "../../utils/order-validation"
|
} from "../../utils/order-validation"
|
||||||
import { validateReturnReasons } from "../../utils/validate-return-reason"
|
import { validateReturnReasons } from "../../utils/validate-return-reason"
|
||||||
|
import { pricingContextResult } from "../../../cart/utils/schemas"
|
||||||
|
|
||||||
function prepareShippingMethodData({
|
function prepareShippingMethodData({
|
||||||
orderId,
|
orderId,
|
||||||
@@ -186,6 +189,7 @@ function prepareFulfillmentData({
|
|||||||
function prepareReturnShippingOptionQueryVariables({
|
function prepareReturnShippingOptionQueryVariables({
|
||||||
order,
|
order,
|
||||||
input,
|
input,
|
||||||
|
setPricingContextResult,
|
||||||
}: {
|
}: {
|
||||||
order: {
|
order: {
|
||||||
currency_code: string
|
currency_code: string
|
||||||
@@ -194,11 +198,13 @@ function prepareReturnShippingOptionQueryVariables({
|
|||||||
input: {
|
input: {
|
||||||
return_shipping?: OrderWorkflow.CreateOrderReturnWorkflowInput["return_shipping"]
|
return_shipping?: OrderWorkflow.CreateOrderReturnWorkflowInput["return_shipping"]
|
||||||
}
|
}
|
||||||
|
setPricingContextResult?: any
|
||||||
}) {
|
}) {
|
||||||
const variables = {
|
const variables = {
|
||||||
id: input.return_shipping?.option_id,
|
id: input.return_shipping?.option_id,
|
||||||
calculated_price: {
|
calculated_price: {
|
||||||
context: {
|
context: {
|
||||||
|
...(setPricingContextResult ? setPricingContextResult : {}),
|
||||||
currency_code: order.currency_code,
|
currency_code: order.currency_code,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -309,8 +315,10 @@ export const createAndCompleteReturnOrderWorkflowId =
|
|||||||
export const createAndCompleteReturnOrderWorkflow = createWorkflow(
|
export const createAndCompleteReturnOrderWorkflow = createWorkflow(
|
||||||
createAndCompleteReturnOrderWorkflowId,
|
createAndCompleteReturnOrderWorkflowId,
|
||||||
function (
|
function (
|
||||||
input: WorkflowData<OrderWorkflow.CreateOrderReturnWorkflowInput>
|
input: WorkflowData<
|
||||||
): WorkflowResponse<ReturnDTO> {
|
OrderWorkflow.CreateOrderReturnWorkflowInput & AdditionalData
|
||||||
|
>
|
||||||
|
) {
|
||||||
const order: OrderDTO = useRemoteQueryStep({
|
const order: OrderDTO = useRemoteQueryStep({
|
||||||
entry_point: "orders",
|
entry_point: "orders",
|
||||||
fields: [
|
fields: [
|
||||||
@@ -329,8 +337,20 @@ export const createAndCompleteReturnOrderWorkflow = createWorkflow(
|
|||||||
|
|
||||||
createCompleteReturnValidationStep({ order, input })
|
createCompleteReturnValidationStep({ order, input })
|
||||||
|
|
||||||
|
const setPricingContext = createHook(
|
||||||
|
"setPricingContext",
|
||||||
|
{
|
||||||
|
order,
|
||||||
|
additional_data: input.additional_data,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
resultValidator: pricingContextResult,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
const setPricingContextResult = setPricingContext.getResult()
|
||||||
|
|
||||||
const returnShippingOptionsVariables = transform(
|
const returnShippingOptionsVariables = transform(
|
||||||
{ input, order },
|
{ input, order, setPricingContextResult },
|
||||||
prepareReturnShippingOptionQueryVariables
|
prepareReturnShippingOptionQueryVariables
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -415,6 +435,8 @@ export const createAndCompleteReturnOrderWorkflow = createWorkflow(
|
|||||||
}).config({ name: "emit-return-received-event" })
|
}).config({ name: "emit-return-received-event" })
|
||||||
)
|
)
|
||||||
|
|
||||||
return new WorkflowResponse(returnCreated)
|
return new WorkflowResponse(returnCreated as ReturnDTO, {
|
||||||
|
hooks: [setPricingContext] as const,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
+43
-5
@@ -1,4 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
|
AdditionalData,
|
||||||
OrderChangeActionDTO,
|
OrderChangeActionDTO,
|
||||||
OrderChangeDTO,
|
OrderChangeDTO,
|
||||||
OrderPreviewDTO,
|
OrderPreviewDTO,
|
||||||
@@ -9,6 +10,7 @@ import { ChangeActionType, OrderChangeStatus } from "@medusajs/framework/utils"
|
|||||||
import {
|
import {
|
||||||
WorkflowData,
|
WorkflowData,
|
||||||
WorkflowResponse,
|
WorkflowResponse,
|
||||||
|
createHook,
|
||||||
createStep,
|
createStep,
|
||||||
createWorkflow,
|
createWorkflow,
|
||||||
parallelize,
|
parallelize,
|
||||||
@@ -26,6 +28,7 @@ import {
|
|||||||
throwIfOrderChangeIsNotActive,
|
throwIfOrderChangeIsNotActive,
|
||||||
} from "../../utils/order-validation"
|
} from "../../utils/order-validation"
|
||||||
import { prepareShippingMethodUpdate } from "../../utils/prepare-shipping-method"
|
import { prepareShippingMethodUpdate } from "../../utils/prepare-shipping-method"
|
||||||
|
import { pricingContextResult } from "../../../cart/utils/schemas"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The data to validate that a return's shipping method can be updated.
|
* The data to validate that a return's shipping method can be updated.
|
||||||
@@ -42,7 +45,10 @@ export type UpdateReturnShippingMethodValidationStepInput = {
|
|||||||
/**
|
/**
|
||||||
* The details of updating the shipping method.
|
* The details of updating the shipping method.
|
||||||
*/
|
*/
|
||||||
input: Pick<OrderWorkflow.UpdateReturnShippingMethodWorkflowInput, "return_id" | "action_id">
|
input: Pick<
|
||||||
|
OrderWorkflow.UpdateReturnShippingMethodWorkflowInput,
|
||||||
|
"return_id" | "action_id"
|
||||||
|
>
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -128,8 +134,10 @@ export const updateReturnShippingMethodWorkflowId =
|
|||||||
export const updateReturnShippingMethodWorkflow = createWorkflow(
|
export const updateReturnShippingMethodWorkflow = createWorkflow(
|
||||||
updateReturnShippingMethodWorkflowId,
|
updateReturnShippingMethodWorkflowId,
|
||||||
function (
|
function (
|
||||||
input: WorkflowData<OrderWorkflow.UpdateReturnShippingMethodWorkflowInput>
|
input: WorkflowData<
|
||||||
): WorkflowResponse<OrderPreviewDTO> {
|
OrderWorkflow.UpdateReturnShippingMethodWorkflowInput & AdditionalData
|
||||||
|
>
|
||||||
|
) {
|
||||||
const orderReturn: ReturnDTO = useRemoteQueryStep({
|
const orderReturn: ReturnDTO = useRemoteQueryStep({
|
||||||
entry_point: "return",
|
entry_point: "return",
|
||||||
fields: [
|
fields: [
|
||||||
@@ -157,6 +165,19 @@ export const updateReturnShippingMethodWorkflow = createWorkflow(
|
|||||||
list: false,
|
list: false,
|
||||||
}).config({ name: "order-change-query" })
|
}).config({ name: "order-change-query" })
|
||||||
|
|
||||||
|
const setPricingContext = createHook(
|
||||||
|
"setPricingContext",
|
||||||
|
{
|
||||||
|
order_return: orderReturn,
|
||||||
|
order_change: orderChange,
|
||||||
|
additional_data: input.additional_data,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
resultValidator: pricingContextResult,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
const setPricingContextResult = setPricingContext.getResult()
|
||||||
|
|
||||||
const shippingOptions = when({ input }, ({ input }) => {
|
const shippingOptions = when({ input }, ({ input }) => {
|
||||||
return input.data?.custom_amount === null
|
return input.data?.custom_amount === null
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
@@ -174,6 +195,18 @@ export const updateReturnShippingMethodWorkflow = createWorkflow(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const pricingContext = transform(
|
||||||
|
{ action, setPricingContextResult },
|
||||||
|
(data) => {
|
||||||
|
return {
|
||||||
|
...(data.setPricingContextResult
|
||||||
|
? data.setPricingContextResult
|
||||||
|
: {}),
|
||||||
|
currency_code: data.action.currency_code,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
const shippingMethod = useRemoteQueryStep({
|
const shippingMethod = useRemoteQueryStep({
|
||||||
entry_point: "order_shipping_method",
|
entry_point: "order_shipping_method",
|
||||||
fields: ["id", "shipping_option_id"],
|
fields: ["id", "shipping_option_id"],
|
||||||
@@ -194,7 +227,7 @@ export const updateReturnShippingMethodWorkflow = createWorkflow(
|
|||||||
variables: {
|
variables: {
|
||||||
id: shippingMethod.shipping_option_id,
|
id: shippingMethod.shipping_option_id,
|
||||||
calculated_price: {
|
calculated_price: {
|
||||||
context: { currency_code: action.currency_code },
|
context: pricingContext,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}).config({ name: "fetch-shipping-option" })
|
}).config({ name: "fetch-shipping-option" })
|
||||||
@@ -216,6 +249,11 @@ export const updateReturnShippingMethodWorkflow = createWorkflow(
|
|||||||
updateOrderShippingMethodsStep([updateData.shippingMethod!])
|
updateOrderShippingMethodsStep([updateData.shippingMethod!])
|
||||||
)
|
)
|
||||||
|
|
||||||
return new WorkflowResponse(previewOrderChangeStep(orderReturn.order_id))
|
return new WorkflowResponse(
|
||||||
|
previewOrderChangeStep(orderReturn.order_id) as OrderPreviewDTO,
|
||||||
|
{
|
||||||
|
hooks: [setPricingContext] as const,
|
||||||
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@@ -85,6 +85,9 @@ export function createHook<Name extends string, TInvokeInput, TInvokeOutput>(
|
|||||||
if (options.resultValidator) {
|
if (options.resultValidator) {
|
||||||
return options.resultValidator.parse(result)
|
return options.resultValidator.parse(result)
|
||||||
}
|
}
|
||||||
|
if (result === undefined) {
|
||||||
|
return new StepResponse(undefined)
|
||||||
|
}
|
||||||
return result
|
return result
|
||||||
},
|
},
|
||||||
() => void 0
|
() => void 0
|
||||||
|
|||||||
Reference in new issue
Block a user