Skip to content

Commit

Permalink
feat: Updated lead service to add user to MA and CRM
Browse files Browse the repository at this point in the history
  • Loading branch information
chavda-bhavik committed Jun 3, 2024
1 parent 9dcbaf9 commit ce2912b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ export class BaseReview {
let field: string;

return errors.reduce((obj, error) => {
console.log(error);
if (error.keyword === 'required') field = error.params.missingProperty;
else [, field] = error.instancePath.split('/');

Expand Down
67 changes: 49 additions & 18 deletions apps/api/src/app/shared/services/lead.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,64 @@ interface ILeadInformation {

@Injectable()
export class LeadService {
private log = false;
private accessTokenDate: Date;
private accessToken: string;
private log = process.env.NODE_ENV === 'local';
private maAccessTokenDate: Date;
private maAccessToken: string;
private crmAccessTokenDate: Date;
private crmAccessToken: string;

private async getAccessToken(): Promise<string> {
if (this.accessToken && this.accessTokenDate && new Date().getTime() - this.accessTokenDate.getTime() < 3500000) {
if (this.log) console.log('Using cached access token');
private async getMaAccessToken(): Promise<string> {
if (
this.maAccessToken &&
this.maAccessTokenDate &&
new Date().getTime() - this.maAccessTokenDate.getTime() < 3500000
) {
if (this.log) console.log('Using cached ma access token');

// 3500000 = 58 minutes
return this.accessToken;
return this.maAccessToken;
}
if (process.env.LEAD_REFRESH_TOKEN && process.env.LEAD_CLIENT_ID && process.env.LEAD_CLIENT_SECRET) {
// eslint-disable-next-line max-len
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}`;
if (this.log) console.log('Lead URL', url);

const response = await axios.post(url);
this.accessTokenDate = new Date();
this.accessToken = response.data.access_token;
if (this.log) console.log('New access token generated', this.accessToken);
this.maAccessTokenDate = new Date();
this.maAccessToken = response.data.access_token;
if (this.log) console.log('New access token generated', this.maAccessToken);

return response.data.access_token;
}
}
private async getCRMAccessToken(): Promise<string> {
if (
this.crmAccessToken &&
this.crmAccessTokenDate &&
new Date().getTime() - this.crmAccessTokenDate.getTime() < 3500000
) {
if (this.log) console.log('Using cached crm access token');

// 3500000 = 58 minutes
return this.maAccessToken;
}
if (process.env.CRM_REFRESH_TOKEN && process.env.CRM_CLIENT_ID && process.env.CRM_CLIENT_SECRET) {
// eslint-disable-next-line max-len
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}`;
if (this.log) console.log('CRM URL', url);

const response = await axios.post(url);
this.crmAccessTokenDate = new Date();
this.crmAccessToken = response.data.access_token;
if (this.log) console.log('New crm token generated', this.crmAccessToken);

return response.data.access_token;
}
}

public async createLead(data: ILeadInformation): Promise<any> {
const accessToken = await this.getAccessToken();
if (accessToken) {
const maAccessToken = await this.getMaAccessToken();
if (maAccessToken) {
// Add Lead to marketing automation
const maUrl = `https://marketingautomation.zoho.com/api/v1/json/listsubscribe?listkey=${
process.env.LEAD_LIST_KEY
Expand All @@ -47,12 +78,14 @@ export class LeadService {
{},
{
headers: {
Authorization: `Zoho-oauthtoken ${accessToken}`,
Authorization: `Zoho-oauthtoken ${maAccessToken}`,
},
}
);
if (this.log) console.log('Lead created', maResponse.data);

}
const crmAccessToken = await this.getCRMAccessToken();
if (crmAccessToken) {
// Add Lead to Zoho CRM
const crmUrl = `https://www.zohoapis.com/crm/v6/Leads`;
if (this.log) console.log(crmUrl);
Expand All @@ -70,13 +103,11 @@ export class LeadService {
},
{
headers: {
Authorization: `Zoho-oauthtoken ${accessToken}`,
Authorization: `Zoho-oauthtoken ${crmAccessToken}`,
},
}
);
if (this.log) console.log('Lead created', crmResponse.data);

return crmResponse.data;
if (this.log) console.log('CRM LEad created', crmResponse.data);
}
}
}
4 changes: 4 additions & 0 deletions apps/api/src/types/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ declare namespace NodeJS {
EMAIL_FROM: string;
EMAIL_FROM_NAME: string;

CRM_REFRESH_TOKEN: string;
CRM_CLIENT_ID: string;
CRM_CLIENT_SECRET: string;

LEAD_REFRESH_TOKEN: string;
LEAD_CLIENT_ID: string;
LEAD_CLIENT_SECRET: string;
Expand Down

0 comments on commit ce2912b

Please sign in to comment.