chore: use product variant workflow in product create workflow (#7548)
what: - reuses the product variant create workflow inside product create workflow
This commit is contained in:
@@ -1,14 +1,12 @@
|
||||
import { ProductTypes, PricingTypes } from "@medusajs/types"
|
||||
import { PricingTypes, ProductTypes } from "@medusajs/types"
|
||||
import {
|
||||
WorkflowData,
|
||||
createWorkflow,
|
||||
transform,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import {
|
||||
createProductVariantsStep,
|
||||
createVariantPricingLinkStep,
|
||||
} from "../steps"
|
||||
import { createPriceSetsStep } from "../../pricing"
|
||||
import { createProductVariantsStep } from "../steps/create-product-variants"
|
||||
import { createVariantPricingLinkStep } from "../steps/create-variant-pricing-link"
|
||||
|
||||
// TODO: Create separate typings for the workflow input
|
||||
type WorkflowInput = {
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
import { ProductTypes } from "@medusajs/types"
|
||||
import {
|
||||
CreateProductWorkflowInputDTO,
|
||||
PricingTypes,
|
||||
ProductTypes,
|
||||
} from "@medusajs/types"
|
||||
import { isPresent } from "@medusajs/utils"
|
||||
import {
|
||||
WorkflowData,
|
||||
createWorkflow,
|
||||
transform,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { createProductsStep } from "../steps/create-products"
|
||||
import { createVariantPricingLinkStep } from "../steps/create-variant-pricing-link"
|
||||
import { createPriceSetsStep } from "../../pricing"
|
||||
import { associateProductsWithSalesChannelsStep } from "../../sales-channel"
|
||||
import { CreateProductWorkflowInputDTO } from "@medusajs/types"
|
||||
import { createProductsStep } from "../steps/create-products"
|
||||
import { createProductVariantsWorkflow } from "./create-product-variants"
|
||||
|
||||
type WorkflowInput = {
|
||||
products: CreateProductWorkflowInputDTO[]
|
||||
@@ -25,10 +28,7 @@ export const createProductsWorkflow = createWorkflow(
|
||||
data.input.products.map((p) => ({
|
||||
...p,
|
||||
sales_channels: undefined,
|
||||
variants: p.variants?.map((v) => ({
|
||||
...v,
|
||||
prices: undefined,
|
||||
})),
|
||||
variants: undefined,
|
||||
}))
|
||||
)
|
||||
|
||||
@@ -50,52 +50,30 @@ export const createProductsWorkflow = createWorkflow(
|
||||
|
||||
associateProductsWithSalesChannelsStep({ links: salesChannelLinks })
|
||||
|
||||
// Note: We rely on the same order of input and output when creating products here, ensure this always holds true
|
||||
const variantsWithAssociatedPrices = transform(
|
||||
{ input, createdProducts },
|
||||
(data) => {
|
||||
return data.createdProducts
|
||||
.map((p, i) => {
|
||||
const inputProduct = data.input.products[i]
|
||||
return p.variants?.map((v, j) => ({
|
||||
...v,
|
||||
prices: inputProduct?.variants?.[j]?.prices ?? [],
|
||||
}))
|
||||
})
|
||||
.flat()
|
||||
}
|
||||
)
|
||||
const variantsInput = transform({ input, createdProducts }, (data) => {
|
||||
// TODO: Move this to a unified place for all product workflow types
|
||||
const productVariants: (ProductTypes.CreateProductVariantDTO & {
|
||||
prices?: PricingTypes.CreateMoneyAmountDTO[]
|
||||
})[] = []
|
||||
|
||||
const pricesToCreate = transform({ variantsWithAssociatedPrices }, (data) =>
|
||||
data.variantsWithAssociatedPrices.map((v) => ({
|
||||
prices: v.prices ?? [],
|
||||
}))
|
||||
)
|
||||
data.createdProducts.forEach((product, i) => {
|
||||
const inputProduct = data.input.products[i]
|
||||
|
||||
const createdPriceSets = createPriceSetsStep(pricesToCreate)
|
||||
|
||||
const variantAndPriceSets = transform(
|
||||
{ variantsWithAssociatedPrices, createdPriceSets },
|
||||
(data) =>
|
||||
data.variantsWithAssociatedPrices.map((variant, i) => ({
|
||||
variant: variant,
|
||||
price_set: data.createdPriceSets[i],
|
||||
}))
|
||||
)
|
||||
|
||||
const variantAndPriceSetLinks = transform(
|
||||
{ variantAndPriceSets },
|
||||
(data) => {
|
||||
return {
|
||||
links: data.variantAndPriceSets.map((entry) => ({
|
||||
variant_id: entry.variant.id,
|
||||
price_set_id: entry.price_set.id,
|
||||
})),
|
||||
for (const inputVariant of inputProduct.variants || []) {
|
||||
isPresent(inputVariant) &&
|
||||
productVariants.push({
|
||||
product_id: product.id,
|
||||
...inputVariant,
|
||||
})
|
||||
}
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
createVariantPricingLinkStep(variantAndPriceSetLinks)
|
||||
return {
|
||||
input: { product_variants: productVariants },
|
||||
}
|
||||
})
|
||||
|
||||
createProductVariantsWorkflow.runAsStep(variantsInput)
|
||||
|
||||
return createdProducts
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user