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 }