From 2d956931b3114d4b71ebcbb4651ed19c16b2df77 Mon Sep 17 00:00:00 2001 From: Riqwan Thamir Date: Thu, 30 May 2024 22:23:43 +0200 Subject: [PATCH] chore: append variants to created products on workflow (#7560) --- .../workflows/create-product-variants.ts | 2 +- .../src/product/workflows/create-products.ts | 21 +++++++++++++++++-- 2 files changed, 20 insertions(+), 3 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 6aebd0bc9d..a3068a2451 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 @@ -74,7 +74,7 @@ export const createProductVariantsWorkflow = createWorkflow( (data) => { return data.variantAndPriceSets.map((variantAndPriceSet) => ({ ...variantAndPriceSet.variant, - ...variantAndPriceSet.price_set, + prices: variantAndPriceSet?.price_set?.prices || [], })) } ) 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 68c9515c44..e5304d4731 100644 --- a/packages/core/core-flows/src/product/workflows/create-products.ts +++ b/packages/core/core-flows/src/product/workflows/create-products.ts @@ -73,8 +73,25 @@ export const createProductsWorkflow = createWorkflow( } }) - createProductVariantsWorkflow.runAsStep(variantsInput) + const createdVariants = + createProductVariantsWorkflow.runAsStep(variantsInput) - return createdProducts + return transform({ createdVariants, input, createdProducts }, (data) => { + const variantMap: Record = {} + + for (const variant of data.createdVariants) { + const array = variantMap[variant.product_id!] || [] + + array.push(variant) + + variantMap[variant.product_id!] = array + } + + for (const product of data.createdProducts) { + product.variants = variantMap[product.id] || [] + } + + return data.createdProducts + }) } )