-
-
Notifications
You must be signed in to change notification settings - Fork 203
/
setup.js
128 lines (115 loc) Β· 4.06 KB
/
setup.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
const express = require('express')
const next = require('next')
const bodyParser = require('body-parser')
const opn = require('opn')
const {
getConfigEnv,
updateConfig,
deleteConfigProperty,
maybeWriteDefaultConfig,
accountProviderSetupComplete,
sheetProviderSetupComplete,
accountSetupComplete
} = require('../lib/common')
const _ = require('lodash')
maybeWriteDefaultConfig().then(() => {
const port = parseInt(process.env.PORT, 10) || 3000
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
app.prepare().then(() => {
const server = express()
server.use(bodyParser.urlencoded({ extended: false }))
server.use(bodyParser.json())
server.get('/config', (req, res) => {
return getConfigEnv()
.then(config =>
res.json({
data: {
...config,
accountProviderSetupComplete: accountProviderSetupComplete(),
sheetProviderSetupComplete: sheetProviderSetupComplete(),
accountSetupComplete: accountSetupComplete()
}
})
)
.catch(error => res.json(error))
})
server.post('/config', (req, res) => {
return updateConfig(req.body.updates)
.then(config => res.json({ data: config }))
.catch(error => res.json(error))
})
server.delete('/config', (req, res) => {
return deleteConfigProperty(req.body.id)
.then(config => res.json({ data: config }))
.catch(error => res.json(error))
})
server.get('/balances', (req, res) => {
switch (process.env.ACCOUNT_PROVIDER) {
case 'plaid':
return require('../lib/plaid')
.fetchBalances()
.then(balances => res.json({ data: balances }))
.catch(error => res.json(error))
default:
return res.json({ data: {} })
}
})
server.post('/token', (req, res) => {
switch (process.env.ACCOUNT_PROVIDER) {
case 'plaid':
return require('../lib/plaid')
.saveAccessToken(req.body.public_token, req.body.accountNickname)
.then(res.redirect(`http://localhost:3000/settings`))
.catch(error => res.json(error))
default:
return res.json({ data: {} })
}
})
server.post('/update', (req, res) => {
switch (process.env.ACCOUNT_PROVIDER) {
case 'plaid':
return require('../lib/plaid')
.createPublicToken(process.env[`PLAID_TOKEN_${req.body.accountNickname}`], req.body.accountNickname)
.then(token => res.json({ data: token }))
.catch(error => res.json(error))
default:
return res.json({ data: {} })
}
})
server.get('/google-sheets-url', (req, res) => {
return require('../lib/google')
.getAuthURL()
.then(url => res.json({ data: url }))
.catch(error => res.json(error))
})
server.get('/google-sheets-oauth2callback', (req, res) => {
return require('../lib/google')
.getToken(req.query.code)
.then(token => res.redirect('http://localhost:3000/sheet-provider-setup'))
.catch(error => res.json(error))
})
server.get('/', (req, res) => {
if (!accountProviderSetupComplete() && !accountSetupComplete() && !sheetProviderSetupComplete()) {
return res.redirect(`http://localhost:3000/welcome`)
} else if (!accountProviderSetupComplete()) {
return res.redirect(`http://localhost:3000/account-provider-setup`)
} else if (!accountSetupComplete()) {
return res.redirect(`http://localhost:3000/account-setup`)
} else if (!sheetProviderSetupComplete()) {
return res.redirect(`http://localhost:3000/sheet-provider-setup`)
} else {
return res.redirect(`http://localhost:3000/settings`)
}
})
server.get('*', (req, res) => {
return handle(req, res)
})
server.listen(port, error => {
if (error) throw error
console.log(`> Ready on http://localhost:${port}`)
opn(`http://localhost:${port}`)
})
})
})