-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial structure, lambda function to list harvest users
- Loading branch information
0 parents
commit 1e91b94
Showing
6 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules/ | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
## Daryl | ||
|
||
### Dev Setup | ||
|
||
You will need AWS credentials, aws-cli, and claudia js installed, speak with Luke or Jimmy if you need help. | ||
|
||
[Installing the AWS Command Line Interface](http://docs.aws.amazon.com/cli/latest/userguide/installing.html) | ||
|
||
[Installing Claudia](https://claudiajs.com/tutorials/installing.html) | ||
|
||
[Claudia Docs](https://claudiajs.com/documentation.html) | ||
|
||
##### see `package.json` for scripts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
'use strict'; | ||
|
||
var _ = require('lodash'); | ||
var rp = require('request-promise'); | ||
var async = require('async'); | ||
|
||
exports.handler = function (event, context) { | ||
|
||
var harvestURL = function harvestURL(type) { | ||
return { | ||
uri: 'https://api.harvestapp.com/' + type, | ||
headers: { | ||
'Authorization': HARVEST_TOKEN, | ||
'Harvest-Account-ID': HARVEST_ID, | ||
'User-Agent': 'Poetic Timetracking Lambda (poeticsystems.com)' | ||
}, | ||
json: true | ||
}; | ||
}; | ||
|
||
var insertUsers = function insertUsers(users) { | ||
users.forEach(function (_ref) { | ||
var first_name = _ref.first_name, | ||
last_name = _ref.last_name; | ||
|
||
console.log('poetic user: ', first_name + ' ' + last_name); | ||
}); | ||
}; | ||
|
||
var syncUsers = function syncUsers() { | ||
function _recursive() { | ||
if (syncing) { | ||
return Promise.resolve().then(function () { | ||
return Promise.resolve().then(function () { | ||
return rp(harvestURL('v2/users?page=1&per_page=100')); | ||
}).then(function (_resp) { | ||
harvestUsers = _resp; | ||
|
||
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(function (err) { | ||
console.log(''); | ||
console.log('syncUsers err: ', err); | ||
console.log(''); | ||
syncing = false; | ||
}); | ||
}).then(function () { | ||
return _recursive(); | ||
}); | ||
} | ||
} | ||
|
||
var syncing, totalSynced, harvestUsers; | ||
return Promise.resolve().then(function () { | ||
console.log(''); | ||
console.log('syncUsers starting'); | ||
console.log(''); | ||
syncing = true; | ||
totalSynced = 0; | ||
return _recursive(); | ||
}).then(function () {}); | ||
}; | ||
|
||
return syncUsers().then(function () { | ||
context.succeed('finished sync'); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"lambda": { | ||
"role": "daryl-executor", | ||
"name": "daryl", | ||
"region": "us-east-1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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'); | ||
}) | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"name": "daryl", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "lambda.js", | ||
"files": [ | ||
"bin" | ||
], | ||
"scripts": { | ||
"transpile": "babel --presets es2015 --plugins async-to-promises lambda.js --out-dir bin", | ||
"create": "npm run transpile && claudia create --region us-east-1 --handler bin/lambda.handler --set-env-from-json .env --timeout 300", | ||
"update": "npm run transpile && claudia update --region us-east-1 --handler bin/lambda.handler --set-env-from-json .env --timeout 300", | ||
"start": "claudia test-lambda", | ||
"destroy": "claudia destroy" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"async": "^2.6.0", | ||
"claudia-api-builder": "^2.5.1", | ||
"lodash": "^4.17.4", | ||
"request": "^2.83.0", | ||
"request-promise": "^4.2.2" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.26.0", | ||
"babel-plugin-async-to-promises": "^1.0.5", | ||
"babel-preset-es2015": "^6.18.0", | ||
"claudia": "^2.5.0" | ||
} | ||
} |