Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mattythedev01 committed Oct 14, 2024
1 parent cc6af7d commit 1f1ab6f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/api/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/**
* API Coming soon
* API for choosing random quotes, and perhaps the trivia and quiz commands
*/
31 changes: 18 additions & 13 deletions src/commands/general/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,18 @@ module.exports = {
await userData.save();
}

// Get total ratings and average rating from userSchema
const totalRatings = userData.TotalRatings || 0;
const avgRating = userData.averageRating
? userData.averageRating.toFixed(2)
: "N/A";
// Get all ratings for the user
const userRatings = await ratingSchema.find({ userID: user.id });

// Calculate total ratings and average rating
const totalRatings = userRatings.length;
const avgRating =
totalRatings > 0
? (
userRatings.reduce((sum, rating) => sum + rating.rating, 0) /
totalRatings
).toFixed(2)
: "N/A";

// Get latest quote
const latestQuote = await quoteSchema
Expand All @@ -62,20 +69,18 @@ module.exports = {
let topRatedQuoteRating = "Not rated yet";

if (latestQuote) {
const latestRating = await ratingSchema.findOne({
quoteID: latestQuote.quoteID,
userID: user.id,
});
const latestRating = userRatings.find(
(rating) => rating.quoteID === latestQuote.quoteID
);
if (latestRating) {
latestQuoteRating = `${latestRating.rating.toFixed(1)} / 5.0`;
}
}

if (topRatedQuote) {
const topRating = await ratingSchema.findOne({
quoteID: topRatedQuote.quoteID,
userID: user.id,
});
const topRating = userRatings.find(
(rating) => rating.quoteID === topRatedQuote.quoteID
);
if (topRating) {
topRatedQuoteRating = `${topRating.rating.toFixed(1)} / 5.0`;
}
Expand Down

0 comments on commit 1f1ab6f

Please sign in to comment.