-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
221 lines (198 loc) · 5.72 KB
/
app.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
const express = require('express');
var request = require('request');
const app = express();
const PORT = process.env.PORT || 5000
var Airtable = require('airtable');
var base = new Airtable({apiKey: 'keyJCRRojtU1jYCBB'}).base('appMm7V8XWifHqCja');
var array = {};
var fields = ['full_name', 'email', 'continuation_url', 'cid', 'state', 'ip', 'User state'];
const CONTACT_STATUS = {
'Incomplete': 'incomplete',
'Basic Info Complete': 'basic_info_complete',
'Video Complete': 'video_complete',
'Complete': 'completed'
}
function getDataFromAirtable(view, aryname, done) {
// Clear current fetched data array
array[aryname] = [];
// Fetch data from Airtable
base('QKids').select({
// select options here
fields: fields,
view: view
}).eachPage(function page(records, fetchNextPage) {
// This function (`page`) will get called for each page of records.
records.forEach(function (record) {
// console.log('Retrieved', record.get('country'));
let newItem = {};
for (let idx in fields) {
const field = fields[idx];
const value = record.get(field);
newItem[field] = value !== undefined ? value : '';
}
array[aryname].push(newItem);
});
// To fetch the next page of records, call `fetchNextPage`.
// If there are more records, `page` will get called again.
// If there are no more records, `done` will get called.
fetchNextPage();
}, done);
}
function postDataToGetresponse() {
console.log('Posting data to Getresponse...')
// console.log(array['main'][0])
postContact(0);
}
function postContact(idx) {
if (idx === array['main'].length) {
console.log('Data posted to Getresponse');
return;
}
const contact = array['main'][idx];
createContact(contact);
setTimeout(postContact, 200, idx + 1);
}
function generateCustomFields(contact) {
var customFieldValues = [];
if (contact.continuation_url !== '') {
customFieldValues.push({
customFieldId: 'NWLAP',
value: [
contact.continuation_url
]
});
}
if (contact.cid !== '') {
customFieldValues.push({
customFieldId: 'NWuC7',
value: [
contact.cid
]
});
}
if (contact.state !== '') {
customFieldValues.push({
customFieldId: 'NyvBo',
value: [
contact.state
]
});
}
customFieldValues.push({
customFieldId: 'N2fYw',
value: [
CONTACT_STATUS[contact['User state']]
]
});
return customFieldValues;
}
function createContact(contact) {
const headers = {
'Content-Type': 'application/json',
'X-Auth-Token': 'api-key 46374eecbb4807ce3997154dbe9f7c1a'
};
var customFieldValues = generateCustomFields(contact);
const body = {
name: contact.full_name,
email: contact.email,
campaign: {
campaignId: '6FQmX'
},
};
if (customFieldValues.length !== 0) {
body.customFieldValues = customFieldValues;
}
if (contact.ip !== '') {
body.ipAddress = contact.ip;
}
const options = {
headers: headers,
body: JSON.stringify(body)
};
request.post('https://api.getresponse.com/v3/contacts', options);
}
function updateContact(contactId, contact) {
const headers = {
'Content-Type': 'application/json',
'X-Auth-Token': 'api-key 46374eecbb4807ce3997154dbe9f7c1a'
};
var customFieldValues = generateCustomFields(contact);
const body = {
name: contact.full_name,
campaign: {
campaignId: '6FQmX'
},
};
if (customFieldValues.length !== 0) {
body.customFieldValues = customFieldValues;
}
const options = {
headers: headers,
body: JSON.stringify(body)
};
const apiUrl = 'https://api.getresponse.com/v3/contacts/' + contactId;
request.post(apiUrl, options);
}
function refreshGetResponse(aryname, idx, callback = () => {}) {
if (idx === array[aryname].length) {
console.log(aryname + ' Refreshed');
callback();
return;
}
const contact = array[aryname][idx];
const headers = {
'Content-Type': 'application/json',
'X-Auth-Token': 'api-key 46374eecbb4807ce3997154dbe9f7c1a'
};
let apiUrl = 'https://api.getresponse.com/v3/contacts?query[email]=' + contact.email + '&fields=contactId';
request.get(apiUrl, {
headers: headers
}, (err, res, body) => {
if (err) { console.log(err); return; }
const contacts = JSON.parse(body);
if (contacts.length !== 0) {
if (contacts[0] === undefined) {
console.log('bug here!');
console.log(body)
}
updateContact(contacts[0].contactId, contact);
} else {
createContact(contact);
}
})
setTimeout(refreshGetResponse, 200, aryname, idx + 1, callback);
}
getDataFromAirtable('Incomplete', 'main', (err) => {
if (err) { console.error(err); return; }
postDataToGetresponse();
setTimeout(() => {
refresh();
setInterval(refresh, 5 * 60000);
}, 250 * array['main'].length);
});
// Call `getDataFromAirtable` function every 1 min (60s).
setInterval(getDataFromAirtable, 1000 * 60, 'Incomplete', 'main', (err) => {
if (err) { console.error(err); return; }
postDataToGetresponse();
});
function refresh() {
console.log('Start refreshing...');
getDataFromAirtable('Basic Info Complete', 'basic', (err) => {
if (err) { console.error(err); return; }
refreshGetResponse('basic', 0, () => {
getDataFromAirtable('Video Complete', 'video', (err) => {
if (err) { console.error(err); return; }
refreshGetResponse('video', 0, () => {
getDataFromAirtable('Completed', 'completed', (err) => {
if (err) { console.error(err); return; }
refreshGetResponse('completed', 0);
});
});
});
});
});
}
app.get('/', (req, res) => {
res.send('v1.1')
})
app.listen(PORT, () => console.log('App listening on port ', PORT))