commit
255575bc9b
15 changed files with 102 additions and 162 deletions
|
|
@ -6,6 +6,6 @@
|
|||
"Level 5": 125,
|
||||
"Level 6": 1000,
|
||||
"Level 7": 2000,
|
||||
"Level 8" : 3000,
|
||||
"Level 9" : 10000
|
||||
"Level 8": 3000,
|
||||
"Level 9": 10000
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ async function CreateAnswer(req, res) {
|
|||
|
||||
if (question.status === 'closed') return res.status(403).send(config.errorForbidden);
|
||||
|
||||
if (question.status === 'protected' && getUserLevel(user.points) < 5) {
|
||||
if (question.status === 'protected' && getUserLevel(user.points) < 6) {
|
||||
return res.status(403).send(config.errorForbidden);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ async function EditAnswerCommentVote(req, res) {
|
|||
return res.status(403).send(config.errorForbidden);
|
||||
}
|
||||
|
||||
if (operation === 'downvote' && userLevel < 4) {
|
||||
if (operation === 'downvote' && userLevel < 5) {
|
||||
return res.status(403).send(config.errorForbidden);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ async function EditAnswerVote(req, res) {
|
|||
return res.status(403).send(config.errorForbidden);
|
||||
}
|
||||
|
||||
if (operation === 'downvote' && userLevel < 4) {
|
||||
if (operation === 'downvote' && userLevel < 5) {
|
||||
return res.status(403).send(config.errorForbidden);
|
||||
}
|
||||
|
||||
|
|
@ -73,14 +73,10 @@ async function EditAnswerVote(req, res) {
|
|||
amount: 1,
|
||||
});
|
||||
|
||||
await createRequest(
|
||||
'patch',
|
||||
`/users/${cachedAnswer.creator}/points`,
|
||||
{
|
||||
operation: 'decrement',
|
||||
amount: 15,
|
||||
}
|
||||
);
|
||||
await createRequest('patch', `/users/${cachedAnswer.creator}/points`, {
|
||||
operation: 'decrement',
|
||||
amount: 15,
|
||||
});
|
||||
|
||||
return res.send({ vote: 'downvoted' });
|
||||
}
|
||||
|
|
@ -117,14 +113,10 @@ async function EditAnswerVote(req, res) {
|
|||
docModel: 'Answer',
|
||||
});
|
||||
|
||||
await createRequest(
|
||||
'patch',
|
||||
`/users/${cachedAnswer.creator}/points`,
|
||||
{
|
||||
operation: 'increment',
|
||||
amount: 15,
|
||||
}
|
||||
);
|
||||
await createRequest('patch', `/users/${cachedAnswer.creator}/points`, {
|
||||
operation: 'increment',
|
||||
amount: 15,
|
||||
});
|
||||
|
||||
return res.send({ vote: 'upvoted' });
|
||||
}
|
||||
|
|
@ -149,14 +141,10 @@ async function EditAnswerVote(req, res) {
|
|||
docModel: 'Answer',
|
||||
});
|
||||
|
||||
await createRequest(
|
||||
'patch',
|
||||
`/users/${cachedAnswer.creator}/points`,
|
||||
{
|
||||
operation: 'increment',
|
||||
amount: 10,
|
||||
}
|
||||
);
|
||||
await createRequest('patch', `/users/${cachedAnswer.creator}/points`, {
|
||||
operation: 'increment',
|
||||
amount: 10,
|
||||
});
|
||||
|
||||
return res.send({ vote: 'upvoted' });
|
||||
} else if (operation === 'downvote') {
|
||||
|
|
@ -179,14 +167,10 @@ async function EditAnswerVote(req, res) {
|
|||
amount: 1,
|
||||
});
|
||||
|
||||
await createRequest(
|
||||
'patch',
|
||||
`/users/${cachedAnswer.creator}/points`,
|
||||
{
|
||||
operation: 'decrement',
|
||||
amount: 5,
|
||||
}
|
||||
);
|
||||
await createRequest('patch', `/users/${cachedAnswer.creator}/points`, {
|
||||
operation: 'decrement',
|
||||
amount: 5,
|
||||
});
|
||||
|
||||
return res.send({ vote: 'downvoted' });
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ async function EditAnswerVote(req, res) {
|
|||
return res.status(403).send(config.errorForbidden);
|
||||
}
|
||||
|
||||
if (operation === 'downvote' && userLevel < 4) {
|
||||
if (operation === 'downvote' && userLevel < 5) {
|
||||
return res.status(403).send(config.errorForbidden);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ async function EditQuestionStatusClosed(req, res) {
|
|||
const { user } = req;
|
||||
const { question_id } = req.params;
|
||||
const { etext, etitle } = req.body;
|
||||
let eObj = [etext, etitle]
|
||||
let eObj = [etext, etitle];
|
||||
// Verify user has required level
|
||||
if (getUserLevel(user.points) < 7) {
|
||||
return res.status(403).send(config.errorForbidden);
|
||||
|
|
@ -49,13 +49,11 @@ async function EditQuestionStatusClosed(req, res) {
|
|||
const question = await Question.findByIdAndUpdate(question_id, {
|
||||
edit: [],
|
||||
eObj,
|
||||
|
||||
});
|
||||
|
||||
const { success } = await createRequest('patch', `/questions/${question_id}`, {
|
||||
etext,
|
||||
eObj
|
||||
|
||||
eObj,
|
||||
});
|
||||
|
||||
return success
|
||||
|
|
|
|||
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;
|
||||
|
|
@ -9,7 +9,7 @@ async function EditQuestionStatusClosed(req, res) {
|
|||
const { question_id } = req.params;
|
||||
|
||||
// Verify user has required level
|
||||
if (getUserLevel(user.points) < 7) {
|
||||
if (getUserLevel(user.points) < 9) {
|
||||
return res.status(403).send(config.errorForbidden);
|
||||
}
|
||||
|
||||
|
|
@ -45,11 +45,9 @@ async function EditQuestionStatusClosed(req, res) {
|
|||
status: 'closed',
|
||||
});
|
||||
|
||||
const { success } = await createRequest(
|
||||
'patch',
|
||||
`/questions/${question_id}`,
|
||||
{ status: 'closed' }
|
||||
);
|
||||
const { success } = await createRequest('patch', `/questions/${question_id}`, {
|
||||
status: 'closed',
|
||||
});
|
||||
await Question.findByIdAndUpdate(question_id, { status: 'closed' });
|
||||
|
||||
return success
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ async function EditQuestionStatusProtected(req, res) {
|
|||
const { question_id } = req.params;
|
||||
|
||||
// Verify user has required level
|
||||
if (getUserLevel(user.points) < 6) {
|
||||
if (getUserLevel(user.points) < 8) {
|
||||
return res.status(403).send(config.errorForbidden);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ async function EditQuestionStatusReopened(req, res) {
|
|||
const { question_id } = req.params;
|
||||
|
||||
// Verify user has permissions
|
||||
if (getUserLevel(user.points) < 7) {
|
||||
if (getUserLevel(user.points) < 9) {
|
||||
return res.status(403).send(config.errorForbidden);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ async function EditQuestionVote(req, res) {
|
|||
return res.status(403).send(config.errorForbidden);
|
||||
}
|
||||
|
||||
if (operation === 'downvote' && userLevel < 4) {
|
||||
if (operation === 'downvote' && userLevel < 5) {
|
||||
return res.status(403).send(config.errorForbidden);
|
||||
}
|
||||
|
||||
|
|
@ -75,10 +75,7 @@ async function EditQuestionVote(req, res) {
|
|||
operation: 'decrement',
|
||||
amount: 6,
|
||||
});
|
||||
await User.findOneAndUpdate(
|
||||
{ username: question.creator },
|
||||
{ $inc: { points: -6 } }
|
||||
);
|
||||
await User.findOneAndUpdate({ username: question.creator }, { $inc: { points: -6 } });
|
||||
|
||||
// Decrement the user's points
|
||||
await createRequest('patch', `/users/${user.username}/points`, {
|
||||
|
|
@ -90,19 +87,12 @@ async function EditQuestionVote(req, res) {
|
|||
return res.send({ vote: 'downvoted' });
|
||||
} else {
|
||||
// Decrement the question creator's points
|
||||
const { success } = await createRequest(
|
||||
'patch',
|
||||
`/users/${question.creator}/points`,
|
||||
{
|
||||
operation: 'decrement',
|
||||
amount: 5,
|
||||
}
|
||||
);
|
||||
const { success } = await createRequest('patch', `/users/${question.creator}/points`, {
|
||||
operation: 'decrement',
|
||||
amount: 5,
|
||||
});
|
||||
|
||||
await User.findOneAndUpdate(
|
||||
{ username: question.creator },
|
||||
{ $inc: { points: -5 } }
|
||||
);
|
||||
await User.findOneAndUpdate({ username: question.creator }, { $inc: { points: -5 } });
|
||||
|
||||
if (!success) return res.status(500).send(config.errorGeneric);
|
||||
|
||||
|
|
@ -139,10 +129,7 @@ async function EditQuestionVote(req, res) {
|
|||
operation: 'increment',
|
||||
amount: 6,
|
||||
});
|
||||
await User.findOneAndUpdate(
|
||||
{ username: question.creator },
|
||||
{ $inc: { points: 6 } }
|
||||
);
|
||||
await User.findOneAndUpdate({ username: question.creator }, { $inc: { points: 6 } });
|
||||
|
||||
return res.send({ vote: 'upvoted' });
|
||||
} else {
|
||||
|
|
@ -151,10 +138,7 @@ async function EditQuestionVote(req, res) {
|
|||
operation: 'increment',
|
||||
amount: 1,
|
||||
});
|
||||
await User.findOneAndUpdate(
|
||||
{ username: question.creator },
|
||||
{ $inc: { points: 1 } }
|
||||
);
|
||||
await User.findOneAndUpdate({ username: question.creator }, { $inc: { points: 1 } });
|
||||
|
||||
// Increment the user's points
|
||||
await createRequest('patch', `/users/${user.username}/points`, {
|
||||
|
|
@ -188,10 +172,7 @@ async function EditQuestionVote(req, res) {
|
|||
operation: 'increment',
|
||||
amount: 5,
|
||||
});
|
||||
await User.findOneAndUpdate(
|
||||
{ username: question.creator },
|
||||
{ $inc: { points: 5 } }
|
||||
);
|
||||
await User.findOneAndUpdate({ username: question.creator }, { $inc: { points: 5 } });
|
||||
|
||||
return res.send({ vote: 'upvoted' });
|
||||
} else if (operation === 'downvote') {
|
||||
|
|
@ -218,10 +199,7 @@ async function EditQuestionVote(req, res) {
|
|||
operation: 'decrement',
|
||||
amount: 1,
|
||||
});
|
||||
await User.findOneAndUpdate(
|
||||
{ username: question.creator },
|
||||
{ $inc: { points: -1 } }
|
||||
);
|
||||
await User.findOneAndUpdate({ username: question.creator }, { $inc: { points: -1 } });
|
||||
|
||||
// Decrement the user's points
|
||||
await createRequest('patch', `/users/${user.username}/points`, {
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
@ -1,20 +1,14 @@
|
|||
function getUserLevel(points) {
|
||||
if (points > 10000)
|
||||
return 7;
|
||||
else if (points > 3000)
|
||||
return 6;
|
||||
else if (points > 1000)
|
||||
return 5;
|
||||
else if (points > 125)
|
||||
return 4;
|
||||
else if (points > 50)
|
||||
return 3;
|
||||
else if (points > 15)
|
||||
return 2;
|
||||
else if (points > 0)
|
||||
return 1;
|
||||
else
|
||||
return 0;
|
||||
if (points > 10000) return 9;
|
||||
else if (points > 3000) return 8;
|
||||
else if (points > 2000) return 7;
|
||||
else if (points > 1000) return 6;
|
||||
else if (points > 125) return 5;
|
||||
else if (points > 75) return 4;
|
||||
else if (points > 50) return 3;
|
||||
else if (points > 15) return 2;
|
||||
else if (points > 0) return 1;
|
||||
else return 0;
|
||||
}
|
||||
|
||||
module.exports = getUserLevel;
|
||||
|
|
|
|||
Reference in a new issue