-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
176 lines (154 loc) · 6.12 KB
/
server.js
File metadata and controls
176 lines (154 loc) · 6.12 KB
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
var port = 8080; // Port of server
//Libraries
var getenv = require('getenv'); // Library for Enviroment Variables, Used for DB Connection
var mysql = require('promise-mysql'); // MySQL Library, With Node Promises
var sha512 = require('sha512'); // SHA512 Library; SHA512 is a hash
var bodyParser = require('body-parser'); // Library for parsing data
var jsonParser = bodyParser.json(); // Using Data type Json
var cors = require("cors"); // Library for handling access headers
//var QRCode = require('qrcode');
var nodemailer = require('nodemailer');
var randomstring = require("randomstring");
const spawn = require("child_process").spawn;
var express = require('express'); // Express Framework for Node
var app = express(); // Establishing Express App
app.use(express.logger());
app.use(cors()); // Cors to Handle URL Authentication
app.use(bodyParser.json()); // Using Body Parser
var server = app.listen(8080); // Set Port
var con = null;
mysql.createConnection({
host: getenv('IP'),
user: getenv('C9_USER'),
password: "",
database: "c9"
}).then(function(connection) { con = connection });
app.get("/registration", async function(req, res) {
console.log(req.query)
let hash = req.query.hash;
console.log(hash)
let [query] = await con.query(`SELECT * FROM Participant WHERE Hash="${hash}"`)
//console.log(query)
if (query) {
let updateStatus = await con.query(`UPDATE Participant SET status = 1 WHERE Hash = "${hash}"`)
//console.log(updateStatus)
if (updateStatus){
res.status(200).json({
message : `${query.Name}Succesfully Registered`
});
}
} else {
res.status(400)
}
});
app.get('/sendQrInvites', async function(req, res) {
let email = req.query.email
if (email == "all") {
var participants = await con.query(`SELECT * From Participant WHERE mailSent = 0 LIMIT 20`)
//var participants = [{ "Name": "Yeshwant", "Hash": null, "Email": "ykethineni@gmail.com" }]
for (var participant of participants) {
await allEmails(participant, res)
}
}
});
async function sendQrEmail(message, emailTo, res, name) {
console.log(name)
var ticketLocation = "./tickets/out-" + name + ".png"
// Generate test SMTP service account from ethereal.email
// Only needed if you don't have a real mail account for testing
nodemailer.createTestAccount((err, account) => {
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: "ssl", // true for 465, false for other ports
auth: {
user: 'oakcodefest@oakridge.in', // generated ethereal user
pass: '' // Add Password before using removed for Git Push
}
});
// setup email data with unicode symbols
let mailOptions = {
from: '"Oakridge Codefest Team"<oakcodefest@oakridge.in>', // sender address
to: emailTo, // list of receivers
cc: ['rahultarak12345@gmail.com', 'gaurangbharti@gmail.com', 'rithvikm8@gmail.com', 'kavesbteja@gmail.com', 'ykethineni@gmail.com'], // Me!
subject: 'Oak Codefest 2019 Invite', // Subject line
html: message,
attachments: [{
filename: 'Oak Codefest Consent Form.pdf',
path: './consentForm.pdf'
}, {
filename: 'Oak Codefest Ticket.png',
path: ticketLocation
}, {
filename: 'Oak Codefest Ticket-Back.png',
path: './backOfTicket.png'
}]
};
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
res.status(200).json({
message: "success"
})
console.log('Message sent: %s', info.messageId);
console.log("Email alert sent");
// Preview only available when sending through an Ethereal account
console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
// Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@blurdybloop.com>
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
});
});
}
async function generateHash() {
var code
while (true) {
code = randomstring.generate(12);
console.log(code)
let [codeExsists] = await con.query(`SELECT * FROM Participant WHERE Hash="${code}"`);
if (!codeExsists) {
break;
}
}
return code
}
async function allEmails(participant, res) {
console.log(participant.Name)
var code;
if (participant.Hash == "") {
code = await generateHash()
}
else {
code = participant.Hash
}
console.log("calling python")
var pythonProcess = spawn('python3', ["./embed.py", code, participant.UUID]);
await pythonProcess.stdout.on('data', async function(data) {
if (data.toString()) {
console.log(data.toString());
console.log(participant);
let newInvite = await con.query(`UPDATE Participant SET mailSent = '1', Hash = '${code}' WHERE UUID = '${participant.UUID}'`)
//let newInvite = true
if (newInvite) {
var message = `Hey ${participant.Name},<br>
<br>
We would like to congratulate you on qualifying for the Oak Codefest 2019 on the <b>19th and 20th January</b>. Reporting time for the event is <b>9:15 am</b><br>
<br>
We would require that you send your school and an emergency contact number. For participants who would be staying overnight,please find the attached consent form which we would require you to submit on the day of the event.<br>
<br>
This year for Oak Codefest we will be selling t-shirts at Rs.300, if you would like to purchase one please inform us beforehand so we can make sure there is enough stock in your size.
<br><br>
We have also set up a discord server: <a href="https://discord.gg/jwJZqDs">https://discord.gg/jwJZqDs</a> where participants can directly communicate with us. We would request the entire team to join as it would help us communicate with the participants before and during the event.<br>
<br>
We would require you to bring your ticket given below on the day of the event.<br>
Regards,<br>
The Oak Codefest Team
`
console.log(message)
await sendQrEmail(message, participant.Email, res, participant.UUID);
}
}
});
}