-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
191 lines (169 loc) · 6.08 KB
/
index.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
let express = require('express');
let app = express();
const ejsMate = require('ejs-mate');
const path = require('path');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const methodOverride = require('method-override');
const User = require('./models/user');
const Stats = require('./models/stats');
const Preferences = require('./models/preferences');
const Matches = require('./models/matches');
const session = require('express-session');
let matches = [];
mongoose.connect('mongodb://localhost:27017/mockInterview', {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => console.log("Database Connected"))
.catch(e => console.log("DB not Connected", e));
app.engine('ejs', ejsMate);
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/review/css', express.static('public/css'));
app.use(express.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(session({ secret: 'notagoodsecret' }))
const requireLogin = (req, res, next) => {
if (!req.session.user_id) {
return res.redirect('/login')
}
next();
}
function calculateJaccardIndex(pref1, pref2, fields) {
const setA = fields.map(field => pref1[field]);
const setB = fields.map(field => pref2[field]);
const intersection = setA.filter(value => setB.includes(value));
const union = new Set([...setA, ...setB]);
return intersection.length / union.size;
}
app.get('/', requireLogin, (req, res) => {
res.render("webApp/dashboard", { foundUser: req.session.user });
})
app.get('/register', (req, res) => {
res.render('webApp/register')
})
app.post('/register', async (req, res) => {
const { name, password, username } = req.body;
const user = new User({ name, username, password })
await user.save();
req.session.user_id = user._id;
res.redirect('webApp/dashboard')
})
app.get('/login', (req, res) => {
res.render('webApp/login');
})
app.post('/login', async (req, res) => {
const { username, password } = req.body;
const foundUser = await User.findAndValidate(username, password);
if (foundUser) {
req.session.user = foundUser;
req.session.user_id = foundUser._id;
res.render('webApp/dashboard', { foundUser });
}
else {
res.redirect('/login');
}
})
app.get('/logout', (req, res) => {
req.session.user_id = null;
req.session.destroy();
res.redirect('/login');
})
app.get('/preferences', (req, res) => {
res.render('webApp/preferences');
})
app.post('/preferences', async (req, res) => {
const { profile, jobType, interviewType, experience, language,
duration } = req.body;
let userID = req.session.user_id;
await Preferences.findOneAndDelete({ user: userID })
const pref = new Preferences({
profile, jobType, interviewType, experience, language,
duration, user: userID
});
await pref.save();
res.redirect('/');
})
app.get('/candidates/:userId', async (req, res) => {
const userPreferences = await Preferences.findOne({ user: req.params.userId });
const otherUsersPreferences = await Preferences.find({ user: { $ne: req.params.userId } });
const fieldsToCompare = ['duration', 'language', 'profile', 'jobType', 'interviewType', 'experience'];
matches = otherUsersPreferences.map(otherPref => {
const similarity = calculateJaccardIndex(userPreferences, otherPref, fieldsToCompare);
return { myID: req.params.userId, peerID: otherPref.user, similarity };
}).filter(match => match.similarity >= 0.5);
// await Matches.deleteMany({ myID: req.params.userId });
// await Matches.insertMany(matches);
const usersToFind = matches.map(a => a.peerID);
const candidates = await User.find({ _id: { $in: usersToFind } });
const data = matches.map(d => {
let us = candidates.find(c => c._id.equals(d.peerID));
return {
...d, name: us.name, username: us.username
}
})
const matchesDb = await Matches.find({ myID: req.params.userId })
res.render('webApp/candidates', { data, matchesDb });
})
app.post('/meetingCode', async (req, res) => {
const myID = req.session.user_id;
const { code, peerId } = req.query;
const nw = await Matches.findOneAndUpdate({ myID: myID, peerID: peerId }, { status: 'accepted', code });
const nw3 = await Matches.findOneAndUpdate({ myID: peerId, peerID: myID }, { status: 'accepted', code });
res.json("Data successfully reached");
})
app.get('/candidates', (req, res) => {
res.render('webApp/candidates');
})
app.get('/lobby', (req, res) => {
res.render('webApp/lobby');
})
app.post('/lobby', (req, res) => {
let roomId = req.body.roomId;
res.redirect(`/interview?roomId=${roomId}`);
});
app.get('/interview', (req, res) => {
const { roomId, peerId } = req.query;
req.session.peerID = peerId;
res.render('webApp/video', { roomId, peerId });
})
app.get('/review/:peerId', async (req, res) => {
const peer = await User.findOne({ _id: req.params.peerId });
res.render('webApp/reviewForm', { peer });
})
app.post('/review', async (req, res) => {
const peer = await User.findOne({ _id: req.query.peerId });
const data = req.body;
for (let key in data) {
if (!isNaN(data[key])) {
data[key] = Number(data[key]);
}
}
data.user = req.query.peerId;
const update = {
$set: {
first: data.first,
second: data.second,
third: data.third,
fourth: data.fourth,
},
$inc: {
meetingCount: 1,
firstOv: data.first,
secondOv: data.second,
thirdOv: data.third,
fourthOv: data.fourth,
}
}
const upd = await Stats.findOneAndUpdate({ user: req.query.peerId }, update, { upsert: true, new: true });
res.redirect('/');
})
app.get('/stats', async (req, res) => {
const scores = await Stats.findOne({ user: req.session.user_id });
res.render('webApp/stats', { scores });
})
app.listen(4321, () => {
console.log("Server is live");
})