@@ -8,26 +8,85 @@ interface ILeadInformation {
88 'Last Name' : string ;
99 'Lead Email' : string ;
1010 'Signup Method' : LEAD_SIGNUP_USING ;
11- 'Mentioned Role' : string ;
12- 'Lead Source' : string ;
13- 'Company Size ' : string ;
11+ Role : string ;
12+ 'CRM Source' : string ;
13+ 'Est. Employees ' : string ;
1414}
1515
1616@Injectable ( )
1717export class LeadService {
1818 private log = process . env . NODE_ENV === 'local' ;
19+ private maAccessTokenDate : Date ;
20+ private maAccessToken : string ;
21+
22+ private async getMaAccessToken ( ) : Promise < string > {
23+ if (
24+ this . maAccessToken &&
25+ this . maAccessTokenDate &&
26+ new Date ( ) . getTime ( ) - this . maAccessTokenDate . getTime ( ) < 3500000
27+ ) {
28+ if ( this . log ) console . log ( 'Using cached ma access token' ) ;
29+
30+ // 3500000 = 58 minutes
31+ return this . maAccessToken ;
32+ }
33+ if ( process . env . LEAD_REFRESH_TOKEN && process . env . LEAD_CLIENT_ID && process . env . LEAD_CLIENT_SECRET ) {
34+ // eslint-disable-next-line max-len
35+ 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 } ` ;
36+ if ( this . log ) console . log ( 'Lead URL' , url ) ;
37+
38+ const response = await axios . post ( url ) ;
39+ this . maAccessTokenDate = new Date ( ) ;
40+ this . maAccessToken = response . data . access_token ;
41+ if ( this . log ) console . log ( 'New access token generated' , this . maAccessToken ) ;
42+
43+ return response . data . access_token ;
44+ }
45+
46+ return undefined ;
47+ }
1948
2049 public async createLead ( data : ILeadInformation ) : Promise < void > {
50+ const maAccessToken = await this . getMaAccessToken ( ) ;
51+ if ( maAccessToken ) {
52+ const leadData = JSON . stringify ( {
53+ 'First Name' : data [ 'First Name' ] ,
54+ 'Last Name' : data [ 'Last Name' ] ,
55+ 'Lead Email' : data [ 'Lead Email' ] ,
56+ Role : data . Role ,
57+ 'Est. Employees' : data [ 'Est. Employees' ] ,
58+ 'CRM Source' : data [ 'CRM Source' ] ,
59+ } ) ;
60+ // Add Lead to marketing automation
61+ // eslint-disable-next-line max-len
62+ 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 } ` ;
63+ if ( this . log ) console . log ( maUrl ) ;
64+
65+ try {
66+ const maResponse = await axios . post (
67+ maUrl ,
68+ { } ,
69+ {
70+ headers : {
71+ Authorization : `Zoho-oauthtoken ${ maAccessToken } ` ,
72+ } ,
73+ }
74+ ) ;
75+ if ( this . log ) console . log ( 'Lead created' , maResponse . data ) ;
76+ } catch ( error ) {
77+ captureException ( error ) ;
78+ }
79+ }
2180 if ( process . env . LEAD_MAKE_WEBHOOK_URL ) {
2281 try {
2382 await axios . post ( process . env . LEAD_MAKE_WEBHOOK_URL , {
2483 firstName : data [ 'First Name' ] ,
2584 lastName : data [ 'Last Name' ] ,
2685 email : data [ 'Lead Email' ] ,
2786 signupMethod : data [ 'Signup Method' ] ,
28- mentionedRole : data [ 'Mentioned Role' ] ,
29- leadSource : data [ 'Lead Source' ] ,
30- companySize : data [ 'Company Size ' ] ,
87+ mentionedRole : data . Role ,
88+ leadSource : data [ 'CRM Source' ] ,
89+ companySize : data [ 'Est. Employees ' ] ,
3190 createdAt : new Date ( ) ,
3291 } ) ;
3392 if ( this . log ) console . log ( 'Lead data sent to Make.com webhook' ) ;
0 commit comments