-
Notifications
You must be signed in to change notification settings - Fork 1
/
fullcontact.js
201 lines (187 loc) · 5.09 KB
/
fullcontact.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
var Client = require('node-rest-client').Client;
var client = new Client();
var emitter = require("../core-integration-server-v2/javascripts/emitter");
var apiKey;
var errMsg = '"Connection timeout error" in FullContact';
function enrichData(data, node) {
try {
var reqObj = node.reqData;
var customObj = {};
var tFollowers = 0, gFollowers = 0, gFollowing = 0, googleFollowers = 0, linkedinFollowers =0, linkedinFollowing = 0;
var dataObj;
var title = "" ;
if(data.hasOwnProperty("socialProfiles")) {
dataObj = data.socialProfiles;
for(var i=0; i<dataObj.length; i++) {
var checkobj = dataObj[i];
if(checkobj.typeName == "Twitter") {
tFollowers = checkobj.followers;
}else if(checkobj.typeName == "github") {
gFollowers = checkobj.followers;
gFollowing = checkobj.following;
}else if(checkobj.typeName == "GooglePlus") {
googleFollowers = checkobj.followers;
}else if(checkobj.typeName == "LinkedIn") {
linkedinFollowers = checkobj.followers;
linkedinFollowing = checkobj.following;
}
}
}
if (data.hasOwnProperty("organizations")){
dataObj = data.organizations[0];
title = dataObj.title;
}
if(reqObj.hasOwnProperty('shippingAddress')) {
reqObj.type = 'order';
} else {
reqObj.type = 'customer';
}
reqObj.twitterFollowers = tFollowers;
reqObj.title = title;
reqObj.githubFollowers = gFollowers;
reqObj.githubFollowing = gFollowing;
reqObj.googleFollowers = googleFollowers;
reqObj.linkedinFollowers = linkedinFollowers;
reqObj.linkedinFollowing = linkedinFollowing;
var message = 'Person with email id ' + reqObj.email + ' had found in Clearbit';
node.dataObj = reqObj;
emitter.emit("save-to-core", node, message);
} catch(e) {
emitter.emit('error', e.message, e.stack,'',node);
}
}
function post(resArr, node, message) {
node.resData = resArr;
emitter.emit("success", node, message);
}
function findFullContact(node) {
try {
var reqObj = node.reqData;
var url = "https://api.fullcontact.com/v2/person.json?email=" + reqObj.email;
var args = {
headers : {
"X-FullContact-APIKey" : apiKey,
Accept : "application/json"
}
};
client.get(url, args, function(data, res){
try {
var status = parseInt(res.statusCode/100);
if(status == 2){
enrichData(data, node);
} else{
if(data.hasOwnProperty('message')) {
errMsg = data.message;
}
emitter.emit('error', errMsg, args.data, url, node);
}
} catch(e) {
emitter.emit('error', e.message, e.stack, url, node);
}
}).on('error', function(err) {
emitter.emit('error', errMsg, args.data, url, node);
});
} catch(e) {
emitter.emit('error', e.message, e.stack, "", node);
}
}
function getCoreCacheData(node) {
try {
actionName = node.connection.actionName.toLowerCase();
emitter.emit("get-from-core", node, function(data) {
try {
var resArr = [];
var resObj;
for(var i = 0; i < data.length; i++) {
resObj = data[i].dataObj;
if(resObj.hasOwnProperty('shippingAddress')) {
resObj.type = 'order';
} else {
resObj.type = 'customer';
}
if(i == data.length-1) {
resObj.isLast = true;
if(actionName == 'slack') {
resObj.slackFlag = true;
}
}
resArr[i] = resObj;
}
post(resArr, node, '');
} catch(e) {
emitter.emit('error', e.message, e.stack, "", node);
}
});
} catch(e) {
emitter.emit('error', e.message, e.stack, "", node);
}
}
function testApp(callback) {
try {
var url = "https://api.fullcontact.com/v2/[email protected]";
var args = {
headers : {
"X-FullContact-APIKey" : apiKey,
Accept : "application/json"
}
};
client.get(url, args, function(data, res) {
try {
var statusCode = parseInt(res.statusCode/100);
if(statusCode == 2) {
result = {
status :'success',
response: data
};
} else{
result = {
status :'error',
response : data
};
}
callback(result);
} catch(e) {
callback({status:"error", response:e.stack});
}
}).on('error', function(err) {
callback({status:"error", response:err});
});
} catch(e) {
callback({status:"error", response:e.stack});
}
}
function run(node) {
try {
var nodeType = node.connector.type.toLowerCase();
if(nodeType == 'trigger') {
getCoreCacheData(node);
} else{
findFullContact(node);
}
} catch(e) {
emitter.emit('error', e.message, e.stack, "", node);
}
}
function test(request, callback) {
try {
var credentials = request.credentials;
apiKey = credentials.apiKey;
testApp(callback);
} catch(e) {
callback({status:"error", response:e.stack});
}
}
function init(node) {
try {
var credentials = node.credentials;
apiKey = credentials.apiKey;
run(node);
} catch(e) {
emitter.emit('error', e.message, e.stack, "", node);
}
}
var FullContact = {
init : init,
test : test
};
module.exports = FullContact;