@replyke/express
Version:
Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.
60 lines (59 loc) • 2.21 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const sequelize_1 = require("sequelize");
const models_1 = require("../../../models");
const populateList_1 = __importDefault(require("../../../helpers/populateList"));
// Main handler function
exports.default = async (req, res) => {
try {
const { update } = req.body;
const { listId } = req.params;
const loggedInUserId = req.userId;
const projectId = req.project.id;
// Validate the presence of required fields.
if (!listId || !update) {
res.status(400).json({
error: "Missing list ID or update data",
code: "list/missing-data",
});
return;
}
const { name } = update;
// Validate the presence of required fields.
if (!name) {
res.status(400).json({
error: "Cannot set the name to blank",
code: "list/blank-name",
});
return;
}
// Update the name in a single operation and retrieve the updated row
const [rowsUpdated, [updatedList]] = await models_1.List.update({ name }, {
where: {
projectId,
userId: loggedInUserId,
id: listId,
parentId: { [sequelize_1.Op.ne]: null }, // Ensure parentId is not null
},
returning: true, // Fetch the updated record in one step
});
// Handle cases where the list is not found.
if (rowsUpdated === 0 || !updatedList) {
res.status(404).json({ error: "List not found", code: "list/not-found" });
return;
}
const populatedList = await (0, populateList_1.default)(updatedList);
res.status(200).json(populatedList);
}
catch (err) {
console.error("Error updating list: ", err);
res.status(500).json({
error: "Server error",
code: "list/server-error",
details: err.message,
});
}
};