From f83e0efea2dc5920715b0436add2766aeafc7be7 Mon Sep 17 00:00:00 2001 From: Baptiste Arnaud Date: Mon, 19 Sep 2022 08:27:06 +0200 Subject: [PATCH] :bug: (stripe) Update additional items when they didn't exist --- .../shared/ChangePlanForm/ProPlanContent.tsx | 24 ++++++++------ .../ChangePlanForm/StarterPlanContent.tsx | 24 ++++++++------ apps/builder/pages/api/stripe/subscription.ts | 16 +++++----- apps/builder/playwright/tests/billing.spec.ts | 32 +++++++++---------- 4 files changed, 51 insertions(+), 45 deletions(-) diff --git a/apps/builder/components/shared/ChangePlanForm/ProPlanContent.tsx b/apps/builder/components/shared/ChangePlanForm/ProPlanContent.tsx index edd697a3c5..24d4a976b7 100644 --- a/apps/builder/components/shared/ChangePlanForm/ProPlanContent.tsx +++ b/apps/builder/components/shared/ChangePlanForm/ProPlanContent.tsx @@ -190,11 +190,13 @@ export const ProPlanContent = ({ size="sm" isLoading={selectedChatsLimitIndex === undefined} > - {parseNumberWithCommas( - chatsLimit.PRO.totalIncluded + - chatsLimit.PRO.increaseStep.amount * - (selectedChatsLimitIndex ?? 0) - )} + {selectedChatsLimitIndex !== undefined + ? parseNumberWithCommas( + chatsLimit.PRO.totalIncluded + + chatsLimit.PRO.increaseStep.amount * + selectedChatsLimitIndex + ) + : undefined} {selectedChatsLimitIndex !== 0 && ( @@ -252,11 +254,13 @@ export const ProPlanContent = ({ size="sm" isLoading={selectedStorageLimitIndex === undefined} > - {parseNumberWithCommas( - storageLimit.PRO.totalIncluded + - storageLimit.PRO.increaseStep.amount * - (selectedStorageLimitIndex ?? 0) - )} + {selectedStorageLimitIndex !== undefined + ? parseNumberWithCommas( + storageLimit.PRO.totalIncluded + + storageLimit.PRO.increaseStep.amount * + selectedStorageLimitIndex + ) + : undefined} {selectedStorageLimitIndex !== 0 && ( diff --git a/apps/builder/components/shared/ChangePlanForm/StarterPlanContent.tsx b/apps/builder/components/shared/ChangePlanForm/StarterPlanContent.tsx index fa9fd2fe0d..573439ee13 100644 --- a/apps/builder/components/shared/ChangePlanForm/StarterPlanContent.tsx +++ b/apps/builder/components/shared/ChangePlanForm/StarterPlanContent.tsx @@ -142,11 +142,13 @@ export const StarterPlanContent = ({ size="sm" isLoading={selectedChatsLimitIndex === undefined} > - {parseNumberWithCommas( - chatsLimit.STARTER.totalIncluded + - chatsLimit.STARTER.increaseStep.amount * - (selectedChatsLimitIndex ?? 0) - )} + {selectedChatsLimitIndex !== undefined + ? parseNumberWithCommas( + chatsLimit.STARTER.totalIncluded + + chatsLimit.STARTER.increaseStep.amount * + selectedChatsLimitIndex + ) + : undefined} {selectedChatsLimitIndex !== 0 && ( @@ -206,11 +208,13 @@ export const StarterPlanContent = ({ size="sm" isLoading={selectedStorageLimitIndex === undefined} > - {parseNumberWithCommas( - storageLimit.STARTER.totalIncluded + - storageLimit.STARTER.increaseStep.amount * - (selectedStorageLimitIndex ?? 0) - )} + {selectedStorageLimitIndex !== undefined + ? parseNumberWithCommas( + storageLimit.STARTER.totalIncluded + + storageLimit.STARTER.increaseStep.amount * + selectedStorageLimitIndex + ) + : undefined} {selectedStorageLimitIndex !== 0 && ( diff --git a/apps/builder/pages/api/stripe/subscription.ts b/apps/builder/pages/api/stripe/subscription.ts index e1bc632aff..f15fd74489 100644 --- a/apps/builder/pages/api/stripe/subscription.ts +++ b/apps/builder/pages/api/stripe/subscription.ts @@ -138,22 +138,22 @@ const updateSubscription = async (req: NextApiRequest) => { : process.env.STRIPE_PRO_PRICE_ID, quantity: 1, }, - currentAdditionalChatsItemId - ? { + additionalChats === 0 && !currentAdditionalChatsItemId + ? undefined + : { id: currentAdditionalChatsItemId, price: process.env.STRIPE_ADDITIONAL_CHATS_PRICE_ID, quantity: additionalChats, deleted: additionalChats === 0, - } - : undefined, - currentAdditionalStorageItemId - ? { + }, + additionalStorage === 0 && !currentAdditionalStorageItemId + ? undefined + : { id: currentAdditionalStorageItemId, price: process.env.STRIPE_ADDITIONAL_STORAGE_PRICE_ID, quantity: additionalStorage, deleted: additionalStorage === 0, - } - : undefined, + }, ].filter(isDefined) await stripe.subscriptions.update(subscription.id, { items, diff --git a/apps/builder/playwright/tests/billing.spec.ts b/apps/builder/playwright/tests/billing.spec.ts index f4b9ef2e77..b29ec54fec 100644 --- a/apps/builder/playwright/tests/billing.spec.ts +++ b/apps/builder/playwright/tests/billing.spec.ts @@ -95,38 +95,36 @@ test('plan changes should work', async ({ page }) => { price: process.env.STRIPE_STARTER_PRICE_ID, quantity: 1, }, - { - price: process.env.STRIPE_ADDITIONAL_CHATS_PRICE_ID, - quantity: 3, - }, - { - price: process.env.STRIPE_ADDITIONAL_STORAGE_PRICE_ID, - quantity: 2, - }, ], - { plan: Plan.STARTER, additionalChatsIndex: 3, additionalStorageIndex: 2 } + { plan: Plan.STARTER, additionalChatsIndex: 0, additionalStorageIndex: 0 } ) // Update plan with additional quotas await page.goto('/typebots') await page.click('text=Settings & Members') await page.click('text=Billing & Usage') - await expect(page.locator('text="/ 3,500"')).toBeVisible() - await expect(page.locator('text="/ 4 GB"')).toBeVisible() - await expect(page.locator('button >> text="3,500"')).toBeVisible() - await expect(page.locator('button >> text="4"')).toBeVisible() - await expect(page.locator('text="$73"')).toBeVisible() - await page.click('button >> text="3,500"') + await expect(page.locator('text="/ 2,000"')).toBeVisible() + await expect(page.locator('text="/ 2 GB"')).toBeVisible() + await expect(page.locator('button >> text="2,000"')).toBeVisible() + await expect(page.locator('button >> text="2"')).toBeVisible() await page.click('button >> text="2,000"') + await page.click('button >> text="3,500"') + await page.click('button >> text="2"') await page.click('button >> text="4"') - await page.click('button >> text="6"') - await expect(page.locator('text="$47"')).toBeVisible() + await expect(page.locator('text="$73"')).toBeVisible() await page.click('button >> text=Update') await expect( page.locator( 'text="Workspace STARTER plan successfully updated 🎉" >> nth=0' ) ).toBeVisible() + await page.click('text="Members"') + await page.click('text="Billing & Usage"') + await expect(page.locator('text="$73"')).toBeVisible() + await expect(page.locator('text="/ 3,500"')).toBeVisible() + await expect(page.locator('text="/ 4 GB"')).toBeVisible() + await expect(page.locator('button >> text="3,500"')).toBeVisible() + await expect(page.locator('button >> text="4"')).toBeVisible() // Upgrade to PRO await page.click('button >> text="10,000"')