-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
162 lines (143 loc) · 3.84 KB
/
app.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
var bodyParser = require("body-parser");
var mongoose = require("mongoose");
var express = require("express");
const { v4: uuidv4 } = require("uuid");
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
express.static("public");
var user_base_schema = new mongoose.Schema({
unique_id: String,
alone: Boolean,
room: String,
});
var user_base = mongoose.model("user_bases", user_base_schema);
mongoose.connect(
"mongodb+srv://wimpywarlord:[email protected]/<dbname>?retryWrites=true&w=majority",
{ dbName: "viit" },
function (err, res) {
if (err) {
console.log("mongo lab server not connected");
console.log(err);
} else {
console.log("CONNECTED TO DB");
}
}
);
app.get("/", function (req, res) {
res.render("index.ejs");
});
app.post("/party", async function (req, res) {
var sort = { _id: 1 };
var list_of_all_user;
console.log("FETCHING STARTED.");
var best_free_room;
var list_of_all_user = await user_base
.find({}, function (err, all_users) {
return new Promise((resolve, reject) => {
if (err) {
console.log("ALL CURRENT USER NOT FETCHED");
reject(err);
} else {
console.log("ALL USERS FETCHED.");
// console.log(all_users);
list_of_all_user = all_users;
resolve(all_users);
}
});
})
.sort(sort);
console.log("FETCHING COMPLETED.");
// console.log(list_of_all_user);
// <--CODE FOR FINDING THE BEST ROOM-->
console.log("STARTING TO FIND BEST ROOM");
if (list_of_all_user != undefined)
list_of_all_user.forEach((element) => {
if (element.alone == true) {
best_free_room = element;
return false;
}
});
if (!best_free_room) {
console.log("NO BEST USER FOUNDS");
} else {
console.log(best_free_room);
}
console.log("END OF BEST ROOM");
// <--CODE FOR FINDING THE BEST ROOM-->
if (best_free_room) {
// console.log("THE BEST USESR IS", best_free_room);
var filter = { unique_id: best_free_room.unique_id };
var update = { alone: false };
// SETTING THE ALONE OF BEST USER TO FALSE
var updating_one_person = new Promise(async (resolve, reject) => {
console.log("STARTING UPDATE USER WHO GOT PAIRED NOW");
await updateDb(filter, update, false);
console.log("ENDING UPDATE USER WHO GOT PAIRED NOW");
});
console.log("late ENDING UPDATE USER WHO GOT PAIRED NOW");
// IF WE FIND SOME FREE ROOM
console.log("STARTING UPDATE OF THE REQUESTING USERS");
await updateDb(
{
unique_id: req.body.unique_id,
},
{
unique_id: req.body.unique_id,
alone: false,
room: best_free_room.room,
},
{
upsert: true,
}
);
console.log("ENDING UPDATE OF THE REQUESTING USERS");
res.redirect(`https://meet.jit.si/${best_free_room.room}`);
} else {
// IF NO FREE ROOM IS AVAILABLE
const room_for_current_user = uuidv4();
console.log("STARTING CREATE NEW USER");
await updateDb(
{
unique_id: req.body.unique_id,
},
{
unique_id: req.body.unique_id,
alone: true,
room: room_for_current_user,
},
{
upsert: true,
}
);
console.log("ENDING CREATE NEW USER");
res.redirect(`https://meet.jit.si/${room_for_current_user}`);
}
// var finding_free_person = new Promise(async (resolve, reject) => { });
// var finding_free_person = new Promise(async (resolve, reject) => { });
// var finding_free_person = new Promise(async (resolve, reject) => { });
// console.log("FETCHING COMPLETED ");
// res.redirect("/");
});
app.listen(process.env.PORT || 3000, function () {
console.log("SERVER 3000 HAS STARTED");
});
async function updateDb(filter, update, upsert) {
return new Promise(async (resolve, reject) => {
await user_base.findOneAndUpdate(
filter,
update,
{
upsert: upsert,
},
function (err, res) {
if (err) {
console.log(err);
reject(err);
} else {
console.log("Done! " + res);
resolve(res);
}
}
);
});
}