Skip to content

Commit

Permalink
db log-emotion
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadil-K committed Oct 31, 2023
1 parent 0f4d965 commit 4af543a
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 25 deletions.
76 changes: 52 additions & 24 deletions backend/controllers/appController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import bcryptjs from 'bcryptjs';
import jwt from 'jsonwebtoken';
import ENV from "../config.js";
import otpGenerator from "otp-generator";
import mongoose from 'mongoose';


/** middleware for verify user */
export async function verifyUser(req, res, next){
Expand Down Expand Up @@ -317,41 +319,67 @@ export async function resetPassword(req,res){

/** PUT: http://localhost:5001/api/updateRecommendation */
export async function updateRecommendation(req, res) {

// const { username, recommendations } = req.body;

try {
// const userId = req.query.id;
const { userId } = req.user;

const emotion = req.body;
const recommendation = { "recommendation": emotion };

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

// update the data
UserModel.updateOne({ _id : userId }, body).exec().then(
(response) => {
res.status(201).send({ msg : "Record Updated...!"})
}
)
if (!user) {
return res.status(404).send({ error: "User not found" });
}

// Get the current month and year
const date = new Date();
const month = date.toLocaleString('default', { month: 'long' });
const year = date.getFullYear();

// Find the log for the current month and year
let log = user.logs.find(log => log.month === month && log.year === year);

if (!log) {
// Create a new log if it does not exist
log = new mongoose.Types.EmbeddedDocument(user, {
month: month,
year: year,
joyful: 0,
surprise: 0,
anger: 0,
sad: 0,
happy: 0,
});

user.logs.push(log);
}

// Increment the counter for the received emotion
if (log[emotion]) {
log[emotion]++;
} else {
return res.status(400).send({ error: "Invalid emotion" });
}

// user.recommendations = recommendations;
// Update the recommendation field
user.recommendation = recommendation;

// await user.save();
// Remove logs that are older than 12 months
user.logs = user.logs.filter(log => {
const logDate = new Date(log.year, new Date(log.month+' 1, 2012').getMonth());
return date.getFullYear() - logDate.getFullYear() < 1 || date.getMonth() - logDate.getMonth() < 0;
});

// return res.status(200).send({ message: "Recommendations updated successfully" });
}else {
await user.save();

return res.status(200).send({ message: "Recommendations and logs 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 recommendations and logs" });
}
}

}


/** GET: http://localhost:5001/api/recommendation/:username */
Expand Down
32 changes: 32 additions & 0 deletions backend/models/User.model.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
import mongoose from "mongoose";

const MonthlyEmotionSchema = new mongoose.Schema({
month: {
type: String,
required: true,
},
year: {
type: Number,
required: true,
},
joyful: {
type: Number,
required: true,
},
surprise: {
type: Number,
required: true,
},
anger: {
type: Number,
required: true,
},
sad: {
type: Number,
required: true,
},
happy: {
type: Number,
required: true,
},
});

export const UserSchema = new mongoose.Schema({
username : {
type: String,
Expand All @@ -22,6 +53,7 @@ export const UserSchema = new mongoose.Schema({
address: { type: String},
profile: { type: String},
recommendation: { type: String},
logs: [MonthlyEmotionSchema],
});

export default mongoose.model.Users || mongoose.model('User', UserSchema );
2 changes: 1 addition & 1 deletion frontend/src/pages/AI_Assistant.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ const AI_Assistant = () => {
setEmotion(response.data.emotion);

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

})
Expand Down

0 comments on commit 4af543a

Please sign in to comment.