From df54201cd084cd08fb006cd13a9f3a9cf95c991a Mon Sep 17 00:00:00 2001 From: Pedro Miranda Date: Tue, 21 Nov 2023 11:40:19 +0000 Subject: [PATCH] fix: set product variant items and subscription plans accordingly --- .changeset/grumpy-berries-kneel.md | 5 + .../bootstrapper/items/createOrUpdateItem.ts | 92 ++++++++++--------- 2 files changed, 52 insertions(+), 45 deletions(-) create mode 100644 .changeset/grumpy-berries-kneel.md diff --git a/.changeset/grumpy-berries-kneel.md b/.changeset/grumpy-berries-kneel.md new file mode 100644 index 0000000..e3c7c1c --- /dev/null +++ b/.changeset/grumpy-berries-kneel.md @@ -0,0 +1,5 @@ +--- +'@crystallize/import-utilities': patch +--- + +fix importing variants with images and subscription plans diff --git a/src/bootstrap-tenant/bootstrapper/items/createOrUpdateItem.ts b/src/bootstrap-tenant/bootstrapper/items/createOrUpdateItem.ts index 2981fab..f11bc7e 100644 --- a/src/bootstrap-tenant/bootstrapper/items/createOrUpdateItem.ts +++ b/src/bootstrap-tenant/bootstrapper/items/createOrUpdateItem.ts @@ -385,60 +385,62 @@ export async function createOrUpdateItem( } else { delete variant.components } + } - if (jsonVariant.subscriptionPlans) { - variant.subscriptionPlans = jsonVariant.subscriptionPlans.map((sP) => { - const meteredVariables = getSubscriptionPlanMeteredVariables({ - planIdentifier: sP.identifier, - context, - }) + if (jsonVariant.images) { + variant.images = await createImagesInput({ + images: jsonVariant.images, + language, + context, + onUpdate, + }) + } - return { - identifier: sP.identifier, - periods: sP.periods.map((p) => { - const id = getSubscriptionPlanPeriodId({ - planIdentifier: sP.identifier, - periodName: p.name, - context, - }) + // This causes an internal error at the API right now. Setting the value to an empty + // array has the same outcome as setting it to null + if (variant.images === null) { + variant.images = [] + } - if (!id) { - throw new Error('Plan period id is null') - } - return { - id, - ...(p.initial && { - initial: subscriptionPlanPrincingJsonToInput( + if (jsonVariant.subscriptionPlans) { + variant.subscriptionPlans = jsonVariant.subscriptionPlans.map((sP) => { + const meteredVariables = getSubscriptionPlanMeteredVariables({ + planIdentifier: sP.identifier, + context, + }) + + return { + identifier: sP.identifier, + periods: sP.periods.map((p) => { + const id = getSubscriptionPlanPeriodId({ + planIdentifier: sP.identifier, + periodName: p.name, + context, + }) + + if (!id) { + throw new Error('Plan period id is null') + } + + return { + id, + ...(p.initial && { + initial: subscriptionPlanPrincingJsonToInput( p.initial, meteredVariables - ), - }), - recurring: subscriptionPlanPrincingJsonToInput( + ), + }), + recurring: subscriptionPlanPrincingJsonToInput( p.recurring, meteredVariables - ), - } - }), - } - }) - } - - if (jsonVariant.images) { - variant.images = await createImagesInput({ - images: jsonVariant.images, - language, - context, - onUpdate, - }) - } - - // This causes an internal error at the API right now. Setting the value to an empty - // array has the same outcome as setting it to null - if (variant.images === null) { - variant.images = [] - } + ), + } + }), + } + }) } + return variant }