|
| 1 | +'use server'; |
| 2 | + |
| 3 | +import { createStripeCustomer } from '@/actions/organization/lib/create-stripe-customer'; |
| 4 | +import { initializeOrganization } from '@/actions/organization/lib/initialize-organization'; |
| 5 | +import { authActionClientWithoutOrg } from '@/actions/safe-action'; |
| 6 | +import { auth } from '@/utils/auth'; |
| 7 | +import { db } from '@comp/db'; |
| 8 | +import { revalidatePath } from 'next/cache'; |
| 9 | +import { headers } from 'next/headers'; |
| 10 | +import { z } from 'zod'; |
| 11 | + |
| 12 | +const createOrganizationMinimalSchema = z.object({ |
| 13 | + organizationName: z.string().min(1, 'Organization name is required'), |
| 14 | + website: z.string().url('Please enter a valid URL'), |
| 15 | + frameworkIds: z.array(z.string()).default([]), |
| 16 | +}); |
| 17 | + |
| 18 | +export const createOrganizationMinimal = authActionClientWithoutOrg |
| 19 | + .inputSchema(createOrganizationMinimalSchema) |
| 20 | + .metadata({ |
| 21 | + name: 'create-organization-minimal', |
| 22 | + track: { |
| 23 | + event: 'create-organization-minimal', |
| 24 | + channel: 'server', |
| 25 | + }, |
| 26 | + }) |
| 27 | + .action(async ({ parsedInput, ctx }) => { |
| 28 | + try { |
| 29 | + const session = await auth.api.getSession({ |
| 30 | + headers: await headers(), |
| 31 | + }); |
| 32 | + |
| 33 | + if (!session) { |
| 34 | + return { |
| 35 | + success: false, |
| 36 | + error: 'Not authorized.', |
| 37 | + }; |
| 38 | + } |
| 39 | + |
| 40 | + // Create a new organization directly in the database |
| 41 | + const randomSuffix = Math.floor(100000 + Math.random() * 900000).toString(); |
| 42 | + |
| 43 | + const newOrg = await db.organization.create({ |
| 44 | + data: { |
| 45 | + name: parsedInput.organizationName, |
| 46 | + website: parsedInput.website, |
| 47 | + members: { |
| 48 | + create: { |
| 49 | + userId: session.user.id, |
| 50 | + role: 'owner', |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }); |
| 55 | + |
| 56 | + const orgId = newOrg.id; |
| 57 | + |
| 58 | + // Create onboarding record for new org (mark as completed since they're skipping) |
| 59 | + await db.onboarding.create({ |
| 60 | + data: { |
| 61 | + organizationId: orgId, |
| 62 | + completed: true, |
| 63 | + }, |
| 64 | + }); |
| 65 | + |
| 66 | + // Create Stripe customer for new org |
| 67 | + const stripeCustomerId = await createStripeCustomer({ |
| 68 | + name: parsedInput.organizationName, |
| 69 | + email: session.user.email, |
| 70 | + organizationId: orgId, |
| 71 | + }); |
| 72 | + |
| 73 | + if (stripeCustomerId) { |
| 74 | + await db.organization.update({ |
| 75 | + where: { id: orgId }, |
| 76 | + data: { stripeCustomerId }, |
| 77 | + }); |
| 78 | + } |
| 79 | + |
| 80 | + // Initialize frameworks if provided |
| 81 | + if (parsedInput.frameworkIds && parsedInput.frameworkIds.length > 0) { |
| 82 | + await initializeOrganization({ |
| 83 | + frameworkIds: parsedInput.frameworkIds, |
| 84 | + organizationId: orgId, |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + // Set new org as active |
| 89 | + await auth.api.setActiveOrganization({ |
| 90 | + headers: await headers(), |
| 91 | + body: { |
| 92 | + organizationId: orgId, |
| 93 | + }, |
| 94 | + }); |
| 95 | + |
| 96 | + const userOrgs = await db.member.findMany({ |
| 97 | + where: { |
| 98 | + userId: session.user.id, |
| 99 | + }, |
| 100 | + select: { |
| 101 | + organizationId: true, |
| 102 | + }, |
| 103 | + }); |
| 104 | + |
| 105 | + for (const org of userOrgs) { |
| 106 | + revalidatePath(`/${org.organizationId}`); |
| 107 | + } |
| 108 | + |
| 109 | + revalidatePath('/'); |
| 110 | + revalidatePath(`/${orgId}`); |
| 111 | + revalidatePath('/setup'); |
| 112 | + |
| 113 | + return { |
| 114 | + success: true, |
| 115 | + organizationId: orgId, |
| 116 | + }; |
| 117 | + } catch (error) { |
| 118 | + console.error('Error during organization setup:', error); |
| 119 | + |
| 120 | + // Return the actual error message for debugging |
| 121 | + if (error instanceof Error) { |
| 122 | + return { |
| 123 | + success: false, |
| 124 | + error: error.message, |
| 125 | + }; |
| 126 | + } |
| 127 | + |
| 128 | + return { |
| 129 | + success: false, |
| 130 | + error: 'Failed to setup organization', |
| 131 | + }; |
| 132 | + } |
| 133 | + }); |
0 commit comments