Skip to content

Commit b7067a2

Browse files
feat: Added zoho marketting automation (#1104)
2 parents 14df668 + cd7b54f commit b7067a2

File tree

4 files changed

+72
-13
lines changed

4 files changed

+72
-13
lines changed

apps/api/src/app/auth/usecases/onboard-user/onboard-user.usecase.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ export class OnboardUser {
4747
'First Name': updatedUser.firstName,
4848
'Last Name': updatedUser.lastName,
4949
'Lead Email': updatedUser.email,
50-
'Lead Source': updatedUser.source,
51-
'Mentioned Role': updatedUser.role,
50+
'CRM Source': updatedUser.source,
51+
Role: updatedUser.role,
5252
'Signup Method': updatedUser.signupMethod as LEAD_SIGNUP_USING,
53-
'Company Size': updatedUser.companySize,
53+
'Est. Employees': updatedUser.companySize,
5454
});
5555
} catch (error) {
5656
captureException(error);

apps/api/src/app/shared/services/lead.service.ts

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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()
1717
export 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');

apps/api/src/app/team/usecase/accept-invitation/accept-invitation.usecase.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,10 @@ export class AcceptInvitation {
108108
'First Name': user.firstName,
109109
'Last Name': user.lastName,
110110
'Lead Email': user.email,
111-
'Lead Source': 'Invitation',
112-
'Mentioned Role': user.role,
111+
'CRM Source': 'Invitation',
112+
Role: user.role,
113113
'Signup Method': LEAD_SIGNUP_USING.EMAIL,
114-
'Company Size': user.companySize,
114+
'Est. Employees': user.companySize,
115115
});
116116
} catch (error) {
117117
captureException(error);

apps/web/config/constants.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ export const HOW_HEARD_ABOUT_US = [
443443
{ value: 'Google Search', label: 'Google Search' },
444444
{ value: 'Bubble.io', label: 'Bubble.io' },
445445
{ value: 'Colleague', label: 'Colleague' },
446-
{ value: 'Linkdin', label: 'Linkdin' },
446+
{ value: 'Linkedin', label: 'Linkedin' },
447447
{ value: 'Invitation', label: 'Invitation' },
448448
{ value: 'AI (ChatGPT, Perplexity, Claude ...)', label: 'AI (ChatGPT, Perplexity, Claude ...)' },
449449
];

0 commit comments

Comments
 (0)