UNPKG

@replyke/express

Version:

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

58 lines (57 loc) 1.97 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")); const config_1 = require("../../../config"); exports.default = async (req, res) => { try { const { entityId } = req.body; const { listId } = req.params; const loggedInUserId = req.userId; const projectId = req.project.id; const { sequelize } = (0, config_1.getCoreConfig)(); if (!listId || !entityId) { res.status(400).json({ error: "Missing listId or 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 does not exist in the list", code: "list/entity-not-found", }); return; } list.set("entityIds", list.entityIds.filter((id) => id !== entityId)); await list.save(); const populatedList = await (0, populateList_1.default)(list); res.status(200).json(populatedList); } catch (err) { console.error("Error removing entity from list: ", err); res.status(500).json({ error: "Internal server error.", code: "list/server-error", details: err.message, }); } };