@@ -16,123 +16,24 @@ interface ILeadInformation {
16
16
@Injectable ( )
17
17
export class LeadService {
18
18
private log = process . env . NODE_ENV === 'local' ;
19
- private maAccessTokenDate : Date ;
20
- private maAccessToken : string ;
21
- private crmAccessTokenDate : Date ;
22
- private crmAccessToken : string ;
23
-
24
- private async getMaAccessToken ( ) : Promise < string > {
25
- if (
26
- this . maAccessToken &&
27
- this . maAccessTokenDate &&
28
- new Date ( ) . getTime ( ) - this . maAccessTokenDate . getTime ( ) < 3500000
29
- ) {
30
- if ( this . log ) console . log ( 'Using cached ma access token' ) ;
31
-
32
- // 3500000 = 58 minutes
33
- return this . maAccessToken ;
34
- }
35
- if ( process . env . LEAD_REFRESH_TOKEN && process . env . LEAD_CLIENT_ID && process . env . LEAD_CLIENT_SECRET ) {
36
- // eslint-disable-next-line max-len
37
- const url = `https://accounts.zoho.com/oauth/v2/token?client_id=${ process . env . LEAD_CLIENT_ID } &grant_type=refresh_token&client_secret=${ process . env . LEAD_CLIENT_SECRET } &refresh_token=${ process . env . LEAD_REFRESH_TOKEN } ` ;
38
- if ( this . log ) console . log ( 'Lead URL' , url ) ;
39
-
40
- const response = await axios . post ( url ) ;
41
- this . maAccessTokenDate = new Date ( ) ;
42
- this . maAccessToken = response . data . access_token ;
43
- if ( this . log ) console . log ( 'New access token generated' , this . maAccessToken ) ;
44
-
45
- return response . data . access_token ;
46
- }
47
-
48
- return undefined ;
49
- }
50
- private async getCRMAccessToken ( ) : Promise < string > {
51
- if (
52
- this . crmAccessToken &&
53
- this . crmAccessTokenDate &&
54
- new Date ( ) . getTime ( ) - this . crmAccessTokenDate . getTime ( ) < 3500000
55
- ) {
56
- if ( this . log ) console . log ( 'Using cached crm access token' ) ;
57
-
58
- // 3500000 = 58 minutes
59
- return this . maAccessToken ;
60
- }
61
- if ( process . env . CRM_REFRESH_TOKEN && process . env . CRM_CLIENT_ID && process . env . CRM_CLIENT_SECRET ) {
62
- // eslint-disable-next-line max-len
63
- const url = `https://accounts.zoho.com/oauth/v2/token?client_id=${ process . env . CRM_CLIENT_ID } &grant_type=refresh_token&client_secret=${ process . env . CRM_CLIENT_SECRET } &refresh_token=${ process . env . CRM_REFRESH_TOKEN } ` ;
64
- if ( this . log ) console . log ( 'CRM URL' , url ) ;
65
-
66
- const response = await axios . post ( url ) ;
67
- this . crmAccessTokenDate = new Date ( ) ;
68
- this . crmAccessToken = response . data . access_token ;
69
- if ( this . log ) console . log ( 'New crm token generated' , this . crmAccessToken ) ;
70
-
71
- return response . data . access_token ;
72
- }
73
-
74
- return undefined ;
75
- }
76
-
77
- public async createLead ( data : ILeadInformation ) : Promise < any > {
78
- const maAccessToken = await this . getMaAccessToken ( ) ;
79
- if ( maAccessToken ) {
80
- const leadData = JSON . stringify ( {
81
- 'First Name' : data [ 'First Name' ] ,
82
- 'Last Name' : data [ 'Last Name' ] ,
83
- 'Lead Email' : data [ 'Lead Email' ] ,
84
- } ) ;
85
- // Add Lead to marketing automation
86
- // eslint-disable-next-line max-len
87
- const maUrl = `https://marketingautomation.zoho.com/api/v1/json/listsubscribe?listkey=${ process . env . LEAD_LIST_KEY } &leadinfo=${ leadData } &topic_id=${ process . env . LEAD_TOPIC_ID } ` ;
88
- if ( this . log ) console . log ( maUrl ) ;
89
-
90
- try {
91
- const maResponse = await axios . post (
92
- maUrl ,
93
- { } ,
94
- {
95
- headers : {
96
- Authorization : `Zoho-oauthtoken ${ maAccessToken } ` ,
97
- } ,
98
- }
99
- ) ;
100
- if ( this . log ) console . log ( 'Lead created' , maResponse . data ) ;
101
- } catch ( error ) {
102
- captureException ( error ) ;
103
- }
104
- }
105
- const crmAccessToken = await this . getCRMAccessToken ( ) ;
106
- if ( crmAccessToken ) {
107
- // Add Lead to Zoho CRM
108
- const crmUrl = `https://www.zohoapis.com/crm/v6/Leads` ;
109
- if ( this . log ) console . log ( crmUrl ) ;
110
19
20
+ public async createLead ( data : ILeadInformation ) : Promise < void > {
21
+ if ( process . env . LEAD_MAKE_WEBHOOK_URL ) {
111
22
try {
112
- const crmResponse = await axios . post (
113
- crmUrl ,
114
- {
115
- data : [
116
- {
117
- Last_Name : data [ 'Last Name' ] ,
118
- First_Name : data [ 'First Name' ] ,
119
- Email : data [ 'Lead Email' ] ,
120
- Lead_Source : data [ 'Lead Source' ] ,
121
- Signup_Method : data [ 'Signup Method' ] ,
122
- Mentioned_Role : data [ 'Mentioned Role' ] ,
123
- Company_Size : [ data [ 'Company Size' ] ] ,
124
- } ,
125
- ] ,
126
- } ,
127
- {
128
- headers : {
129
- Authorization : `Zoho-oauthtoken ${ crmAccessToken } ` ,
130
- } ,
131
- }
132
- ) ;
133
- if ( this . log ) console . log ( 'CRM LEad created' , crmResponse . data ) ;
23
+ await axios . post ( process . env . LEAD_MAKE_WEBHOOK_URL , {
24
+ firstName : data [ 'First Name' ] ,
25
+ lastName : data [ 'Last Name' ] ,
26
+ email : data [ 'Lead Email' ] ,
27
+ signupMethod : data [ 'Signup Method' ] ,
28
+ mentionedRole : data [ 'Mentioned Role' ] ,
29
+ leadSource : data [ 'Lead Source' ] ,
30
+ companySize : data [ 'Company Size' ] ,
31
+ createdAt : new Date ( ) ,
32
+ } ) ;
33
+ if ( this . log ) console . log ( 'Lead data sent to Make.com webhook' ) ;
134
34
} catch ( error ) {
135
35
captureException ( error ) ;
36
+ console . error ( 'Error sending data to Make.com webhook:' , error ) ;
136
37
}
137
38
}
138
39
}
0 commit comments