Skip to content

Commit

Permalink
updateEmotion
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadil-K committed Nov 1, 2023
1 parent bebfe93 commit 2242442
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 12 deletions.
44 changes: 33 additions & 11 deletions backend/controllers/appController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import bcryptjs from 'bcryptjs';
import jwt from 'jsonwebtoken';
import ENV from "../config.js";
import otpGenerator from "otp-generator";
import moment from 'moment';

/** middleware for verify user */
export async function verifyUser(req, res, next){
Expand Down Expand Up @@ -398,28 +399,49 @@ export async function getRecommendation(req, res) {
// }
}

/** PUT: http://localhost:5001/api/updateRecommendation */
/** PUT: http://localhost:5001/api/updateEmotion */
export async function updateEmotion(req, res){
try {
const { userId } = req.user;
const { emotion } = req.body;

if (userId) {
const user = await UserModel.findById(userId);
if (!user) {
return res.status(404).send({ error: "User not found" });
}

const body = req.body;
// Get current day
const currentDay = moment().format('dddd');

// Initialize emotions field if it does not exist
if (!user.emotions) {
user.emotions = {
Monday: { Joy: 0, Surprise: 0, Anger: 0, Sad: 0, Happy: 0 },
Tuesday: { Joy: 0, Surprise: 0, Anger: 0, Sad: 0, Happy: 0 },
Wednesday: { Joy: 0, Surprise: 0, Anger: 0, Sad: 0, Happy: 0 },
Thursday: { Joy: 0, Surprise: 0, Anger: 0, Sad: 0, Happy: 0 },
Friday: { Joy: 0, Surprise: 0, Anger: 0, Sad: 0, Happy: 0 },
Saturday: { Joy: 0, Surprise: 0, Anger: 0, Sad: 0, Happy: 0 },
Sunday: { Joy: 0, Surprise: 0, Anger: 0, Sad: 0, Happy: 0 },
};
}

// update the data
UserModel.updateOne({ _id : userId }, body).exec().then(
(response) => {
res.status(201).send({ msg : "Record Updated...!"})
}
)
// Increment the count of the relevant emotion
if (emotion in user.emotions[currentDay]) {
user.emotions[currentDay][emotion]++;
} else {
return res.status(400).send({ error: "Invalid emotion" });
}

}else {
await user.save();

return res.status(200).send({ message: "Emotion updated successfully" });
} else {
return res.status(401).send({ error : "User Not Found...!"});
}

} catch (error) {
return res.status(500).send({ error: "Failed to update recommendations" });
return res.status(500).send({ error: "Failed to update emotion" });
}
}

Expand Down
1 change: 1 addition & 0 deletions backend/routes/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ router.route("/getRecommendation/:username").get(controller.getRecommendation);
router.route("/updateuser").put(Auth, controller.updateUser);
router.route("/resetPassword").put(controller.verifyUser, controller.resetPassword);
router.route("/updateRecommendation").put(Auth, controller.updateRecommendation);
router.route("/updateEmotion").put(Auth, controller.updateEmotion);


export default router;
2 changes: 1 addition & 1 deletion frontend/src/components/chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Chart } from 'react-google-charts';

const EmotionLineChart = () => {
const data = [
['Day', 'Joyful', 'Surprise', 'Anger', 'Sad', 'Happy'],
['Day', 'Joy', 'Surprise', 'Anger', 'Sad', 'Happy'],
['Monday', 20, 15, 10, 5, 8],
['Tuesday', 18, 13, 11, 7, 10],
['Wednesday', 22, 17, 9, 4, 6],
Expand Down
13 changes: 13 additions & 0 deletions frontend/src/helper/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@ export async function updateRecommendation(response){
}
}

/** update user emotion function */
export async function updateEmotion(response){
try {

const token = await localStorage.getItem('token');
const data = await axios.put(`http://localhost:5001/api/updateEmotion`, response, { headers : { "Authorization" : `Bearer ${token}`}});

return Promise.resolve({ data })
} catch (error) {
return Promise.reject({ error : "Couldn't Update Profile...!"})
}
}

/** generate OTP */
export async function generateOTP(username){
try {
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/pages/AI_Assistant.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { MediaRecorder, register } from 'extendable-media-recorder';
import { connect } from 'extendable-media-recorder-wav-encoder';
// import 'dotenv/config';
import { updateRecommendation } from '../helper/helper'
import { updateEmotion } from '../helper/helper'


import axios from 'axios';
Expand Down Expand Up @@ -99,6 +100,7 @@ const AI_Assistant = () => {
setEmotion(response.data.emotion);
console.log(emotion)
updateRecommendation({"recommendation" : emotion})
updateEmotion(emotion)
console.log('database update done')
})
.catch((error) => console.error(error));
Expand Down Expand Up @@ -146,6 +148,7 @@ const AI_Assistant = () => {

console.log(emotion);
updateRecommendation({"recommendation" : emotion})
updateEmotion(emotion)
console.log('database update done')

})
Expand Down

0 comments on commit 2242442

Please sign in to comment.