UNPKG

@replyke/express

Version:

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

60 lines (59 loc) 2.07 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 { userId: userIdProp } = req.query; // Extract projectId and userId from query parameters. const loggedInUserId = req.userId; const projectId = req.project.id; let userId = null; if ((req.isMaster || req.isService) && typeof userIdProp === "string") { 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; } // Search for the list using Sequelize's findOne method. let list = (await models_1.List.findOne({ where: { projectId, // Ensure the types match userId, parentId: null, isRoot: true, }, })); // If no list is found, create a new blank one. if (!list) { list = (await models_1.List.create({ projectId, name: "root", userId: loggedInUserId, isRoot: true, })); } const populatedList = await (0, populateList_1.default)(list); // Return the list with a 200 (OK) status. res.status(200).send(populatedList); } catch (err) { console.error("Error fetching root list: ", err); res.status(500).json({ error: "Server error", code: "list/server-error", details: err.message, }); } };