@replyke/express
Version:
Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.
68 lines (67 loc) • 2.41 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 { listName, userId: userIdProp } = req.body;
const { listId } = req.params;
const loggedInUserId = req.userId;
const projectId = req.project.id;
// Validate the presence of required fields.
if (!listId || !listName) {
res.status(400).json({
error: "Missing required parameters in request body",
code: "list/missing-parameters",
});
return;
}
const parentList = await models_1.List.findByPk(listId);
if (!parentList) {
res.status(400).json({
error: "Invalid parent list ID for sub-list",
code: "list/invalid-parent",
});
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;
}
// Create a new list (folder)
const newList = await models_1.List.create({
projectId,
userId,
parentId: listId,
name: listName,
}, {
// Explicitly specify that `parentList` is the association for parentId
include: [{ association: models_1.List.associations.parentList }],
});
const populatedList = await (0, populateList_1.default)(newList);
// Return the created list with a 200 (OK) status.
res.status(201).json(populatedList);
}
catch (err) {
console.error("Error creating a list: ", err);
res.status(500).json({
error: "Server error",
code: "list/server-error",
details: err.message,
});
}
};