@replyke/express
Version:
Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.
71 lines (70 loc) • 2.33 kB
JavaScript
;
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, userId: userIdProp } = req.body;
const { listId } = req.params;
const loggedInUserId = req.userId;
const projectId = req.project.id;
if (!listId || !entityId) {
res.status(400).json({
error: "Missing listId or entityId",
code: "list/missing-data",
});
return;
}
let userId = null;
if ((req.isMaster || req.isService) && userIdProp) {
userId = userIdProp;
}
else if (loggedInUserId) {
userId = loggedInUserId;
}
// Validate the presence of userId.
if (typeof userId !== "string") {
res.status(400).json({
error: "Missing userId",
code: "list/missing-data",
});
return;
}
const list = (await models_1.List.findOne({
where: {
projectId,
userId,
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,
});
}
};