-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
166 lines (147 loc) · 4.84 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
const express = require('express')
const app = express()
const https = require('https')
const ins = require('infusionsoft-node-sdk')
const conf = require('./config.js')
const mailer = require('./mailer.js')
const cronJob = require('cron').CronJob
const db = require('./db.js')
const exec = require('./exec.js')
const http = require('http')
const request = require('request')
const moment = require('moment')
require('console-stamp')(console, 'HH:MM:ss.l')
let infusionsoftConf = conf.get('infusionsoft')
let serverConfig = conf.get('serverConfig')
let {
clientId: ci,
clientSecret: cs,
redirectUri: r,
token: t
} = infusionsoftConf;
let infusionSoft = new ins(infusionsoftConf)
app.get('/reauth', (req, res) => {
console.log("Initializing token reauthentication")
const authUrl = infusionSoft.getAuthorizationUrl()
res.redirect(authUrl)
})
app.get('/redirect', (req, res) => {
let {
code: code,
scope: scope,
state: state
} = req.query
infusionSoft.requestAccessToken(code).then(response => {
console.log('Successfully reauthenticated application')
let expiresAt = new Date()
expiresAt.setSeconds(expiresAt.getSeconds() + response.extra.data.expires_in)
let newValues = {
'token': {
'accessToken': response.extra.data.access_token,
'refreshToken': response.extra.data.refresh_token,
'expiresIn': response.extra.data.expires_in,
'expiresAt': expiresAt,
'extra': ''
}
}
exec.startProcess().then((response) => {
if (response) {
conf.update('infusionsoft', newValues)
console.log(response.message)
res.send(`Updated new token to configuration file and ${response.message}`)
}
}).catch((err) => {
if (err) {
conf.update('infusionsoft', newValues)
console.error(err)
res.send(`Updated new token to configuration file yet error on starting cbis-app: ${err}`)
}
})
}).catch(err => {
let errMsg = err.toString()
console.error(`Error on reauthenticating application ${errMsg}`)
mailer.sendEmail(serverConfig.toEmail,
'Infusionsoft Request Access Token Error',
errMsg
)
exec.stopProcess().then((response) => {
if (response) {
res.send(`${response.message} - reauthentication error, email sent to ${serverConfig.toEmail}`)
}
}).catch((err) => {
if (err) {
res.send(`Error running exec: ${err} on reauthentication error, email sent to ${serverConfig.toEmail}`)
}
})
})
})
app.get('/refresh', (req, res) => {
console.log('Initializing token refresh...')
infusionSoft.refreshToken().then(response => {
console.log("Successfull refresh..")
let expiresAt = new Date()
expiresAt.setSeconds(expiresAt.getSeconds() + response.extra.data.expires_in)
let newValues = {
'token': {
'accessToken': response.extra.data.access_token,
'refreshToken': response.extra.data.refresh_token,
'expiresIn': response.extra.data.expires_in,
'expiresAt': expiresAt,
'extra': ''
}
}
res.send('Updated new token to configuration file')
}).catch(err => {
let errMsg = err.toString()
console.error(`Error on refreshing token: ${errMsg}. Please reauthenticate!`)
errMsg += ', please run ' + serverConfig.hostname + ':' + serverConfig.port + '/reauth ' +
'to reauthenticate to the infusionsoft server API.'
mailer.sendEmail(serverConfig.toEmail,
'Infusionsoft Refresh Access Token Error',
errMsg
)
exec.stopProcess().then((response) => {
if (response) {
res.send(`${response.message} Token invalid, email sent to ${serverConfig.toEmail}`)
}
}).catch((err) => {
if (err) {
res.send(`${err} Token invalid, email sent to ${serverConfig.toEmail}`)
}
})
})
})
app.get(infusionsoftConf.apiCheckToken, (req, res) => {
console.log('Initializing token check...')
db.checkContacts().then((response) => {
if (!response.queryStatus) {
console.log('Token invalid...')
request.get(serverConfig.hostname + ':' + serverConfig.port + '/refresh').pipe(res);
} else {
console.log('Token valid. Message: ' + response.message)
res.send('Token valid. Message: ' + response.message)
}
}).catch((err) => {
let errMsg = err.toString()
console.error(`Token error found! ${errMsg}`)
errMsg += ', please run ' + serverConfig.hostname + ':' + serverConfig.port + '/reauth ' +
'to reauthenticate to the infusionsoft server API.'
mailer.sendEmail(serverConfig.toEmail,
'Reauthenticate Email Notification',
errMsg
)
exec.stopProcess().then((response) => {
if (response) {
res.send(`${response.message} Token invalid, email sent to ${serverConfig.toEmail}`)
}
}).catch((err) => {
if (err) {
res.send(`${err} Token invalid, email sent to ${serverConfig.toEmail}`)
}
})
})
})
new cronJob('*/15 * * * *', () => {
request.get(serverConfig.hostname + ':' + serverConfig.port + infusionsoftConf.apiCheckToken)
}, null, true, 'Pacific/Auckland')
app.listen(3000, () => console.log('Successfully listened to app 3000'))