Skip to content

Commit

Permalink
fix: set product variant items and subscription plans accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
opedromiranda committed Nov 21, 2023
1 parent ea6fd28 commit df54201
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 45 deletions.
5 changes: 5 additions & 0 deletions .changeset/grumpy-berries-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@crystallize/import-utilities': patch
---

fix importing variants with images and subscription plans
92 changes: 47 additions & 45 deletions src/bootstrap-tenant/bootstrapper/items/createOrUpdateItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit df54201

Please sign in to comment.