-
Notifications
You must be signed in to change notification settings - Fork 290
fix(onboarding): fix org creation timeout and improve error handling #2503
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
726760d
fix(onboarding): fix org creation timeout and improve error handling
tofikwest a9cb9c5
fix(onboarding): don't delete org after session activation succeeds
tofikwest 81425cb
Merge branch 'main' into fix/onboarding-org-creation-timeout
tofikwest 8e53a10
fix(onboarding): disable Complete button while server action is running
tofikwest 7d990c2
feat(onboarding): add cancel button to abandon onboarding and return …
tofikwest b1dec0e
fix(onboarding): harden cancel action — guard completed orgs, switch …
tofikwest 14a35df
fix(onboarding): sanitize error messages shown to users
tofikwest bca9525
Merge branch 'main' into fix/onboarding-org-creation-timeout
tofikwest 03452e3
fix(onboarding): require fallback org before allowing cancel
tofikwest 9b884f0
fix(onboarding): rollback active org switch if delete fails
tofikwest 9d02143
Merge branch 'main' into fix/onboarding-org-creation-timeout
tofikwest 887dfa9
fix(onboarding): hide cancel button while onboarding submission is in…
tofikwest 438d371
Merge branch 'main' into fix/onboarding-org-creation-timeout
tofikwest File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
apps/app/src/app/(app)/onboarding/actions/cancel-onboarding.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| 'use server'; | ||
|
|
||
| import { authActionClientWithoutOrg } from '@/actions/safe-action'; | ||
| import { auth } from '@/utils/auth'; | ||
| import { db } from '@db/server'; | ||
| import { headers } from 'next/headers'; | ||
| import { z } from 'zod'; | ||
|
|
||
| const cancelSchema = z.object({ | ||
| organizationId: z.string().min(1), | ||
| }); | ||
|
|
||
| export const cancelOnboarding = authActionClientWithoutOrg | ||
| .inputSchema(cancelSchema) | ||
| .metadata({ | ||
| name: 'cancel-onboarding', | ||
| track: { | ||
| event: 'cancel-onboarding', | ||
| channel: 'server', | ||
| }, | ||
| }) | ||
| .action(async ({ parsedInput, ctx }) => { | ||
| const session = await auth.api.getSession({ | ||
| headers: await headers(), | ||
| }); | ||
|
|
||
| if (!session) { | ||
| return { success: false, error: 'Not authorized.' }; | ||
| } | ||
|
|
||
| // Verify the user owns this org and it's still incomplete | ||
| const member = await db.member.findFirst({ | ||
| where: { | ||
| userId: session.user.id, | ||
| organizationId: parsedInput.organizationId, | ||
| role: { contains: 'owner' }, | ||
| }, | ||
| include: { organization: { select: { onboardingCompleted: true } } }, | ||
| }); | ||
|
|
||
| if (!member) { | ||
| return { success: false, error: 'Only the owner can cancel onboarding.' }; | ||
| } | ||
|
|
||
| if (member.organization.onboardingCompleted) { | ||
| return { success: false, error: 'Cannot cancel a completed organization.' }; | ||
| } | ||
|
|
||
| // Find a fallback org to switch to BEFORE deleting | ||
| const fallbackOrg = await db.member.findFirst({ | ||
| where: { | ||
| userId: session.user.id, | ||
| organizationId: { not: parsedInput.organizationId }, | ||
| deactivated: false, | ||
| organization: { | ||
| onboardingCompleted: true, | ||
| hasAccess: true, | ||
| }, | ||
| }, | ||
| select: { organizationId: true }, | ||
| orderBy: { createdAt: 'desc' }, | ||
| }); | ||
|
|
||
| // Must have a fallback org — refuse to delete if there's nowhere to go. | ||
| // The UI guards this too, but a race condition could remove fallback orgs | ||
| // between page render and action execution. | ||
| if (!fallbackOrg) { | ||
| return { success: false, error: 'No other organization to switch to.' }; | ||
| } | ||
|
|
||
| // Switch active org BEFORE deletion so the session never | ||
| // references a deleted org (even if the client redirect is slow). | ||
| try { | ||
| await auth.api.setActiveOrganization({ | ||
| headers: await headers(), | ||
| body: { organizationId: fallbackOrg.organizationId }, | ||
| }); | ||
| } catch (error) { | ||
| console.error('Failed to switch to fallback org:', error); | ||
| return { success: false, error: 'Failed to switch organization.' }; | ||
| } | ||
|
|
||
| // Delete the incomplete org (cascade handles related records). | ||
| // If this fails, roll back the active org switch to keep state consistent. | ||
| try { | ||
| await db.organization.delete({ | ||
| where: { id: parsedInput.organizationId }, | ||
| }); | ||
| } catch (error) { | ||
| console.error('Failed to delete organization:', error); | ||
| try { | ||
| await auth.api.setActiveOrganization({ | ||
| headers: await headers(), | ||
| body: { organizationId: parsedInput.organizationId }, | ||
| }); | ||
| } catch (rollbackError) { | ||
| console.error('Failed to rollback active org switch:', rollbackError); | ||
| } | ||
| return { success: false, error: 'Failed to cancel onboarding.' }; | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return { | ||
| success: true, | ||
| fallbackOrgId: fallbackOrg?.organizationId ?? null, | ||
| }; | ||
| }); | ||
73 changes: 73 additions & 0 deletions
73
apps/app/src/app/(app)/onboarding/components/CancelOnboardingButton.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| 'use client'; | ||
|
|
||
| import { Button } from '@trycompai/ui/button'; | ||
| import { useAction } from 'next-safe-action/hooks'; | ||
| import { useState } from 'react'; | ||
| import { toast } from 'sonner'; | ||
| import { cancelOnboarding } from '../actions/cancel-onboarding'; | ||
|
|
||
| interface CancelOnboardingButtonProps { | ||
| organizationId: string; | ||
| hasOtherOrgs: boolean; | ||
| } | ||
|
|
||
| export function CancelOnboardingButton({ | ||
| organizationId, | ||
| hasOtherOrgs, | ||
| }: CancelOnboardingButtonProps) { | ||
| const [confirming, setConfirming] = useState(false); | ||
|
|
||
| const cancelAction = useAction(cancelOnboarding, { | ||
| onSuccess: ({ data }) => { | ||
| if (data?.success) { | ||
| const target = data.fallbackOrgId ? `/${data.fallbackOrgId}` : '/setup'; | ||
| window.location.assign(target); | ||
| } else { | ||
| toast.error(data?.error || 'Failed to cancel'); | ||
| setConfirming(false); | ||
| } | ||
| }, | ||
| onError: ({ error }) => { | ||
| toast.error(error.serverError || 'Failed to cancel'); | ||
| setConfirming(false); | ||
| }, | ||
| }); | ||
|
|
||
| if (!hasOtherOrgs) return null; | ||
|
|
||
| if (!confirming) { | ||
| return ( | ||
| <Button | ||
| type="button" | ||
| variant="ghost" | ||
| className="text-muted-foreground" | ||
| onClick={() => setConfirming(true)} | ||
| > | ||
| Cancel | ||
| </Button> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <div className="flex items-center gap-2"> | ||
| <span className="text-sm text-muted-foreground">Delete this org?</span> | ||
| <Button | ||
| type="button" | ||
| variant="destructive" | ||
| size="sm" | ||
| disabled={cancelAction.isExecuting} | ||
| onClick={() => cancelAction.execute({ organizationId })} | ||
| > | ||
| {cancelAction.isExecuting ? 'Canceling...' : 'Yes, cancel'} | ||
| </Button> | ||
| <Button | ||
| type="button" | ||
| variant="ghost" | ||
| size="sm" | ||
| onClick={() => setConfirming(false)} | ||
| > | ||
| No | ||
| </Button> | ||
| </div> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.