Skip to content

Commit

Permalink
Merge pull request #28 from mathanraj0601/fix/try-catch
Browse files Browse the repository at this point in the history
Solve fixes : #15 : Fixed async operation
  • Loading branch information
Aryainguz committed Jan 2, 2024
2 parents cda454d + 32ec641 commit 3ff6929
Showing 1 changed file with 102 additions and 71 deletions.
173 changes: 102 additions & 71 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,85 +1,115 @@
const express = require("express")
const bodyParser = require("body-parser")
const axios = require("axios")
const sgMail = require("@sendgrid/mail")
const inputValidator = require("./middlewares/inputvalidator")
let flash = require('connect-flash');
let session = require('express-session');
const express = require("express");
const bodyParser = require("body-parser");
const axios = require("axios");
const sgMail = require("@sendgrid/mail");
const inputValidator = require("./middlewares/inputvalidator");
let flash = require("connect-flash");
let session = require("express-session");

require("dotenv").config()
require("dotenv").config();

const API_KEY = process.env.SENDGRID_API_KEY
sgMail.setApiKey(API_KEY)
const API_KEY = process.env.SENDGRID_API_KEY;
sgMail.setApiKey(API_KEY);

const PORT = process.env.PORT || 3000;
const app = express();
const receiver_email = [];
const verdict = [];

const PORT = 3000
const app = express()
const reciever_email = []

const verdict = []

app.use(express.static(__dirname + "/Public")); //use this to use css files insie templates
app.use(express.static(__dirname + "/Public")); //use this to use css files insie templates
app.use(bodyParser.urlencoded({ extended: true }));

app.use(session({
secret: process.env.SECRET,
cookie: { maxAge : 60000 },
resave: false,
saveUninitialized: false

}));
app.use(
session({
secret: process.env.SECRET,
cookie: { maxAge: 60000 },
resave: false,
saveUninitialized: false,
})
);
app.use(flash());

app.set('views', __dirname + '/views');
app.set("views", __dirname + "/views");
app.set("view engine", "ejs");


app.get("/", (req, res) => {
res.render("index")
})
res.render("index");
});

app.post("/question", (req, res) => {
const reciever = req.body.reciever_email
const validateEmail= (/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/).test(reciever) /* validate email */
if(!validateEmail) return res.render("error",{errorMessage: "email validation failed!"})

reciever_email.push(reciever)
const receiver = req.body.reciever_email;
const validateEmail = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/.test(
receiver
); /* validate email */
if (!validateEmail)
return res.render("error", { errorMessage: "email validation failed!" });

reciever_email.push(receiver);
req.flash("message", "");
res.render("questions", {message: req.flash('message')});
})
res.render("questions", { message: req.flash("message") });
});

app.post("/result", inputValidator, async (req, res) => {
let p = (parseFloat(req.body.avalue) + parseFloat(req.body.bvalue) + parseFloat(req.body.cvalue) + parseFloat(req.body.dvalue)) / 2
let e = (parseFloat(req.body.evalue) + parseFloat(req.body.fvalue) + parseFloat(req.body.gvalue) + parseFloat(req.body.hvalue)) / 2
let r = (parseFloat(req.body.ivalue) + parseFloat(req.body.jvalue) + parseFloat(req.body.kvalue) + parseFloat(req.body.lvalue)) / 2
let m = (parseFloat(req.body.mvalue) + parseFloat(req.body.nvalue) + parseFloat(req.body.ovalue) + parseFloat(req.body.pvalue)) / 2
let a = (parseFloat(req.body.qvalue) + parseFloat(req.body.rvalue) + parseFloat(req.body.svalue) + parseFloat(req.body.tvalue)) / 2

let p =
(parseFloat(req.body.avalue) +
parseFloat(req.body.bvalue) +
parseFloat(req.body.cvalue) +
parseFloat(req.body.dvalue)) /
2;
let e =
(parseFloat(req.body.evalue) +
parseFloat(req.body.fvalue) +
parseFloat(req.body.gvalue) +
parseFloat(req.body.hvalue)) /
2;
let r =
(parseFloat(req.body.ivalue) +
parseFloat(req.body.jvalue) +
parseFloat(req.body.kvalue) +
parseFloat(req.body.lvalue)) /
2;
let m =
(parseFloat(req.body.mvalue) +
parseFloat(req.body.nvalue) +
parseFloat(req.body.ovalue) +
parseFloat(req.body.pvalue)) /
2;
let a =
(parseFloat(req.body.qvalue) +
parseFloat(req.body.rvalue) +
parseFloat(req.body.svalue) +
parseFloat(req.body.tvalue)) /
2;

if (p < e && p < r && p < m && p < a) {
verdict.push("I've noticed that you've been experiencing some fluctuations in your positive emotions lately. It's completely normal to have ups and downs, but it's essential to take note of how you're feeling overall. Let's work together to identify activities and practices that bring you joy and help you maintain a positive outlook on life. Remember, small steps can lead to significant changes in your overall well-being.")
}
else if (r < p && r < e && r < m && r < a) {
verdict.push("I hope that you have healthy and positive relationships with others. However, nurturing these connections further can be very beneficial for your overall well-being. Let's discuss ways to deepen these relationships, communicate effectively, and build a support system that you can rely on during challenging times. Strong social ties contribute significantly to happiness and resilience.")
}
else if (e < r && e < r && e < m && e < a) {
verdict.push("I've observed that you haven't been as engaged and enthusiastic in your daily activities recently. It's crucial to find a sense of flow and fulfillment in what you do. Let's explore your interests and passions, and see how we can incorporate them into your routine. Engaging in activities that resonate with you can boost your energy and motivation, leading to a more satisfying and productive life.")
}
else if (m < r && m < p && m < e && m < a) {
verdict.push("It seems like you've been contemplating the bigger picture and searching for a sense of purpose in your life. Finding meaning is a journey that requires self-reflection and exploration of your values and aspirations. Let's work together to uncover your passions and align your actions with what truly matters to you. Discovering your sense of purpose will bring more fulfillment and a greater sense of direction.")
}
else if (a < r && a < p && a < m && a < e) {
verdict.push("I want you to know that i beleive you've achieved some remarkable things, and your hard work is commendable. However, I also sense that you might be setting high expectations for yourself. It's essential to recognize and celebrate your accomplishments, no matter how small they might seem. We'll work on setting realistic and achievable goals, which will give you a sense of progress and success, boosting your self-confidence and well-being.")
verdict.push(
"I've noticed that you've been experiencing some fluctuations in your positive emotions lately. It's completely normal to have ups and downs, but it's essential to take note of how you're feeling overall. Let's work together to identify activities and practices that bring you joy and help you maintain a positive outlook on life. Remember, small steps can lead to significant changes in your overall well-being."
);
} else if (r < p && r < e && r < m && r < a) {
verdict.push(
"I hope that you have healthy and positive relationships with others. However, nurturing these connections further can be very beneficial for your overall well-being. Let's discuss ways to deepen these relationships, communicate effectively, and build a support system that you can rely on during challenging times. Strong social ties contribute significantly to happiness and resilience."
);
} else if (e < r && e < r && e < m && e < a) {
verdict.push(
"I've observed that you haven't been as engaged and enthusiastic in your daily activities recently. It's crucial to find a sense of flow and fulfillment in what you do. Let's explore your interests and passions, and see how we can incorporate them into your routine. Engaging in activities that resonate with you can boost your energy and motivation, leading to a more satisfying and productive life."
);
} else if (m < r && m < p && m < e && m < a) {
verdict.push(
"It seems like you've been contemplating the bigger picture and searching for a sense of purpose in your life. Finding meaning is a journey that requires self-reflection and exploration of your values and aspirations. Let's work together to uncover your passions and align your actions with what truly matters to you. Discovering your sense of purpose will bring more fulfillment and a greater sense of direction."
);
} else if (a < r && a < p && a < m && a < e) {
verdict.push(
"I want you to know that i beleive you've achieved some remarkable things, and your hard work is commendable. However, I also sense that you might be setting high expectations for yourself. It's essential to recognize and celebrate your accomplishments, no matter how small they might seem. We'll work on setting realistic and achievable goals, which will give you a sense of progress and success, boosting your self-confidence and well-being."
);
}

if (isNaN(p) || isNaN(e) || isNaN(r) || isNaN(m) || isNaN(a)) {

res.render("error",{ errorMessage: "Please answer all the questions to calculate PERMA Score!"})
}
else {

res.render("error", {
errorMessage: "Please answer all the questions to calculate PERMA Score!",
});
} else {
const message = {
to: reciever_email.pop(),
to: receiver_email.pop(),
from: process.env.FROM,
subject: "Euphoria Check",
html: `<html>
Expand Down Expand Up @@ -188,18 +218,19 @@ app.post("/result", inputValidator, async (req, res) => {
</body>
</html>
`,
};

try {
await sgMail.send(message);
console.log("Message sent !");
} catch (error) {
console.error(error.message);
}
await sgMail
.send(message)
.then((response) => console.log("Email sent!"))
.catch((error) => console.log(error.message))

res.render("result", { pos: p, eng: e, mea: m, rel: r, acc: a })
res.render("result", { pos: p, eng: e, mea: m, rel: r, acc: a });
}
});


})

app.listen(process.env.PORT || 3000, function () {
console.log("Server Started Sucessfully")
})
app.listen(PORT, function () {
console.log(`Server Started Sucessfully on port ${PORT}`);
});

0 comments on commit 3ff6929

Please sign in to comment.