-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
294 lines (264 loc) · 8.5 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
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
const { functionsIn } = require("lodash");
var express = require("express"),
app = express(),
bodyParser = require("body-parser"),
mongoose = require("mongoose"),
//pug = require("pug"),
nodemailer = require('nodemailer'),
Esri = require("./models/esri.js");
//MIDDLEWARE for Authentication
function isAuthenticated(req,res,next){
//check whether logged in
// if they are, attach the user to request object and then call next
// if not send redirect to login page
// with a message saying log in
}
//MONGO CONFIGRATION
var uri = process.env.DATABASEURI || "mongodb+srv://sudhanshumohan:hesoyam@cluster0-3z3hj.mongodb.net/hospital_data?retryWrites=true&w=majority"
mongoose.connect(uri,{
useNewUrlParser:true,
useCreateIndex:true,
useUnifiedTopology:true,
useFindAndModify: false
}).then(()=>{
console.log("Connected to Database");
}).catch(err =>{
console.log("ERROR:",err.message);
});
//==========================
//SOME OTHER PACKAGES CONFIG
//==========================
app.set("view engine","ejs");
//setting view engine to ejs
app.use(express.static(__dirname + '/public'));
//serving custom resources
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
//using bodyparser so recieve req object
//Serving HomePage
app.get("/",function(req,res){
res.render("about.ejs");
});
app.get("/map",function(req,res){
Esri.find({},function(err,foundObj){
if(err){
console.log(err);
} else {
res.render("index.ejs",{foundArr:foundObj});
}
})
});
app.get("/AboutProject",function(req,res){
res.render("AboutProject.ejs");
});
app.get("/login",function(req,res){
res.render("login.ejs");
});
app.get('/dashboard',isAuthenticated,function(req,res){
res.render('dashboard.ejs');
});
// ======== plasma bank ========= //
app.get("/index_plasma",function(req,res){
res.render("index_plasma.ejs");
});
app.get("/amenities",function(req,res){
res.render("amenities.ejs");
});
app.get("/form_donor",function(req,res){
res.render("form_donor.ejs");
});
app.get("/form_patient",function(req,res){
res.render("form_patient.ejs");
});
app.get("/common_pool",function(req,res){
res.render("common_pool.ejs");
});
app.get("/plasma_bank",function(req,res){
res.render("plasma_bank.ejs");
});
//==========AJAX TESTING ROUTES =========
app.get("/test",function(req,res){
res.render("test.ejs");
});
//========API Routes============//
//========THIS API GETS ALL THE DATA===========//
app.get("/api/getall",function(req,res){
Esri.find({},function(err,data){
if(err){
res.send(err);
} else {
res.send(data);
}
}).limit(10);
});
//========THIS SEARCHES FOR A SPECIFIC MONGO ID=========//
app.get("/api/",(req,res) =>{
var _id = req.query._id;
Esri.findById(_id,function(err,data){
if(err){
res.send(err);
} else {
res.send(data);
}
})
});
//===========THIS API Route Searches and Updates the document ============//
app.put("/api/",(req,res)=>{
Esri.findByIdAndUpdate(req.body._id,req.body,{upsert:false,new:true},function(err,updatedObject){
if(err){
console.log(err);
} else {
res.send(updatedObject);
}
})
});
//=================THIS API Creates New Documents================//
app.post("/api/",(req,res)=>{
var newEsri = new Esri({
X:req.body.X,
Y:req.body.Y,
FID:req.body.FID,
HOSPITAL_NAME:req.body.HOSPITAL_NAME,
HOSPITAL_TYPE:req.body.HOSPITAL_TYPE,
HQ_ADDRESS:req.body.HQ_ADDRESS,
HQ_CITY:req.body.HQ_CITY,
HQ_STATE:req.body.HQ_STATE,
HQ_ZIP_CODE:req.body.HQ_ZIP_CODE,
COUNTY_NAME:req.body.COUNTY_NAME,
STATE_NAME:req.body.STATE_NAME,
FIPS:req.body.FIPS,
NUM_LICENSED_BEDS:req.body.NUM_LICENSED_BEDS,
NUM_STAFFED_BEDS:req.body.NUM_STAFFED_BEDS,
NUM_ICU_BEDS:req.body.NUM_ICU_BEDS,
ADULT_ICU_BEDS:req.body.ADULT_ICU_BEDS,
PEDI_ICU_BEDS:req.body.PEDI_ICU_BEDS,
BED_UTILIZATION:req.body.BED_UTILIZATION,
Potential_Increase_In_Bed_Capac:req.body.Potential_Increase_In_Bed_Capac,
AVG_VENTILATOR_USAGE:req.body.AVG_VENTILATOR_USAGE
});
newEsri.save(function(err,obj){
if(err){
console.log(err);
} else {
res.send(obj);
}
});
});
//==================DELELTE BY ID=============//
app.delete("/api/",(req,res)=>{
Esri.findByIdAndRemove(req.body._id,function(err,deletedObj){
if(err){
console.log(err);
} else {
res.send(deletedObj);
}
})
});
//=============Fuzzy Search based on raw data ==========//
app.get("/api/fuzzy",(req,res) =>{
var COUNTY_NAME = req.query.COUNTY_NAME;
var STATE_NAME = req.query.STATE_NAME;
var HOSPITAL_NAME = req.query.HOSPITAL_NAME;
var HQ_CITY = req.query.HQ_CITY;
Esri.find({STATE_NAME:{$regex:STATE_NAME,$options:"$i"},COUNTY_NAME:{$regex:COUNTY_NAME,$options:"$i"},HOSPITAL_NAME:{$regex:HOSPITAL_NAME,$options:"$i"},HQ_CITY:{$regex:HQ_CITY,$options:"$i"}},function(err,data){
if(err){
res.send(err);
} else {
res.send(data);
}
})
});
//===============get request Fallback===========//
app.get("*", (req,res) => {
res.render("404.ejs");
});
// using Twilio SendGrid's v3 Node.js Library
// https://github.com/sendgrid/sendgrid-nodejs
app.post("/email",function(req,res){
var email = req.body.email;
var name = req.body.name;
var number = req.body.number;
var age = req.body.age;
var gender = req.body.gender;
var country = req.body.country;
var state = req.body.state;
var city = req.body.city;
var bg = req.body.bg;
/*console.log(email);
sgMail.setApiKey("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");
const msg = {
to: email,
from: 'pj.flux2001@gmail.com',
subject: 'Team BITSians : Success',
text: 'Dear '+name+', Your form has been successfully pushed into our database. It will expire in next 72 hours, hence take the required steps to comply with the same. The information that was pushed into this form : Age : <br>Gender : <br>Country : <br>State : <br>City : <br>Blood Group :'+ bg +' We will contact you @'+number+' in case of any emergency. Best Regards, Team BITSians ',
html: 'Dear '+name+',<br><br>Your form has been successfully pushed into our database. It will expire in next <strong>72 hours</strong>, hence take the required steps to comply with the same.<br><br>The information that was pushed into this form :<br><p>Age : '+age+'<br>Gender : '+gender+'<br>Country : '+country+'<br>State : '+state+'<br>City : '+city+'<br>Blood Group : '+ bg +'<br></p>We will contact you @'+number+' in case of any emergency.<br><br> Best Regards,<br> Team BITSians',
};
sgMail.send(msg);*/
let transport = nodemailer.createTransport({
host: 'smtp.googlemail.com',
port: 465,
secure: true,
auth: {
user: 'carequest69@gmail.com',
pass: 'CareQuest@69'
}
});
const message1 = {
from: 'CareQuest <carequest69@gmail.com>', // Sender address
to: email, // List of recipients
subject: 'CareQuest : Form Submission Success', // Subject line
html: 'Dear '+name+',<br><br>Your form has been successfully pushed into our database. It will expire in next <strong>72 hours</strong>, hence take the required steps to comply with the same.<br><br>The information that was pushed into this form :<br><p>Age : '+age+'<br>Gender : '+gender+'<br>Country : '+country+'<br>State : '+state+'<br>City : '+city+'<br>Blood Group : '+ bg +'<br></p>We will contact you @'+number+' in case of any emergency.<br><br> Best Regards,<br> Team CareQuest'// Plain text body
};
transport.sendMail(message1, function(err, info) {
if (err) {
console.log(err)
} else {
console.log(info);
}
});
res.render("common_pool");
})
//========================//
//RAPID : SMS
var axios = require('axios');
var config = {
method: 'get',
url: 'https://rapidapi.rmlconnect.net:9443/bulksms/bulksms?username=617bf2e0245383001100f89b&password=617bf2e0245383001100f89b&type=0&dlr=1&destination=+917038170634&source=RMLPRD&message=Logged_In_Success',
headers: { }
};
axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
});
//RAPID : Whatsapp
// var axios = require('axios');
var data = JSON.stringify({
"phone": "+917038170634",
"text": "Logged_In_Success",
"extra": "WA"
});
var config = {
method: 'post',
url: 'https://rapidapi.rmlconnect.net/wbm/v1/message',
headers: {
'Content-Type': 'application/json',
'Authorization': '617bf2e0245383001100f89b'
},
data : data
};
axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
});
//========================//
//LISTENER PROCESS
var port = process.env.PORT || 31000
app.listen(port,process.env.IP,function(){
console.log("Server started at port:"+port);
});