-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
242 lines (204 loc) · 5.81 KB
/
index.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
var request = require('request');
var _ = require('lodash');
var RelateIQ = (function() {
var url = 'https://api.relateiq.com/v2/';
function RelateIQ(apiKey, apiSecret) {
request = request.defaults({
auth: {
user: apiKey,
pass: apiSecret,
sendImmediately: true
}
});
}
var makeRequest = function(uri, data, cb) {
// set defaults and set the
// api user and secret key
// as the request's basic auth
data = data || {};
data.method = data.method || "GET";
request(url + uri, data, function(err, httpReq, body) {
if (err) return cb(err);
switch(httpReq.statusCode) {
case 200:
case 204:
break;
case 400:
return cb(new Error('bad request'));
break;
case 401:
return cb(new Error('unauthorized'));
break;
case 403:
return cb(new Error('forbidden'));
break;
case 404:
return cb(new Error('not found'));
break;
case 422:
return cb(new Error('unprocessable entity'));
break;
case 429:
return cb(new Error('too many requests'));
break;
case 500:
return cb(new Error('internal server error'))
break;
case 503:
return cb(new Error('service unavailable'));
break;
default:
return cb(new Error('unrecognized http status code: '+httpReq.statusCode));
break;
}
try {
if ("string" === typeof body) {
body = JSON.parse(body);
}
} catch (e) {
return cb(new Error('unreadable data'));
}
if (body && body.objects) {
body = body.objects;
}
cb(err, body);
});
};
RelateIQ.prototype.getAccounts = function(cb) {
makeRequest('accounts', {}, cb);
};
RelateIQ.prototype.getAccount = function(accountId, cb) {
makeRequest('accounts/' + accountId, {}, cb);
};
RelateIQ.prototype.getAccountFields = function(cb) {
makeRequest('accounts/fields', {}, cb);
};
RelateIQ.prototype.createAccount = function(account, cb) {
if (!account.name) return cb(new Error('Name is required'));
account = _.pick(account, ['name', 'fieldValues']);
makeRequest('accounts', {
method: 'POST',
json: account
}, cb);
};
RelateIQ.prototype.createContact = function(contact, cb) {
if (!contact.name) return cb(new Error('Name is required'));
contact = _.pick(contact, [
'name',
'email',
'phone',
'address',
'company',
'title'
]);
_.each(contact, function(values, key) {
if (Array.isArray(values)) {
values = _.map(values, function(value) {
return {
"value": value
}
})
} else if ("string" === typeof values) {
values = [{
"value": values
}];
} else {
return; // unrecognized
}
contact[key] = values;
});
makeRequest('contacts', {
method: 'POST',
json: {
properties: contact
}
}, cb);
};
RelateIQ.prototype.getContactByEmail = function(email, cb) {
makeRequest('contacts?properties.email='+email, {}, cb);
};
RelateIQ.prototype.updateContact = function(contact, cb) {
makeRequest('contacts/' + contact.id, {
method: 'PUT',
json: contact
}, cb);
};
RelateIQ.prototype.getContacts = function(cb) {
makeRequest('contacts', {}, cb);
};
RelateIQ.prototype.getContact = function(contactid, cb) {
makeRequest('contacts/' + contactid, {}, cb);
};
RelateIQ.prototype.getUser = function(userid, cb) {
makeRequest('users/' + userid, {}, cb);
};
RelateIQ.prototype.getLists = function(cb) {
makeRequest('lists?_start=0&_limit=50', {}, cb);
};
RelateIQ.prototype.getListItems = function(listId, cb) {
var res = [];
var getItemsStart = function (start) {
makeRequest('lists/' + listId + '/listitems?_start=' + (start * 50) + '&_limit=50', {}, function (err, data) {
if (err)
cb(err);
else {
res = res.concat(data);
if (data.length < 50) {
cb(err, res);
} else {
getItemsStart(start + 1);
}
}
});
}
getItemsStart(0);
};
RelateIQ.prototype.getList = function(listId, cb) {
makeRequest('lists/' + listId, {}, cb);
};
RelateIQ.prototype.getListItem = function(listId, listItemId, cb) {
makeRequest('lists/' + listId + '/listitems/' + listItemId, {}, cb);
};
RelateIQ.prototype.getListItemsByContactId = function(listId, contactIds, cb) {
contactIds = [].concat(contactIds);
makeRequest('lists/' + listId + '/listitems?contactIds=' + contactIds.join(','), {}, cb);
};
RelateIQ.prototype.createListItem = function(listId, listItem, cb) {
if (!listItem.accountId && !listItem.contactIds) {
return cb(new Error('accountId or contactIds is required'));
}
listItem = _.pick(listItem, [
"accountId",
"contactIds",
"name",
"fieldValues"
]);
var req = {
method: "POST",
json: listItem
};
makeRequest('lists/' + listId + '/listitems', req, cb);
};
RelateIQ.prototype.updateListItem = function(listId, listItemId, listItem, cb) {
var req = {
method: "PUT",
json: listItem
};
makeRequest('lists/' + listId + '/listitems/' + listItemId, req, cb);
};
RelateIQ.prototype.removeListItem = function(listId, listItemId, cb) {
var req = {
method: "DELETE"
};
makeRequest('lists/' + listId + '/listitems/' + listItemId, req, cb);
};
RelateIQ.prototype.createEvent = function(body, cb) {
var req = {
method: "PUT",
json: body
};
makeRequest('events/', req, cb);
};
return RelateIQ;
})();
module.exports = RelateIQ;