fix: Add shipping method data validation (#9542)
* fix: Add shipping method data validation * fix: return type
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
import { Modules, promiseAll } from "@medusajs/framework/utils"
|
||||
import { IFulfillmentModuleService } from "@medusajs/types"
|
||||
import { createStep, StepResponse } from "@medusajs/workflows-sdk"
|
||||
|
||||
export interface ValidateShippingMethodsDataInput {
|
||||
context: Record<string, unknown>
|
||||
options_to_validate: {
|
||||
id: string
|
||||
provider_id: string
|
||||
option_data: Record<string, unknown>
|
||||
method_data: Record<string, unknown>
|
||||
}[]
|
||||
}
|
||||
|
||||
export const validateAndReturnShippingMethodsDataStepId =
|
||||
"validate-and-return-shipping-methods-data"
|
||||
/**
|
||||
* This step validates shipping options to ensure they can be applied on a cart.
|
||||
*/
|
||||
export const validateAndReturnShippingMethodsDataStep = createStep(
|
||||
validateAndReturnShippingMethodsDataStepId,
|
||||
async (data: ValidateShippingMethodsDataInput, { container }) => {
|
||||
const { options_to_validate = [] } = data
|
||||
|
||||
if (!options_to_validate.length) {
|
||||
return new StepResponse(void 0)
|
||||
}
|
||||
|
||||
const fulfillmentModule = container.resolve<IFulfillmentModuleService>(
|
||||
Modules.FULFILLMENT
|
||||
)
|
||||
|
||||
const validatedData = await promiseAll(
|
||||
options_to_validate.map(async (option) => {
|
||||
const validated = await fulfillmentModule.validateFulfillmentData(
|
||||
option.provider_id,
|
||||
option.option_data,
|
||||
option.method_data,
|
||||
data.context
|
||||
)
|
||||
|
||||
return {
|
||||
[option.id]: validated,
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
return new StepResponse(validatedData)
|
||||
}
|
||||
)
|
||||
@@ -12,6 +12,7 @@ import {
|
||||
validateCartShippingOptionsStep,
|
||||
} from "../steps"
|
||||
import { validateCartStep } from "../steps/validate-cart"
|
||||
import { validateAndReturnShippingMethodsDataStep } from "../steps/validate-shipping-methods-data"
|
||||
import { cartFieldsForRefreshSteps } from "../utils/fields"
|
||||
import { updateCartPromotionsWorkflow } from "./update-cart-promotions"
|
||||
import { updateTaxLinesWorkflow } from "./update-tax-lines"
|
||||
@@ -28,7 +29,7 @@ export const addShippingMethodToCartWorkflowId = "add-shipping-method-to-cart"
|
||||
/**
|
||||
* This workflow adds shipping methods to a cart.
|
||||
*/
|
||||
export const addShippingMethodToWorkflow = createWorkflow(
|
||||
export const addShippingMethodToCartWorkflow = createWorkflow(
|
||||
addShippingMethodToCartWorkflowId,
|
||||
(
|
||||
input: WorkflowData<AddShippingMethodToCartWorkflowInput>
|
||||
@@ -59,6 +60,7 @@ export const addShippingMethodToWorkflow = createWorkflow(
|
||||
"name",
|
||||
"calculated_price.calculated_amount",
|
||||
"calculated_price.is_calculated_price_tax_inclusive",
|
||||
"provider_id",
|
||||
],
|
||||
variables: {
|
||||
id: optionIds,
|
||||
@@ -68,8 +70,30 @@ export const addShippingMethodToWorkflow = createWorkflow(
|
||||
},
|
||||
}).config({ name: "fetch-shipping-option" })
|
||||
|
||||
const shippingMethodInput = transform(
|
||||
const validateShippingMethodsDataInput = transform(
|
||||
{ input, shippingOptions },
|
||||
(data) => {
|
||||
return data.input.options.map((inputOption) => {
|
||||
const shippingOption = data.shippingOptions.find(
|
||||
(so) => so.id === inputOption.id
|
||||
)
|
||||
return {
|
||||
id: inputOption.id,
|
||||
provider_id: shippingOption?.provider_id,
|
||||
option_data: shippingOption?.data ?? {},
|
||||
method_data: inputOption.data ?? {},
|
||||
}
|
||||
})
|
||||
}
|
||||
)
|
||||
|
||||
const validatedMethodData = validateAndReturnShippingMethodsDataStep({
|
||||
options_to_validate: validateShippingMethodsDataInput,
|
||||
context: {}, // TODO: Add cart, when we have a better idea about what's appropriate to pass
|
||||
})
|
||||
|
||||
const shippingMethodInput = transform(
|
||||
{ input, shippingOptions, validatedMethodData },
|
||||
(data) => {
|
||||
const options = (data.input.options ?? []).map((option) => {
|
||||
const shippingOption = data.shippingOptions.find(
|
||||
@@ -83,13 +107,17 @@ export const addShippingMethodToWorkflow = createWorkflow(
|
||||
)
|
||||
}
|
||||
|
||||
const methodData = data.validatedMethodData?.find((methodData) => {
|
||||
return methodData?.[option.id]
|
||||
})
|
||||
|
||||
return {
|
||||
shipping_option_id: shippingOption.id,
|
||||
amount: shippingOption.calculated_price.calculated_amount,
|
||||
is_tax_inclusive:
|
||||
!!shippingOption.calculated_price
|
||||
.is_calculated_price_tax_inclusive,
|
||||
data: option.data ?? {},
|
||||
data: methodData?.[option.id] ?? {},
|
||||
name: shippingOption.name,
|
||||
cart_id: data.input.cart_id,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user