-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
310 lines (282 loc) · 7.6 KB
/
index.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
'use strict';
// external deps
var config = require('config');
var express = require('express');
var bodyParser = require('body-parser');
var Sequelize = require('sequelize');
var mustacheExpress = require('mustache-express');
var moment = require('moment');
// internal deps
var ModelClass = require('./lib/model.js');
var plaidClient = require('./lib/plaid-wrapper.js');
var APP_PORT = config.get('PLAID.APP_PORT');
var PLAID_PUBLIC_KEY = config.get('PLAID.PUBLIC_KEY');
var PLAID_ENV = config.get('PLAID.ENV');
var APP_DB_STRING = config.get("DB_STRING");
var sequelize = new Sequelize(APP_DB_STRING);
sequelize.sync();
// Initialize Models
var Models = new ModelClass(sequelize);
var app = express();
app.use(function (req, res, next) {
console.log('Request for: "%s" by ip=%s user=', req.url, req.ip, req.user);
next();
});
app.use(express.static('public'));
app.set('view engine', 'mustache');
app.engine('mustache', mustacheExpress());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());
app.get('/', function(request, response) {
Models.User.findAll()
.then((users) => {
console.log(users.map((user) => user.toJSON()));
response.render('user-select.mustache', {
PLAID_PUBLIC_KEY: PLAID_PUBLIC_KEY,
PLAID_ENV: PLAID_ENV,
users: users.map((user) => user.toJSON())
});
})
});
app.param("user", function(req, res, next, id) {
Models.User.findById(id)
.then((user) => {
req.user = user;
next();
}).catch((err) => {
console.log(err);
next(err);
});
});
app.get('/user/:user', function(req, res) {
Models.Item.findAll({
where: {
userId: Number.parseInt(req.params.user, 10)
}
}).then(items => {
return Promise.all(items.map(item => {
return plaidClient.validateItem(item.accessToken)
.then(valid => {
let publicToken = Promise.resolve(false);
if (!valid) {
publicToken =
plaidClient.createPublicToken(item.accessToken)
}
return publicToken;
}).then(publicToken => ({
institution: item.institutionName,
publicToken: publicToken.public_token
})).catch(e => {
console.log("Unexpected failure to create publicToken",
item.toJSON());
console.log(e);
return {
institution: item.institutionName,
error: true
};
});
}));
}).then(items => {
res.render('index.mustache', {
userId: req.user.id,
displayName: req.user.displayName,
PLAID_PUBLIC_KEY: PLAID_PUBLIC_KEY,
PLAID_ENV: PLAID_ENV,
items: items
});
})
});
app.get('/user/:user/balances', function(req, res) {
res.render('balance.mustache', {
userId: req.user.id,
displayName: req.user.displayName,
});
});
app.post('/user/:user/balances', function(req, res) {
let balances = Models.BalanceHistory.findAll({
include: [{
model: Models.Account,
where: {
$or: [
{type: 'depository'},
{type: 'other'}
]
}
}]
});
balances.then(bs => {
let jsonBalances = bs.map(b => b.toJSON());
res.json(jsonBalances);
});
});
app.get('/user/:user/transactions/:page?', function(req, res) {
const itemsPerPage = 30;
const order = ['dateOf', 'DESC']; // default to get latest
let page = 0;
if (req.params.page) {
page = Number.parseInt(req.params.page, 10)
}
let offset = page*itemsPerPage;
Models.Transaction.findAll({
order: [order],
offset,
limit: itemsPerPage,
include: [{
model: Models.Account,
include: [{
model: Models.Item,
where: {
userId: Number.parseInt(req.params.user, 10)
}
}]
}]
}).then(transactions=> {
transactions = transactions.map(t => {
let transaction = t.toJSON();
//we don't have time accuracy in the underlying data
// so limit to date
transaction.dateOf = moment(transaction.dateOf).format(
"dddd, MMMM Do YYYY");
transaction.updatedAt = moment(transaction.updatedAt).format(
"MMM Do, YYYY");
transaction.cents = transaction.amount;
let dollars = Number.parseInt(transaction.amount, 10)/100;
transaction.amount = dollars.toLocaleString(
undefined, {
style: "currency",
currency: "USD"
});
transaction.drawPositive = dollars < 0; // negative charge is positive
transaction.location = prettyPickLocation(transaction.location);
let accountName =
transaction.account.name + " " + transaction.account.mask;
if (transaction.account.name == "CREDIT CARD") {
accountName = transaction.account.item.institutionName + " " + accountName;
}
transaction.accountName = accountName;
return transaction;
});
res.render('transactions.mustache', {
userId: req.user.id,
displayName: req.user.displayName,
transactions: transactions
})
});
});
function prettyPickLocation(locationStr) {
try {
let location = JSON.parse(locationStr);
let output = '';
if (location.zip) {
output = output + location.zip;
}
if (location.state) {
output = location.state + " " + output;
}
if (location.store_number) {
output = "Store # " + location.store_number + " " + output;
}
if (location.city) {
output = location.city + " " + output;
}
if (location.address) {
output = location.address + " " + output;
}
return output;
// lat/lon are ignored
}
catch (e) {
console.error(e);
return '';
}
}
app.get("/users", function(req, res) {
Models.User.findAll()
.then((users) => {
res.json(users);
});
});
app.put("/user", function(req, res) {
console.log(req.body);
Models.User.max('id')
.then((id) => {
if (!Number.isInteger(id)) {
id = 0;
}
return Models.User.create({
displayName: req.body.displayName,
id: id+1
});
}).then((result) => {
res.json(result);
});
});
app.delete("/user", function(req, res) {
let id = Number.parseInt(req.body.id, 10);
if (Number.isNaN(id)) {
console.error("Bad id: ", req.body.id);
return res.status(400).json({error: true});
}
Models.User.destroy({
where: {
id: id
}
}).then((row) => {
res.json(row);
});
});
app.get("/items", function(req, res) {
Models.Item.findAll()
.then((items) => {
res.json(items);
});
});
app.put("/item", function(req, res) {
let publicToken = req.body.publicToken;
let userId = req.body.userId;
let instName = req.body.institutionName;
let instId = req.body.institutionId;
console.log("/item: ", publicToken, userId, instName, instId);
plaidClient.exchangePublicToken(publicToken, function(error, tokenResponse) {
console.log("/item: ", publicToken, userId, tokenResponse, error);
if (error != null) {
var msg = 'Could not exchange public_token!';
console.log(msg + '\n' + error);
return res.json({
error: msg
});
}
Models.Item.create({
id: tokenResponse.item_id,
accessToken: tokenResponse.access_token,
userId: userId,
institutionName: instName,
institutionId: instId
}).then((result) => {
res.json(result);
});
});
});
app.delete("/item", function(req, res) {
Models.Item.destroy({
where: {
id: req.body.id
}
}).then((row) => {
res.json(row);
});
});
app.delete("/transaction", function(req, res) {
Models.Transaction.destroy({
where: {
id: req.body.id
}
}).then((row) => {
res.send('Success');
});
});
var server = app.listen(APP_PORT, function() {
console.log('Account Set Up listening on ' + APP_PORT);
});