-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
198 lines (189 loc) · 7.76 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
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
const express = require('express'),
app = express(),
mysql = require('mysql'),
joi = require('joi'),
bodyparser = require('body-parser'),
{google} = require('googleapis'),
keys = require('./keys.json'),
client = new google.auth.JWT(
keys.client_email,
null,
keys.private_key,
['https://www.googleapis.com/auth/spreadsheets'],
),
swig = require('swig'),
nodeMailer = require('nodemailer'),
transporter = nodeMailer.createTransport({
service: 'gmail',
auth: {
user: 'YourEmail',
pass: 'YourPassword'
}
});
app.engine('html', swig.renderFile);
app.set('view engine', 'html');
app.set('views', __dirname + '/form');
app.use(bodyparser.json());
app.use(express.urlencoded());
app.use(express.json());
app.use('/css',express.static(__dirname +'/form/css'));
app.use('/js',express.static(__dirname +'/form/js'));
app.use('/images',express.static(__dirname +'/form/images'));
app.use('/webfonts',express.static(__dirname +'/form/webfonts'));
app.use('/fonts',express.static(__dirname +'/form/fonts'));
var pool = mysql.createPool({
connectionLimit : 60,
host:'localhost',
user: 'root',
password: '',
database: 'ieee',
multipleStatements: true
});
client.authorize(function(err, tokens) {
if(err) {
console.log('Google Sheets API Failed \n Error: ' + JSON.stringify(err,undefined,2));
return;
} else {
console.log('Google Sheets API Connected!');
}
});
const gsapi = google.sheets({
version:'v4',
auth: client
});
// app.use('/', express.static('form'));
app.get('/', (req,res) => {
res.render('index');
});
app.post('/', function (req, res) {
const SchemaValidation = {
name: joi.string().min(4)
.required()
.error(() => {
return {
message: 'Name is required. (min:4 chars)',
};
}),
email: joi.string()
.email()
.error(() => {
return {
message: 'Email field can\'t be Empty',
};
}),
phone: joi.string()
.min(8)
.max(14)
.required()
.error(() => {
return {
message: 'Phone Number field is Required (min: 8 characters - max: 14 characters)',
};
}),
university: joi.string()
.required()
.error(() => {
return {
message: 'University field is Required',
};
}),
faculty: joi.string()
.required()
.error(() => {
return {
message: 'Faculty field is Required',
};
}),
academicyear: joi.string()
.required()
.error(() => {
return {
message: 'Academic Year field is Required and should range from 1-6',
};
}),
workshop: joi.array()
.items(joi.string().error(() => {
return {
message: 'You must pickup 2 Committees',
};
})),
first_choice: joi.string()
.required()
.error(() => {
return {
message: 'You should pickup first choice',
};
}),
second_choice: joi.string()
.required()
.error(() => {
return {
message: 'You should pickup second choice',
};
}),
};
joi.validate(req.body,SchemaValidation,(err, result) => {
if(err) {
res.render('index', {output: `<div class="alert alert-danger" role="alert">${err.details[0].message}</div>`});
return; // don't try saving to db if the schema isnt valid
}
pool.getConnection(function(err, connection) {
if(err) {
console.log('Can\'t connect to MySQL Database \n Errors: ' + err);
res.render('index', {output: `<div class="alert alert-danger" role="alert">Can't Connect to MySQL Database</div>`});
} else {
connection.query('INSERT INTO `form` (`name`,`email`,`phone`,`university`,`faculty`,`academic_year`,`first_choice`,`second_choice`) VALUES ("'+req.body.name+'","'+req.body.email+'","'+req.body.phone+'","'+req.body.university+'","'+req.body.faculty+'","'+req.body.academicyear+'","'+req.body.first_choice+'","'+req.body.second_choice+'")', function(err, users) {
if(err){
res.render('index', {output: `<div class="alert alert-danger" role="alert">Make sure you entered Correct Data</div>`});
connection.release();
}
else
{
connection.query(`SELECT id FROM form WHERE phone = '${req.body.phone}' LIMIT 1;`, function(err, rows) {
if(err) {
console.log(`MYSQL DB ERROR: Can't Find User with Phone: ${req.body.phone} \n Error: ${err}`)
connection.release();
} else {
let ID = rows[0].id,
dataArray = [ID, req.body.name, req.body.email, req.body.phone, req.body.university, req.body.faculty, req.body.academicyear, req.body.first_choice, req.body.second_choice];
const insertData = {
spreadsheetId: '1AoTk7I1iWiNJJojK4YNBM4A2XwpwYaMgjnUESFSc4pA',
range: 'A2',
valueInputOption: 'USER_ENTERED',
resource: { values: [dataArray]}
};
gsapi.spreadsheets.values.append(insertData);
var mailOptions = {
from: 'midomailer@gmail.com',
to: req.body.email,
subject: 'IEEE Application Submitted',
html: `<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
</head>
<body>
<div style='font-size:18px;'>Hello ${req.body.name} <br/>
Thank you for registering, You've selected <b>${req.body.first_choice}</b> as your first preference and <b>${req.body.second_choice}</b> as your second preference, you'll be contacted soon for your interview.</div>
</body>
</html>`
};
transporter.sendMail(mailOptions, (error, info) => {
if(error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
res.render('success');
connection.release();
}
});
}
});
}
});
})
});
app.listen(3000);