UNPKG

@replyke/express

Version:

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

63 lines (62 loc) 2.01 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 populateList_1 = __importDefault(require("../../../helpers/populateList")); exports.default = async (req, res) => { try { const { entityId } = req.body; const { listId } = req.params; const loggedInUserId = req.userId; const projectId = req.project.id; if (!listId) { res.status(400).json({ error: "Missing listId", code: "list/missing-data", }); return; } if (!entityId) { res.status(400).json({ error: "Missing entityId", code: "list/missing-data", }); return; } const list = (await models_1.List.findOne({ where: { projectId, userId: loggedInUserId, id: listId, }, })); if (!list) { res.status(404).json({ error: "List not found", code: "list/not-found", }); return; } if (list.entityIds.includes(entityId)) { res.status(409).json({ error: "Entity already exists in the list", code: "list/entity-exists", }); return; } list.set("entityIds", [...list.entityIds, entityId]); await list.save(); const populatedList = await (0, populateList_1.default)(list); res.status(200).json(populatedList); } catch (err) { console.error("Error adding entity to list: ", err); res.status(500).json({ error: "Internal server error.", code: "list/server-error", details: err.message, }); } };