|
1 | 1 | 'use server'; |
2 | 2 |
|
3 | 3 | import { createStripeCustomer } from '@/actions/organization/lib/create-stripe-customer'; |
| 4 | +import { getFrameworkNames } from '@/actions/organization/lib/get-framework-names'; |
4 | 5 | import { initializeOrganization } from '@/actions/organization/lib/initialize-organization'; |
5 | 6 | import { authActionClientWithoutOrg } from '@/actions/safe-action'; |
| 7 | +import { isHubSpotConfigured } from '@/hubspot/api-client'; |
| 8 | +import { createOrUpdateCompany, findCompanyByDomain } from '@/hubspot/companies'; |
| 9 | +import { findContactByEmail } from '@/hubspot/contacts'; |
6 | 10 | import { createFleetLabelForOrg } from '@/jobs/tasks/device/create-fleet-label-for-org'; |
7 | 11 | import { onboardOrganization as onboardOrganizationTask } from '@/jobs/tasks/onboarding/onboard-organization'; |
8 | 12 | import { auth } from '@/utils/auth'; |
@@ -64,6 +68,75 @@ export const createOrganization = authActionClientWithoutOrg |
64 | 68 |
|
65 | 69 | const orgId = newOrg.id; |
66 | 70 |
|
| 71 | + // Create HubSpot company if configured |
| 72 | + if (isHubSpotConfigured()) { |
| 73 | + try { |
| 74 | + console.log('[HubSpot] Creating company for organization:', orgId); |
| 75 | + |
| 76 | + // Extract domain from website URL |
| 77 | + let domain = ''; |
| 78 | + try { |
| 79 | + const url = new URL(parsedInput.website); |
| 80 | + domain = url.hostname.replace('www.', ''); |
| 81 | + } catch (e) { |
| 82 | + console.warn('[HubSpot] Could not parse website URL:', parsedInput.website); |
| 83 | + } |
| 84 | + |
| 85 | + // Check if company already exists |
| 86 | + let companyId: string | null = null; |
| 87 | + if (domain) { |
| 88 | + const existingCompany = await findCompanyByDomain(domain); |
| 89 | + companyId = existingCompany.companyId; |
| 90 | + } |
| 91 | + |
| 92 | + // Get framework names in lowercase snake_case format |
| 93 | + const frameworkNames = await getFrameworkNames(parsedInput.frameworkIds); |
| 94 | + |
| 95 | + // Extract employee count from the context answers |
| 96 | + let employeeCount = 0; |
| 97 | + const employeeCountAnswer = parsedInput.teamSize; |
| 98 | + if (employeeCountAnswer) { |
| 99 | + // Parse employee count range (e.g., "10-50" -> 30) |
| 100 | + const match = employeeCountAnswer.match(/(\d+)-(\d+)/); |
| 101 | + if (match) { |
| 102 | + employeeCount = Math.floor((parseInt(match[1]) + parseInt(match[2])) / 2); |
| 103 | + } else if (employeeCountAnswer.includes('+')) { |
| 104 | + employeeCount = parseInt(employeeCountAnswer.replace('+', '')); |
| 105 | + } else { |
| 106 | + employeeCount = parseInt(employeeCountAnswer) || 0; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + // Check if contact exists for this user |
| 111 | + const contactResult = await findContactByEmail(session.user.email); |
| 112 | + |
| 113 | + // Create or update company |
| 114 | + const hubspotCompanyId = await createOrUpdateCompany({ |
| 115 | + companyName: parsedInput.organizationName, |
| 116 | + ...(employeeCount > 0 && { companySize: employeeCount }), |
| 117 | + complianceNeeds: frameworkNames, |
| 118 | + existingCompanyId: companyId || undefined, |
| 119 | + domain, |
| 120 | + orgId, |
| 121 | + }); |
| 122 | + |
| 123 | + if (hubspotCompanyId && contactResult.contactId) { |
| 124 | + // Associate contact with company |
| 125 | + console.log('[HubSpot] Company created/updated:', hubspotCompanyId); |
| 126 | + |
| 127 | + const { associateContactWithCompany } = await import('@/hubspot/contacts'); |
| 128 | + await associateContactWithCompany({ |
| 129 | + contactId: contactResult.contactId, |
| 130 | + companyId: hubspotCompanyId, |
| 131 | + }); |
| 132 | + console.log('[HubSpot] Associated contact with company'); |
| 133 | + } |
| 134 | + } catch (error) { |
| 135 | + console.error('[HubSpot] Error creating company:', error); |
| 136 | + // Don't throw - we don't want to block organization creation if HubSpot fails |
| 137 | + } |
| 138 | + } |
| 139 | + |
67 | 140 | // Create onboarding record for new org |
68 | 141 | await db.onboarding.create({ |
69 | 142 | data: { |
|
0 commit comments