From 8e37fe1739afe08609b607cbf3787d73f7621823 Mon Sep 17 00:00:00 2001 From: Mike Murray Date: Fri, 13 Mar 2020 15:22:36 -0700 Subject: [PATCH 1/3] fix: variant clone produces null and undefined values Signed-off-by: Mike Murray --- .../product/mutations/cloneProductVariants.js | 43 +++++++++++++------ 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/src/core-services/product/mutations/cloneProductVariants.js b/src/core-services/product/mutations/cloneProductVariants.js index c6913300790..137284ab146 100644 --- a/src/core-services/product/mutations/cloneProductVariants.js +++ b/src/core-services/product/mutations/cloneProductVariants.js @@ -75,29 +75,44 @@ export default async function cloneProductVariants(context, input) { await Promise.all(sortedVariants.map(async (sortedVariant) => { const originalVariantId = sortedVariant._id; let type = "child"; - const clonedVariantObject = {}; + let clonedVariantObject; if (variantId === sortedVariant._id) { type = "parent"; - Object.assign(clonedVariantObject, sortedVariant, { + clonedVariantObject = { + ...sortedVariant, _id: variantNewId, - title: `${sortedVariant.title} - copy`, - optionTitle: `${sortedVariant.optionTitle} - copy` - }); + title: `${sortedVariant.title || "Untitled"} - copy`, + optionTitle: `${sortedVariant.optionTitle || "Untitled"} - copy` + }; } else { const parentIndex = sortedVariant.ancestors.indexOf(variantId); const ancestorsClone = sortedVariant.ancestors.slice(0); // if variantId exists in ancestors, we override it by new _id if (parentIndex >= 0) ancestorsClone.splice(parentIndex, 1, variantNewId); - Object.assign(clonedVariantObject, existingVariant, { + + clonedVariantObject = { + ...existingVariant, _id: Random.id(), ancestors: ancestorsClone, - title: `${sortedVariant.title}`, - optionTitle: `${sortedVariant.optionTitle}`, - height: `${sortedVariant.height}`, - width: `${sortedVariant.width}`, - weight: `${sortedVariant.weight}`, - length: `${sortedVariant.length}` - }); + title: `${sortedVariant.title || "Untitled"} - copy`, + optionTitle: `${sortedVariant.optionTitle || "Untitled"} - copy` + }; + + if (typeof sortedVariant.height === "number" && sortedVariant.height >= 0) { + clonedVariantObject.height = sortedVariant.height; + } + + if (typeof sortedVariant.width === "number" && sortedVariant.width >= 0) { + clonedVariantObject.width = sortedVariant.width; + } + + if (typeof sortedVariant.weight === "number" && sortedVariant.weight >= 0) { + clonedVariantObject.weight = sortedVariant.weight; + } + + if (typeof sortedVariant.length === "number" && sortedVariant.length >= 0) { + clonedVariantObject.length = sortedVariant.length; + } } delete clonedVariantObject.updatedAt; delete clonedVariantObject.createdAt; @@ -125,7 +140,7 @@ export default async function cloneProductVariants(context, input) { })); const newFinalProduct = await Products.findOne({ _id: variantNewId }); - + console.log("NEW FINAL VARIANT PRICE", newFinalProduct._id, newFinalProduct.length) return newFinalProduct; })); From 42a29344125da5cf543397d9def579797db00f1e Mon Sep 17 00:00:00 2001 From: Mike Murray Date: Fri, 13 Mar 2020 15:53:29 -0700 Subject: [PATCH 2/3] fix: remove console log Signed-off-by: Mike Murray --- src/core-services/product/mutations/cloneProductVariants.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core-services/product/mutations/cloneProductVariants.js b/src/core-services/product/mutations/cloneProductVariants.js index 137284ab146..22ef8640693 100644 --- a/src/core-services/product/mutations/cloneProductVariants.js +++ b/src/core-services/product/mutations/cloneProductVariants.js @@ -140,7 +140,7 @@ export default async function cloneProductVariants(context, input) { })); const newFinalProduct = await Products.findOne({ _id: variantNewId }); - console.log("NEW FINAL VARIANT PRICE", newFinalProduct._id, newFinalProduct.length) + return newFinalProduct; })); From a7dff9767fa8ff52e78d9708f85124be0d57378c Mon Sep 17 00:00:00 2001 From: Mike Murray Date: Fri, 13 Mar 2020 16:52:32 -0700 Subject: [PATCH 3/3] fix: only include title and optionTitle if defined Signed-off-by: Mike Murray --- .../product/mutations/cloneProductVariants.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/core-services/product/mutations/cloneProductVariants.js b/src/core-services/product/mutations/cloneProductVariants.js index 22ef8640693..0b51317b6fe 100644 --- a/src/core-services/product/mutations/cloneProductVariants.js +++ b/src/core-services/product/mutations/cloneProductVariants.js @@ -93,11 +93,17 @@ export default async function cloneProductVariants(context, input) { clonedVariantObject = { ...existingVariant, _id: Random.id(), - ancestors: ancestorsClone, - title: `${sortedVariant.title || "Untitled"} - copy`, - optionTitle: `${sortedVariant.optionTitle || "Untitled"} - copy` + ancestors: ancestorsClone }; + if (typeof sortedVariant.title === "string") { + clonedVariantObject.title = sortedVariant.title; + } + + if (typeof sortedVariant.optionTitle === "string") { + clonedVariantObject.optionTitle = sortedVariant.optionTitle; + } + if (typeof sortedVariant.height === "number" && sortedVariant.height >= 0) { clonedVariantObject.height = sortedVariant.height; }