@@ -36,59 +36,6 @@ function sendToLoops(body: any) {
3636 } )
3737}
3838
39- function createHubspotContact ( userData : {
40- id : string
41- email : string
42- raw_user_meta_data ?: {
43- full_name ?: string
44- name ?: string
45- is_fragments_user ?: boolean
46- }
47- } ) {
48- const name =
49- userData . raw_user_meta_data ?. full_name || userData . raw_user_meta_data ?. name
50-
51- // Fix for names without spaces - firstName should be the entire name, lastName should be undefined
52- let firstName : string | undefined
53- let lastName : string | undefined
54-
55- if ( name ) {
56- const nameParts = name . split ( ' ' )
57- if ( nameParts . length > 1 ) {
58- firstName = nameParts [ 0 ]
59- lastName = nameParts [ 1 ]
60- } else {
61- firstName = name // Use the entire name as firstName if no space is found
62- }
63- }
64-
65- // For HubSpot checkbox fields, we need to pass "true" or "false" as strings
66- const isFragmentsUser = userData . raw_user_meta_data ?. is_fragments_user
67- ? 'true'
68- : 'false'
69-
70- // Map Supabase user data to HubSpot properties
71- const hubspotContactData = {
72- properties : {
73- email : userData . email ,
74- firstname : firstName ,
75- lastname : lastName ,
76- db_user_id : userData . id ,
77- db_is_fragments_user : isFragmentsUser ,
78- } ,
79- }
80-
81- return fetch ( 'https://api.hubapi.com/crm/v3/objects/contacts' , {
82- method : 'POST' ,
83- headers : {
84- 'Content-Type' : 'application/json' ,
85- Accept : 'application/json' ,
86- Authorization : `Bearer ${ Deno . env . get ( 'ACCESS_TOKEN_HUBSPOT' ) } ` ,
87- } ,
88- body : JSON . stringify ( hubspotContactData ) ,
89- } )
90- }
91-
9239serve ( async ( req ) => {
9340 /**
9441 * Creates a new contact in Loops.so on a new created user in users table.
@@ -100,7 +47,7 @@ serve(async (req) => {
10047 console . log ( `New user with email: ${ userEmail } created` )
10148
10249 const errors : {
103- service : 'Loops' | 'Slack' | 'HubSpot'
50+ service : 'Loops' | 'Slack'
10451 message : string
10552 } [ ] = [ ]
10653
@@ -131,26 +78,6 @@ serve(async (req) => {
13178 } )
13279 }
13380
134- // Create HubSpot contact
135- try {
136- const hubspotResponse = await createHubspotContact ( userRecord )
137- if ( hubspotResponse . ok ) {
138- console . log ( 'Successfully created HubSpot contact' )
139- } else {
140- const errMessage = await hubspotResponse . text ( )
141- errors . push ( {
142- service : 'HubSpot' ,
143- message : errMessage ,
144- } )
145- }
146- } catch ( error ) {
147- console . error ( `Failed to create HubSpot contact: ${ error } ` )
148- errors . push ( {
149- service : 'HubSpot' ,
150- message : error . message ,
151- } )
152- }
153-
15481 if ( errors . length == 0 ) {
15582 return new Response ( undefined , { status : 200 } )
15683 } else {
0 commit comments