-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
94 lines (76 loc) · 2.37 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
const express = require('express');
const path = require('path');
const passport = require('passport');
const { Strategy } = require('passport-oauth2');
const api = require('./api');
const config = require('./config');
const User = require('./db/user');
User.createTable();
const app = express();
passport.use(
'pipedrive',
new Strategy({
authorizationURL: 'https://oauth.pipedrive.com/oauth/authorize',
tokenURL: 'https://oauth.pipedrive.com/oauth/token',
clientID: config.clientID || '',
clientSecret: config.clientSecret || '',
callbackURL: config.callbackURL || ''
}, async (accessToken, refreshToken, profile, done) => {
const userInfo = await api.getUser(accessToken);
const user = await User.add(
userInfo.data.name,
accessToken,
refreshToken
);
done(null, user);
}
)
);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
app.use(express.static(path.join(__dirname, 'public')));
app.use(passport.initialize());
app.use(async (req, res, next) => {
req.user = await User.getById(1);
next();
});
// Remove this section on Glitch
app.get('/auth/pipedrive', passport.authenticate('pipedrive'));
app.get('/auth/pipedrive/callback', passport.authenticate('pipedrive', {
session: false,
failureRedirect: '/',
successRedirect: '/'
}));
app.get('/', async (req, res) => {
if (req.user.length < 1) {
return res.redirect('/auth/pipedrive');
}
try {
const deals = await api.getDeals(req.user[0].access_token);
res.render('deals', {
name: req.user[0].username,
deals: deals.data
});
} catch (error) {
console.log(error);
return res.send('Failed to get deals');
}
});
app.get('/deals/:id', async (req, res) => {
const randomBoolean = Math.random() >= 0.5;
const outcome = randomBoolean === true ? 'won' : 'lost';
try {
await api.updateDeal(req.params.id, outcome, req.user[0].access_token);
res.render('outcome', { outcome });
} catch (error) {
console.log(error);
return res.send('Failed to update the deal');
}
});
// End of section to remove on Glitch
app.listen(process.env.PORT, () => console.log(`App listening on port ${process.env.PORT}`));
if (process.env.IS_LOCAL === 'true') {
console.log(`🟢 App has started. \n🔗 Development URL: http://localhost:3000`);
} else {
console.log(`🟢 App has started. \n🔗 Live URL: https://${process.env.PROJECT_DOMAIN}.glitch.me`);
}