Skip to content

Commit

Permalink
use env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 6, 2019
1 parent b36a3d9 commit 5a70fe3
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 43 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
.DS_Store
node_modules
transactions.json
lib/credentials.json
.env
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require('dotenv').config()

const { fetchTransactions } = require('./lib/fetch')
const { transformTransactionsToUpdates } = require('./lib/transform')
const { updateSheet } = require('./lib/update')
Expand Down
15 changes: 0 additions & 15 deletions lib/credentials.sample.json

This file was deleted.

42 changes: 28 additions & 14 deletions lib/fetch.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const moment = require('moment')
const client = require('./plaidClient')
const creds = require('./credentials.json')

// start from beginning of last month...
const targetMonth = Math.max(1, moment().month() - 1)
Expand All @@ -10,7 +9,7 @@ const startDate = moment().month(targetMonth - 1).date(1).format('YYYY-MM-DD')
// and keep current month up-to-date
const endDate = moment().format('YYYY-MM-DD')

const fetchOptions = [
const transactionFetchOptions = [
startDate,
endDate,
{
Expand All @@ -19,27 +18,42 @@ const fetchOptions = [
}
]

const plaidAccountTokens = Object.keys(process.env)
.filter(key => key.startsWith(`PLAID_TOKEN`))
.map(key => ({
account: key.replace(/^PLAID_TOKEN_/, ''),
token: process.env[key]
}))

exports.fetchTransactions = async function() {
// fetch in parallel
const fetchPromises = Object.keys(creds.plaid.tokens).map(accountName => {
const token = creds.plaid.tokens[accountName]
return client.getTransactions(token, ...fetchOptions)
.then(res => ({
account: accountName,
transactions: res.transactions
const rawTransactions = await Promise.all(plaidAccountTokens.map(({ account, token }) => {
return client.getTransactions(token, ...transactionFetchOptions)
.then(({ transactions }) => ({
account,
transactions
}))
})

const fetchResults = await Promise.all(fetchPromises)
}))

// concat all transactions
return fetchResults.reduce((all, { account, transactions }) => {
return rawTransactions.reduce((all, { account, transactions }) => {
return all.concat(transactions.map(({ name, date, amount }) => ({
// Simplify the format and only keep things we need.
account,
name,
date,
amount: -amount
})))
}, [])
}

exports.fetchBalances = async function() {
const rawBalances = await Promise.all(plaidAccountTokens.map(({ account, token }) => {
return client.getBalance(token)
}))

return rawBalances.reduce((all, { accounts }) => {
return all.concat(accounts.map(({ name, balances }) => ({
name,
balance: balances.current
})))
}, [])
}
7 changes: 3 additions & 4 deletions lib/googleClient.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const { google } = require('googleapis')
const creds = require('./credentials.json')

module.exports = new google.auth.OAuth2(
creds.sheets.client_id,
creds.sheets.client_secret,
creds.sheets.redirect_uris[0]
process.env.SHEETS_CLIENT_ID,
process.env.SHEETS_CLIENT_SECRET,
process.env.SHEETS_REDIRECT_URI
)
7 changes: 3 additions & 4 deletions lib/plaidClient.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const plaid = require('plaid')
const creds = require('./credentials.json')

module.exports = new plaid.Client(
creds.plaid.client_id,
creds.plaid.secret,
creds.plaid.public_key,
process.env.PLAID_CLIENT_ID,
process.env.PLAID_SECRET,
process.env.PLAID_PUBLIC_KEY,
plaid.environments.development,
{
version: '2018-05-22'
Expand Down
11 changes: 8 additions & 3 deletions lib/update.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
const { google } = require('googleapis')
const creds = require('./credentials.json')
const moment = require('moment')
const oAuth2Client = require('./googleClient')

oAuth2Client.setCredentials(creds.sheets.token)
oAuth2Client.setCredentials({
access_token: process.env.SHEETS_ACCESS_TOKEN,
refresh_token: process.env.SHEETS_REFRESH_TOKEN,
scope: process.env.SHEETS_SCOPE,
token_type: process.env.SHEETS_TOKEN_TYPE,
expiry_date: process.env.SHEETS_EXPIRY_DATE
})

const sheets = google.sheets({
version: 'v4',
Expand All @@ -12,7 +17,7 @@ const sheets = google.sheets({

exports.updateSheet = async function(updates) {
sheets.spreadsheets.values.batchUpdate({
spreadsheetId: creds.sheets.sheet_id,
spreadsheetId: process.env.SHEETS_SHEET_ID,
resource: {
valueInputOption: `USER_ENTERED`,
data: updates.map(p => ({
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"dependencies": {
"body-parser": "^1.18.3",
"dotenv": "^6.2.0",
"ejs": "^2.6.1",
"express": "^4.16.4",
"fs-extra": "^7.0.1",
Expand Down
2 changes: 2 additions & 0 deletions scripts/getSheetsToken.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require('dotenv').config()

const fs = require('fs')
const path = require('path')
const readline = require('readline')
Expand Down
4 changes: 3 additions & 1 deletion scripts/plaidServer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require('dotenv').config()

const account = process.argv[2]
if (!account) {
throw new Error('An account name must be provided.')
Expand All @@ -23,7 +25,7 @@ app.use(bodyParser.json())
app.get('/', (req, res, next) => {
res.render(path.resolve(__dirname, 'plaid.ejs'), {
PLAID_ACCOUNT: account,
PLAID_PUBLIC_KEY: creds.plaid.public_key
PLAID_PUBLIC_KEY: process.env.PLAID_PUBLIC_KEY
})
})

Expand Down
2 changes: 2 additions & 0 deletions scripts/testPlaid.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require('dotenv').config()

const path = require('path')
const { writeFile } = require('fs-extra')
const { fetchTransactions } = require('../lib/fetch')
Expand Down
2 changes: 2 additions & 0 deletions scripts/testSheets.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require('dotenv').config()

const { updateSheet } = require('../lib/update')

updateSheet([{
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ destroy@~1.0.4:
resolved "https://registry.yarnpkg.com/destroy/-/destroy-1.0.4.tgz#978857442c44749e4206613e37946205826abd80"
integrity sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA=

dotenv@^6.2.0:
version "6.2.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-6.2.0.tgz#941c0410535d942c8becf28d3f357dbd9d476064"
integrity sha512-HygQCKUBSFl8wKQZBSemMywRWcEDNidvNbjGVyZu3nbZ8qq9ubiPoGLMdRDpfSrpkkm9BXYFkpKxxFX38o/76w==

ecc-jsbn@~0.1.1:
version "0.1.2"
resolved "https://registry.yarnpkg.com/ecc-jsbn/-/ecc-jsbn-0.1.2.tgz#3a83a904e54353287874c564b7549386849a98c9"
Expand Down

0 comments on commit 5a70fe3

Please sign in to comment.