"use strict";
(() => {
var exports = {};
exports.id = 3432;
exports.ids = [3432];
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");

/***/ }),

/***/ 2081:
/***/ ((module) => {

module.exports = require("child_process");

/***/ }),

/***/ 6113:
/***/ ((module) => {

module.exports = require("crypto");

/***/ }),

/***/ 7147:
/***/ ((module) => {

module.exports = require("fs");

/***/ }),

/***/ 1017:
/***/ ((module) => {

module.exports = require("path");

/***/ }),

/***/ 5830:
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {

__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "config": () => (/* binding */ config),
/* harmony export */   "default": () => (/* binding */ handler)
/* harmony export */ });
/* harmony import */ var lodash__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(6517);
/* harmony import */ var lodash__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(lodash__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var _package_shared_functions_backend_db_runQuery__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(8499);
/* harmony import */ var _package_shared_functions_backend_db_runQuery__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_package_shared_functions_backend_db_runQuery__WEBPACK_IMPORTED_MODULE_1__);
/* harmony import */ var _package_shared_functions_backend_decrypt__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(5425);
/* harmony import */ var _package_shared_functions_backend_decrypt__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(_package_shared_functions_backend_decrypt__WEBPACK_IMPORTED_MODULE_2__);
/* harmony import */ var _functions_backend_serverError__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(2163);
/* harmony import */ var _functions_backend_serverError__WEBPACK_IMPORTED_MODULE_3___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_serverError__WEBPACK_IMPORTED_MODULE_3__);
/* harmony import */ var _package_shared_functions_backend_api_cred__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(1007);
/* harmony import */ var _package_shared_functions_backend_api_cred__WEBPACK_IMPORTED_MODULE_4___default = /*#__PURE__*/__webpack_require__.n(_package_shared_functions_backend_api_cred__WEBPACK_IMPORTED_MODULE_4__);
// @ts-check
/**
 * ==============================================================================
 * Imports
 * ==============================================================================
 */ const fs = __webpack_require__(7147);
const path = __webpack_require__(1017);
const { execSync  } = __webpack_require__(2081);





/** ****************************************************************************** */ const config = {
    api: {
        bodyParser: {
            sizeLimit: "50mb"
        }
    }
};
/** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** @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({
        msg: "Failed!"
    });
    /**
     * Send Response
     *
     * @description Send a boolean response
     */ let results;
    try {
        /**
         * User auth
         *
         * @description Authenticate user
         */ /**
         * Grab Body
         */ let { query , database , tableName , queryValues  } = req.body;
        const authorization = req.headers.authorization;
        const apiCred = _package_shared_functions_backend_api_cred__WEBPACK_IMPORTED_MODULE_4___default()({
            key: authorization,
            database: database,
            table: tableName
        });
        if (!apiCred?.user_id) {
            throw new Error("Api Credentials invalid!");
        }
        const { user_id , full_access  } = apiCred;
        if (!full_access) return res.json({
            success: false,
            msg: "Unauthorized"
        });
        const dbFullName = `datasquirel_user_${user_id}_${database}`;
        /**
         * Input Validation
         *
         * @description Input Validation
         */ if (typeof query === "string" && query?.match(/^create |^alter |^drop /i)) {
            return res.json({
                success: false,
                msg: "Wrong Input"
            });
        }
        if (typeof query === "object" && query?.action?.match(/^create |^alter |^drop /i)) {
            return res.json({
                success: false,
                msg: "Wrong Input"
            });
        }
        /**
         * Grab db Schema
         */ /** @type {import("@/package-shared/types").DSQL_DatabaseSchemaType | undefined} */ let dbSchema;
        const targetDbSchemaPath = `${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${user_id.toString().replace(/\//g, "")}/main.json`;
        if (fs.existsSync(targetDbSchemaPath)) {
            try {
                dbSchema = JSON.parse(fs.readFileSync(targetDbSchemaPath, "utf8")).filter((/** @type {any} */ db)=>db.dbFullName === dbFullName)[0];
            } catch (_err) {}
        }
        /**
         * Create new user folder and file
         *
         * @description Create new user folder and file
         */ try {
            let { result , error  } = await _package_shared_functions_backend_db_runQuery__WEBPACK_IMPORTED_MODULE_1___default()({
                dbFullName: dbFullName,
                query: query,
                dbSchema: dbSchema,
                queryValuesArray: queryValues,
                tableName
            });
            results = result;
            if (error) throw error;
            /** @type {import("@/package-shared/types").DSQL_TableSchemaType | undefined} */ let tableSchema;
            if (dbSchema) {
                const targetTable = dbSchema.tables.find((table)=>table.tableName === tableName);
                if (targetTable) {
                    const clonedTargetTable = lodash__WEBPACK_IMPORTED_MODULE_0___default().cloneDeep(targetTable);
                    delete clonedTargetTable.childTable;
                    delete clonedTargetTable.childTableDbFullName;
                    delete clonedTargetTable.childTableName;
                    delete clonedTargetTable.childrenTables;
                    delete clonedTargetTable.updateData;
                    delete clonedTargetTable.tableNameOld;
                    delete clonedTargetTable.indexes;
                    tableSchema = clonedTargetTable;
                }
            }
            res.json({
                success: true,
                payload: results,
                error: error,
                schema: tableName && tableSchema ? tableSchema : undefined
            });
        ////////////////////////////////////////
        } catch (/** @type {any} */ error1) {
            _functions_backend_serverError__WEBPACK_IMPORTED_MODULE_3___default()({
                component: "/api/query/post/lines-132-142",
                message: error1.message
            });
            ////////////////////////////////////////
            res.json({
                success: false,
                payload: results,
                error: error1.message
            });
        }
    ////////////////////////////////////////
    } catch (/** @type {any} */ error2) {
        ////////////////////////////////////////
        _functions_backend_serverError__WEBPACK_IMPORTED_MODULE_3___default()({
            component: "/api/query/post/main-catch-error",
            message: error2.message
        });
        res.json({
            success: false,
            msg: "Wrong Credentials"
        });
    ////////////////////////////////////////
    }
}


/***/ })

};
;

// 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, [2224,2163,3017,3403,7547,5886,5338,8326,1007,6147,4733], () => (__webpack_exec__(5830)));
module.exports = __webpack_exports__;

})();