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:
Riqwan Thamir authored and GitHub committed 2024-05-30 14:04:59 +00:00
1 parent 4117beed58
commit 22891060f9
2 files changed
+32 -56

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) => {
const inputProduct = data.input.products[i]
return p.variants?.map((v, j) => ({
...v,
prices: inputProduct?.variants?.[j]?.prices ?? [],
}))
})
.flat()
}
)
const pricesToCreate = transform({ variantsWithAssociatedPrices }, (data) => data.createdProducts.forEach((product, i) => {
data.variantsWithAssociatedPrices.map((v) => ({ const inputProduct = data.input.products[i]
prices: v.prices ?? [],
}))
)
const createdPriceSets = createPriceSetsStep(pricesToCreate) for (const inputVariant of inputProduct.variants || []) {
isPresent(inputVariant) &&
const variantAndPriceSets = transform( productVariants.push({
{ variantsWithAssociatedPrices, createdPriceSets }, product_id: product.id,
(data) => ...inputVariant,
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,
})),
} }
} })
)
createVariantPricingLinkStep(variantAndPriceSetLinks) return {
input: { product_variants: productVariants },
}
})
createProductVariantsWorkflow.runAsStep(variantsInput)
return createdProducts return createdProducts
} }