-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
381 lines (341 loc) · 12.7 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
const express = require('express');
const mongoose = require('mongoose');
const path = require('path');
const localStorage = require('localStorage');
const shortid = require('shortid');
const {google} = require('googleapis');
const {OAuth2} = google.auth
const User = require('./models/User');
const _ = require('lodash');
const Appointment = require('./models/Appointment')
const app = express();
const { auth, requiresAuth } = require('express-openid-connect');
require('dotenv').config();
const config = {
authRequired: false,
auth0Logout: true,
secret: 'a long, randomly-generated string stored in env',
baseURL: 'http://localhost:3000',
clientID: '0LBhnWNCWFXEmU4lf7Q23ID5rf8xolmR',
issuerBaseURL: 'https://dev-b0ouulctqf6y4w8a.us.auth0.com',
};
const oAuth2Client = new OAuth2(
`${process.env.G_CLIENT_ID}`, `${process.env.G_CLIENT_SECRET}`
);
oAuth2Client.setCredentials({refresh_token: `${process.env.REFRESH_TOKEN}`})
const calendar = google.calendar({ version: "v3", auth: oAuth2Client });
app.set('view engine', 'ejs')
app.set('views', path.join(__dirname, 'views'));
app.use(express.static(__dirname + '/public'));
app.use(express.urlencoded({extended: false}));
app.use(express.json());
app.use(auth(config));
//DB Config
const db = require('./config/keys').MongoURI;
const { response } = require('express');
//Connect DB
mongoose.connect(db, {useNewUrlParser: true,useUnifiedTopology: true})
.then(() => console.log('db connected...'))
.catch(err => console.log(err))
app.get('/', (req, res) => {
if(req.oidc.isAuthenticated()){
res.redirect('/verify')
}else{
res.render('home')
}
});
app.get('/chillzone', (req,res)=>{
res.render('voice.ejs')
})
app.get('/verify', requiresAuth(), (req,res)=>{
var response = req.oidc.user
if(response.email_verified == false){
res.render('verification',{email: response.email})
}else{
res.redirect('/details')
}
});
app.get('/details', requiresAuth(), (req,res)=>{
const email = req.oidc.user.email
User.findOne({email: email})
.then((doc)=>{
if(doc){
res.redirect('/dashboard')
}else{
res.render('details')
}
})
.catch((err)=>console.log(err))
localStorage.setItem('email', email)
});
app.post('/post-info', (req,res)=>{
const {username , age, typeOfUser,domain} = req.body
const email = localStorage.getItem('email')
let pfpList = ["https://cdn.discordapp.com/attachments/751511569971675216/818749306893762570/Untitled-3.png","https://cdn.discordapp.com/attachments/751511569971675216/818749761368752138/Untitled-4.png","https://cdn.discordapp.com/attachments/751511569971675216/818750283445174332/Untitled-5.png","https://cdn.discordapp.com/attachments/751511569971675216/818750816444743750/Untitled-6.png"]
let pfpIndex = Math.floor(Math.random() * (4 - 0) + 0);
const pfp = pfpList[pfpIndex];
const appointments = []
console.log(domain);
newUser = new User({
'name': username,
'age': age,
'email': email,
'appointments': appointments,
'typeOfUser': typeOfUser,
'domain': domain,
'pfpUrl': pfp,
});
newUser.save()
.then(()=>{
res.redirect('/dashboard')
})
.catch((err)=>console.log(err))
})
app.get('/dashboard', requiresAuth(), (req,res)=>{
const email = req.oidc.user.email
User.findOne({email: email})
.then((doc)=>{
if(doc){
Appointment.find({})
.then(docs=>{
res.render('dashboard',{user:doc, appointments:docs})
})
.catch(err=>console.log(err))
}else{
res.send('Error, No Document Found')
}
})
.catch((err)=>console.log(err))
});
app.post('/add-appoint', async(req,res)=>{
const {name, email, date, time} = req.body;
const id = await shortid.generate();
newAppointment = new Appointment({
'ownerName': name,
'ownerEmail': email,
'appointmentCode': id,
'appointmentDate': date,
'appointmentTime': time,
'link': 'Not Approved Yet',
'status': 'Added'
})
newAppointment.save()
.then(()=>{
const newApp = {'date': date, 'time': time, 'code': id, 'status':'Added', 'link':'Not Approved Yet'}
User.updateOne({email},{
"$push" : {
"appointments": newApp
},
}).then(response=>{
res.redirect('/dashboard')
}).catch(err=>console.log(err))
}).catch(err=>console.log(err))
});
app.get('/:type/:code', requiresAuth(),(req,res)=>{
const email = req.oidc.user.email
User.findOne
Appointment.findOne({appointmentCode: req.params.code})
.then(doc=>{
if(req.params.type == 'request'){
res.render('request',{owner:doc, memberEmail: email})
}else if(req.params.type == 'approve'){
res.render('approve',{owner:doc})
}else if(req.params.type == 'delete'){
res.render('delete',{owner:doc})
}
})
.catch(err=>console.log(err))
});
app.post('/request-appoint',(req,res)=>{
const{appointmentCode, memberEmail, ownerEmail} = req.body
User.findOne({email:memberEmail})
.then(user=>{
if(user){
Appointment.updateOne({appointmentCode:appointmentCode},{
$set : {
'memberName': user.name,
'memberEmail': user.email,
'status': 'Pending'
}
})
.then(()=>{
User.findOne({email:ownerEmail})
.then((doc)=>{
let newOwnerAppointments = doc.appointments
if(doc){
for(let i=0; i< doc.appointments.length; i++){
if(doc.appointments[i].code == appointmentCode){
newOwnerAppointments[i]['status'] = 'Pending'
User.updateOne({email:ownerEmail},{
$set : {
appointments: newOwnerAppointments
}
})
.then(()=>{
res.redirect('/dashboard')
})
.catch(err=>console.log(err))
}
}
}
})
.catch(err=>console.log(err))
}).catch(err=>console.log(err))
}else{
console.log('User not found')
}
}).catch(err=>console.log(err))
});
app.post('/approve-appoint', (req,res)=>{
const{appointmentCode, memberEmail, ownerEmail, date, time} = req.body
const resource = {
start: { dateTime: `${date}T${time}:00.000+05:30`},
end: { dateTime: `${date}T${time}:00.000+03:30` },
attendees: [{ email: memberEmail},{email: ownerEmail}],
conferenceData: {
createRequest: {
requestId: appointmentCode.toString(),
conferenceSolutionKey: { type: "hangoutsMeet" },
},
},
summary: "Baatcheet Meeting",
description: `Appointment with ${memberEmail}`,
};
calendar.events
.insert({
calendarId: '[email protected]',
resource: resource,
conferenceDataVersion: 1,
})
.then((data)=>{
const link = data.data.hangoutLink
User.findOne({email:memberEmail})
.then(user=>{
if(user){
Appointment.updateOne({appointmentCode:appointmentCode},{
$set : {
'link': link,
'status': 'Approved'
}
})
.then(()=>{
User.findOne({email:ownerEmail})
.then((doc)=>{
let newOwnerAppointments = doc.appointments
if(doc){
for(let i=0; i< doc.appointments.length; i++){
if(doc.appointments[i].code == appointmentCode){
newOwnerAppointments[i]['status'] = 'Approved'
newOwnerAppointments[i]['link'] = link
User.updateOne({email:ownerEmail},{
$set : {
appointments: newOwnerAppointments
}
})
.then(()=>{
res.redirect('/dashboard')
})
.catch(err=>console.log(err))
}
}
}
})
.catch(err=>console.log(err))
}).catch(err=>console.log(err))
}else{
console.log('User not found')
}
}).catch(err=>console.log(err))
})
.catch((errs)=>{
console.log(errs)
});
});
app.post('/delete-appoint',(req,res)=>{
const{appointmentCode, memberEmail, ownerEmail} = req.body
User.findOne({email:memberEmail})
.then(user=>{
if(user){
Appointment.findOneAndRemove({appointmentCode:appointmentCode},{
})
.then(()=>{
User.findOne({email:ownerEmail})
.then((doc)=>{
let newOwnerAppointments = doc.appointments
if(doc){
for(let i=0; i< doc.appointments.length; i++){
if(doc.appointments[i].code == appointmentCode){
if (i > -1) {
newOwnerAppointments.splice(i, 1);
}
User.updateOne({email:ownerEmail},{
$set : {
appointments: newOwnerAppointments
}
})
.then(()=>{
res.redirect('/dashboard')
})
.catch(err=>console.log(err))
}
}
}
})
.catch(err=>console.log(err))
}).catch(err=>console.log(err))
}else{
console.log('User not found')
}
}).catch(err=>console.log(err))
});
// app.get('/search', (req, res) => {
// const searchQuery = req.query.query;
// console.log(searchQuery);
// // Search the database for the query
// User.find({ $or: [{ domain: searchQuery }, { description: searchQuery }] }, (err, data) => {
// if (err) {
// console.log(err);
// } else {
// res.render('search', { data: data});
// }
// });
// });
app.get('/search', (req, res) => {
const searchQuery = req.query.query;
console.log(searchQuery);
// Use Lodash to create a case-insensitive regular expression from the search query
const regex = new RegExp(_.escapeRegExp(searchQuery), 'i');
// Search the database for the query using the case-insensitive regular expression
User.find({ $or: [{ domain: regex }, { description: regex }] }, (err, data) => {
if (err) {
console.log(err);
} else {
res.render('search', { data: data });
}
});
});
//apointement page
app.get('/appointments', requiresAuth(), async (req, res) => {
try {
const email = req.oidc.user.email;
const user = await User.findOne({ email: email });
if (user) {
// Find all appointments for the user
const appointments = await Appointment.find({ });
// Render the appointments view with the appointments and user data
res.render('appointments', { owner: user, appointments: appointments });
} else {
console.log('User not found');
}
} catch (err) {
console.log(err);
res.status(500).send('Internal Server Error');
}
});
//logout route
app.post('/logout', (req, res) => {
req.logout(); // assuming you are using a session-based authentication middleware like Passport.js
res.redirect('/'); // redirect to the login page after logout
});
const port = process.env.PORT || 3000
app.listen(port, console.log(`Server listening on ${port}`))