( userId: string, userName?: string, userEmail?: string, organizationSlug?: string )
| 52 | } |
| 53 | |
| 54 | export async function createOrganizationForTeamPlan( |
| 55 | userId: string, |
| 56 | userName?: string, |
| 57 | userEmail?: string, |
| 58 | organizationSlug?: string |
| 59 | ): Promise<string> { |
| 60 | try { |
| 61 | const existingOrgId = await getUserOwnedOrganization(userId) |
| 62 | if (existingOrgId) { |
| 63 | return existingOrgId |
| 64 | } |
| 65 | |
| 66 | const organizationName = userName || `${userEmail || 'User'}'s Team` |
| 67 | const slug = |
| 68 | organizationSlug || |
| 69 | `${userId}-team-${Date.now()}` |
| 70 | .toLowerCase() |
| 71 | .replace(/[^a-z0-9-_]+/g, '-') |
| 72 | .replace(/^-|-$/g, '') |
| 73 | |
| 74 | const { organizationId: orgId } = await createOrganizationWithOwner({ |
| 75 | ownerUserId: userId, |
| 76 | name: organizationName, |
| 77 | slug, |
| 78 | metadata: { |
| 79 | createdForTeamPlan: true, |
| 80 | originalUserId: userId, |
| 81 | }, |
| 82 | }) |
| 83 | |
| 84 | logger.info('Created organization for team/enterprise plan', { |
| 85 | userId, |
| 86 | organizationId: orgId, |
| 87 | organizationName, |
| 88 | }) |
| 89 | |
| 90 | return orgId |
| 91 | } catch (error) { |
| 92 | logger.error('Failed to create organization for team/enterprise plan', { |
| 93 | userId, |
| 94 | error, |
| 95 | }) |
| 96 | throw error |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | export async function ensureOrganizationForTeamSubscription( |
| 101 | subscription: SubscriptionData |
no test coverage detected