From 22891060f92487314fc5883de21fc313d4d6e90b Mon Sep 17 00:00:00 2001 From: Riqwan Thamir Date: Thu, 30 May 2024 16:04:59 +0200 Subject: [PATCH] chore: use product variant workflow in product create workflow (#7548) what: - reuses the product variant create workflow inside product create workflow --- .../workflows/create-product-variants.ts | 8 +- .../src/product/workflows/create-products.ts | 80 +++++++------------ 2 files changed, 32 insertions(+), 56 deletions(-) diff --git a/packages/core/core-flows/src/product/workflows/create-product-variants.ts b/packages/core/core-flows/src/product/workflows/create-product-variants.ts index 58f8747863..6aebd0bc9d 100644 --- a/packages/core/core-flows/src/product/workflows/create-product-variants.ts +++ b/packages/core/core-flows/src/product/workflows/create-product-variants.ts @@ -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 = { diff --git a/packages/core/core-flows/src/product/workflows/create-products.ts b/packages/core/core-flows/src/product/workflows/create-products.ts index fc22f47d0d..68c9515c44 100644 --- a/packages/core/core-flows/src/product/workflows/create-products.ts +++ b/packages/core/core-flows/src/product/workflows/create-products.ts @@ -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 }