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/GetComments.js
2022-07-28 20:13:57 -04:00

40 lines
1.3 KiB
JavaScript

const config = require('server/config.json');
const Comment = require('server/db/models/Comment');
const { getQuestion, refreshQuestion } = require('server/utils/question');
const fetchComments = require('server/utils/fetchComments');
async function GetComments(req, res) {
const { question_id } = req.params;
const question = await getQuestion(question_id);
if (!question) return res.status(404).send(config.errorNotFound);
// Refresh comments if expired
if (Number(question.lastCommentFetch) + config.commentExpires < Date.now()) {
const { success, requests } = await fetchComments(
`/questions/${question_id}/comments`
);
if (!success) return res.status(500).send(config.errorGeneric);
// Reformat comments and patch to database
await requests
.map(({ comment_id, ...comment }) => {
return {
...comment,
id: comment_id,
parent_id: question_id
}
})
.map(async (comment) => {
return Comment.findByIdAndUpdate(comment.id, comment, {
upsert: true,
});
});
}
const comments = await Comment.find({ parent_id: question_id });
return res.send({ comments });
}
module.exports = GetComments;