-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathplaid.js
149 lines (136 loc) · 4.06 KB
/
plaid.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
'use strict';
const plaid = require('../plaid');
const db = require('../db/web2');
const { encrypt, decrypt } = require('../encrypt-decrypt');
const findUserByWallet = require('../utils/query-user');
const fetchTransactions = require('../utils/fetch-transactions');
const errors = require('../errors');
/**
* @api {post} / savePlaidAccessToken
* @apiVersion 1.0.0
* @apiName savePlaidAccessToken
* @apiGroup Plaid
*
* @apiParam {String} wallet wallet ID
* @apiParam {String} publicToken Plaid public token
*
* @apiSuccess {Integer} message Message.
*
* @apiParamExample {json} Request-Example:
* {
* "jsonrpc": "2.0",
* "method":"savePlaidAccessToken",
* "id":1,
* "params":{
* "wallet": "WALLET_ID",
* "publicToken":"PLAID_PLUBIC_TOKEN"
* }
* }
*
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* "jsonrpc": "2.0",
* "id": 1,
* "result": "Ok"
* }
*
* @apiExample {curl} Example usage:
* curl --location --request POST 'http://localhost:3003/' --header 'Content-Type: application/json' --data-raw '{"jsonrpc": "2.0", "method": "savePlaidAccessToken", "id":1, "params":{"wallet": "walletID", "publicToken":"PLAID_PUBLIC_TOKEN"}}'
*
* @apiUse InvalidMethodError
*
* @apiUse InvalidRequestError
*/
async function savePlaidAccessToken({ wallet, publicToken }) {
try {
// We dont actually need the user but we want to make sure they exist.
await findUserByWallet(wallet);
const accessToken = await plaid.authenticate(publicToken);
const ciphertext = encrypt(accessToken);
// Save encrypted token.
await db('users')
.where({ wallet })
.update({ plaid_access_token: ciphertext });
return 'Ok';
} catch (err) {
console.error(err);
throw errors.UnknownError;
}
}
/**
* @api {post} / getPlaidTransactions
* @apiVersion 1.0.0
* @apiName getPlaidTransactions
* @apiGroup Plaid
*
* @apiParam {String} wallet wallet ID
* @apiParam {String} account account ID
*
* @apiSuccess {Object} transactions Plaid transactions for last 60 days.
*
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* "jsonrpc": "2.0",
* "id": 1,
* "result": []
* }
*
* @apiExample {curl} Example usage:
* curl --location --request POST 'http://localhost:3003/' --header 'Content-Type: application/json' --data-raw '{"jsonrpc": "2.0", "method": "getPlaidTransactions", "id":1, "params":{"wallet":"walletID","account": "accountID"}}'
*
* @apiUse InvalidMethodError
*
* @apiUse InvalidRequestError
*/
async function getPlaidTransactions({ wallet, account }) {
try {
if (!account || typeof account !== 'string') {
throw 'Missing account parameter';
}
const user = await findUserByWallet(wallet);
const accessToken = decrypt(user.plaid_access_token);
return await fetchTransactions(wallet, account, accessToken);
} catch (err) {
console.error(err);
throw errors.UnknownError;
}
}
/**
* @api {post} / getPlaidIncome
* @apiVersion 1.0.0
* @apiName getPlaidIncome
* @apiGroup Plaid
*
* @apiParam {String} wallet wallet ID
*
* @apiSuccess {Object} income Plaid user income.
*
* @apiExample {curl} Example usage:
* curl --location --request POST 'http://localhost:3003/' --header 'Content-Type: application/json' --data-raw '{"jsonrpc": "2.0", "method": "getPlaidIncome", "id":1, "params":{"publicToken":"PLAID_PUBLIC_TOKEN"}}'
*
* @apiUse InvalidMethodError
*
* @apiUse InvalidRequestError
*/
async function getPlaidIncome({ wallet }) {
try {
const user = await findUserByWallet(wallet);
const accessToken = decrypt(user.plaid_access_token);
const { income } = await plaid.client.getIncome(accessToken);
// Save transactions.
await db('users')
.where({ wallet })
.update({ plaid_income: income });
return income;
} catch (err) {
console.error(err);
throw errors.UnknownError;
}
}
module.exports = {
savePlaidAccessToken,
getPlaidTransactions,
getPlaidIncome
};