UNPKG

@replyke/express

Version:

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

44 lines (43 loc) 1.55 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 { // Extract projectId and userId from query parameters. const loggedInUserId = req.userId; const projectId = req.project.id; // Search for the list using Sequelize's findOne method. let list = (await models_1.List.findOne({ where: { projectId, // Ensure the types match userId: loggedInUserId, 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, }); } };