-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathleadNotesReport.js
109 lines (103 loc) · 2.79 KB
/
leadNotesReport.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const AWS = require('aws-sdk');
const fs = require('fs');
const s3 = new AWS.S3();
const fetch = require('node-fetch');
const btoa = require('btoa');
const fileName = 'leadNotes.json';
if (fs.existsSync(fileName)) {
fs.unlinkSync(fileName);
}
let headers = {
'Authorization': 'Basic ' + btoa(process.env.INSIGHTLY_API_KEY),
'Accept-Encoding': 'gzip'
};
const insightlyLeads = [];
const insightlyLeadNotes = {};
fetchInsightlyLeads(0);
function fetchInsightlyLeads(skip) {
fetch(`https://api.insight.ly/v2.2/Leads/?converted=true&top=500&skip=${skip}`, {
method: 'GET',
headers
})
.then(res => {
return res.json();
})
.then(leads => {
if (typeof leadStatuses === 'string') {
console.log(leadStatuses);
if (leadStatuses.contains('Second')) {
headers = {
'Authorization': 'Basic ' + btoa(process.env.INSIGHTLY_API_KEY_2),
'Accept-Encoding': 'gzip'
};
return fetchInsightlyLeads();
} else {
return process.exit();
}
}
Array.prototype.push.apply(insightlyLeads, leads);
console.log(`fetched ${leads.length} leads`);
if (leads.length === 500) {
setTimeout(() => {
fetchInsightlyLeads(skip + 500);
}, 200);
} else {
console.log('fetching insightly lead notes');
fetchInsightlyLeadNotes(0);
}
}).catch(error => {
console.log(error);
});
}
function fetchInsightlyLeadNotes(idx) {
const lead = insightlyLeads[idx];
fetch(`https://api.insight.ly/v2.2/Leads/${lead.LEAD_ID}/Notes`, {
method: 'GET',
headers
})
.then(res => {
return res.json();
})
.then(notes => {
if (typeof notes === 'string') {
console.log(notes);
if (notes.contains('Second')) {
headers = {
'Authorization': 'Basic ' + btoa(process.env.INSIGHTLY_API_KEY_2),
'Accept-Encoding': 'gzip'
};
return fetchInsightlyLeadNotes();
} else {
return process.exit();
}
}
if ((idx + 1) % 500 === 0) console.log(`fetched notes for ${idx + 1} leads`);
insightlyLeadNotes[lead.LEAD_ID] = notes;
if (idx < insightlyLeads.length - 1) {
setTimeout(() => {
fetchInsightlyLeadNotes(++idx);
}, 200);
} else {
fs.writeFileSync(fileName, JSON.stringify(insightlyLeadNotes, null, 2), 'utf8');
console.log('uploading user notes to S3');
uploadNotes();
}
})
.catch(error => {
console.log(error);
});
}
function uploadNotes() {
s3.putObject({
Bucket: process.env.S3_BUCKET_NAME,
Key: `${fileName}`,
Body: new Buffer(fs.readFileSync(fileName))
}, (err, data) => {
if (err) {
console.log(err)
} else {
console.log(`Successfully uploaded lead notes to Amazon S3/${process.env.S3_BUCKET_NAME}/${fileName}`);
}
process.exit();
});
}