-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
351 lines (308 loc) · 9.55 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
var express = require("express");
var path = require("path");
var bodyParser = require("body-parser");
var mongodb = require("mongodb");
var request = require("request");
var ObjectID = mongodb.ObjectID;
//var basicAuth = require('basic-auth');
//var authCreds = require("./auth-creds.js");
var nodemailer = require('nodemailer');
var router = express.Router();
var app = express();
app.use(express.static(__dirname + "/public", {
extensions: ['html','htm']
}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }))
// var server = app.listen(process.env.PORT || 80, function () {
// var port = server.address().port;
// console.log("App now running on port", port);
// });
// app.get("/circles", function(req,res) {
// res.sendFile(__dirname + "/public/circles.html");
// });
//
// app.get("/permissions", function(req,res) {
// res.sendFile(__dirname + "/public/permissions.html");
// });
//
// app.get("/disagree", function(req,res) {
// res.sendFile(__dirname + "/public/permissions.html");
// });
// var auth = function (req, res, next) {
// function unauthorized(res) {
// res.set('WWW-Authenticate', 'Basic realm=Authorization Required');
// return res.send(401);
// };
//
// var user = basicAuth(req);
//
// if (!user || !user.name || !user.pass) {
// return unauthorized(res);
// };
//
// if (user.name === authCreds.user && user.pass === authCreds.pass) {
// return next();
// } else {
// return unauthorized(res);
// };
// };
var PATIENTS_COLLECTION = "patients";
// app.use(cookieParser());
// app.use(session({secret: authCreds.secret}))
// Create a database variable outside of the database connection callback to reuse the connection pool in your app.
var database;
// Connect to the database before starting the application server.
var MongoClient = require('mongodb').MongoClient
, format = require('util').format;
MongoClient.connect('mongodb://heroku_30ntgntk:[email protected]:53980/heroku_30ntgntk', function (err, db) {
if (err) {
throw err;
} else {
console.log("successfully connected to the database");
}
database = db
var server = app.listen(process.env.PORT || 80, function () {
var port = server.address().port;
console.log("App now running on port", port);
db.collection("test").find({"test1":"testa"}).toArray(function(err,docs){
console.log(docs);
});
});
});
// API Routes Below
// Generic error handler used by all endpoints.
function handleError(res, reason, message, code) {
console.log("ERROR: " + reason);
res.status(code || 500).json({"error": message});
}
//
//
// /* "/drugs"
// * GET: finds all drugs
// *
// */
// app.get("/api/v1/drugs", function(req, res){
// database.collection(DRUGS_COLLECTION).find({}).toArray(function(err, docs) {
// if (err) {
// handleError(res, err.message, "Failed to get drugs.");
// } else {
// res.status(200).json(docs);
// }
// });
// });
//
// /* "/drugs"
// * POST: save new drug
// */
// app.post("/drugs", function(req, res) {
// var newDrug = req.body;
// console.log(req.body);
// newDrug.createDate = new Date();
// newDrug.updateDate = new Date();
//
// database.collection(DRUGS_COLLECTION).insertOne(newDrug, function(err, doc) {
// if (err) {
// handleError(res, err.message, "Failed to create new drug.");
// } else {
// res.status(201).json(doc.ops[0]);
// }
// });
// });
//
// /* "/drugs"
// * PUT: update existing drug
// */
// app.put("/drugs/:id", function(req, res) {
// var newDrug = req.body;
// newDrug.updateDate = new Date();
//
// // if (!(req.body.firstName || req.body.lastName)) {
// // handleError(res, "Invalid user input", "Must provide a first or last name.", 400);
// // }
//
// database.collection(DRUGS_COLLECTION).insertOne(newDrug, function(err, doc) {
// if (err) {
// handleError(res, err.message, "Failed to create new drug.");
// } else {
// res.status(201).json(doc.ops[0]);
// }
// });
// });
//
// /* "/drugs/:id"
// * GET: find drug by id
// *
// *
// */
// app.get("/api/v1/drugs/:id", function(req, res) {
// db.collection(DRUGS_COLLECTION).findOne({ "name" : req.params.id }, function(err, doc) {
// if (err) {
// handleError(res, err.message, "Failed to get drug");
// } else {
// res.status(200).json(doc);
// }
// });
// });
var patients = [{
"phn": "9000123456",
"name": "Edna Gartner",
"phoneNumber": "(778) 876-5432",
"address": "123 Evergreen Terrace, Vancouver, BC",
"dob": "1948-06-01",
"sex": "Female"
}];
var circleMembers = {
"inner": [{
"name": "Susie Skinner",
"phone": "(604) 604-0406",
"email": "[email protected]",
"relationship": "Daughter"
}],
"outer": [{
"name": "George Simpson",
"phone": "(604) 222-2222",
"email": "[email protected]",
"relationship": "Neighbour"
}],
"extended": [{
"name": "Barry Chalmers",
"phone": "(778) 778-7788",
"email": "[email protected]",
"relationship": "Friend"
}]
};
var enrolledPatients = [];
app.post("/enroll", function(req,res) {
enrolledPatients.push(req.body);
});
app.get("/patients", function(req,res) {
var found = false;
patients.forEach(patient => {
if (!found && patient.phn == req.query.phn){
res.status(200).json(patient);
found = true;
}
});
if (!found){
res.status(400).send("Patient not found with this PHN");
}
});
/* "/patients"
* POST: save new patient
*/
app.post("/patients", function(req, res) {
var newPatient = req.body;
console.log(req.body);
newPatient.createDate = new Date();
newPatient.updateDate = new Date();
database.collection(PATIENTS_COLLECTION).insertOne(newPatient, function(err, doc) {
if (err) {
handleError(res, err.message, "Failed to create new patient.");
} else {
res.status(201).json(doc.ops[0]);
}
});
});
app.post("/circlemembers/inner", function(req,res) {
circleMembers["inner"].push(req.body);
console.log("POST /circlemembers/inner");
console.log(req.body);
res.status(200).send();
});
app.post("/circlemembers/outer", function(req,res) {
circleMembers["outer"].push(req.body);
console.log("POST /circlemembers/outer")
console.log(req.body);
res.status(200).send();
});
app.post("/circlemembers/extended", function(req,res) {
circleMembers["extended"].push(req.body);
console.log("POST /circlemembers/extended")
console.log(req.body);
res.status(200).send();
});
app.get("/circlemembers", function(req,res) {
res.status(200).json(circleMembers);
});
app.post("/update", function(req,res) {
patients.push(req.body);
});
app.post("/permissions", function(req,res) {
console.log(req.body);
res.status(200).send();
});
app.post("/patients/:phn/permissions", function(req,res) {
database.collection(PATIENTS_COLLECTION).update({phn:req.params.phn}, {$set: {permissions:req.body}}, function(err, doc){
if (err) {
handleError(res, err.message, "Failed to update permissions.");
}
});
console.log(req.body);
res.status(200).send();
});
//
// app.post("/email", function(req, res) {
// // Contact Form send email using Zoho SMTP Server and NodeMailer
// var transporter = nodemailer.createTransport({
// service: 'Zoho',
// auth: {
// user: authCreds.smtp.user, // Your email id
// pass: authCreds.smtp.pass // Your password
// }
// });
//
// var mailOptions = {
// from: '[email protected]',
// //replyTo: '[email protected]',
// to: req.body.email, // list of receivers
// subject: 'Care Circles Info', // Subject line
// text: "Test Email"
// };
//
// transporter.sendMail(mailOptions, function(error, info){
// if(error){
// // console.log(error);
// res.status(500).send("There was a problem sending the message. Please send email to [email protected]");
// }else{
// // console.log('Message sent: ' + info.response);
// res.status(200).send("Message sent successfully, we will reply as soon as possible!");
// };
// });
// });
app.post("/sms", function(req,res){
console.log(req.body.phone);
console.log(req.body.smspassword);
console.log(req.body.message)
var smsPassword = process.env.SMS_PASSWORD; // Our password to secure SMS functionality
if (req.body.smspassword != smsPassword){
res.status(401).send("SMS Password is incorrect");
return;
}
var accountSid = process.env.TWILIO_SID; // Your Account SID from www.twilio.com/console
var authToken = process.env.TWILIO_TOKEN; // Your Auth Token from www.twilio.com/console
var twilio = require('twilio');
var client = new twilio(accountSid, authToken);
client.messages.create({
body: req.body.message,
to: req.body.phone, // Text this number
from: process.env.TWILIO_PHONE // From a valid Twilio number
})
.then((message) => console.log(message.sid));
res.status(200).send();
});
app.post("/contactform", function(req,res) {
var messageText = '*Name:* ' + req.body.name + '\n' + '*Email:* ' + req.body.email + '\n' + '*Message:* ' + req.body.message
request.post(process.env.SLACK_CONTACT_FORM_URL,
{ json: { mrkdwn: true, text: messageText } },
function (error, response, body) {
if (!error && response.statusCode == 200) {
//console.log(body)
} else if (error) {
console.log(error)
} else if (!error){
console.log(body)
}
});
res.status(200).send();
});