-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
140 lines (121 loc) · 3.14 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
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const cors = require('cors');
const http = require('http');
const { Server } = require('socket.io');
const Victim = require('./models/Victim');
const HelpRequest = require('./models/HelpRequest');
const app = express();
require('dotenv/config');
//* Import Routes --------------------------------------------------------------------
const adminRoute = require('./routes/admins');
const Count = require('./models/Count');
//* ----------------------------------------------------------------------------------
app.use(cors());
app.use(bodyParser.json());
//? Socket ============================================================================
const server = http.createServer(app);
const io = new Server(server, {
cors: {
origin: '*',
},
});
io.on('connection', (socket) => {
// console.log(`User connected: ${socket.id}`);
socket.on('send_request', async (data) => {
let response;
duplicate = await HelpRequest.findOne({ uniqueId: data.uniqueId });
if (duplicate) {
response = {
success: true,
message: "Please don't send multiple requests",
};
} else {
const request = new HelpRequest({
uniqueId: data.uniqueId,
latitude: data.latitude,
longitude: data.longitude,
});
try {
const savedRequest = await request.save();
response = {
success: true,
message: 'Request Send Sucessfully',
};
} catch (err) {
response = {
success: false,
message: `Request failed`,
};
}
}
socket.emit('callback', response);
let requests = [];
requests = await HelpRequest.find();
socket.broadcast.emit('recieve_request', requests);
});
});
server.listen(process.env.PORT || 5000, () => {
var host = server.address().address;
var port = server.address().port;
console.log(`App listening at ${port}`);
});
//* Routes ===============================================================================
app.use('/admin', adminRoute);
//Find victim
app.post('/searchByAadhar', async (req, res) => {
try {
const victim = await Victim.findOne({ aadhar: req.body.aadhar });
if (!victim) {
res.status(401).send({
message: 'Not Found',
error: err,
});
}
res.status(200).send(victim);
} catch (err) {
res.status(401).send({
message: 'Not Found',
error: err,
});
}
});
app.post('/searchByPhone', async (req, res) => {
try {
const victim = await Victim.findOne({ phone: req.body.phone });
if (!victim) {
res.status(401).send({
message: 'Not Found',
error: err,
});
}
res.status(200).send(victim);
} catch (err) {
res.status(401).send({
message: 'Not Found',
error: err,
});
}
});
app.get('/count', async (req, res) => {
try {
const count = await Count.find();
const victim = await Victim.find();
const saved = victim.length;
const savedData = {
name: 'saved',
count: saved,
};
const result = [count[0], savedData, count[1]];
// count.push(savedData);
res.json(result);
} catch (err) {
res.json({
message: err,
});
}
});
mongoose.connect(process.env.MONGO_URI, () =>
console.log('~~~CONNECTED TO DB~~~')
);