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 parent
4117beed58
commit
22891060f9
2 files changed
+29
-53
No files matched your search
@@ -1,14 +1,12 @@
|
|||||||
import { ProductTypes, PricingTypes } from "@medusajs/types"
|
import { PricingTypes, ProductTypes } from "@medusajs/types"
|
||||||
import {
|
import {
|
||||||
WorkflowData,
|
WorkflowData,
|
||||||
createWorkflow,
|
createWorkflow,
|
||||||
transform,
|
transform,
|
||||||
} from "@medusajs/workflows-sdk"
|
} from "@medusajs/workflows-sdk"
|
||||||
import {
|
|
||||||
createProductVariantsStep,
|
|
||||||
createVariantPricingLinkStep,
|
|
||||||
} from "../steps"
|
|
||||||
import { createPriceSetsStep } from "../../pricing"
|
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
|
// TODO: Create separate typings for the workflow input
|
||||||
type WorkflowInput = {
|
type WorkflowInput = {
|
||||||
|
|||||||
@@ -1,14 +1,17 @@
|
|||||||
import { ProductTypes } from "@medusajs/types"
|
import {
|
||||||
|
CreateProductWorkflowInputDTO,
|
||||||
|
PricingTypes,
|
||||||
|
ProductTypes,
|
||||||
|
} from "@medusajs/types"
|
||||||
|
import { isPresent } from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
WorkflowData,
|
WorkflowData,
|
||||||
createWorkflow,
|
createWorkflow,
|
||||||
transform,
|
transform,
|
||||||
} from "@medusajs/workflows-sdk"
|
} 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 { associateProductsWithSalesChannelsStep } from "../../sales-channel"
|
||||||
import { CreateProductWorkflowInputDTO } from "@medusajs/types"
|
import { createProductsStep } from "../steps/create-products"
|
||||||
|
import { createProductVariantsWorkflow } from "./create-product-variants"
|
||||||
|
|
||||||
type WorkflowInput = {
|
type WorkflowInput = {
|
||||||
products: CreateProductWorkflowInputDTO[]
|
products: CreateProductWorkflowInputDTO[]
|
||||||
@@ -25,10 +28,7 @@ export const createProductsWorkflow = createWorkflow(
|
|||||||
data.input.products.map((p) => ({
|
data.input.products.map((p) => ({
|
||||||
...p,
|
...p,
|
||||||
sales_channels: undefined,
|
sales_channels: undefined,
|
||||||
variants: p.variants?.map((v) => ({
|
variants: undefined,
|
||||||
...v,
|
|
||||||
prices: undefined,
|
|
||||||
})),
|
|
||||||
}))
|
}))
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -50,52 +50,30 @@ export const createProductsWorkflow = createWorkflow(
|
|||||||
|
|
||||||
associateProductsWithSalesChannelsStep({ links: salesChannelLinks })
|
associateProductsWithSalesChannelsStep({ links: salesChannelLinks })
|
||||||
|
|
||||||
// Note: We rely on the same order of input and output when creating products here, ensure this always holds true
|
const variantsInput = transform({ input, createdProducts }, (data) => {
|
||||||
const variantsWithAssociatedPrices = transform(
|
// TODO: Move this to a unified place for all product workflow types
|
||||||
{ input, createdProducts },
|
const productVariants: (ProductTypes.CreateProductVariantDTO & {
|
||||||
(data) => {
|
prices?: PricingTypes.CreateMoneyAmountDTO[]
|
||||||
return data.createdProducts
|
})[] = []
|
||||||
.map((p, i) => {
|
|
||||||
|
data.createdProducts.forEach((product, i) => {
|
||||||
const inputProduct = data.input.products[i]
|
const inputProduct = data.input.products[i]
|
||||||
return p.variants?.map((v, j) => ({
|
|
||||||
...v,
|
for (const inputVariant of inputProduct.variants || []) {
|
||||||
prices: inputProduct?.variants?.[j]?.prices ?? [],
|
isPresent(inputVariant) &&
|
||||||
}))
|
productVariants.push({
|
||||||
|
product_id: product.id,
|
||||||
|
...inputVariant,
|
||||||
})
|
})
|
||||||
.flat()
|
|
||||||
}
|
}
|
||||||
)
|
})
|
||||||
|
|
||||||
const pricesToCreate = transform({ variantsWithAssociatedPrices }, (data) =>
|
|
||||||
data.variantsWithAssociatedPrices.map((v) => ({
|
|
||||||
prices: v.prices ?? [],
|
|
||||||
}))
|
|
||||||
)
|
|
||||||
|
|
||||||
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 {
|
return {
|
||||||
links: data.variantAndPriceSets.map((entry) => ({
|
input: { product_variants: productVariants },
|
||||||
variant_id: entry.variant.id,
|
|
||||||
price_set_id: entry.price_set.id,
|
|
||||||
})),
|
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
)
|
|
||||||
|
|
||||||
createVariantPricingLinkStep(variantAndPriceSetLinks)
|
createProductVariantsWorkflow.runAsStep(variantsInput)
|
||||||
|
|
||||||
return createdProducts
|
return createdProducts
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user