Skip to content

Commit 3f07b01

Browse files
Add new_user hubspot contact creation (#660)
In order to populate our crm (hubspot) with new user sign-up data, we need to send the data received by our edge function to the hubspot api. The hubspot api call got tested via postman and works successfully. The new environment variable is already added to our supabase production projects edge function secrets.
2 parents f919847 + 9732573 commit 3f07b01

1 file changed

Lines changed: 84 additions & 21 deletions

File tree

supabase/functions/new_user/index.ts

Lines changed: 84 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ import { serve } from 'https://deno.land/std@0.168.0/http/server.ts'
33
// Created via https://slack.com/apps/new/A0F7XDUAZ-incoming-webhooks
44
const SLACK_WEBHOOK_URL = Deno.env.get('SLACK_USER_SIGNUP_WEBHOOK_URL')
55
if (!SLACK_WEBHOOK_URL) {
6-
throw new Error('Missing SLACK_NEW_USER_FEEDBACK_APP_OAUTH_TOKEN environment variable')
6+
throw new Error(
7+
'Missing SLACK_NEW_USER_FEEDBACK_APP_OAUTH_TOKEN environment variable'
8+
)
79
}
810
const SLACK_CHANNEL = 'user-signups'
911

10-
11-
1212
function sendSlackMessage(email: string) {
1313
const message = `:fire: *New User Signed Up *\n*User*\n${email}\n`
1414

15-
return fetch(SLACK_WEBHOOK_URL,{
15+
return fetch(SLACK_WEBHOOK_URL, {
1616
method: 'POST',
1717
headers: {
1818
'Content-Type': 'application/json',
@@ -25,38 +25,82 @@ function sendSlackMessage(email: string) {
2525
}
2626

2727
function sendToLoops(body: any) {
28-
return fetch(
29-
'https://app.loops.so/api/v1/contacts/create',
30-
{
31-
method: 'POST',
32-
headers: {
33-
'Content-Type': 'application/json',
34-
'Accept': 'application/json',
35-
'Authorization': `Bearer ${Deno.env.get('API_KEY_LOOPS')}`,
36-
},
37-
body: JSON.stringify(body),
28+
return fetch('https://app.loops.so/api/v1/contacts/create', {
29+
method: 'POST',
30+
headers: {
31+
'Content-Type': 'application/json',
32+
Accept: 'application/json',
33+
Authorization: `Bearer ${Deno.env.get('API_KEY_LOOPS')}`,
34+
},
35+
body: JSON.stringify(body),
36+
})
37+
}
38+
39+
function createHubspotContact(userData: {
40+
id: string
41+
email: string
42+
raw_user_meta_data?: {
43+
full_name?: string
44+
name?: string
45+
}
46+
}) {
47+
const name =
48+
userData.raw_user_meta_data?.full_name || userData.raw_user_meta_data?.name
49+
50+
// Fix for names without spaces - firstName should be the entire name, lastName should be undefined
51+
let firstName: string | undefined
52+
let lastName: string | undefined
53+
54+
if (name) {
55+
const nameParts = name.split(' ')
56+
if (nameParts.length > 1) {
57+
firstName = nameParts[0]
58+
lastName = nameParts[1]
59+
} else {
60+
firstName = name // Use the entire name as firstName if no space is found
3861
}
39-
)
62+
}
63+
64+
// Map Supabase user data to HubSpot properties
65+
const hubspotContactData = {
66+
properties: {
67+
db_user_id: userData.id,
68+
email: userData.email,
69+
firstname: firstName,
70+
lastname: lastName,
71+
},
72+
}
73+
74+
return fetch('https://api.hubapi.com/crm/v3/objects/contacts', {
75+
method: 'POST',
76+
headers: {
77+
'Content-Type': 'application/json',
78+
Accept: 'application/json',
79+
Authorization: `Bearer ${Deno.env.get('ACCESS_TOKEN_HUBSPOT')}`,
80+
},
81+
body: JSON.stringify(hubspotContactData),
82+
})
4083
}
4184

4285
serve(async (req) => {
4386
/**
4487
* Creates a new contact in Loops.so on a new created user in users table.
4588
*/
4689
const data = await req.json()
47-
const userEmail = data.record.email
90+
const userRecord = data.record
91+
const userEmail = userRecord.email
4892

4993
console.log(`New user with email: ${userEmail} created`)
5094

5195
const errors: {
52-
service: 'Loops' | 'Slack',
53-
message: string,
96+
service: 'Loops' | 'Slack' | 'HubSpot'
97+
message: string
5498
}[] = []
5599

56100
// Send to Loops
57101
const loopsResponse = await sendToLoops({
58102
email: userEmail,
59-
userGroup: 'Authenticated'
103+
userGroup: 'Authenticated',
60104
})
61105
if (loopsResponse.ok) {
62106
console.log('Successfully sent email to Loops')
@@ -72,15 +116,34 @@ serve(async (req) => {
72116
try {
73117
await sendSlackMessage(userEmail)
74118
console.log('Successfully sent Slack message')
75-
}
76-
catch (error) {
119+
} catch (error) {
77120
console.error(`Failed to send Slack message: ${error}`)
78121
errors.push({
79122
service: 'Slack',
80123
message: error.message,
81124
})
82125
}
83126

127+
// Create HubSpot contact
128+
try {
129+
const hubspotResponse = await createHubspotContact(userRecord)
130+
if (hubspotResponse.ok) {
131+
console.log('Successfully created HubSpot contact')
132+
} else {
133+
const errMessage = await hubspotResponse.text()
134+
errors.push({
135+
service: 'HubSpot',
136+
message: errMessage,
137+
})
138+
}
139+
} catch (error) {
140+
console.error(`Failed to create HubSpot contact: ${error}`)
141+
errors.push({
142+
service: 'HubSpot',
143+
message: error.message,
144+
})
145+
}
146+
84147
if (errors.length == 0) {
85148
return new Response(undefined, { status: 200 })
86149
} else {

0 commit comments

Comments
 (0)