-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
536 lines (460 loc) · 12.6 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
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
require("dotenv").config();
const express = require("express");
const http = require("http")
const { Server } = require("socket.io")
const { checkSchema } = require("express-validator");
const multer = require("multer"); //require multer
const cors = require("cors");
const app = express();
const morgan = require('morgan') //morgan required files
const path = require('path') //morgan required files
const fs = require('fs')
const cloudinary = require("cloudinary").v2;
app.use(cors());
app.use(express.json());
app.use('/images', express.static('./public/images'));
const port = process.env.PORT || 3060;
//databaseConfiguration
const configDb = require("./config/db");
configDb();
//server creation
const server = http.createServer(app)
const adminSocketId = 'admin'; // Fixed socket ID for admin
//web socket connection
const io = new Server(server, {
cors: {
origin: "https://resortify-frontend.vercel.app",
methods: ["GET", "POST"],
credentials: true
}
})
//morgan files
app.use(morgan('common'))
let logStream = fs.createWriteStream(path.join('log files','server.log'),{flags:'a'})
app.use(morgan('common',{stream:logStream}))
//authorization and authentication
const { authorizeUser, authenticateUser } = require("./App/middleware/auth");
//validationSchema
const {
userRegistrationValidation,
verifyEmailValidationSchema,
resendOTPEmailValidationSchema,
loginValidationSchema,
forgotPasswordValidation,
userUpdatingDetailsValidationSchema,
userUpdatingPassword,
} = require("./App/validations/user-validaiton");
//controllers
const userCntrl = require("./App/controllers/user-controller");
//routes users
app.post(
"/api/users",
checkSchema(userRegistrationValidation),
userCntrl.create
);
app.post(
"/api/users/verifyEmail",
checkSchema(verifyEmailValidationSchema),
userCntrl.verifyEmail
);
//for reverification of email while login if not verified and for forgot password mail to verify mail
app.post(
"/api/users/reVerifiyEmail",
checkSchema(resendOTPEmailValidationSchema),
userCntrl.resendOTP
);
//for forgot password -> mail and otp and new password
app.put(
"/api/users/forgotPassword",
checkSchema(forgotPasswordValidation),
userCntrl.forgotPassword
);
app.post(
"/api/users/login",
checkSchema(loginValidationSchema),
userCntrl.login
);
//for update
app.put(
"/api/users",
authenticateUser,
checkSchema(userUpdatingDetailsValidationSchema),
userCntrl.update
);
//for delete
app.delete("/api/users", authenticateUser, userCntrl.destroy);
//for updating password
app.put(
"/api/users/updatePassword",
authenticateUser,
checkSchema(userUpdatingPassword),
userCntrl.updatePassword
);
//for all users lists
app.get(
"/api/users",
authenticateUser,
authorizeUser(["admin"]),
userCntrl.lists
);
//for account
app.get("/api/users/account", authenticateUser, userCntrl.account);
//multer
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "./public/images");
},
filename: function (req, file, cb) {
cb(null, `${Date.now()}-${file.originalname}`);
},
});
const upload = multer({ storage });
//cloudinary configuration
cloudinary.config({
cloud_name: process.env.CLOUD_NAME,
api_key: process.env.CLOUD_API_KEY,
api_secret: process.env.CLOUD_SECRET_KEY
});
const uploader = (file, folder) => {
return new Promise((resolve, reject) => {
cloudinary.uploader.upload(
file,
{
resource_type: "auto", // Corrected from resource: auto
folder: folder
},
(error, result) => {
if (error) reject(error);
else resolve(result.url);
}
);
});
};
module.exports = {uploader: uploader}
// property controlller
const propertyController = require("./App/controllers/property-controller");
//amenity controller
const amenityController = require("./App/controllers/amenities-controller");
// room controller
const roomController = require("./App/controllers/room-controller");
// reviews
const reviewController = require("./App/controllers/reviews-controller");
//booking-controller
const bookingCntrl = require("./App/controllers/booking-controller");
// property validation schema
const {propertyValidationsSchema, generalModelValidationSchema} = require("./App/validations/property-validations");
//amenities validation schema
const amenititiesValidationSchema = require("./App/validations/amenities-validations");
// room validation schema
const roomValidationSchema = require("./App/validations/room-validations");
// review validation schema
const reviewValidationSchema = require("./App/validations/review-validations");
// static data
const dataController = require("./App/controllers/staticData-controller");
//booking validation schema
const {
bookingValidaton,
updateStatusValidation,
updateCheckInOutValidation,
bookingCancelSchema,
} = require("./App/validations/booking-validation");
//get all the resorts
app.get("/api/users/resorts", propertyController.list);
// get one resort
app.get("/api/users/resorts/:id", propertyController.listOne);
// list of my searches
app.get(
"/api/users/resorts/:id/recentsearches",
authenticateUser,
authorizeUser(["user"]),
propertyController.listForMySearches
);
app.get("/api/propertyStats", propertyController.Stats);
// create the resort
// app.post(
// "/api/owners/propertydetails",
// authenticateUser,
// authorizeUser(["owner"]),
// propertyController.create
// );
//update the resort
app.put(
"/api/owners/propertydetails/:id",
authenticateUser,
authorizeUser(["owner"]),
checkSchema(propertyValidationsSchema),
propertyController.update
);
//delete the resort
app.delete(
"/api/owners/propertydetails/:id",
authenticateUser,
authorizeUser(["owner"]),
propertyController.delete
);
// admin approval of the resort
app.put(
"/api/admin/propertydetails/:id",
authenticateUser,
authorizeUser(["admin"]),
propertyController.adminApprove
);
//to get properties based query - on 04th april by Suraj
app.get("/api/properties/lists", propertyController.lists);
// rooms api
// update a room
app.put(
"/api/owners/propertydetails/rooms/:id",
authenticateUser,
authorizeUser(["owner"]),
roomController.update
);
// delete a room
app.delete(
"/api/owners/propertydetails/room/:id",
authenticateUser,
authorizeUser(["owner"]),
roomController.delete
);
// store total room
app.post("/api/propertydetails/addrooms/:id", propertyController.addRooms);
// post amenities
app.post(
"/api/owners/amenities",
authenticateUser,
authorizeUser(["admin"]),
checkSchema(amenititiesValidationSchema),
amenityController.create
);
// update amenities
app.put(
"/api/owners/amenities/:id",
authenticateUser,
authorizeUser(["admin"]),
checkSchema(amenititiesValidationSchema),
amenityController.update
);
// delete amenities
app.delete(
"/api/owners/amenities/:id",
authenticateUser,
authorizeUser(["admin"]),
amenityController.destroy
);
//list all amenities
app.get(
"/api/owners/amenities",
authenticateUser,
authorizeUser(["owner", "admin"]),
amenityController.list
);
//list one amentity
app.get(
"/api/owners/amenities/:id",
authenticateUser,
authorizeUser(["owner", "admin"]),
amenityController.listOne
);
//get one reviews of one property
app.get("/api/reviews/:id", reviewController.listOne);
// post the review
app.post(
"/api/users/reviews/:id",
authenticateUser,
authorizeUser(["user"]),
upload.array("files", 5),
checkSchema(reviewValidationSchema),
reviewController.create
);
// update the review
app.put(
"/api/users/reviews/:id",
authenticateUser,
authorizeUser(["user"]),
checkSchema(reviewValidationSchema),
reviewController.update
);
// delete a review
app.delete(
"/api/users/reviews/:id",
authenticateUser,
authorizeUser(["user"]),
reviewController.delete
);
//bookingCntollers
//for booking
app.post(
"/api/bookings",
authenticateUser,
authorizeUser(["user"]),
checkSchema(bookingValidaton),
bookingCntrl.create
);
// changes to get the sum of the bookings as per the month
app.get(
"/api/bookings/calculate",
authenticateUser,
authorizeUser(["owner"]),
bookingCntrl.calculate
);
// controller get the status of the bookings
app.get(
"/api/bookingStatus",
authenticateUser,
authorizeUser(["owner"]),
bookingCntrl.Stats
);
//for changing booking status have to pass query
app.put(
"/api/bookings/:id",
authenticateUser,
authorizeUser(["owner"]),
checkSchema(updateStatusValidation),
bookingCntrl.changeStatus
);
//for changing checkedIn checkedOut
app.put(
"/api/bookings/in-out/:id",
authenticateUser,
authorizeUser(["owner"]),
checkSchema(updateCheckInOutValidation),
bookingCntrl.changeCheckInOut
);
//for cancellation
app.put(
"/api/bookings/cancellation/:id",
authenticateUser,
authorizeUser(["user"]),
checkSchema(bookingCancelSchema),
bookingCntrl.cancellation
);
//for all today bookings
app.get(
"/api/today/bookings",
authenticateUser,
authorizeUser(["owner"]),
bookingCntrl.listTodayBookings
);
//for all bookings in given range
app.get(
"/api/bookings",
authenticateUser,
authorizeUser(["owner"]),
bookingCntrl.listBookings
);
//for one booking
app.get("/api/bookings/:id", bookingCntrl.listOne);
//paymentCNtrl
const paymentsCltr = require("./App/controllers/paymentController");
// app.post('/api/create-checkout-session',authenticateUser, authorizeUser(['user']), paymentsCltr.pay)
app.post("/api/create-checkout-session", paymentsCltr.pay);
app.put("/api/payments/:id/success", paymentsCltr.successUpdate);
app.put("/api/payments/:id/failed", paymentsCltr.failedUpdate);
// request for static data
app.get("/api/static-data", dataController.list);
//for testing multer
app.post(
"/api/propertyphotos",
upload.array("files"),
authenticateUser,
authorizeUser(["owner"]),
propertyController.photos
);
app.post("/api/roomphotos", upload.array("files"), roomController.photos);
app.post(
"/api/documentsphotos",
upload.array("files"),
propertyController.documents
);
//to check property exist or not
app.get("/api/owners/property",
authenticateUser,
authorizeUser(['owner']),
propertyController.propertyExist
)
/// for step form
app.post(
"/api/owners/propertydetails",
authenticateUser,
authorizeUser(["owner"]),
checkSchema(propertyValidationsSchema),
propertyController.propertycreate
);
app.post(
"/api/owners/propertydetails/generalModel",
authenticateUser,
authorizeUser(["owner"]),
checkSchema(generalModelValidationSchema),
propertyController.generalModelCreate
);
app.post(
"/api/owners/propertydetails/roomtypemodel",
authenticateUser,
authorizeUser(["owner"]),
propertyController.roomtypecreate
);
app.post(
"/api/reviewsphotos",
authenticateUser,
authorizeUser(["user"]),
upload.array("files"),
reviewController.photos
);
app.get(
"/api/generalmodel",
authenticateUser,
authorizeUser(["admin"]),
propertyController.generalModeList
);
app.get(
"/api/bookingstats",
authenticateUser,
authorizeUser(["owner"]),
bookingCntrl.Stats
);
//first time connection
app.get("/", (req, res)=>{
res.json('Connection Successfull 29th May')
})
//Chat Support System
// Admin namespace
const adminNamespace = io.of('/admin');
// Event listener for admin connections
adminNamespace.on('connection', (socket) => {
// Emit event when admin socket connects
// console.log('Admin connected');
//to join admin to adminSocketId
socket.join(adminSocketId)
// Handle admin messages
socket.on('adminMessage', (message) => {
// console.log('Admin message:', message);
// Broadcast admin message to all connected users
io.emit('adminMessage', message);
});
socket.on('admin_message', (data) => {
const { socketId, adminMsg } = data
// console.log(socketId, adminMsg)
io.to(socketId).emit('admin_response', { socketId, adminMsg })
})
socket.on("disconnect", () => {
console.log("user Disconnected", socket.id)
})
});
io.on("connection", (socket) => {
// console.log("user connected", socket.id)
// Store the socket ID in the map
socket.emit('welcome_msg','Welcome to Resotify...')
adminNamespace.to(adminSocketId).emit('new_user', socket.id);
socket.on("disconnect", () => {
// console.log("user Disconnected", socket.id)
adminNamespace.to(adminSocketId).emit('user_disconnected', socket.id);
})
socket.on('user_message', (data) => {
// console.log('User message:', data);
// Forward user message to admin
adminNamespace.to(adminSocketId).emit('userMessage', data);
});
})
server.listen(port, () => {
console.log("Server runnning on port" + " " + port);
});