"use strict";
(() => {
var exports = {};
exports.id = 1176;
exports.ids = [1176];
exports.modules = {

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

module.exports = require("lodash");

/***/ }),

/***/ 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");

/***/ }),

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

module.exports = require("http");

/***/ }),

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

module.exports = require("path");

/***/ }),

/***/ 3802:
/***/ ((__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_userAuth__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(6825);
/* harmony import */ var _functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var _functions_backend_serverError__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(2163);
/* harmony import */ var _functions_backend_serverError__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_serverError__WEBPACK_IMPORTED_MODULE_1__);
/* harmony import */ var lodash__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(6517);
/* harmony import */ var lodash__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(lodash__WEBPACK_IMPORTED_MODULE_2__);
/* harmony import */ var _package_shared_utils_backend_global_db_DB_HANDLER__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(2224);
/* harmony import */ var _package_shared_utils_backend_global_db_DB_HANDLER__WEBPACK_IMPORTED_MODULE_3___default = /*#__PURE__*/__webpack_require__.n(_package_shared_utils_backend_global_db_DB_HANDLER__WEBPACK_IMPORTED_MODULE_3__);
/* harmony import */ var path__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(1017);
/* harmony import */ var path__WEBPACK_IMPORTED_MODULE_4___default = /*#__PURE__*/__webpack_require__.n(path__WEBPACK_IMPORTED_MODULE_4__);
/* harmony import */ var child_process__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(2081);
/* harmony import */ var child_process__WEBPACK_IMPORTED_MODULE_5___default = /*#__PURE__*/__webpack_require__.n(child_process__WEBPACK_IMPORTED_MODULE_5__);
// @ts-check
const fs = __webpack_require__(7147);
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////






/** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /**
 * 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({
        user: null,
        msg: "Registration Failed!"
    });
    /**
     * User Auth
     *
     * @description User Auth
     */ const user = await _functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_0___default()(req, res, true);
    if (!user) {
        return res.json({
            success: false,
            msg: "Unauthorized"
        });
    }
    /**
     * Validate Form
     *
     * @description Check if request body is valid
     */ /** @type {import("@/package-shared/types").DSQL_DatabaseSchemaType} */ const schema = req.body.schema;
    const dbId = req.body.dbId;
    /**
     * Validate Form
     *
     * @description Check if request body is valid
     */ try {
        /**
         * @type {import("@/package-shared/types").DSQL_MYSQL_user_databases_Type[] | null}
         */ const targetDBArray = await _package_shared_utils_backend_global_db_DB_HANDLER__WEBPACK_IMPORTED_MODULE_3___default()(`SELECT * FROM user_databases WHERE user_id = ?  AND id = ?`, [
            user.id,
            dbId
        ]);
        if (!targetDBArray?.[0]) {
            throw new Error("Invalid Database");
        }
        const targetDb = targetDBArray[0];
        const userSchemaPath = path__WEBPACK_IMPORTED_MODULE_4___default().resolve(process.cwd(), `${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${user.id}/main.json`);
        /** @type {import("@/package-shared/types").DSQL_DatabaseSchemaType[]} */ const userSchema = JSON.parse(fs.readFileSync(userSchemaPath, "utf-8"));
        const targetDatabase = userSchema.find((db)=>db.dbFullName == targetDb.db_full_name);
        if (!targetDatabase) {
            throw new Error("This Database doesn't exist");
        }
        targetDatabase.tables = schema.tables;
        if (targetDatabase.childrenDatabases) {
            for(let i = 0; i < targetDatabase.childrenDatabases.length; i++){
                const childDb = targetDatabase.childrenDatabases[i];
                const { dbFullName  } = childDb;
                const targetChildDb = userSchema.find((db)=>db.dbFullName === dbFullName);
                if (!targetChildDb || !targetChildDb.tables) {
                    targetDatabase.childrenDatabases?.splice(i, 1);
                    continue;
                }
                targetChildDb.tables = schema.tables;
            }
        }
        fs.writeFileSync(`${userSchemaPath}`, JSON.stringify(userSchema), "utf8");
        ////////////////////////////////////////
        ////////////////////////////////////////
        ////////////////////////////////////////
        const targetShellPath = path__WEBPACK_IMPORTED_MODULE_4___default().join(process.cwd(), "/shell");
        const dbShellUpdate = (0,child_process__WEBPACK_IMPORTED_MODULE_5__.execSync)(`node createDbFromSchema.js --user ${user.id} --database ${targetDb.db_full_name}`, {
            cwd: targetShellPath
        });
        console.log(dbShellUpdate.toString());
        ////////////////////////////////////////
        ////////////////////////////////////////
        ////////////////////////////////////////
        res.json({
            success: true
        });
    ////////////////////////////////////////
    ////////////////////////////////////////
    ////////////////////////////////////////
    } catch (/** @type {any} */ error) {
        _functions_backend_serverError__WEBPACK_IMPORTED_MODULE_1___default()({
            component: "/api/acceptUserInvitation/catch-error",
            message: error.message,
            user: user
        });
        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, [2224,2163,6825], () => (__webpack_exec__(3802)));
module.exports = __webpack_exports__;

})();