UNPKG

@replyke/express

Version:

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

65 lines (64 loc) 2.65 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 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; // Validate the presence of required fields. if (!commentId) { res.status(400).json({ error: "Missing required data", code: "comment/missing-data", }); return; } const { sequelize } = (0, config_1.getCoreConfig)(); // Retrieve the comment to remove the upvote. 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 liked the comment. if (!comment.upvotes.includes(loggedInUserId)) { res.status(409).json({ error: "User hasn't upvoted this comment", code: "comment/not-upvoted", }); return; } const { comment: updatedComment } = await sequelize.transaction(async (transaction) => { // Update the comment to remove the user's upvote. comment.set("upvotes", comment.upvotes.filter((id) => id !== 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()); } catch (err) { console.error("Error removing comment upvote:", err); res.status(500).json({ error: "Internal server error.", code: "comment/server-error", details: err.message, }); } };