-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
474 lines (422 loc) · 13.9 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
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
/*------------------------------------
ExpressConfig
--------------------------------------*/
const express = require("express");
const app = express();
/*------------------------------------
dotenv
--------------------------------------*/
require("dotenv").config();
/*------------------------------------
Firebase Config
--------------------------------------*/
//const key = require("./key.json");
const { initializeApp, cert } = require("firebase-admin/app");
const { getFirestore } = require("firebase-admin/firestore");
initializeApp({
credential: cert(process.env),
});
const db = getFirestore();
/*--------------------------------------------
External Functions
----------------------------------------------*/
const { addUser, authenticate, signout } = require("./src/user_service");
const errorHandler = require("./src/errorhandler");
const { requireAuth, checkUser } = require("./src/authMiddleware");
/*--------------------------------------
MiddleWares
----------------------------------------*/
const bodyParser = require("body-parser");
const cors = require("cors");
const cookieParser = require("cookie-parser");
const PORT = process.env.PORT || 3000;
const axios = require("axios");
//-----------paths---------------------------
const path = require("path");
const staticPath = path.join(__dirname, "/public");
const templatePath = path.join(__dirname, "/templates");
//--------use & sets-------------------------------
app.set("view engine", "hbs");
app.set("views", templatePath);
app.use(express.static(staticPath));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(cors());
//--------------------jwt-------------------------------
const jwt = require("jsonwebtoken");
const maxAge = 3 * 24 * 60 * 60;
const createToken = (id) => {
return jwt.sign({ id }, process.env.JWT_TOKEN, {
expiresIn: maxAge,
});
};
//--------------------nodemailer-------------------------------
const nodemailer = require("nodemailer");
const transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
port: 587 || 465,
secure: false,
auth: {
user: process.env.email,
pass: process.env.password,
},
});
//--------------------twilio-------------------------------
/*const twilioAccountSid = process.env.twilioAccountSid;
const twilioAuthToken = process.env.twilioAuthToken;
const client = require("twilio")(twilioAccountSid, twilioAuthToken);*/
/*-------------------------------------------
Endpoints
--------------------------------------------*/
//checkuser
//app.get("*", checkUser);
//*-----------------------Home---------------------------------
app.get("/", async (req, res) => {
res.sendFile(staticPath + "/index.html");
});
//----------------------Authentication----------------------------
//signup (get)
app.get("/signup", async (req, res) => {
res.sendFile(staticPath + "/page/signup.html");
});
//signup (post)- create account
app.post("/signup", async (req, res) => {
const { email, password } = req.body;
try {
const userResponse = await addUser(email, password);
const token = createToken(email);
res.cookie("jwt", token, { httpOnly: true, maxAge: maxAge * 1000 });
res.status(201).json(userResponse.user);
} catch (error) {
res.status(400).json({ error: errorHandler.handleErrors(error) });
}
});
//signin (get)
app.get("/signin", async (req, res) => {
res.sendFile(staticPath + "/page/signin.html");
});
//signin (post) - to account
app.post("/signin", async (req, res) => {
const { email, password } = req.body;
try {
const userResponse = await authenticate(email, password);
const token = createToken(userResponse.user.email);
res.cookie("jwt", token, { httpOnly: true, maxAge: maxAge * 1000 });
res.status(200).json(userResponse.user);
} catch (error) {
res.status(400).json({ error: errorHandler.handleErrors(error) });
}
});
//signout (get)
app.get("/signout", requireAuth, async (req, res) => {
await signout
.then(() => {
res.cookie("jwt", "", { maxAge: 1 });
res.status(200).send("OK");
})
.catch((error) => {
res.status(400).json({ error: errorHandler.handleErrors(error) });
});
});
//----------------------Profile section----------------------------
//render profile section
app.get("/profile", requireAuth, async (req, res) => {
try {
const ID = jwt.verify(req.cookies.jwt, process.env.JWT_TOKEN).id;
const dietDocRef = db.collection("dietPlan").doc(ID);
const dietResponse = await dietDocRef.get();
const dietData = dietResponse.data();
const workoutDocRef = db.collection("workoutPlan").doc(ID);
const workoutResponse = await workoutDocRef.get();
const workoutData = workoutResponse.data();
res.render(templatePath + "/profile.hbs", {
dietData: dietData,
workoutData: workoutData,
});
} catch (error) {
res.status(400).json({ error: errorHandler.handleErrors(error) });
}
});
//delete diet plan
app.post("/deleteDietPlan", requireAuth, async (req, res) => {
try {
const ID = jwt.verify(req.cookies.jwt, process.env.JWT_TOKEN).id;
const response = await db.collection("dietPlan").doc(ID).delete();
res.status(200).json(response);
} catch (error) {
res.status(400).json({ error: errorHandler.handleErrors(error) });
}
});
//delete workout plan
app.post("/deleteWorkoutPlan", requireAuth, async (req, res) => {
try {
const ID = jwt.verify(req.cookies.jwt, process.env.JWT_TOKEN).id;
const response = await db.collection("workoutPlan").doc(ID).delete();
res.status(200).json(response);
} catch (error) {
res.status(400).json({ error: errorHandler.handleErrors(error) });
}
});
//add phone number
app.post("/addPhoneNumber", requireAuth, async (req, res) => {
try {
const ID = jwt.verify(req.cookies.jwt, process.env.JWT_TOKEN).id;
const phoneNumnerJson = {
number: req.body.number,
};
const response = await db
.collection("phoneNumber")
.doc(ID)
.set(phoneNumnerJson);
res.status(200).json(response);
} catch (error) {
res.status(400).json({ error: errorHandler.handleErrors(error) });
}
});
//----------------------Workout recommendation----------------------------
//workout
app.get("/workout", requireAuth, async (req, res) => {
res.sendFile(staticPath + "/page/workout.html");
});
//get muscles
app.get("/getMuscles", requireAuth, async (req, res) => {
try {
const options = {
method: "GET",
url: process.env.x_rapidapi_url + "muscles/",
headers: {
"x-rapidapi-key": process.env.x_rapidapi_key,
"x-rapidapi-host": process.env.x_rapidapi_host,
},
};
const response = await axios.request(options);
const data = response.data;
res.json(data);
} catch (error) {
console.error(error);
res.status(500).send("Server error");
}
});
//get exercise by name
app.get("/exerciseByName/:id", requireAuth, async (req, res) => {
try {
const options = {
method: "GET",
url: key["x-rapidapi-url"],
params: { name: req.params.id },
headers: {
"x-rapidapi-key": process.env.x_rapidapi_key,
"x-rapidapi-host": process.env.x_rapidapi_host,
},
};
const response = await axios.request(options);
const data = response.data;
res.json(data[0]);
} catch (error) {
console.error(error);
res.status(500).send("Server error");
}
});
//get exercise by primary muscles
app.get("/exerciseByPrimaryMuscle/:id", requireAuth, async (req, res) => {
try {
const options = {
method: "GET",
url: process.env.x_rapidapi_url,
params: { primaryMuscle: req.params.id },
headers: {
"x-rapidapi-key": process.env.x_rapidapi_key,
"x-rapidapi-host": process.env.x_rapidapi_host,
},
};
const response = await axios.request(options);
const data = response.data;
const ID = jwt.verify(req.cookies.jwt, process.env.JWT_TOKEN).id;
if (data.length != 0)
await db.collection("workoutPlan").doc(ID).set({ data });
res.json(data);
} catch (error) {
console.error(error);
res.status(500).send("Server error");
}
});
//get exercise by Secondry muscles
app.get("/exerciseBySecondryMuscle/:id", requireAuth, async (req, res) => {
try {
const options = {
method: "GET",
url: process.env.x_rapidapi_url,
params: { secondaryMuscle: req.params.id },
headers: {
"x-rapidapi-key": process.env.x_rapidapi_key,
"x-rapidapi-host": process.env.x_rapidapi_host,
},
};
const response = await axios.request(options);
const data = response.data;
//save data to database
const ID = jwt.verify(req.cookies.jwt, process.env.JWT_TOKEN).id;
if (data.length != 0)
await db.collection("workoutPlan").doc(ID).set({ data });
res.json(data);
} catch (error) {
console.error(error);
res.status(500).send("Server error");
}
});
//----------------------Diet recommendation----------------------------
// Function to make API requests to the Spoonacular API
async function makeRequest(endpoint, params) {
const response = await axios.get(`https://api.spoonacular.com${endpoint}`, {
params: {
apiKey: process.env.diet_api_key,
...params,
},
});
return response.data;
}
//diet
app.get("/diet", requireAuth, async (req, res) => {
res.sendFile(staticPath + "/page/diet.html");
});
//generate meal plan
app.post("/generate-meal-plan", requireAuth, async (req, res) => {
try {
const { diet, calories, timeFrame, ingredients } = req.body;
const exclude = ingredients
? ingredients
.split(",")
.map((i) => i.trim())
.join(",")
: undefined;
const mealPlan = await makeRequest("/mealplanner/generate", {
diet,
targetCalories: calories,
timeFrame,
exclude,
});
//save data to database
const ID = jwt.verify(req.cookies.jwt, process.env.JWT_TOKEN).id;
if (mealPlan.length != 0)
await db.collection("dietPlan").doc(ID).set(mealPlan);
res.json(mealPlan);
} catch (error) {
console.error(error);
res.status(500).send("An error occurred while generating the meal plan.");
}
});
/*---------------------------------
IOT
------------------------------------*/
let sensorData = null;
//data fetching from iot sensor
app.post("/data", async (req, res) => {
const data = {
temperature: req.body.temperature,
humidity: req.body.humidity,
gas: req.body.gas,
};
// Store the data in the global variable
sensorData = data;
console.log(
`Temperature: ${req.body.temperature} °C | Humidity: ${req.body.humidity} % | Gas: ${req.body.gas} ppm`
);
});
//threshold value
const thresholdTemperature = 40;
const thresholdHumidity = 50;
const thresholdGas = 22.5;
//sending iot device data to frontend
app.get("/iotdata", requireAuth, (req, res) => {
// Check if sensorData has been populated
if (!sensorData) {
res.send({ error: "Sensor data not available" });
return;
}
// Compare current values and gas with threshold values
if (
sensorData.temperature > thresholdTemperature ||
sensorData.humidity > thresholdHumidity ||
sensorData.gas > thresholdGas
) {
const rEmail = jwt.verify(req.cookies.jwt, process.env.JWT_TOKEN).id;
sendAlert(
sensorData.temperature,
sensorData.humidity,
sensorData.gas,
rEmail
);
}
// Return the sensorData in JSON format
res.json(sensorData);
});
//let rPhoneNumber = "";
//getting food detect page
app.get("/foodDetect", requireAuth, async (req, res) => {
/*const ID = jwt.verify(req.cookies.jwt, process.env.JWT_TOKEN).id;
await db
.collection("phoneNumber")
.doc(ID)
.get()
.then((doc) => {
if (doc.exists) {
const data = doc.data();
rPhoneNumber = data.number;
} else {
console.log("No such document!");
}
})
.catch((error) => {
console.log("Error getting document:", error);
});*/
res.sendFile(staticPath + "/page/foodDetect.html");
});
//global values
//let isEmailSent = false;
let lastEmailSent = 0;
function sendAlert(temperature, humidity, gas, rEmail) {
// Check if an email has already been sent within the past hour
const now = Date.now();
if (now - lastEmailSent > 3600000) {
// Update lastEmailSent time
lastEmailSent = now;
// Update email message with current temperature values
const mailOptions = {
from: "Alert@HealthIQ.com",
to: rEmail,
subject: "Food Spoilage Detected",
text: `Temperature has exceeded the threshold value of ${thresholdTemperature}°C. The current temperature is ${temperature}°C.\nHumidity has exceeded the threshold value of ${thresholdHumidity}%. The current Humidity is ${humidity}%.\nMethane Gas has exceeded the threshold value of ${thresholdGas}ppm. The current Methane Gas is ${gas}ppm.`,
};
//send sms
/*if (rPhoneNumber != "") {
rPhoneNumber = "+917240974211";
sendAlertSMS(temperature, humidity, gas, rPhoneNumber);
}*/
// Send email
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
} else {
console.log("Email sent: " + info.response);
}
});
}
}
// Send SMS alert
/*function sendAlertSMS(temperature, humidity, gas, rPhoneNumber) {
client.messages
.create({
body: `Temperature has exceeded the threshold value of ${thresholdTemperature}°C. The current temperature is ${temperature}°C.\nHumidity has exceeded the threshold value of ${thresholdHumidity}%. The current Humidity is ${humidity}%.\nMethane Gas has exceeded the threshold value of ${thresholdGas}ppm. The current Methane Gas is ${gas}ppm.`,
from: key.twilioPhoneNumber,
to: rPhoneNumber,
})
.then((message) => console.log(message.sid))
.catch((error) => console.log(error));
}*/
/*---------------------------------
app listen
------------------------------------*/
app.listen(PORT, () => {
console.log(`Server is running on PORT http://localhost:${PORT}`);
});