-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
207 lines (184 loc) · 6.95 KB
/
server.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
const express = require('express');
const hbs = require('hbs');
const firebase = require('firebase');
const socketIO = require('socket.io');
const path = require('path');
const http = require('http');
const spawn = require('child_process').spawn;
const fs = require('fs');
const crypto = require('crypto');;
var app = express();
var server = http.createServer(app);
var io = socketIO(server);
app.io = io;
//TEMPLATE RENDERING CODE
var path1 = path.join(__dirname, 'views');
app.use(express.static(path1));
app.set('view engine', 'hbs');
//TEMPLATING COMPLETE
//FIREBASE linking Code
// Ask for config info
//LINK COMPLETE
//Club info in connected server
var club = {
clubName: "",
cvList: "",
htmlCV: "",
currentUser: ""
}
//HASHED PASSWORDS OF ALL CLUBS
var password = {
englishclub: "40d6d32f29d78b185443b75544e5cff9bae81e2dbfd7c9c9799e94333d36063d",
appteam: "c7a099afc459faf474bebbacd7b55b1a6506ef83aa7bd629223b820bb5eac643"
}
//SOCKET EVENTS
io.on('connection', (socket) => {
socket.on('input', (clubData) => { // When Club Member logs in through the home form
[clubData, pass] = clubData.split(';') // Extracting club name and club password
var hashedPassword = crypto.createHmac('sha256', 'dontmesswithusbitch')
.update(pass) // Hashing Entered Password
.digest('hex');
clubData = clubData.toLowerCase().replace(" ", ""); //Formatting Club Name
//Access Data of all the clubs
var ref = firebase.database().ref();
ref.on("value", (snapshot, e) => {
if(e) { //ERROR DETECTION
console.log(e);
}
if(snapshot.val()[clubData] === undefined){ //Check for existence of club in database
socket.emit('error1');
return 0; //EXIT CODE
}
club.clubName = clubData; //club: {clubName: "{clubData}", cvList: /*Data of that club*/}
club.cvList = snapshot.val()[clubData];
if(hashedPassword === password[club.clubName]){ // Verifying password
socket.emit('redirect', {location: '/club'}); //window.location being changed with 'club' object being modified
} else {
socket.emit('wrongPassword')
}
});
});
socket.on('getData', (data) => { // Respond to getData request from club.hbs
socket.emit('CVData', { // Provides all data inside club object made in server.js
cvs: club.cvList,
name: club.clubName
});
});
socket.on('rating', (data) => { // Socket data coming from cv.js after user has been rated
var currentUserData; // Object to collect data of the 'currently-interviewing' candidate
var ref = firebase.database().ref(`/${club.clubName}/${club.currentUser}`);
ref.on("value", (snapshot, e) => { // Read firebase data of the candidate being interviewed
currentUserData = snapshot.val();
});
// DEFINING UNDEFINED PROPERTIES IN FIREBASE CANDIDATE DATA
var keys = ["Name", "Mobile", "Email", "Branch", "Skills", "Achievements", "Area of Interest", "Ques1", "Ques2", "Ques3", "Ques4"];
for(var key in keys){
if(!currentUserData.hasOwnProperty(keys[key])) {
currentUserData[keys[key]] = "";
}
}
//Adding rating and comments from User's input in cv.hbs to firebase
currentUserData["rating"] = data.rating;
currentUserData["comments"] = data.comments;
//Updating the data
//TODO: We can shorten the update code.
ref.set({
Achievements: currentUserData.Achievements,
Name: currentUserData.Name,
Mobile: currentUserData.Mobile,
Email: currentUserData.Email,
Branch: currentUserData.Branch,
Skills: currentUserData.Skills,
Ques1: currentUserData.Ques1,
Ques2: currentUserData.Ques2,
Ques3: currentUserData.Ques3,
Ques4: currentUserData.Ques4,
rating: currentUserData.rating,
comments: currentUserData.comments,
"Area of Interest": currentUserData["Area of Interest"]
});
// DO NOT ATTEMPT TO DEFINE AN OBJECT IN ref.set(). It will modify the firebase data, but instead of the rollno id,
// currentUserData will be placed
});
});
//GET ROUTES DEFINED
app.get('/', (req, res) => {
res.render('home');
});
app.get('/club', (req, res) => {
var rg = /(^\w{1}|\.\s*\w{1})/gi;
var formattedName = club.clubName.replace(rg, function(toReplace) {
return toReplace.toUpperCase();})
res.render('club', {
clubName: formattedName
});
});
app.get('/api/getpdf/:club/:id', (req, res) => {
var club = req.params.club;
var id = req.params.id;
var base_filename = path.join(__dirname, '/generated_pdfs', club+id)
var pdf_filename = base_filename+'.pdf';
fs.stat(pdf_filename, function(err, stat) {
if(err == null) {
console.log('File exists');
res.sendFile(pdf_filename);
} else if(err.code == 'ENOENT') {
// file does not exist
makepdf(club,id,res);
} else {
console.log('Some other error: ', err.code);
}
});
});
app.get('/cv/:club/:id', (req, res) => {
club.currentUser = req.params.id;
candidate={};
var ref = firebase.database().ref(`/${club.clubName}/${club.currentUser}`);
ref.on("value", (snapshot, e) => {
candidate = snapshot.val();
// console.log('Candidate: ',candidate);
});
if(!candidate.hasOwnProperty('comments')){
console.log('Comment Property not there');
}
app.io.emit('loadRatings', candidate);
res.render('cv', {
clubName: req.params.club,
id: req.params.id,
comments: candidate.comments,
rating: candidate.rating
})
});
server.listen(3000, () => {
console.log('Port up and running');
});
//LaTex TEMPLATING
function makepdf(club, id, res){
var ref = firebase.database().ref();
var base_filename = path.join(__dirname, '/generated_pdfs', club+id)
var pdf_filename = base_filename+'.pdf';
ref.on("value", (snapshot, e)=>{
var fs = require('fs');
var json_obj = snapshot.val()[club][id];
json_obj['rollno'] = id;
fs.writeFile(base_filename+'.json', JSON.stringify(json_obj), function(err) {});
var prc = spawn('python', ["makepdf.py", base_filename+'.json']);
//noinspection JSUnresolvedFunction
prc.stdout.setEncoding('utf8');
prc.stdout.on('data', function (data) {
var str = data.toString()
var lines = str.split(/(\r?\n)/g);
console.log(lines.join(""));
});
prc.stderr.setEncoding('utf8');
prc.stderr.on('data', function (data) {
var str = data.toString()
var lines = str.split(/(\r?\n)/g);
console.log(lines.join(""));
});
prc.on('close', function (code) {
res.sendFile(pdf_filename);
console.log('process exit code ' + code);
});
});
}