Skip to content

Commit a04f77a

Browse files
committed
fix(already-cancelled-sub): UI should allow restoring subscription
1 parent dba7514 commit a04f77a

File tree

2 files changed

+23
-27
lines changed
  • apps/sim/app
    • api/billing/portal
    • workspace/[workspaceId]/w/components/sidebar/components/settings-modal/components/subscription/components/cancel-subscription

2 files changed

+23
-27
lines changed

apps/sim/app/api/billing/portal/route.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { db } from '@sim/db'
22
import { subscription as subscriptionTable, user } from '@sim/db/schema'
3-
import { and, eq } from 'drizzle-orm'
3+
import { and, eq, or } from 'drizzle-orm'
44
import { type NextRequest, NextResponse } from 'next/server'
55
import { getSession } from '@/lib/auth'
66
import { requireStripeClient } from '@/lib/billing/stripe-client'
@@ -38,7 +38,10 @@ export async function POST(request: NextRequest) {
3838
.where(
3939
and(
4040
eq(subscriptionTable.referenceId, organizationId),
41-
eq(subscriptionTable.status, 'active')
41+
or(
42+
eq(subscriptionTable.status, 'active'),
43+
eq(subscriptionTable.cancelAtPeriodEnd, true)
44+
)
4245
)
4346
)
4447
.limit(1)

apps/sim/app/workspace/[workspaceId]/w/components/sidebar/components/settings-modal/components/subscription/components/cancel-subscription/cancel-subscription.tsx

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
AlertDialogTitle,
1313
} from '@/components/ui/alert-dialog'
1414
import { Button } from '@/components/ui/button'
15-
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@/components/ui/tooltip'
1615
import { useSession, useSubscription } from '@/lib/auth-client'
1716
import { createLogger } from '@/lib/logs/console/logger'
1817
import { getBaseUrl } from '@/lib/urls/utils'
@@ -202,7 +201,9 @@ export function CancelSubscription({ subscription, subscriptionData }: CancelSub
202201
<>
203202
<div className='flex items-center justify-between'>
204203
<div>
205-
<span className='font-medium text-sm'>Manage Subscription</span>
204+
<span className='font-medium text-sm'>
205+
{isCancelAtPeriodEnd ? 'Restore Subscription' : 'Manage Subscription'}
206+
</span>
206207
{isCancelAtPeriodEnd && (
207208
<p className='mt-1 text-muted-foreground text-xs'>
208209
You'll keep access until {formatDate(periodEndDate)}
@@ -217,22 +218,24 @@ export function CancelSubscription({ subscription, subscriptionData }: CancelSub
217218
'h-8 rounded-[8px] font-medium text-xs transition-all duration-200',
218219
error
219220
? 'border-red-500 text-red-500 dark:border-red-500 dark:text-red-500'
220-
: 'text-muted-foreground hover:border-red-500 hover:bg-red-500 hover:text-white dark:hover:border-red-500 dark:hover:bg-red-500'
221+
: isCancelAtPeriodEnd
222+
? 'text-muted-foreground hover:border-green-500 hover:bg-green-500 hover:text-white dark:hover:border-green-500 dark:hover:bg-green-500'
223+
: 'text-muted-foreground hover:border-red-500 hover:bg-red-500 hover:text-white dark:hover:border-red-500 dark:hover:bg-red-500'
221224
)}
222225
>
223-
{error ? 'Error' : 'Manage'}
226+
{error ? 'Error' : isCancelAtPeriodEnd ? 'Restore' : 'Manage'}
224227
</Button>
225228
</div>
226229

227230
<AlertDialog open={isDialogOpen} onOpenChange={setIsDialogOpen}>
228231
<AlertDialogContent>
229232
<AlertDialogHeader>
230233
<AlertDialogTitle>
231-
{isCancelAtPeriodEnd ? 'Manage' : 'Cancel'} {subscription.plan} subscription?
234+
{isCancelAtPeriodEnd ? 'Restore' : 'Cancel'} {subscription.plan} subscription?
232235
</AlertDialogTitle>
233236
<AlertDialogDescription>
234237
{isCancelAtPeriodEnd
235-
? 'Your subscription is set to cancel at the end of the billing period. You can reactivate it or manage other settings.'
238+
? 'Your subscription is set to cancel at the end of the billing period. Would you like to keep your subscription active?'
236239
: `You'll be redirected to Stripe to manage your subscription. You'll keep access until ${formatDate(
237240
periodEndDate
238241
)}, then downgrade to free plan.`}{' '}
@@ -260,10 +263,10 @@ export function CancelSubscription({ subscription, subscriptionData }: CancelSub
260263
<AlertDialogFooter className='flex'>
261264
<AlertDialogCancel
262265
className='h-9 w-full rounded-[8px]'
263-
onClick={handleKeep}
266+
onClick={isCancelAtPeriodEnd ? () => setIsDialogOpen(false) : handleKeep}
264267
disabled={isLoading}
265268
>
266-
Keep Subscription
269+
{isCancelAtPeriodEnd ? 'Cancel' : 'Keep Subscription'}
267270
</AlertDialogCancel>
268271

269272
{(() => {
@@ -275,23 +278,13 @@ export function CancelSubscription({ subscription, subscriptionData }: CancelSub
275278
: false)
276279
) {
277280
return (
278-
<TooltipProvider delayDuration={0}>
279-
<Tooltip>
280-
<TooltipTrigger asChild>
281-
<div className='w-full'>
282-
<AlertDialogAction
283-
disabled
284-
className='h-9 w-full cursor-not-allowed rounded-[8px] bg-muted text-muted-foreground opacity-50'
285-
>
286-
Continue
287-
</AlertDialogAction>
288-
</div>
289-
</TooltipTrigger>
290-
<TooltipContent side='top'>
291-
<p>Subscription will be cancelled at end of billing period</p>
292-
</TooltipContent>
293-
</Tooltip>
294-
</TooltipProvider>
281+
<AlertDialogAction
282+
onClick={handleKeep}
283+
className='h-9 w-full rounded-[8px] bg-green-500 text-white transition-all duration-200 hover:bg-green-600 dark:bg-green-500 dark:hover:bg-green-600'
284+
disabled={isLoading}
285+
>
286+
{isLoading ? 'Restoring...' : 'Restore Subscription'}
287+
</AlertDialogAction>
295288
)
296289
}
297290
return (

0 commit comments

Comments
 (0)