UNPKG

@replyke/express

Version:

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

97 lines (96 loc) 4.21 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 models_2 = require("../../../models"); 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 { entityId } = req.params; const loggedInUserId = req.userId; const projectId = req.project.id; const { sequelize } = (0, config_1.getCoreConfig)(); // Validate the presence of entityId if (!entityId) { res.status(400).json({ error: "Missing entityId in request.", code: "entity/invalid-id", }); return; } // First, fetch the entity using Sequelize's findOne method. let entity = (await models_1.Entity.findOne({ where: { projectId, id: entityId }, })); if (!entity) { res.status(404).json({ error: "Entity not found.", code: "entity/not-found", }); return; } // Check if user already liked the entity if (entity.upvotes.includes(loggedInUserId)) { res.status(409).json({ error: "User already upvoted entity.", code: "entity/already-upvoted", }); return; } await sequelize.transaction(async (transaction) => { // Atomically increment upvotesCount and update upvotes array in the database // await entity.increment("upvotesCount", { by: 1, transaction }); entity.set("upvotes", [...entity.upvotes, loggedInUserId]); // Add userId to upvotes entity.set("downvotes", entity.downvotes.filter((downvote) => downvote !== loggedInUserId)); // Make sure userId is removed from downvotes in case they were there await entity.save({ transaction }); if (entity.userId) { await (0, updateUserReputation_1.default)(entity.userId, reputation_scores_1.default.upvote, transaction); } }); // Fetch the entity again but populated now const populatedEntity = (await models_1.Entity.findOne({ where: { id: entity.id }, ...sequelize_query_params_1.entityParams, })); // Return the updated or newly created entity. res.status(200).json(populatedEntity.toJSON()); // Fetch the user to ensure it exists (optional but recommended) const user = (await models_2.User.findByPk(loggedInUserId)); if (!user) { res.status(404).send("Logged in user object wasn't found"); return; } if (entity.userId) { (0, createNotification_1.default)(req, res, { userId: entity.userId, // The recipient user ID, assumed here projectId, type: "entity-upvote", action: "open-entity", metadata: { entityId: entity.id, entityShortId: entity.shortId, entityTitle: entity.title, entityContent: (entity.content || "").slice(0, 200), initiatorId: loggedInUserId, initiatorName: user.name, initiatorUsername: user.username, initiatorAvatar: user.avatar, }, }); } } catch (err) { console.error("Error upvoting an entity:", err); res.status(500).json({ error: "Internal server error.", code: "entity/server-error", details: err.message, }); } };