@replyke/express
Version:
Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.
54 lines (53 loc) • 1.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const sequelize_query_params_1 = require("../../../constants/sequelize-query-params");
const models_1 = require("../../../models");
exports.default = async (req, res) => {
try {
const { withParent } = req.query;
const { commentId } = req.params;
const projectId = req.project.id;
if (!commentId || typeof commentId !== "string") {
res.status(400).json({
error: "Missing a valid comment ID in request query",
code: "comment/invalid-request",
});
return;
}
const comment = (await models_1.Comment.findOne({
where: { projectId, id: commentId },
...sequelize_query_params_1.commentParams,
}));
if (!comment) {
res.status(404).json({
error: "Comment not found",
code: "comment/not-found",
});
return;
}
if (!withParent || !comment.parentId) {
res.status(200).json({
comment: comment.toJSON(),
parentComment: null,
});
return;
}
let parentComment = (await models_1.Comment.findOne({
where: { projectId, id: comment.parentId },
...sequelize_query_params_1.commentParams,
}));
// Return the comment and it's parent with a 200 (OK) status.
res.status(200).json({
comment: comment.toJSON(),
parentComment: parentComment?.toJSON(),
});
}
catch (err) {
console.error("Error fetching a comment: ", err);
res.status(500).json({
error: "Internal server error.",
code: "comment/server-error",
details: err.message,
});
}
};