-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FIX: getting password & changes on voting system
- Loading branch information
1 parent
be57b86
commit 1e1199b
Showing
5 changed files
with
62 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
const router = require("express").Router(); | ||
|
||
const { rateUser } = require("../controllers/vote.controller"); | ||
const { rateUser, getRatedLogs } = require("../controllers/vote.controller"); | ||
|
||
const isLoggedIn = require("../middlewares/authenticate.middleware"); | ||
|
||
//rate friend | ||
router.route("/user/:action/:userId").post(isLoggedIn, rateUser); | ||
|
||
// get rated logs | ||
router.route("/").get(isLoggedIn, getRatedLogs); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,79 @@ | ||
const Query = require("../utils/query"); | ||
const User = require("../models/User.model"); | ||
const Votes = require("../models/Votes.model"); | ||
|
||
// 1 min = 60,000 milliseconds | ||
const oneMinToMilli = 60_000; | ||
|
||
const updateExpiryTimeForRating = 2 * oneMinToMilli; | ||
|
||
exports.rateUser = async (req, res) => { | ||
try { | ||
const { userId, action } = req.params; | ||
let flag = 0; | ||
let { userId, action } = req.params; | ||
|
||
action = parseInt(action); | ||
|
||
if (!userId) { | ||
return res.status(400).json({ error: "Please provide user id" }); | ||
if (!userId || !action) { | ||
return res | ||
.status(400) | ||
.json({ error: "Please provide userId and vote(-1 or 1) in params" }); | ||
} | ||
|
||
let user = await User.findById(userId); | ||
const Vote = await Votes.findOne({ user: userId, voter: req.user._id }); | ||
const user = await User.findById(userId); | ||
const currTime = new Date().getTime(); | ||
|
||
for (let ratingUser of user.ratedBy) { | ||
if (ratingUser.user.toString() === req.user._id.toString()) { | ||
if (currTime <= ratingUser.expiryTime) { | ||
return res | ||
.status(400) | ||
.json({ error: "You have already rated this user" }); | ||
} | ||
if (currTime >= ratingUser.expiryTime) { | ||
flag = 1; | ||
ratingUser.expiryTime = | ||
new Date().getTime() + updateExpiryTimeForRating; | ||
} | ||
} | ||
} | ||
|
||
if (flag === 0) { | ||
user.ratedBy.push({ | ||
user: req.user._id, | ||
expiryTime: new Date().getTime() + updateExpiryTimeForRating, | ||
if (Vote && currTime <= Vote.expiryTime) { | ||
return res.status(400).json({ | ||
error: `you already had rated ${user.name}, you can revote after sometime`, | ||
}); | ||
} | ||
|
||
if (action === "upvote") { | ||
user.upvotes += 1; | ||
if (action) user.upvotes += 1; | ||
else user.downvotes -= 1; | ||
user.rating = (user.upvotes / (user.upvotes + user.downvotes || 1)) * 100; | ||
user.votes.push(req.user._id); | ||
|
||
if (!Vote) { | ||
await Votes.create({ | ||
user: userId, | ||
voter: req.user._id, | ||
status: action, | ||
expiryTime: currTime + updateExpiryTimeForRating, | ||
}); | ||
} else { | ||
user.downvotes += 1; | ||
} | ||
Vote.expiryTime = currTime + updateExpiryTimeForRating; | ||
Vote.status = action; | ||
|
||
user.votes += 1; | ||
user.rating = (user.upvotes / (user.upvotes + user.downvotes || 1)) * 100; | ||
await Vote.save(); | ||
} | ||
|
||
await user.save(); | ||
|
||
res | ||
.status(200) | ||
.json({ success: true, message: `${user.name} was rated successfully` }); | ||
res.status(200).json({ | ||
success: true, | ||
message: `${user.name} was rated successfully`, | ||
}); | ||
} catch (error) { | ||
console.log(error); | ||
res | ||
.status(500) | ||
.json({ error: "Server has occured some problem, please try again" }); | ||
} | ||
}; | ||
|
||
exports.getRatedLogs = async (req, res) => { | ||
try { | ||
const currTime = new Date().getTime(); | ||
|
||
const ratingUsers = await Votes.find({ | ||
user: req.user._id, | ||
}).populate("voter", "name rating"); | ||
|
||
res.status(200).json({ | ||
success: true, | ||
ratingUsers, | ||
}); | ||
} catch (error) { | ||
console.log(error); | ||
res.status(500).json({ error: "Cannot able to fetch users" }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1e1199b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
social-coding-experience – ./
social-coding-experience-pratik9333.vercel.app
social-coding-experience-git-main-pratik9333.vercel.app
social-coding-experience.vercel.app