feat(core-flows, product): options checks on product create/update (#9171)
**What** - validate that variants are unique with respect to options on product update/create and variant update/create - validate that the product has options upon creation - ensure variants have the same number of option values as the product has options - admin error handling - update tests --- FIXES FRMW-2707 CC-556
This commit is contained in:
@@ -4,19 +4,52 @@ import {
|
||||
PricingTypes,
|
||||
ProductTypes,
|
||||
} from "@medusajs/framework/types"
|
||||
import { ProductWorkflowEvents, isPresent } from "@medusajs/framework/utils"
|
||||
import {
|
||||
ProductWorkflowEvents,
|
||||
isPresent,
|
||||
MedusaError,
|
||||
} from "@medusajs/framework/utils"
|
||||
import {
|
||||
WorkflowData,
|
||||
WorkflowResponse,
|
||||
createHook,
|
||||
createWorkflow,
|
||||
transform,
|
||||
createStep,
|
||||
} from "@medusajs/framework/workflows-sdk"
|
||||
import { emitEventStep } from "../../common"
|
||||
import { associateProductsWithSalesChannelsStep } from "../../sales-channel"
|
||||
import { createProductsStep } from "../steps/create-products"
|
||||
import { createProductVariantsWorkflow } from "./create-product-variants"
|
||||
|
||||
interface ValidateProductInputStepInput {
|
||||
products: CreateProductWorkflowInputDTO[]
|
||||
}
|
||||
|
||||
const validateProductInputStepId = "validate-product-input"
|
||||
/**
|
||||
* This step validates a product data before creation.
|
||||
*/
|
||||
const validateProductInputStep = createStep(
|
||||
validateProductInputStepId,
|
||||
async (data: ValidateProductInputStepInput) => {
|
||||
const { products } = data
|
||||
|
||||
const missingOptionsProductTitles = products
|
||||
.filter((product) => !product.options?.length)
|
||||
.map((product) => product.title)
|
||||
|
||||
if (missingOptionsProductTitles.length) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
`Product options are not provided for: [${missingOptionsProductTitles.join(
|
||||
", "
|
||||
)}].`
|
||||
)
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
export type CreateProductsWorkflowInput = {
|
||||
products: CreateProductWorkflowInputDTO[]
|
||||
} & AdditionalData
|
||||
@@ -37,6 +70,8 @@ export const createProductsWorkflow = createWorkflow(
|
||||
}))
|
||||
)
|
||||
|
||||
validateProductInputStep({ products: productWithoutExternalRelations })
|
||||
|
||||
const createdProducts = createProductsStep(productWithoutExternalRelations)
|
||||
|
||||
const salesChannelLinks = transform({ input, createdProducts }, (data) => {
|
||||
|
||||
Reference in New Issue
Block a user