From 1e91b94411f543bc45087152766ad4b2fb4a145b Mon Sep 17 00:00:00 2001 From: Jimmy Thigpen Date: Wed, 29 Nov 2017 20:03:35 -0500 Subject: [PATCH] initial structure, lambda function to list harvest users --- .gitignore | 2 ++ README.md | 13 +++++++++ bin/lambda.js | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++ claudia.json | 7 +++++ lambda.js | 54 +++++++++++++++++++++++++++++++++++++ package.json | 31 ++++++++++++++++++++++ 6 files changed, 180 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 bin/lambda.js create mode 100644 claudia.json create mode 100644 lambda.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3ec544c --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules/ +.env \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..6ea417a --- /dev/null +++ b/README.md @@ -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 diff --git a/bin/lambda.js b/bin/lambda.js new file mode 100644 index 0000000..2245146 --- /dev/null +++ b/bin/lambda.js @@ -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'); + }); +}; \ No newline at end of file diff --git a/claudia.json b/claudia.json new file mode 100644 index 0000000..32c3452 --- /dev/null +++ b/claudia.json @@ -0,0 +1,7 @@ +{ + "lambda": { + "role": "daryl-executor", + "name": "daryl", + "region": "us-east-1" + } +} \ No newline at end of file diff --git a/lambda.js b/lambda.js new file mode 100644 index 0000000..1282daf --- /dev/null +++ b/lambda.js @@ -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'); + }) +}; \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..0372bd3 --- /dev/null +++ b/package.json @@ -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" + } +}