-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
2994 lines (2728 loc) · 68.3 KB
/
Copy pathserver.js
File metadata and controls
2994 lines (2728 loc) · 68.3 KB
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var express = require("express");
var request = require('request'); // "Request" library
var cors = require('cors');
var querystring = require('querystring');
var fs = require("fs");
var {google} = require('googleapis');
var cookieParser = require("cookie-parser");
const res = require("express/lib/response");
var bodyParser = require('body-parser');
var webpush = require('web-push');
/*
var {Client} = require("pg");
*/
const PORT = process.env.PORT || 12232;
var app = express();
app.use(express.static(__dirname + '/public')).use(cors()).use(cookieParser()).use(bodyParser.json({limit:"50mb"}));
console.log(process.env.HOST);
console.log()
const { MongoClient, ServerApiVersion } = require('mongodb');
const uri = process.env.MONGODB_URI;
// "mongodb+srv://mzhang0213:<db_password>@heroku.qkcqp9r.mongodb.net/?retryWrites=true&w=majority&appName=heroku"
// Create a MongoClient with a MongoClientOptions object to set the Stable API version
const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
}
});
const imDone = async function(){
setTimeout(async function(){
await client.close();
},200)
}
//websocketing
//음악 퀴즈
const { createServer } = require("http");
const { Server } = require("socket.io");
const httpServer = createServer(app);
/*
const io = new Server(httpServer, { }); //options
var dowebsocketstuff = function(){
console.log("doing web socket stuff");
io.on("connection", (socket) => {
console.log(`connected with transport ${socket.conn.transport.name}`);
socket.conn.on("upgrade", (transport) => {
console.log(`transport upgraded to ${transport.name}`);
});
socket.on("disconnect", (reason) => {
console.log(`disconnected due to ${reason}`);
});
socket.on("mod-mod_code",(body)=>{
io.emit("client-mod_code",body);
});
socket.on("client-code_response",(body)=>{
io.to(body.modId).emit("mod-code_response",body);
})
socket.on("client-가자",(body)=>{
io.to(body.modId).emit("client-가자",body);
})
});
}
*/
//proxying
// include dependencies
const { createProxyMiddleware } = require('http-proxy-middleware');
// proxy middleware options
/** @type {import('http-proxy-middleware/dist/types').Options} */
/*
const options = {
target: 'https://instagram.com/direct/inbox/', // target host
changeOrigin: false, // needed for virtual hosted sites
ws: true, // proxy websockets
ignorePath:true,
pathRewrite: {
'^/api/old-path': '/api/new-path', // rewrite path
'^/api/remove/path': '/path', // remove base path
},
router: {
// when request.headers.host == 'dev.localhost:3000',
// override target 'http://www.example.org' to 'http://localhost:8000'
'michaelzhangwebsite.herokuapp.com': 'https://instagram.com/direct/inbox',
}
};
// create the proxy (without context)
const exampleProxy = createProxyMiddleware(options);
// mount `exampleProxy` in web server
app.use('/prox', exampleProxy);
*/
// BOBABYTE PLATFORM - generalized to "platform"
//create the frameworks for websocket connection to work
const server = createServer(app);
const io = new Server(httpServer, { /* options */ });
var bobabytewebsocket = function(){
console.log("doing web socket stuff");
//instantiating websocket with events
io.on("connection", (socket) => {
console.log(`connected with transport ${socket.conn.transport.name}`);
socket.conn.on("upgrade", (transport) => {
console.log(`transport upgraded to ${transport.name}`);
});
socket.on("disconnect", (reason) => {
console.log(`disconnected due to ${reason}`);
});
socket.on("testMessage", (body)=>{
console.log(body);
})
});
}
app.get("/sendSuperSecretMessage",(req,res)=>{
io.emit("messageBack","this is message from sorvor");
io.emit("messageBack",{"안녕":"하세요"});
res.send("pog");
})
//name of database in mongodb
const hackDbName = "bobabyte2024";
/**
* Creates a new user given a list [req.body.names]
* containing objects that list first and last name.
*/
/* req.body.names:
[
{
first:"Michael",
last:"Jang"
},
{}
]
*/
app.post("/platform-newUser", async (req,res)=>{
async function run(){
try{
await client.connect();
const db = client.db(hackDbName).collection("accounts");
const currContent = await db.findOne();
const users = currContent.usernames;
var found=-1; //holds group id if found, else -1 (bad code, bad variable purpose, idc)
var submit = users;
var msg = {
error:0
}
for (var i=0;i<req.body.names.length;i++){
var newUser = req.body.names[i];
if(req.body.names[i].user===undefined||req.body.names[i].user===null||req.body.names[i].user===""){
var newName = (req.body.names[i].first.charAt(0)+req.body.names[i].last.charAt(0)).toLowerCase();
for (var j=0;j<5;j++)newName+=Math.floor(Math.random()*10);
newUser.user = newName;
}
submit.push(newUser);
}
const filter = {title:"accounts"}
const updateDoc = {
$set: {
usernames:submit
}
}
await db.updateOne(filter,updateDoc);
res.send(JSON.stringify(msg));
}catch (error){
console.log(error);
}finally{
await client.close();
}
}
await run();
})
/**
* Given username submission, lets the client
* know that username is good and that login
* is successful
*/
//req.body.user is the username submitted
app.post("/platform-login", async (req,res)=>{
async function run(){
try{
await client.connect();
const db = client.db(hackDbName).collection("accounts");
const currContent = await db.findOne();
const usernames = currContent.usernames;
var found=false;
var msg = {
error:0
}
for (var i=0;i<usernames.length;i++){
if (req.body.user==usernames[i].user){
//usernames[i] is the correct registered username
msg.user=usernames[i].user;
msg.first=usernames[i].first;
msg.last=usernames[i].last;
found=true;
}
}
if (!found){
msg.error=1;
console.log("toast");
}
res.send(JSON.stringify(msg))
}catch (error){
console.log(error);
}finally{
await client.close();
}
}
await run();
})
/**
* Given username submission, lets the client
* know that username is good and that login
* is successful
* We let the client know using body.error
*/
//req.body.user is the username submitted
app.post("/platform-staff-login", async (req,res)=>{
async function run(){
try{
await client.connect();
const db = client.db(hackDbName).collection("accounts");
const currContent = await db.findOne();
const staff = currContent.staff;
var found=false;
var msg = {
error:0
}
for (var i=0;i<staff.length;i++){
if (req.body.user==staff[i]){
found=true;
break;
}
}
if (!found){
msg.error=1;
console.log("toast");
}
res.send(JSON.stringify(msg))
}catch (error){
console.log(error);
}finally{
await client.close();
}
}
await run();
})
/*
groups are stored in an array
a single group's schematic:
{
group:"group_name"
id:"######" //6 random numbers
members:[<usernames>]
}
*/
/**
* Reads the current groups to see if
* [req.body.group] exists in the database
* already. If so, add [req.body.user] to
* the group and update database. If not,
* respond to client asking if the submitted
* group name is correct.
*/
//req.body.group is the username submitted, req.body.user is the user's username
app.post("/platform-glogin", async (req,res)=>{
async function run(){
try{
await client.connect();
const db = client.db(hackDbName).collection("accounts");
const currContent = await db.findOne();
const groups = currContent.groups;
var found=-1; //holds group id if found, else -1 (bad code, bad variable purpose, idc)
var submit = [];
var msg = {
error:0
}
for (var i=0;i<groups.length;i++){
if (req.body.group===groups[i].group){
found=groups[i].id;
groups[i].members.push(req.body.user);
}
submit.push(groups[i]);
}
//얼마든지 맞아줄게
console.log(submit);
if (found!==-1){
const filter = {title:"accounts"}
const updateDoc = {
$set: {
groups:submit
}
}
await db.updateOne(filter,updateDoc);
}else {
//new group, but i want to send confirmation that they are creating new group
msg.error=1;
}
msg.group=req.body.group;
msg.id=found;
msg.user=req.body.user;
res.send(JSON.stringify(msg));
}catch (error){
console.log(error);
}finally{
await client.close();
}
}
await run();
})
/**
* Create a new group in database with the
* given [req.body.user] as the first member
* in the group, named [req.body.group]. Creates
* the group with a random ID.
*/
//req.body.group, req.body.user
app.post("/platform-glogin-confirm", async (req,res)=>{
async function run(){
try{
await client.connect();
const db = client.db(hackDbName).collection("accounts");
const currContent = await db.findOne();
const groups = currContent.groups;
var submit = groups;
var randomName = (Math.floor(Math.random()*10))+""+(Math.floor(Math.random()*10))+""+(Math.floor(Math.random()*10))+""+(Math.floor(Math.random()*10))+""+(Math.floor(Math.random()*10))+""+(Math.floor(Math.random()*10))+"";
var members = [req.body.user];
var currGroup = {
group:req.body.group, //gname
id:randomName, //random name
members:members
}
submit.push(currGroup);
const filter = {title:"accounts"}
const updateDoc = {
$set: {
groups:submit
}
}
await db.updateOne(filter,updateDoc);
var msg = {
group:req.body.group,
id:randomName
}
res.send(JSON.stringify(msg))
}catch (error){
console.log(error);
}finally{
await client.close();
}
}
await run();
})
/**
* Given [req.body.user] and [req.body.id],
* removes the given user from the group.
*
* IF the user is the last member in the group,
* the group is completely deleted
*
* On client-side, this method should only be
* able to be properly used by client who is
* actually in the group (using [req.body.id]
* which is not readily accessible to client)
*/
//req.body.user req.body.id
app.post("/platform-removeGroup", async (req,res)=>{
async function run(){
try{
var msg = {
error:0
}
await client.connect();
const db_accs = client.db(hackDbName).collection("accounts");
const currContent_groups = await db_accs.findOne();
const groups = currContent_groups.groups;
var submit = [];
for (var i=0;i<groups.length;i++){
var dont=false
if (groups[i].id===req.body.id){
//found the correct group, now remove user
var newMembers = []
for (var j=0;j<groups[i].members.length;j++){
if (groups[i].members[j]!==req.body.user){
newMembers.push(groups[i].members[j]);
}
}
if (newMembers.length===0){
//well now there are no group members; don't push this group back into storage
dont=true;
}else{
groups[i].members=newMembers;
}
}else{
submit.push(groups[i]);
}
if (!dont) submit.push(groups[i]);
}
const filter = {title:"accounts"}
const updateDoc = {
$set: {
groups:submit
}
}
await db_accs.updateOne(filter,updateDoc);
res.send(JSON.stringify(msg))
}catch (error){
console.log(error);
}finally{
await client.close();
}
}
await run();
})
/* db.voting
groups: [
{
id:"group_id",
votes:[
{
user: "username",
category: 1|2|3 <integer>
},
{
user: "username",
category: 1|2|3 <integer>
}
] //easy use of votes.length to get total votes for a group
}
],
finals: [<same struc as above>]
*/
/*
req.body.votes: {
1: ["groupid","groupid"],
2: [],
3: []
}
*/
/**
* Given [req.body.user] and [req.body.votes: Set],
* submits votes for the user. Goes through all
* groups and updates user's newly intended votes.
* If the user, for instance, changed their votes,
* then a deletion would be made under one group and
* addition to under another group.
* This process is done to either the groups or finals
* array, based on the round of voting occurring
*/
//req.body.user req.body.votes req.body.round
app.post("/platform-vote", async (req,res)=>{
async function run(){
try{
await client.connect();
const db = client.db(hackDbName).collection("voting");
const currContent = await db.findOne();
var allGroups = []; if (req.body.round==="groups")allGroups=currContent.groups; else allGroups=currContent.finals;
//const userVotes = new Set(req.body.votes);
var submit = [];
var msg = {
error:0
}
for (var i=0;i<allGroups.length;i++){
//for all existing groups, try to find our user-casted vote
//if we found it, we want to have our user-casted vote in allGroups[].votes
//if you can't find it, then for this group re-create the group
//EXCLUSIVELY WITHOUT the user's vote in there, whether it is there or not
var found = false;
var category = -1;
for (key of Object.keys(req.body.votes)){
var currCategory = new Set(req.body.votes[key]);
if (currCategory.has(allGroups[i].id)){
found = true;
category = key;
break;
}
}
var updatedVotes = [];
for (var j=0;j<allGroups[i].votes.length;j++){
if (allGroups[i].votes[j].user!==req.body.user){
updatedVotes.push(allGroups[i].votes[j]);
}
}
if (found){
//if we want the user-submitted vote to be in allGroups[].votes,
//then lets create it and push it in!
updatedVotes.push({
"user":req.body.user,
"category":category
});
}
allGroups[i].votes=updatedVotes;
submit.push(allGroups[i]);
}
//possible that allGroups has no groups instantiated
//in that case, lets go thru all submitted votes and
//see which voted-for groups need a new creation in
//the database
for (key of Object.keys(req.body.votes)){
var currCategory = req.body.votes[key];
for (var i=0;i<currCategory.length;i++){
var found=false;
//go thru submitted to see if they exist in db
for (var j=0;j<allGroups.length;j++){
if (currCategory[i]===allGroups[j].id){
found=true;
break;
}
}
if (!found){
//create a whole new vote profile for this group
var newGroup = {};
newGroup.id=currCategory[i];
newGroup.votes=[{
"user":req.body.user,
"category":key
}];
submit.push(newGroup);
}
}
}
/*
for (key of Object.keys(req.body.votes)){
var currCategory = new Set(req.body.votes[key]);
//looping through categories, each containing distinct votes
//for each vote made by user,
//try to find it in database and replace
//-----
//if not found, create a new vote profile
//for that project in voting db
var foundCategories = []; for (c of currCategory) foundCategories.push(false);
for (var i=0;i<allGroups.length;i++){
if (currCategory.has(allGroups[i].id)){
//we found the right group
//IE, EXPLICITLY: this curr group under this groupID
//exists in the current category of votes we are
//looping through
var updatedVotes = [];
for (var j=0;j<allGroups[i].votes.length;j++){
if (allGroups[i].votes[j].user!==req.body.user){
updatedVotes.push(allGroups[i].votes[j]);
}
}
updatedVotes.push({
"user":req.body.user,
"category":key
})
allGroups[i].votes=updatedVotes;
currCategory.delete(allGroups[i].id);
}
submit.push(allGroups[i]);
}
for (group of currCategory){
//these guys are new, create new
var newGroup = {};
newGroup.id=group;
newGroup.votes=[{
"user":req.body.user,
"category":key
}];
submit.push(newGroup);
}
}
*/
/*
for (var i=0;i<votes.length;i++){
var updatedVotes = [];
for (var j=0;j<votes[i].votes.length;j++){
if (votes[i].votes[j]!==req.body.user){
updatedVotes.push(votes[i].votes[j]);
}
}
if (userVotes.has(votes[i])){
updatedVotes.push(req.body.user);
}
submit.push(votes[i]);
}
*/
const filter = {title:"voting"}
var updateDoc = {}
if (req.body.round==="groups"){
updateDoc={
$set: {
groups:submit
}
}
}else{
updateDoc={
$set: {
finals:submit
}
}
}
await db.updateOne(filter,updateDoc);
res.send(JSON.stringify(msg))
}catch (error){
console.log(error);
}finally{
await client.close();
}
}
await run();
})
/**
* Creates a new project in database
* with the given the project data.
*/
/* db.projects = [
{
projName:"poggers",
groupName:"epic team",
groupMembers:["Michael Zhang", "Bob Smith"],
id:12345,
projDesc:"epic description",
mediaLink:"bobabyte.org"
},
{},,,,,,,
]
*/
/*
data: (req.body.xxx)
projName
groupId
projDesc
mediaLink
*/
app.post("/platform-proj", async (req,res)=>{
async function run(){
try{
var msg = {
error:0
}
await client.connect();
const db = client.db(hackDbName).collection("projects");
const currContent = await db.findOne();
const projects = currContent.projects;
var submit = [];
var editing = false;
for (var i=0;i<projects.length;i++){
if (projects[i].id===req.body.id){
//edited submission
editing = true;
projects[i].projName=req.body.projName
projects[i].projDesc=req.body.projDesc
projects[i].mediaLink=req.body.mediaLink
}
submit.push(projects[i]);
}
if (!editing) {
var members = [];
var groupId = "";
const db_accs = client.db(hackDbName).collection("accounts");
const currContent_accs = await db_accs.findOne();
const groups = currContent_accs.groups;
const users = currContent_accs.usernames;
console.log(groups);
var found=false;
for (var i=0;i<groups.length;i++){
if (groups[i].id===req.body.id){
//found the group
found=true;
members=groups[i].members;
groupId=groups[i].id;
break;
}
}
if (!found){
msg.error=1
}else{
var proj = {
projName:req.body.projName,
groupName:req.body.groupName,
groupMembers:members,
id:groupId,
projDesc:req.body.projDesc,
mediaLink:req.body.mediaLink
}
submit.push(proj);
}
}
const filter = {title:"projects"}
const updateDoc = {
$set: {
projects:submit
}
}
await db.updateOne(filter,updateDoc);
res.send(JSON.stringify(msg));
}catch (error){
console.log(error);
}finally{
await client.close();
}
}
await run();
})
/**
* Makes an announcement in database title and body
* and broadcasts the announcement to everyone
*/
app.post("/platform-anno", async(req,res)=>{
async function run(){
try {
await client.connect();
const db_annos = client.db(hackDbName).collection("annos");
const currContent_annos = await db_annos.findOne();
var annos_content = currContent_annos.annos;
var submit = [];
for (var i=0;i<annos_content.length;i++){
submit.push(annos_content[i]);
}
var currAnno = {
title:req.body.title,
date:Date.now(),
body:req.body.body
}
submit.push(currAnno);
const filter = {title:"annos"}
const updateDoc = {
$set: {
annos:submit
}
}
await db_annos.updateOne(filter,updateDoc);
var msg = {
body:req.body.body
}
var message = {
title:req.body.title,
body:req.body.body
};
io.emit("platform-anno",message);
res.send(JSON.stringify(msg))
}catch (error){
console.log(error);
}finally{
await client.close();
}
}
await run();
})
/**
* Gets the list of announcements in database
*/
app.get("/platform-getAnnos",async (req,res)=>{
async function run(){
try{
await client.connect();
const db = client.db(hackDbName).collection("annos");
const currContent = await db.findOne();
var db_annos = currContent.annos;
var msg = {
annos:db_annos
}
res.send(JSON.stringify(msg));
}catch (error){
console.log(error);
}finally{
await client.close();
}
}
await run();
})
/**
* Gets the total list of groups
*/
app.get("/platform-getGroups",async (req,res)=>{
async function run(){
try{
await client.connect();
const db = client.db(hackDbName).collection("accounts");
const currContent = await db.findOne();
var db_groups = currContent.groups;
var msg = {
groups:db_groups
}
res.send(JSON.stringify(msg));
}catch (error){
console.log(error);
}finally{
await client.close();
}
}
await run();
})
/*
body.members = [
{
user:"username",
first:"bob",
last:"smith"
}
]
*/
/**
* Gets the members of a group given
* the id of a group
* Also includes the group name
*/
//req.body.id
app.post("/platform-getMembers",async (req,res)=>{
async function run(){
try{
await client.connect();
const db = client.db(hackDbName).collection("accounts");
const currContent = await db.findOne();
var db_group = currContent.groups;
var db_users = currContent.usernames;
var msg = {
error:0,
members:[]
}
var found = false;
var memberUsernames = [];
for (var i=0;i<db_group.length;i++){
if (db_group[i].id===req.body.id){
//found the group, now ret members
found = true;
memberUsernames = db_group[i].members;
msg.groupName = db_group[i].group;
}
}
//add names into members
console.log(memberUsernames);
for (var i=0;i<memberUsernames.length;i++){
for (var j=0;j<db_users.length;j++){
if (memberUsernames[i]===db_users[j].user){
msg.members.push(db_users[j]);
}
}
}
console.log(msg.members);
if (!found){
msg.error=1;
}
res.send(JSON.stringify(msg));
}catch (error){
console.log(error);
}finally{
await client.close();
}
}
await run();
})
/**
* Gets all the projects in database
*/
app.get("/platform-getProjects",async (req,res)=>{
async function run(){
try{
await client.connect();
const db = client.db(hackDbName).collection("projects");
const currContent = await db.findOne();
var db_proj = currContent.projects;
var msg = {
projects:db_proj
}
res.send(JSON.stringify(msg));
}catch (error){
console.log(error);
}finally{
await client.close();
}
}
await run();
})
/**
* Return all votes from either groups or finals
* phase of voting from [req.body.round]
*/
app.post("/platform-getVoting",async (req,res)=>{
async function run(){
try{
await client.connect();
const db = client.db(hackDbName).collection("voting");
const currContent = await db.findOne();
var voting = []; if (req.body.round==="groups")voting=currContent.groups; else voting=currContent.finals;
var msg = {
voting:voting
}
res.send(JSON.stringify(msg));
}catch (error){
console.log(error);
}finally{
await client.close();
}
}
await run();
})
/**
* Given group id [req.body.id], return project
* associated with that group.
*/
app.post("/platform-getMyProject",async (req,res)=>{
async function run(){
try{
await client.connect();
const db = client.db(hackDbName).collection("projects");
const currContent = await db.findOne();
var msg = {
error:-1
}
var projects = currContent.projects;
for (var i=0;i<projects.length;i++){
if (projects[i].id===req.body.id){
//found
msg.error=0;
msg.project=projects[i];
}
}
if(msg.error===-1)msg.error=1;
res.send(JSON.stringify(msg));
}catch (error){
console.log(error);
}finally{
await client.close();
}
}
await run();
})