From bea6c45fc0788a30bf2dc20fa7c2bb81a712cfea Mon Sep 17 00:00:00 2001 From: Fig Yogurt Date: Wed, 17 Feb 2021 02:49:54 -0500 Subject: [PATCH] feat(analyze): added new route that shows the user their average mood from all their entries --- server/controllers/analyze.controller.js | 38 +++++++++++++++++++++++- server/routes/analyze.route.js | 3 ++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/server/controllers/analyze.controller.js b/server/controllers/analyze.controller.js index 71bc035..538356c 100644 --- a/server/controllers/analyze.controller.js +++ b/server/controllers/analyze.controller.js @@ -1,4 +1,5 @@ -const { Op } = require("sequelize"); +const Sequelize = require("sequelize"); +const { Op } = Sequelize; const { EntryTone, SentenceTone } = require("../db"); module.exports.searchEntries = async (req, res) => { @@ -42,3 +43,38 @@ module.exports.searchSentences = async (req, res) => { res.json({ message: "Error occured when searching your query!", error }); } }; + +module.exports.detectAverageMood = async (req, res) => { + try { + const { userId } = req; + let averageMood; + + const allEntries = await EntryTone.findAll({ + where: { + userId, + }, + attributes: [ + [Sequelize.fn("AVG", Sequelize.col("anger")), "anger"], + [Sequelize.fn("AVG", Sequelize.col("disgust")), "disgust"], + [Sequelize.fn("AVG", Sequelize.col("fear")), "fear"], + [Sequelize.fn("AVG", Sequelize.col("joy")), "joy"], + [Sequelize.fn("AVG", Sequelize.col("sadness")), "sadness"], + [Sequelize.fn("AVG", Sequelize.col("analytical")), "analytical"], + [Sequelize.fn("AVG", Sequelize.col("confident")), "confident"], + [Sequelize.fn("AVG", Sequelize.col("tentative")), "tentative"], + ], + }).then((data) => { + averageMood = data; + }); + res.json({ + message: "Your average mood has been computed!", + data: averageMood, + }); + } catch (error) { + console.log(error); + res.json({ + message: "Error occured when trying to detect the average mood!", + error, + }); + } +}; diff --git a/server/routes/analyze.route.js b/server/routes/analyze.route.js index dbf6e02..6098ae4 100644 --- a/server/routes/analyze.route.js +++ b/server/routes/analyze.route.js @@ -1,6 +1,7 @@ const { searchEntries, searchSentences, + detectAverageMood, } = require("../controllers/analyze.controller"); const { authJwt } = require("../middleware"); @@ -19,4 +20,6 @@ module.exports = function (app) { [authJwt.verifyToken], searchSentences ); + + app.get("/api/analyze/averagemood", [authJwt.verifyToken], detectAverageMood); };