Fixed question bounty
This commit is contained in:
parent
d07f866ef6
commit
7f8111bacd
5 changed files with 51 additions and 63 deletions
|
|
@ -3,7 +3,6 @@ const router = express.Router();
|
|||
|
||||
const tokenAuth = require('server/middleware/tokenAuth');
|
||||
|
||||
const HandleBounty = require('./question/HandleBounty');
|
||||
const CreateAnswer = require('./question/CreateAnswer');
|
||||
const CreateAnswerComment = require('./question/CreateAnswerComment');
|
||||
const CreateComment = require('./question/CreateComment');
|
||||
|
|
@ -17,6 +16,7 @@ const EditAnswerVote = require('./question/EditAnswerVote');
|
|||
const EditCommentVote = require('./question/EditCommentVote');
|
||||
const EditQuestion = require('./question/EditQuestion');
|
||||
const EditQuestionBody = require('./question/EditQuestionBody');
|
||||
const EditQuestionBounty = require('./question/EditQuestionBounty');
|
||||
const EditQuestionStatusClosed = require('./question/EditQuestionStatusClosed');
|
||||
const EditQuestionStatusProtected = require('./question/EditQuestionStatusProtected');
|
||||
const EditQuestionStatusReopened = require('./question/EditQuestionStatusReopened');
|
||||
|
|
@ -32,11 +32,11 @@ const GetQuestionVote = require('./question/GetQuestionVote');
|
|||
const Search = require('./question/Search');
|
||||
|
||||
router.get('/search', Search);
|
||||
router.patch('/:question_id/addBounty', HandleBounty);
|
||||
router.post('/', tokenAuth, CreateQuestion);
|
||||
router.get('/:question_id', GetQuestion);
|
||||
router.patch('/:question_id', tokenAuth, EditQuestion);
|
||||
router.patch('/:question_id/edit', tokenAuth, EditQuestionBody);
|
||||
router.patch('/:question_id/addBounty', tokenAuth, EditQuestionBounty);
|
||||
router.patch('/:question_id/close', tokenAuth, EditQuestionStatusClosed);
|
||||
router.patch('/:question_id/protect', tokenAuth, EditQuestionStatusProtected);
|
||||
router.patch('/:question_id/reopen', tokenAuth, EditQuestionStatusReopened);
|
||||
|
|
|
|||
|
|
@ -11,9 +11,9 @@ async function EditAnswerAccepted(req, res) {
|
|||
|
||||
// Find question and verify that it exists
|
||||
const question = await getQuestion(question_id);
|
||||
|
||||
|
||||
if (!question) return res.status(404).send(config.errorNotFound);
|
||||
const {hasBounty} = question;
|
||||
const { hasBounty } = question;
|
||||
// Verify user owns question and question does not already have an accepted answer
|
||||
if (question.creator !== user.username || question.hasAccepted) {
|
||||
return res.status(403).send(config.errorForbidden);
|
||||
|
|
@ -39,14 +39,21 @@ async function EditAnswerAccepted(req, res) {
|
|||
await Question.findByIdAndUpdate(question_id, { hasAcceptedAnswer: true });
|
||||
|
||||
// Increment points of answer creator
|
||||
await createRequest('patch', `/users/${cachedAnswer.creator}/points`, {
|
||||
operation: 'increment',
|
||||
amount: 15,
|
||||
});
|
||||
if(hasBounty){
|
||||
await User.findOneAndUpdate({ username: cachedAnswer.creator }, { $inc: { points: 15 + hasBounty } });
|
||||
}else{
|
||||
await User.findOneAndUpdate({ username: cachedAnswer.creator }, { $inc: { points: 15} });
|
||||
if (hasBounty) {
|
||||
await User.findOneAndUpdate(
|
||||
{ username: cachedAnswer.creator },
|
||||
{ $inc: { points: 15 + hasBounty } }
|
||||
);
|
||||
await createRequest('patch', `/users/${cachedAnswer.creator}/points`, {
|
||||
operation: 'increment',
|
||||
amount: 15 + hasBounty,
|
||||
});
|
||||
} else {
|
||||
await User.findOneAndUpdate({ username: cachedAnswer.creator }, { $inc: { points: 15 } });
|
||||
await createRequest('patch', `/users/${cachedAnswer.creator}/points`, {
|
||||
operation: 'increment',
|
||||
amount: 15,
|
||||
});
|
||||
}
|
||||
return res.sendStatus(200);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ async function EditQuestionStatusClosed(req, res) {
|
|||
const { etext, etitle } = req.body;
|
||||
let eObj = [etext, etitle];
|
||||
// Verify user has required level
|
||||
if (getUserLevel(user.points) < 9) {
|
||||
if (getUserLevel(user.points) < 7) {
|
||||
return res.status(403).send(config.errorForbidden);
|
||||
}
|
||||
|
||||
|
|
|
|||
31
server/routes/question/EditQuestionBounty.js
Normal file
31
server/routes/question/EditQuestionBounty.js
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
const Question = require('server/db/models/Question');
|
||||
const User = require('server/db/models/User');
|
||||
const createRequest = require('server/utils/api');
|
||||
const getUserLevel = require('server/utils/getUserLevel');
|
||||
const config = require('server/config.json');
|
||||
|
||||
async function EditQuestionBounty(req, res) {
|
||||
const { user } = req.user;
|
||||
const { question_id } = req.params;
|
||||
const { bounty } = req.body;
|
||||
|
||||
if (!bounty) return res.status(400).send(config.errorIncomplete);
|
||||
const userLevel = getUserLevel(user.points);
|
||||
|
||||
if (userLevel < 4 || user.points < bounty) {
|
||||
return res.status(403).send(config.errorForbidden);
|
||||
}
|
||||
|
||||
await Question.findByIdAndUpdate(question_id, { hasBounty: bounty });
|
||||
|
||||
await User.findOneAndUpdate(
|
||||
{ username: cachedAnswer.creator },
|
||||
{ $inc: { points: Math.abs(bounty) * -1 } }
|
||||
);
|
||||
await createRequest('patch', `/users/${cachedAnswer.creator}/points`, {
|
||||
operation: 'decrement',
|
||||
amount: bounty,
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = EditQuestionBounty;
|
||||
|
|
@ -1,50 +0,0 @@
|
|||
const Question = require('server/db/models/Question')
|
||||
const User = require('server/db/models/User')
|
||||
const createRequest = require('server/utils/api');
|
||||
async function HandleBounty(req,res){
|
||||
const {user, hasAcceptedAnswer, amount, isOpen, hasBounty} = req.body;
|
||||
const {question_id} = req.params;
|
||||
|
||||
|
||||
const userPull = await createRequest(
|
||||
'get',
|
||||
`/users/${user}`
|
||||
);
|
||||
|
||||
const questionPull = await createRequest(
|
||||
'get',
|
||||
`/questions/${question_id}`
|
||||
);
|
||||
const question = questionPull.question;
|
||||
|
||||
if(userPull.success){
|
||||
const {points} = userPull.user;
|
||||
if(points >= 75 && isOpen && !hasAcceptedAnswer && !hasBounty){
|
||||
if(amount >= 75 && amount <= 500){
|
||||
if(points - amount >= 75){
|
||||
//updates question
|
||||
await Question.findByIdAndUpdate(question_id, {hasBounty : amount});
|
||||
//updates user in api
|
||||
await createRequest('patch', `/users/${user}/points`, {
|
||||
operation: 'decrement',
|
||||
amount: amount,
|
||||
});
|
||||
//updates user in db
|
||||
await User.findOneAndUpdate(
|
||||
{ username: user },
|
||||
{ $inc: { points: -1 * amount } }
|
||||
);
|
||||
}else{
|
||||
return res.status(403).send("This operation would leave you with less than 75 points")
|
||||
}
|
||||
}else{
|
||||
return res.status(403).send("Must be between 75 and 500 points")
|
||||
}
|
||||
}else{
|
||||
return res.status(403).send("Need to be level 4")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = HandleBounty;
|
||||
Reference in a new issue