-
Notifications
You must be signed in to change notification settings - Fork 0
/
mondo-helpers.js
63 lines (54 loc) · 1.24 KB
/
mondo-helpers.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
import mondo from 'mondo-bank'
const auth = () => {
const {env} = process
const config = {
client_id: env.MONDO_CLIENT_ID,
client_secret: env.MONDO_CLIENT_SECRET,
username: env.MONDO_USERNAME,
password: env.MONDO_PASSWORD,
}
return new Promise((resolve, reject) => {
mondo.token(config)
.then(resolve)
.catch(reject)
})
}
const getTransactions = (id, token) => new Promise((resolve, reject) => {
mondo.transactions(id, token)
.then(({transactions}) => resolve(transactions))
.catch(reject)
})
const getUserAccount = (token) => new Promise((resolve, reject) => {
mondo.accounts(token)
.then(user => {
const {id} = user.accounts[0]
resolve(id)
})
.catch(reject)
})
const authAndGetUserAccount = () => new Promise((resolve, reject) => {
auth()
.then(user => {
const token = user.access_token
getUserAccount(token)
.then(id => resolve({id, token}))
.catch(reject)
})
.catch(reject)
})
const authAndGetTransactions = () => new Promise((resolve, reject) => {
authAndGetUserAccount()
.then(({id, token}) => {
getTransactions(id, token)
.then(resolve)
.catch(reject)
})
.catch(reject)
})
export {
auth,
getTransactions,
getUserAccount,
authAndGetUserAccount,
authAndGetTransactions,
}