fix GetAnswers, GetComments

This commit is contained in:
“spicecat” 2022-07-21 22:09:04 -05:00
commit 34e69b806c
4 changed files with 18 additions and 18 deletions

View file

@ -19,13 +19,13 @@ async function GetAnswers(req, res) {
// Reformat questions and patch to cache
await requests
.reduce(async (acc, req) => {
const reformat = req.answers.map((answer) => ({
.map(({ answer_id, ...answer }) => {
return {
...answer,
id: answer.answer_id,
}));
return [...reformat, ...acc];
}, [])
id: answer_id,
questionID
}
})
.map(async (answer) => {
if (answer.accepted) {
await Question.findByIdAndUpdate(questionID, {

View file

@ -14,18 +14,18 @@ async function GetComments(req, res) {
const { success, requests } = await fetchComments(
`/questions/${questionID}/comments`
);
console.log(123123, success, requests)
if (!success) return res.status(500).send(config.errorGeneric);
// Reformat comments and patch to database
await requests
.reduce(async (acc, req) => {
const reformat = req.comments.map((comment) => ({
.map(({ comment_id, ...comment }) => {
return {
...comment,
id: comment.comment_id,
}));
return [...reformat, ...acc];
}, [])
id: comment_id,
parentID: questionID
}
})
.map(async (comment) => {
return Comment.findByIdAndUpdate(comment.id, comment, {
upsert: true,

View file

@ -16,7 +16,7 @@ async function fetchAnswers(
const newAcc = {
success: true,
requests: [...acc.requests, request],
requests: [...acc.requests, ...request.answers],
};
const oldest = request.answers[request.answers.length - 1];
@ -24,7 +24,7 @@ async function fetchAnswers(
return newAcc;
}
return fetchAnswers(recentTimestamp, newAcc, oldest['answer_id']);
return fetchAnswers(url, recentTimestamp, newAcc, oldest['answer_id']);
}
module.exports = fetchAnswers;

View file

@ -10,13 +10,13 @@ async function fetchComments(
after = ''
) {
const request = await createRequest('get', url, { after });
if (!request.success) return config.errorGeneric;
if (!request.comments.length) return acc;
const newAcc = {
success: true,
requests: [...acc.requests, request],
requests: [...acc.requests, ...request.comments],
};
const oldest = request.comments[request.comments.length - 1];
@ -24,7 +24,7 @@ async function fetchComments(
return newAcc;
}
return fetchComments(recentTimestamp, newAcc, oldest['comment_id']);
return fetchComments(url, recentTimestamp, newAcc, oldest['comment_id']);
}
module.exports = fetchComments;