forked from ChristianJHughes/DonutBaron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
136 lines (118 loc) · 4.66 KB
/
app.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
// The MAIN entry point for the application. Launch by calling "node app.js" from the command line.
var express = require('express'),
app = express(),
loadUser = require('./middleware/load_user.js'),
sessions = require('client-sessions'),
bodyParser = require('body-parser'),
schedule = require('node-schedule'),
db = require('./db'),
email = require('./email.js');
// Enable template engine
app.set('view engine', 'ejs');
// Serve static files
app.use(express.static('public'));
// Enable sessions
app.use(sessions({
cookieName: 'session',
secret: 'donutbarondonutbarondonutbaron',
duration: 24 * 60 * 60 * 1000,
activeDuration: 1000 * 60 * 5
}));
// Add middleware body parser
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// Add routes to the homepage. It will only reach the homepage if the user has been logged in (and has an active session).
var homepage = require('./endpoints/homepage.js');
app.get('/index', loadUser, homepage.getHomepageData);
app.get('/', loadUser, homepage.getHomepageData);
app.post('/index/rate', loadUser, homepage.RateDonuts);
app.get('/comments', homepage.getComments);
app.post('/comments/add', homepage.addComment);
// Add routes for the login page. Should create an active session if successful.
var login = require('./endpoints/session.js');
app.get('/login', login.new);
app.post('/login', login.create);
app.get('/logout', login.destroy);
app.post('/register', login.register);
app.get('/register', login.viewRegister);
// Add a route to the rankings page.
var rankings = require('./endpoints/rankings.js');
app.get('/rankings', loadUser, rankings.getRankingsData);
// Launch the application on port 8080.
app.listen(8080, () => {
console.log('Listening on port 8080...');
});
// Updates the database every day at 1am.
var daily = schedule.scheduleJob('0 1 * * 0-7', function () {
console.log("Starting the cron job");
db.get("SELECT * FROM upcomingList", function (err, row) {
if (err) {
console.error(err);
return res.sendStatus(500);
}
// Create a new date, and set it to tomorrow's date.
var date = new Date();
date.setDate(date.getDate() + 1);
if (row.date == date.toISOString().slice(0, 10)) {
db.get("SELECT * FROM users WHERE userID = ?", row.userID, function (err, user) {
if (err) {
console.error(err);
return res.sendStatus(500);
}
email.getOAuth2Client(function (err, oauth2Client) {
if (err) {
console.log('err:', err);
}
else {
email.sendSampleMail(oauth2Client, user.email_address, function (err, results) {
if (err) {
console.log('err:', err);
}
});
}
console.log("I sent " + user.email_address + " an email!");
});
});
}
date = new Date();
date.setDate(date.getDate());
// compare the current date with the next date in the table. If they match, then it's time to switch up the donut baron.
if (row.date == date.toISOString().slice(0, 10)) {
console.log("I updated the donut list!");
var oldDonutBaron;
// Make sure the database calls run in order.
db.serialize(function () {
// Save the last weeks donut baron so that they can be added to the end of the list.
db.get("SELECT * FROM users WHERE is_donut_baron=?", 1, function (err, previousDonutBaron) {
if (err) {
console.error(err);
return res.sendStatus(500);
};
oldDonutBaron = previousDonutBaron;
});
// Assign the new donut baron proper donut baron status.
db.run("UPDATE users SET is_donut_baron=? WHERE userID=?", 1, row.userID, function (err) {
if (err) {
console.error(err);
return res.sendStatus(500);
};
});
// Delete the furst row from the upcomingList, as they are now the donut baron.
db.run("DELETE FROM upcomingList WHERE listID=?", row.listID);
// At the next open slot...
db.get("SELECT * FROM upcomingList WHERE userID=?", -1, function (err, openSlot) {
if (err) {
console.error(err);
return res.sendStatus(500);
}
// ... Insert the old donut baron.
db.run("UPDATE upcomingList SET userID = ?, real_name = ? WHERE listID = ?", oldDonutBaron.userID, oldDonutBaron.real_name, openSlot.listID);
// Then revoke that donut baron's status.
db.run("UPDATE users SET is_donut_baron=? WHERE userID=?", 0, oldDonutBaron.userID);
});
});
};
db.run("DELETE FROM comments");
});
});
module.exports = exports = app;