This repository has been archived on 2025-11-04. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
qoverflow/server/routes/question/CreateQuestion.js
2022-08-18 16:54:23 -04:00

41 lines
1.2 KiB
JavaScript

const config = require('server/config.json');
const Question = require('server/db/models/Question');
const User = require('server/db/models/User');
const createRequest = require('server/utils/api');
async function CreateQuestion(req, res) {
const user = req.user;
const { title, text, tags } = req.body;
if (!title || !text) return res.status(400).send(config.errorIncomplete);
if (tags && tags.split(' ').length > 5) {
return res.status(400).send(config.errorIncomplete);
}
const { success, question } = await createRequest('post', `/questions`, {
creator: user.username,
title,
text,
});
if (!success) return res.status(500).send(config.errorGeneric);
const newQuestion = await Question.create({
...question,
tags: tags.split(' '),
_id: question.question_id,
});
// Increment user points by 1
await createRequest('patch', `/users/${user.username}/points`, {
operation: 'increment',
amount: 1,
});
await User.findByIdAndUpdate(user.id, { $inc: { points: 1 } });
return res.send({ question: newQuestion });
}
module.exports = CreateQuestion;