-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcli.js
191 lines (172 loc) · 5.19 KB
/
cli.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
'use strict';
// external deps
var commander = require('commander');
var config = require('config');
var moment = require('moment');
var Sequelize = require('sequelize');
// internal deps
var ModelClass = require('./lib/model.js');
var plaid = require('./lib/plaid-wrapper.js');
var APP_DB_STRING = config.get("DB_STRING");
var sequelize = new Sequelize(APP_DB_STRING);
sequelize.sync();
// Initialize Models
var Models = new ModelClass(sequelize);
// initialize commands
initCommands();
function initCommands() {
commander
.command('fetch [numDays] [userId]')
.description(
`Fetch all transactions (and update all accounts info)
in the past numDays for the given user.
numDays defaults to 30
userId defaults to 1`)
.action(function(numDays, userId) {
// https://support.plaid.com/customer/portal/articles/2530257
// this is how much each bank stores
let user = parseIntOrDefault(userId, 1);
let daysI = parseIntOrDefault(numDays, 30);
fetch(user, daysI);
});
commander
.command('fetchAccounts [userId]')
.description(
`Retrieve and update all accounts info for the user
userId defaults to 1`)
.action(function(userId) {
let user = parseIntOrDefault(userId, 1);
let tokensPromise = getAllAccessTokens(user);
tokensPromise.then(fetchAccounts);
});
commander
.command('updateItemInfo [userId]')
.action(function(userId) {
let user = parseIntOrDefault(userId, 1);
let tokensPromise = getAllAccessTokens(user);
tokensPromise.then((tokens) => {
return plaid.getItems(tokens)
}).then((resp) => {
resp.map((item) => {
let itemId = item.item.item_id;
let instId = item.item.institution_id;
plaid.getInstitutionById(instId)
.then((inst) => {
let name = inst.institution.name;
return Models.Item.update({
"institutionId": instId,
"institutionName": name
},
{
"where": {
"id": itemId
}
})
}).then(console.log.bind(console))
.catch((e) => {
console.log("Failed for ", itemId, instId);
console.log(e);
})
});
});
});
commander
.command('balance [userId]')
.description(
`Retrieve the balance for all checkings/savings accounts.
And add it as a data point to the balance table.
userId defaults to 1`
)
.action(function(userId) {
let user = parseIntOrDefault(userId, 1);
let tokensPromise = Models.Item.findAll({
include: [{
model: Models.Account,
where: {
$or: [
{type: 'depository'},
{type: 'other'}
]
}
}],
where: {
userId: user
},
});
tokensPromise.then(function(items) {
let tokens = items.map((item) => {
return item.accessToken;
});
return plaid.getAccounts(tokens);
}).then((accounts) => {
let balances = accounts.map((account) => {
return {
dateOf: new Date(),
accountId: account.id,
balance: account.balance
}
});
return Models.BalanceHistory.bulkCreate(balances)
}).then((models) => {
console.log("Succesfully wrote balances: ", models.length);
})
.catch(console.log.bind(console));
});
commander.parse(process.argv);
}
function getAllAccessTokens(userId) {
let tokensPromise = Models.Item.findAll({
where: {
userId: userId
}
}).then((items) => {
let accessTokens = items.map((anItem) => anItem.accessToken);
console.log(accessTokens);
return accessTokens;
});
return tokensPromise;
}
function fetch(userId, days) {
var tokensPromise = getAllAccessTokens(userId);
// update accounts records
tokensPromise.then(fetchAccounts);
tokensPromise.then((tokens) => {
return fetchTransactions(tokens, days)
});
}
function fetchAccounts(itemTokens) {
return plaid.getAccounts(itemTokens)
.then((accounts) => {
console.log(accounts);
return Models.Account.bulkCreate(accounts, {
ignoreDuplicates: true
});
}).then((rows) => {
return rows;
}).catch(function(err) {
console.log(err);
});
}
function fetchTransactions(itemTokens, days) {
return plaid.getTransactionsPastNDays(itemTokens, days)
.then(function({startDate, endDate, transactions}) {
let promises = transactions.map((transaction) => {
return Models.Transaction.upsert(transaction);
});
return promises;
}).then(function(createdBooleanList) {
return Promise.all(createdBooleanList)
}).then(function(createdBooleanList) {
console.log(createdBooleanList);
let totalCreated = createdBooleanList.reduce(
(sum, value) => sum + (value ? 1 : 0), 0);
console.log("Number of transactions upserted: ",
createdBooleanList.length);
console.log("Number of transactions explicitly created: ",
totalCreated);
});
}
function parseIntOrDefault(str, def) {
let int= Number.parseInt(str, 10);
return Number.isNaN(int) ? def : int;
}