|
| 1 | +'use server'; |
| 2 | + |
| 3 | +import { authActionClientWithoutOrg } from '@/actions/safe-action'; |
| 4 | +import { auth } from '@/utils/auth'; |
| 5 | +import { db } from '@db/server'; |
| 6 | +import { headers } from 'next/headers'; |
| 7 | +import { z } from 'zod'; |
| 8 | + |
| 9 | +const cancelSchema = z.object({ |
| 10 | + organizationId: z.string().min(1), |
| 11 | +}); |
| 12 | + |
| 13 | +export const cancelOnboarding = authActionClientWithoutOrg |
| 14 | + .inputSchema(cancelSchema) |
| 15 | + .metadata({ |
| 16 | + name: 'cancel-onboarding', |
| 17 | + track: { |
| 18 | + event: 'cancel-onboarding', |
| 19 | + channel: 'server', |
| 20 | + }, |
| 21 | + }) |
| 22 | + .action(async ({ parsedInput, ctx }) => { |
| 23 | + const session = await auth.api.getSession({ |
| 24 | + headers: await headers(), |
| 25 | + }); |
| 26 | + |
| 27 | + if (!session) { |
| 28 | + return { success: false, error: 'Not authorized.' }; |
| 29 | + } |
| 30 | + |
| 31 | + // Verify the user owns this org and it's still incomplete |
| 32 | + const member = await db.member.findFirst({ |
| 33 | + where: { |
| 34 | + userId: session.user.id, |
| 35 | + organizationId: parsedInput.organizationId, |
| 36 | + role: { contains: 'owner' }, |
| 37 | + }, |
| 38 | + include: { organization: { select: { onboardingCompleted: true } } }, |
| 39 | + }); |
| 40 | + |
| 41 | + if (!member) { |
| 42 | + return { success: false, error: 'Only the owner can cancel onboarding.' }; |
| 43 | + } |
| 44 | + |
| 45 | + if (member.organization.onboardingCompleted) { |
| 46 | + return { success: false, error: 'Cannot cancel a completed organization.' }; |
| 47 | + } |
| 48 | + |
| 49 | + // Find a fallback org to switch to BEFORE deleting |
| 50 | + const fallbackOrg = await db.member.findFirst({ |
| 51 | + where: { |
| 52 | + userId: session.user.id, |
| 53 | + organizationId: { not: parsedInput.organizationId }, |
| 54 | + deactivated: false, |
| 55 | + organization: { |
| 56 | + onboardingCompleted: true, |
| 57 | + hasAccess: true, |
| 58 | + }, |
| 59 | + }, |
| 60 | + select: { organizationId: true }, |
| 61 | + orderBy: { createdAt: 'desc' }, |
| 62 | + }); |
| 63 | + |
| 64 | + // Must have a fallback org — refuse to delete if there's nowhere to go. |
| 65 | + // The UI guards this too, but a race condition could remove fallback orgs |
| 66 | + // between page render and action execution. |
| 67 | + if (!fallbackOrg) { |
| 68 | + return { success: false, error: 'No other organization to switch to.' }; |
| 69 | + } |
| 70 | + |
| 71 | + // Switch active org BEFORE deletion so the session never |
| 72 | + // references a deleted org (even if the client redirect is slow). |
| 73 | + try { |
| 74 | + await auth.api.setActiveOrganization({ |
| 75 | + headers: await headers(), |
| 76 | + body: { organizationId: fallbackOrg.organizationId }, |
| 77 | + }); |
| 78 | + } catch (error) { |
| 79 | + console.error('Failed to switch to fallback org:', error); |
| 80 | + return { success: false, error: 'Failed to switch organization.' }; |
| 81 | + } |
| 82 | + |
| 83 | + // Delete the incomplete org (cascade handles related records). |
| 84 | + // If this fails, roll back the active org switch to keep state consistent. |
| 85 | + try { |
| 86 | + await db.organization.delete({ |
| 87 | + where: { id: parsedInput.organizationId }, |
| 88 | + }); |
| 89 | + } catch (error) { |
| 90 | + console.error('Failed to delete organization:', error); |
| 91 | + try { |
| 92 | + await auth.api.setActiveOrganization({ |
| 93 | + headers: await headers(), |
| 94 | + body: { organizationId: parsedInput.organizationId }, |
| 95 | + }); |
| 96 | + } catch (rollbackError) { |
| 97 | + console.error('Failed to rollback active org switch:', rollbackError); |
| 98 | + } |
| 99 | + return { success: false, error: 'Failed to cancel onboarding.' }; |
| 100 | + } |
| 101 | + |
| 102 | + return { |
| 103 | + success: true, |
| 104 | + fallbackOrgId: fallbackOrg?.organizationId ?? null, |
| 105 | + }; |
| 106 | + }); |
0 commit comments