This repository has been archived by the owner on Nov 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.js
207 lines (187 loc) · 10.7 KB
/
router.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
require('dotenv').config();
let express = require('express');
let router = express.Router();
const mailer = require('./mailer');
const CAFETERIA_EMAIL = process.env.CAFETERIA_EMAIL; // [email protected]
const GYM_EMAIL = process.env.GYM_EMAIL; // [email protected]
const GYM_AFTER_HOURS = process.env.GYM_AFTER_HOURS; // [email protected]
const LIBRARY_EMAIL = process.env.LIBRARY_EMAIL; // [email protected]
const CCC_EMAIL = process.env.CCC_EMAIL; // [email protected]
if (!(CAFETERIA_EMAIL && GYM_EMAIL && GYM_AFTER_HOURS && LIBRARY_EMAIL && CCC_EMAIL)) {
console.warn('Could not load environment variables for emails');
}
router.get('/', (req, res) => {
res.render('index');
});
router.get('/form', (req, res) => {
res.render('form');
});
router.get('/track', (req, res) => {
res.render('track');
});
router.get('/trackForm', (req, res) => {
res.render('trackForm');
});
router.get('/allForms', (req, res) => {
res.render('allForms');
});
router.get('/approve', (req, res) => {
res.render('approve');
});
router.get('/approve/:person/:id', (req, res) => {
res.render('approve');
});
router.get('/approvalDetails/:person', (req, res) => {
res.render('approvalDetails');
});
router.get('/reject', (req, res) => {
res.render('reject');
});
router.get('/reject/:person/:id', (req, res) => {
res.render('reject');
});
router.get('/asb', (req, res) => {
res.render('asb');
});
router.post('/email', (req, res) => {
console.log('sending');
console.log('recipient: : ' + req.body.recipient);
console.log('subjuect: : ' + req.body.subject);
console.log('message: : ' + req.body.message);
mailer.sendMail({
recipient: req.body.recipient,
subject: req.body.subject,
messageHTML: req.body.message,
}).then(() => {
console.log('successfully sent');
}).catch((err) => {
console.error('error ' + err);
});
});
router.post('/submit_form', (req, res) => {
let formObject = JSON.parse(req.body.form_data);
let studentEmail = mailer.sendMail({
recipient: formObject.general.student_email,
subject: 'Confirmation for submitting your approval',
messageHTML: '<center><div style="width: 90%; background-color: #f4f4f4"><p style="font-size: 30px">You have submitted an approval, you can track it\'s progress here: <a href=\'https://mvhs-approvals.herokuapp.com/track\'>https://mvhs-approvals.herokuapp.com/track</a> using the id: \'' +
formObject.id_num + '\'' + '</p></div></center>',
});
let advisorEmail = mailer.sendMail({
recipient: formObject.general.advisor_email,
subject: 'A student is requesting your approval',
messageHTML: '<center><div style="width: 90%; background-color: #f4f4f4"><p style="text-align: center; font-size: 30px;">' +
formObject.general.student_name +
' has submitted an approval for ' + formObject.general.club_name +
'. <br /> You can view more details about the approval <a href=\'https://mvhs-approvals.herokuapp.com/approvalDetails/' + formObject.id_num + '\'>here</a>' +
'. <br />Please accept or reject the approval below.</p><br /><center><button style="background-color: green; width: 200px; font-size: 15px; height: 100px"><a href=\'https://mvhs-approvals.herokuapp.com/approve/' +
formObject.id_num +
'/clubAdvisor\'><p style="font-size: 25px; color: black">Accept</p></a></button><button style="background-color: red; width: 200px; font-size: 15px; height: 100px"><a href=\'https://mvhs-approvals.herokuapp.com/reject/' +
formObject.id_num +
'/clubAdvisor\'><p style="font-size: 25px; color: black">Reject</p></a></button></center>' +
'</div></center>',
});
let cafeterialEmail = (formObject.campus.cafeteria === true)
? mailer.sendMail({
recipient: CAFETERIA_EMAIL,
subject: 'A student is requesting your approval for use of the Cafeteria',
messageHTML: '<center><div style="width: 90%; background-color: #f4f4f4"><p style="text-align: center; font-size: 30px;">' +
formObject.general.student_name +
' has submitted an approval for ' + formObject.general.club_name +
'. <br /> You can view more details about the approval <a href=\'https://mvhs-approvals.herokuapp.com/approvalDetails/' + formObject.id_num + '\'>here</a>' +
'. <br />Please accept or reject the approval below.</p><br /><center><button style="background-color: green; width: 200px; font-size: 15px; height: 100px"><a href=\'https://mvhs-approvals.herokuapp.com/approve/' +
formObject.id_num +
'/cafeteria\'><p style="font-size: 25px; color: black">Accept</p></a></button><button style="background-color: red; width: 200px; font-size: 15px; height: 100px"><a href=\'https://mvhs-approvals.herokuapp.com/reject/' +
formObject.id_num +
'/cafeteria\'><p style="font-size: 25px; color: black">Reject</p></a></button></center>' +
'</div></center>',
})
: Promise.resolve();
let gymEmail = (formObject.campus.gym === true && formObject.dates.length !==
0 && new Date(formObject.dates[0].from).getHours() > 7 &&
new Date(formObject.dates[0].from).getHours() < 15 )
? mailer.sendMail({
recipient: GYM_EMAIL,
subject: 'A student is requesting your approval for use of the Gym/fields during school hours',
messageHTML: '<center><div style="width: 90%; background-color: #f4f4f4"><p style="text-align: center; font-size: 30px;">' +
formObject.general.student_name +
' has submitted an approval for ' + formObject.general.club_name +
'. <br /> You can view more details about the approval <a href=\'https://mvhs-approvals.herokuapp.com/approvalDetails/' + formObject.id_num + '\'>here</a>' +
'. <br />Please accept or reject the approval below.</p><br /><center><button style="background-color: green; width: 200px; font-size: 15px; height: 100px"><a href=\'https://mvhs-approvals.herokuapp.com/approve/' +
formObject.id_num +
'/gym\'><p style="font-size: 25px; color: black">Accept</p></a></button><button style="background-color: red; width: 200px; font-size: 15px; height: 100px"><a href=\'https://mvhs-approvals.herokuapp.com/reject/' +
formObject.id_num +
'/gym\'><p style="font-size: 25px; color: black">Reject</p></a></button></center>' +
'</div></center>',
})
: Promise.resolve();
let gymEmailAfterHours = (formObject.campus.gym === true &&
formObject.dates.length !== 0 &&
(new Date(formObject.dates[0].from).getHours() < 7 ||
new Date(formObject.dates[0].from).getHours() > 15 ))
? mailer.sendMail({
recipient: GYM_AFTER_HOURS,
subject: 'A student is requesting your approval for use of the Gym/fields after school hours',
messageHTML: '<center><div style="width: 90%; background-color: #f4f4f4"><p style="text-align: center; font-size: 30px;">' +
formObject.general.student_name +
' has submitted an approval for ' + formObject.general.club_name +
'. <br /> You can view more details about the approval <a href=\'https://mvhs-approvals.herokuapp.com/approvalDetails/' + formObject.id_num + '\'>here</a>' +
'. <br />Please accept or reject the approval below.</p><br /><center><button style="background-color: green; width: 200px; font-size: 15px; height: 100px"><a href=\'https://mvhs-approvals.herokuapp.com/approve/' +
'. Please accept or reject the approval below.</p><br /><center><button style="background-color: green; width: 200px; font-size: 15px; height: 100px"><a href=\'https://mvhs-approvals.herokuapp.com/approve/' +
formObject.id_num +
'/gym\'><p style="font-size: 25px; color: black">Accept</p></a></button><button style="background-color: red; width: 200px; font-size: 15px; height: 100px"><a href=\'https://mvhs-approvals.herokuapp.com/reject/' +
formObject.id_num +
'/gym\'><p style="font-size: 25px; color: black">Reject</p></a></button></center>' +
'</div></center>',
})
: Promise.resolve();
let libraryEmail = (formObject.campus.library === true)
? mailer.sendMail({
recipient: LIBRARY_EMAIL,
subject: 'A student is requesting your approval for use of the library',
messageHTML: '<center><div style="width: 90%; background-color: #f4f4f4"><p style="text-align: center; font-size: 30px;">' +
formObject.general.student_name +
' has submitted an approval for ' + formObject.general.club_name +
'. <br /> You can view more details about the approval <a href=\'https://mvhs-approvals.herokuapp.com/approvalDetails/' + formObject.id_num + '\'>here</a>' +
'. <br />Please accept or reject the approval below.</p><br /><center><button style="background-color: green; width: 200px; font-size: 15px; height: 100px"><a href=\'https://mvhs-approvals.herokuapp.com/approve/' +
formObject.id_num +
'/library\'><p style="font-size: 25px; color: black">Accept</p></a></button><button style="background-color: red; width: 200px; font-size: 15px; height: 100px"><a href=\'https://mvhs-approvals.herokuapp.com/reject/' +
formObject.id_num +
'/library\'><p style="font-size: 25px; color: black">Reject</p></a></button></center>' +
'</div></center>',
})
: Promise.resolve();
let cccEmail = (formObject.campus.ccc === true)
? mailer.sendMail({
recipient: CCC_EMAIL,
subject: 'A student is requesting your approval for use of the College and Career Center',
messageHTML: '<center><div style="width: 90%; background-color: #f4f4f4"><p style="text-align: center; font-size: 30px;">' +
formObject.general.student_name +
' has submitted an approval for ' + formObject.general.club_name +
'. <br /> You can view more details about the approval <a href=\'https://mvhs-approvals.herokuapp.com/approvalDetails/' + formObject.id_num + '\'>here</a>' +
'. <br />Please accept or reject the approval below.</p><br /><center><button style="background-color: green; width: 200px; font-size: 15px; height: 100px"><a href=\'https://mvhs-approvals.herokuapp.com/approve/' +
formObject.id_num +
'/ccc\'><p style="font-size: 25px; color: black">Accept</p></a></button><button style="background-color: red; width: 200px; font-size: 15px; height: 100px"><a href=\'https://mvhs-approvals.herokuapp.com/reject/' +
formObject.id_num +
'/ccc\'><p style="font-size: 25px; color: black">Reject</p></a></button></center>' +
'</div></center>',
})
: Promise.resolve();
let mailTasks = [
studentEmail,
advisorEmail,
cafeterialEmail,
gymEmail,
gymEmailAfterHours,
libraryEmail,
cccEmail];
Promise.all(mailTasks).then(() => {
res.json({
success: true,
});
}).catch((err) => {
console.error('error sending mail ' + err);
});
})
;
router.post('/');
module.exports = router;