@replyke/express
Version:
Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.
41 lines (40 loc) • 1.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = default_1;
const sequelize_1 = require("sequelize");
const models_1 = require("../../../models");
const constants_1 = require("../../../constants");
async function default_1(req, res) {
try {
const { entityId } = req.params;
const projectId = req.project.id; // assumes project middleware added this
if (!entityId) {
res.status(400).json({
error: "Missing entityId in request.",
code: "entity/missing-entity-id",
});
}
const topComment = await models_1.Comment.findOne({
where: {
entityId,
projectId,
deletedAt: null,
parentDeletedAt: null,
},
order: [
[(0, sequelize_1.fn)("array_length", (0, sequelize_1.col)("upvotes"), 1), "DESC"],
["createdAt", "ASC"], // fallback to oldest if tie
],
...constants_1.commentParams,
});
res.status(200).json(topComment ?? null);
}
catch (err) {
console.error("Error fetching top comment:", err);
res.status(500).json({
error: "Internal server error.",
code: "entity/server-error",
details: err.message,
});
}
}