-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda.js
54 lines (49 loc) · 1.39 KB
/
lambda.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
var _ = require('lodash');
var rp = require('request-promise');
var async = require('async');
exports.handler = function (event, context) {
const harvestURL = type => (
{
uri: `https://api.harvestapp.com/${type}`,
headers: {
'Authorization': HARVEST_TOKEN,
'Harvest-Account-ID': HARVEST_ID,
'User-Agent': 'Poetic Timetracking Lambda (poeticsystems.com)'
},
json: true,
}
);
const insertUsers = (users) => {
users.forEach(({ first_name, last_name }) => {
console.log('poetic user: ', `${first_name} ${last_name}`);
})
}
const syncUsers = async () => {
console.log('');
console.log('syncUsers starting');
console.log('');
let syncing = true;
let totalSynced = 0;
while (syncing) {
try {
const harvestUsers = await rp(harvestURL('v2/users?page=1&per_page=100'))
if (harvestUsers.users) {
insertUsers(harvestUsers.users);
totalSynced = totalSynced + harvestUsers.users.length;
}
console.log('');
console.log(`syncUsers complete, ${totalSynced} users synced`);
console.log('');
syncing = false
} catch(err) {
console.log('');
console.log('syncUsers err: ', err);
console.log('');
syncing = false
}
}
}
return syncUsers().then(() => {
context.succeed('finished sync');
})
};