dsql-admin/dsql-app/.local_dist/server/pages/api/admin/docs/[action].js

556 lines
19 KiB
JavaScript
Raw Normal View History

2024-11-05 11:12:42 +00:00
"use strict";
(() => {
var exports = {};
exports.id = 9875;
exports.ids = [9875];
exports.modules = {
/***/ 6517:
/***/ ((module) => {
module.exports = require("lodash");
/***/ }),
/***/ 6109:
/***/ ((module) => {
module.exports = require("sanitize-html");
/***/ }),
/***/ 2261:
/***/ ((module) => {
module.exports = require("serverless-mysql");
/***/ }),
/***/ 4300:
/***/ ((module) => {
module.exports = require("buffer");
/***/ }),
/***/ 6113:
/***/ ((module) => {
module.exports = require("crypto");
/***/ }),
/***/ 7147:
/***/ ((module) => {
module.exports = require("fs");
/***/ }),
/***/ 3685:
/***/ ((module) => {
module.exports = require("http");
/***/ }),
/***/ 1017:
/***/ ((module) => {
module.exports = require("path");
/***/ }),
/***/ 1689:
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
// @ts-check
/**
* Imports: Handle imports
*/
const sanitizeHtml = __webpack_require__(6109);
const DB_HANDLER = __webpack_require__(2224);
const addDbEntry = __webpack_require__(5338);
const sanitizeHtmlOptions = __webpack_require__(9544);
/**
* Add a db Entry Function
* ==============================================================================
* @description Description
* @async
*
* @param {object} params - An object containing the function parameters.
* @param {any} params.body - Request body
* @param {boolean} [params.update] - Update if existing
*
* @returns {Promise<object|null>}
*/ module.exports = async function({ body , update }) {
/**
* Initialize variables
*/ const slug = body.title.toLowerCase().replace(/ /g, "-").replace(/[^a-z0-9\-]/g, "");
const currentOrder = await async function() {
if (update) return null;
try {
const lastOrder = await DB_HANDLER(`SELECT page_order FROM docs_pages ORDER BY page_order DESC LIMIT 1`);
return lastOrder[0].page_order;
} catch (error) {
return null;
}
}();
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/**
* Handle function logic
* @type {any}
*/ const addDocPage = await addDbEntry({
data: {
title: body.title,
slug: slug,
description: sanitizeHtml(body.description, sanitizeHtmlOptions),
content: sanitizeHtml(body.content, sanitizeHtmlOptions),
text_content: body.text_content,
level: body.level,
page_order: update ? body.page_order : parseInt(currentOrder) + 1,
parent_id: body?.parent_id
},
tableName: "docs_pages",
update: update,
duplicateColumnName: "id",
duplicateColumnValue: body.id
});
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/**
* Return statement
*/ if (addDocPage?.insertId) {
return addDocPage;
} else if (update && addDocPage.affectedRows) {
return addDocPage;
}
{
return null;
}
}; ////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/***/ }),
/***/ 6672:
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
// @ts-check
const DB_HANDLER = __webpack_require__(2224);
/**
* Imports: Handle imports
*/ /**
* Add a db Entry Function
* ==============================================================================
* @description Description
* @async
*
* @param {object} params - An object containing the function parameters.
* @param {any} params.body - Request body
* @param {boolean} [params.update] - Update if existing
*
* @returns {Promise<object|null>}
*/ module.exports = async function({ body }) {
/**
* Initialize variables
*/ ////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/**
* Handle function logic
*/ const deleteDocPage = await DB_HANDLER(`DELETE FROM docs_pages WHERE id=?`, [
body.id
]);
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/**
* Return statement
*/ if (deleteDocPage) {
return deleteDocPage;
} else {
return null;
}
}; ////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/***/ }),
/***/ 2755:
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
// @ts-check
/**
* Imports: Handle imports
*/
const fs = __webpack_require__(7147);
/**
* Add a db Entry Function
* ==============================================================================
* @description Description
* @async
*
* @param {object} params - An object containing the function parameters.
* @param {object} [params.body] - Request body
* @param {boolean} [params.update] - Update if existing
*
* @returns {Promise<object|null>}
*/ module.exports = async function() {
/**
* Initialize variables
*/ const snippetsDirectory = "./components/su/components/RichText/html-presets";
/** @type {any[]} */ let snippets = [];
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/**
* Handle function logic
*/ if (fs.existsSync(snippetsDirectory)) {
const presetFiles = fs.readdirSync(snippetsDirectory);
presetFiles.forEach((file)=>{
if (file?.endsWith(".html")) {
/** @type {any} */ const pushObject = {};
pushObject[file.replace(/\.html$/, "")] = fs.readFileSync(`${snippetsDirectory}/${file}`, "utf-8");
snippets.push({
...pushObject
});
}
});
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/**
* Return statement
*/ return snippets;
}; ////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/***/ }),
/***/ 5217:
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
// @ts-check
/**
* Imports: Handle imports
*/
const fs = __webpack_require__(7147);
const DB_HANDLER = __webpack_require__(2224);
/**
* Add a db Entry Function
* ==============================================================================
* @description Description
* @async
*
* @param {object} params - An object containing the function parameters.
* @param {any} params.body - Request body
*
* @returns {Promise<object|null>}
*/ module.exports = async function({ body }) {
/**
* Initialize variables
*/ let results;
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/**
* Handle function logic
*/ const pages = await DB_HANDLER(`SELECT id, title, slug FROM docs_pages WHERE parent_id = ?`, [
body.pageId
]);
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/**
* Return statement
*/ if (!pages[0]) return null;
return pages;
}; ////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/***/ }),
/***/ 5030:
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
// @ts-check
/**
* Imports: Handle imports
*/
const DB_HANDLER = __webpack_require__(2224);
/**
* Add a db Entry Function
* ==============================================================================
* @description Description
* @async
*
* @param {object} params - An object containing the function parameters.
* @param {any} params.body - Request body
*
* @returns {Promise<object|null>}
*/ module.exports = async function({ body }) {
/**
* Initialize variables
*/ let results;
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/**
* Handle function logic
*/ const pages = body?.currentPageId ? await DB_HANDLER(`SELECT * FROM docs_pages WHERE level = ? AND id != ?`, [
body.pagesLevel,
body.currentPageId
]) : await DB_HANDLER(`SELECT * FROM docs_pages WHERE level = ?`, [
body.pagesLevel,
]);
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/**
* Return statement
*/ return pages;
}; ////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/***/ }),
/***/ 4751:
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
// @ts-check
/**
* Imports: Handle imports
*/
const fs = __webpack_require__(7147);
const DB_HANDLER = __webpack_require__(2224);
/**
* Add a db Entry Function
* ==============================================================================
* @description Description
* @async
*
* @param {object} params - An object containing the function parameters.
* @param {any} params.body - Request body
*
* @returns {Promise<any>}
*/ module.exports = async function({ body }) {
/**
* Initialize variables
*/ const { pull } = body;
const filePath = "./jsonData/docs.json";
// return null;
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/**
* Handle function logic
*/ if (pull) {
try {
const jsonPages = JSON.parse(fs.readFileSync(filePath, "utf-8"));
for(let i = 0; i < jsonPages.length; i++){
const page = jsonPages[i];
const existingPageEntry = await DB_HANDLER(`SELECT id FROM docs_pages WHERE slug = ? AND level = ?`, [
page.slug,
page.level
]);
if (existingPageEntry && existingPageEntry[0]) {
let trimmedPage = {
...page
};
delete trimmedPage.date_created;
delete trimmedPage.date_created_code;
delete trimmedPage.date_created_timestamp;
delete trimmedPage.date_updated;
delete trimmedPage.date_updated_code;
delete trimmedPage.date_updated_timestamp;
delete trimmedPage.id;
const update = await DB_HANDLER(`UPDATE docs_pages SET ? WHERE slug = '${page.slug}' AND level = '${page.level}'`, trimmedPage);
////////////////////////////////////////
} else {
////////////////////////////////////////
let trimmedPage1 = {
...page
};
delete trimmedPage1.date_created_timestamp;
delete trimmedPage1.date_updated_timestamp;
const newPage = await DB_HANDLER(`INSERT INTO docs_pages SET ?`, trimmedPage1);
}
}
} catch (error) {
// console.log(error.message);
return null;
}
} else {
const pages = await DB_HANDLER(`SELECT * FROM docs_pages`);
fs.writeFileSync(filePath, JSON.stringify(pages, null, 4), "utf-8");
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/**
* Return statement
*/ return true;
}; ////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/***/ }),
/***/ 4108:
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ "default": () => (/* binding */ handler)
/* harmony export */ });
/* harmony import */ var _functions_backend_admin_addDocPage__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1689);
/* harmony import */ var _functions_backend_admin_addDocPage__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_admin_addDocPage__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var _functions_backend_admin_deleteDocPage__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(6672);
/* harmony import */ var _functions_backend_admin_deleteDocPage__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_admin_deleteDocPage__WEBPACK_IMPORTED_MODULE_1__);
/* harmony import */ var _functions_backend_admin_getSnippets__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(2755);
/* harmony import */ var _functions_backend_admin_getSnippets__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_admin_getSnippets__WEBPACK_IMPORTED_MODULE_2__);
/* harmony import */ var _functions_backend_admin_grabChildPages__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(5217);
/* harmony import */ var _functions_backend_admin_grabChildPages__WEBPACK_IMPORTED_MODULE_3___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_admin_grabChildPages__WEBPACK_IMPORTED_MODULE_3__);
/* harmony import */ var _functions_backend_admin_grabParentPages__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(5030);
/* harmony import */ var _functions_backend_admin_grabParentPages__WEBPACK_IMPORTED_MODULE_4___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_admin_grabParentPages__WEBPACK_IMPORTED_MODULE_4__);
/* harmony import */ var _functions_backend_admin_persistDocs__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(4751);
/* harmony import */ var _functions_backend_admin_persistDocs__WEBPACK_IMPORTED_MODULE_5___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_admin_persistDocs__WEBPACK_IMPORTED_MODULE_5__);
/* harmony import */ var _functions_backend_suAdminUserAuth__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(4432);
/* harmony import */ var _functions_backend_suAdminUserAuth__WEBPACK_IMPORTED_MODULE_6___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_suAdminUserAuth__WEBPACK_IMPORTED_MODULE_6__);
// @ts-check
/** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /**
* API handler
* ==============================================================================
* @type {import("next").NextApiHandler}
*/ async function handler(req, res) {
/**
* Check method
*
* @description Check request method and return if invalid
*/ if (req.method !== "POST") return res.json({
success: false,
msg: "Wrong Method"
});
/**
* Validate Form
*
* @description Check if request body is valid
*/ const user = await _functions_backend_suAdminUserAuth__WEBPACK_IMPORTED_MODULE_6___default()(req);
if (!user?.logged_in_status) {
return res.json({
success: false,
log: "Unauthorized"
});
}
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
/**
* Validate Form
*
* @description Check if request body is valid
*/ const { action } = req.query;
const reqBody = req.body;
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
try {
/**
* Validate Form
*
* @description Check if request body is valid
*/ let result;
switch(action){
case "add-page":
result = await _functions_backend_admin_addDocPage__WEBPACK_IMPORTED_MODULE_0___default()({
body: reqBody
});
break;
case "update-page":
result = await _functions_backend_admin_addDocPage__WEBPACK_IMPORTED_MODULE_0___default()({
body: reqBody,
update: true
});
break;
case "delete-page":
result = await _functions_backend_admin_deleteDocPage__WEBPACK_IMPORTED_MODULE_1___default()({
body: reqBody
});
break;
case "get-snippets":
result = await _functions_backend_admin_getSnippets__WEBPACK_IMPORTED_MODULE_2___default()();
break;
case "grab-parent-pages":
result = await _functions_backend_admin_grabParentPages__WEBPACK_IMPORTED_MODULE_4___default()({
body: reqBody
});
break;
case "get-child-pages":
result = await _functions_backend_admin_grabChildPages__WEBPACK_IMPORTED_MODULE_3___default()({
body: reqBody
});
break;
case "persist":
result = await _functions_backend_admin_persistDocs__WEBPACK_IMPORTED_MODULE_5___default()({
body: reqBody
});
break;
default:
break;
}
if (!result) throw new Error("Operation failed!");
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
res.json({
success: true,
result
});
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
} catch (error) {
res.json({
success: false
});
}
}
/***/ })
};
;
// load runtime
var __webpack_require__ = require("../../../../webpack-api-runtime.js");
__webpack_require__.C(exports);
var __webpack_exec__ = (moduleId) => (__webpack_require__(__webpack_require__.s = moduleId))
var __webpack_exports__ = __webpack_require__.X(0, [5425,2224,3017,3403,7547,5886,5338,4432], () => (__webpack_exec__(4108)));
module.exports = __webpack_exports__;
})();