UNPKG

@replyke/express

Version:

Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.

100 lines (99 loc) 4.18 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const models_1 = require("../../../models"); const sequelize_query_params_1 = require("../../../constants/sequelize-query-params"); const createNotification_1 = __importDefault(require("../../../helpers/createNotification")); const updateUserReputation_1 = __importDefault(require("../../../helpers/updateUserReputation")); const reputation_scores_1 = __importDefault(require("../../../constants/reputation-scores")); const config_1 = require("../../../config"); exports.default = async (req, res) => { try { const { commentId } = req.params; const loggedInUserId = req.userId; const projectId = req.project.id; const { sequelize } = (0, config_1.getCoreConfig)(); // Validate the presence of required fields. if (!commentId) { res.status(400).json({ error: "Missing required data", code: "comment/missing-data", }); return; } // Retrieve the comment to be upvoted. const comment = (await models_1.Comment.findOne({ where: { id: commentId, projectId }, ...sequelize_query_params_1.commentParams, })); if (!comment) { res.status(404).json({ error: "Comment not found", code: "comment/not-found", }); return; } // Check if the user has already upvoted the comment. if (comment.upvotes.includes(loggedInUserId)) { res.status(409).json({ error: "User has already upvoted this comment", code: "comment/already-upvoted", }); return; } const { comment: updatedComment } = await sequelize.transaction(async (transaction) => { // Update the comment's upvotes by adding the user's ID and removing it from downvotes if present. comment.set("upvotes", [...comment.upvotes, loggedInUserId]); comment.set("downvotes", comment.downvotes.filter((downvote) => downvote !== loggedInUserId)); // Save the updated comment. await comment.save({ transaction }); await (0, updateUserReputation_1.default)(comment.userId, reputation_scores_1.default.upvote, transaction); return { comment }; }); // Return the updated comment. res.status(200).json(updatedComment.toJSON()); // Fetch the user const user = (await models_1.User.findByPk(loggedInUserId)); if (!user) { console.error("Logged in user object wasn't found"); return; } // First, fetch the entity using Sequelize's findOne method. const entity = (await models_1.Entity.findOne({ where: { projectId, id: comment.entityId }, })); if (!entity) { console.error("Entity object wasn't found"); return; } const entityJSON = entity.toJSON(); (0, createNotification_1.default)(req, res, { userId: comment.userId, // The recipient user ID, assumed here projectId, type: "comment-upvote", action: "open-comment", metadata: { entityId: entityJSON.id, entityShortId: entityJSON.shortId, entityTitle: entityJSON.title, entityContent: (entityJSON.content || "").slice(0, 200), commentId, commentContent: comment.content, initiatorId: user.id, initiatorName: user.name, initiatorUsername: user.username, initiatorAvatar: user.avatar, }, }); } catch (err) { console.error("Error upvoting comment:", err); res.status(500).json({ error: "Internal server error.", code: "comment/server-error", details: err.message, }); } };