Skip to content

Commit

Permalink
initial structure, lambda function to list harvest users
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmythigpen committed Nov 30, 2017
0 parents commit 1e91b94
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
.env
13 changes: 13 additions & 0 deletions README.md
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
73 changes: 73 additions & 0 deletions bin/lambda.js
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');
});
};
7 changes: 7 additions & 0 deletions claudia.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"lambda": {
"role": "daryl-executor",
"name": "daryl",
"region": "us-east-1"
}
}
54 changes: 54 additions & 0 deletions lambda.js
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');
})
};
31 changes: 31 additions & 0 deletions package.json
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"
}
}

0 comments on commit 1e91b94

Please sign in to comment.