357 lines
12 KiB
JavaScript
357 lines
12 KiB
JavaScript
"use strict";
|
|
(() => {
|
|
var exports = {};
|
|
exports.id = 4266;
|
|
exports.ids = [4266];
|
|
exports.modules = {
|
|
|
|
/***/ 6517:
|
|
/***/ ((module) => {
|
|
|
|
module.exports = require("lodash");
|
|
|
|
/***/ }),
|
|
|
|
/***/ 6109:
|
|
/***/ ((module) => {
|
|
|
|
module.exports = require("sanitize-html");
|
|
|
|
/***/ }),
|
|
|
|
/***/ 2261:
|
|
/***/ ((module) => {
|
|
|
|
module.exports = require("serverless-mysql");
|
|
|
|
/***/ }),
|
|
|
|
/***/ 7441:
|
|
/***/ ((module) => {
|
|
|
|
module.exports = require("sharp");
|
|
|
|
/***/ }),
|
|
|
|
/***/ 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");
|
|
|
|
/***/ }),
|
|
|
|
/***/ 6339:
|
|
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
|
|
|
|
// ESM COMPAT FLAG
|
|
__webpack_require__.r(__webpack_exports__);
|
|
|
|
// EXPORTS
|
|
__webpack_require__.d(__webpack_exports__, {
|
|
"default": () => (/* binding */ handler)
|
|
});
|
|
|
|
// EXTERNAL MODULE: ./package-shared/functions/backend/encrypt.js
|
|
var encrypt = __webpack_require__(7547);
|
|
// EXTERNAL MODULE: ./functions/backend/fsWriteImageToDiskFromBase64.js
|
|
var fsWriteImageToDiskFromBase64 = __webpack_require__(5910);
|
|
// EXTERNAL MODULE: ./functions/backend/serverError.js
|
|
var serverError = __webpack_require__(2163);
|
|
var serverError_default = /*#__PURE__*/__webpack_require__.n(serverError);
|
|
// EXTERNAL MODULE: ./functions/backend/userAuth.js
|
|
var userAuth = __webpack_require__(6825);
|
|
var userAuth_default = /*#__PURE__*/__webpack_require__.n(userAuth);
|
|
// EXTERNAL MODULE: external "lodash"
|
|
var external_lodash_ = __webpack_require__(6517);
|
|
var external_lodash_default = /*#__PURE__*/__webpack_require__.n(external_lodash_);
|
|
// EXTERNAL MODULE: ./package-shared/utils/backend/global-db/DB_HANDLER.js
|
|
var DB_HANDLER = __webpack_require__(2224);
|
|
var DB_HANDLER_default = /*#__PURE__*/__webpack_require__.n(DB_HANDLER);
|
|
// EXTERNAL MODULE: ./functions/backend/importExportTableDataFn.js
|
|
var importExportTableDataFn = __webpack_require__(2630);
|
|
// EXTERNAL MODULE: ./functions/backend/grabDelegatedUserFromCookie.js
|
|
var grabDelegatedUserFromCookie = __webpack_require__(1352);
|
|
;// CONCATENATED MODULE: ./functions/backend/delegatedUser.js
|
|
// @ts-check
|
|
/**
|
|
* ==============================================================================
|
|
* Imports
|
|
* ==============================================================================
|
|
*/ const http = __webpack_require__(3685);
|
|
const delegatedUser_serverError = __webpack_require__(2163);
|
|
const delegatedUser_DB_HANDLER = __webpack_require__(2224);
|
|
/** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /**
|
|
* @typedef {object} delegatedUserTableObject
|
|
* @property {string} db
|
|
* @property {string} table
|
|
*/ /**
|
|
* ==============================================================================
|
|
* Main Function
|
|
* ==============================================================================
|
|
* @async
|
|
*
|
|
* @param {object} params
|
|
* @param {delegatedUserTableObject[]} params.tbls - Database name slug
|
|
* @param {{ id: number, first_name: string, last_name: string }} params.user - user object
|
|
* @param {any} params.query - query params
|
|
*
|
|
* @returns {Promise<{
|
|
* isPriviledged: boolean,
|
|
* rootUserId?: number,
|
|
* error?: string,
|
|
* }>} new user auth object payload
|
|
*/ async function delegatedUser({ tbls , user , query }) {
|
|
/**
|
|
* Fetch user
|
|
*
|
|
* @description Fetch user from db
|
|
*/ let rootUserId;
|
|
if (!query?.delegated) {
|
|
return {
|
|
isPriviledged: true,
|
|
rootUserId: user.id
|
|
};
|
|
}
|
|
try {
|
|
for(let i = 0; i < tbls.length; i++){
|
|
const obj = tbls[i];
|
|
const check = await delegatedUser_DB_HANDLER(`SELECT * FROM delegated_user_tables WHERE delegated_user_id='${user.id}' AND \`database\`='${obj.db}' AND \`table\`='${obj.table}'`);
|
|
if (!check?.[0]?.root_user_id) throw new Error("Unauthorized!");
|
|
const delegateObj = check[0];
|
|
if (!rootUserId) rootUserId = delegateObj.root_user_id;
|
|
}
|
|
return {
|
|
isPriviledged: true,
|
|
rootUserId
|
|
};
|
|
} catch (/** @type {any} */ error) {
|
|
return {
|
|
isPriviledged: false,
|
|
rootUserId,
|
|
error: error.message
|
|
};
|
|
}
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
} ////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
|
|
// EXTERNAL MODULE: ./functions/backend/grabUserSchemaData.js
|
|
var grabUserSchemaData = __webpack_require__(8164);
|
|
;// CONCATENATED MODULE: ./pages/api/sync-table-data.js
|
|
// @ts-check
|
|
/**
|
|
* ==============================================================================
|
|
* Imports
|
|
* ==============================================================================
|
|
*/ const fs = __webpack_require__(7147);
|
|
const path = __webpack_require__(1017);
|
|
const { execSync } = __webpack_require__(2081);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /**
|
|
* @typedef {object} SyncTableBodySrcDstObject
|
|
* @property {string} db
|
|
* @property {string} table
|
|
*/ /**
|
|
* @typedef {object} SyncTableBody
|
|
* @property {"Pull" | "Push"} paradigm
|
|
* @property {SyncTableBodySrcDstObject} src
|
|
* @property {SyncTableBodySrcDstObject} dst
|
|
* @property {any} [delegated]
|
|
*/ /**
|
|
* 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({
|
|
msg: "Failed!"
|
|
});
|
|
/**
|
|
* User auth
|
|
*
|
|
* @description Authenticate user
|
|
*/ const user = await userAuth_default()(req, res, true);
|
|
if (!user) {
|
|
return res.json({
|
|
success: false,
|
|
msg: "Unauthorized"
|
|
});
|
|
}
|
|
/**
|
|
* @type {SyncTableBody}
|
|
*/ const { paradigm , src , dst } = req.body;
|
|
/**
|
|
* Send Response
|
|
*
|
|
* @description Send a boolean response
|
|
*/ try {
|
|
const delegatedUserObj = await delegatedUser({
|
|
tbls: [
|
|
{
|
|
db: src.db,
|
|
table: src.table
|
|
},
|
|
{
|
|
db: dst.db,
|
|
table: dst.table
|
|
},
|
|
],
|
|
user,
|
|
query: req.query
|
|
});
|
|
if (!delegatedUserObj.rootUserId || !delegatedUserObj.isPriviledged) throw new Error(delegatedUserObj.error || "Unauthorized!");
|
|
const userDbSchemaData = (0,grabUserSchemaData["default"])({
|
|
userId: delegatedUserObj.rootUserId
|
|
});
|
|
if (!userDbSchemaData) throw new Error("User Schema Data not Found!");
|
|
const dstDbName = `${process.env.DSQL_USER_DB_PREFIX}${delegatedUserObj.rootUserId}_${dst.db}`;
|
|
const srcDbName = `${process.env.DSQL_USER_DB_PREFIX}${delegatedUserObj.rootUserId}_${src.db}`;
|
|
/** @type {import("@/package-shared/types").DSQL_DatabaseSchemaType | undefined} */ const dstDbSchema = userDbSchemaData.find((db)=>db.dbFullName == dstDbName);
|
|
/** @type {import("@/package-shared/types").DSQL_DatabaseSchemaType | undefined} */ const srcDbSchema = userDbSchemaData.find((db)=>db.dbFullName == srcDbName);
|
|
const dstTableSchema = dstDbSchema?.tables.find((tbl)=>tbl.tableName == dst.table);
|
|
const srcTableSchema = srcDbSchema?.tables.find((tbl)=>tbl.tableName == src.table);
|
|
if (!dstTableSchema || !srcTableSchema) throw new Error("Operation Failed! Tables Schemas not present.");
|
|
const isEqual = external_lodash_default().isEqual(srcTableSchema.fields, dstTableSchema.fields);
|
|
if (!isEqual) throw new Error("Tables incompatible");
|
|
switch(paradigm){
|
|
case "Push":
|
|
const srcData = await DB_HANDLER_default()(`SELECT * FROM ${srcDbName}.${src.table}`);
|
|
const pushData = await (0,importExportTableDataFn/* default */.Z)({
|
|
dbName: dst.db,
|
|
paradigm: "import",
|
|
payload: srcData,
|
|
tableName: dst.table,
|
|
userId: delegatedUserObj.rootUserId,
|
|
payloadType: "object"
|
|
});
|
|
await pruneTable({
|
|
dbName: dst.db,
|
|
tableName: dst.table,
|
|
data: srcData,
|
|
userId: delegatedUserObj.rootUserId
|
|
});
|
|
return res.json({
|
|
success: true,
|
|
payload: pushData
|
|
});
|
|
case "Pull":
|
|
const dstData = await DB_HANDLER_default()(`SELECT * FROM ${dstDbName}.${dst.table}`);
|
|
const pullData = await (0,importExportTableDataFn/* default */.Z)({
|
|
dbName: src.db,
|
|
paradigm: "import",
|
|
payload: dstData,
|
|
tableName: src.table,
|
|
userId: delegatedUserObj.rootUserId,
|
|
payloadType: "object"
|
|
});
|
|
await pruneTable({
|
|
dbName: src.db,
|
|
tableName: src.table,
|
|
data: dstData,
|
|
userId: delegatedUserObj.rootUserId
|
|
});
|
|
return res.json({
|
|
success: true,
|
|
payload: pullData
|
|
});
|
|
default:
|
|
break;
|
|
}
|
|
res.json({
|
|
success: false
|
|
});
|
|
////////////////////////////////////////
|
|
} catch (/** @type {any} */ error) {
|
|
console.log(error.message);
|
|
////////////////////////////////////////
|
|
serverError_default()({
|
|
component: "/api/sync-table-data",
|
|
message: error.message,
|
|
user: user
|
|
});
|
|
res.json({
|
|
success: false,
|
|
msg: error.message
|
|
});
|
|
////////////////////////////////////////
|
|
}
|
|
}
|
|
/**
|
|
*
|
|
* @param {object} param0
|
|
* @param {string | number} param0.userId
|
|
* @param {string} param0.dbName
|
|
* @param {string} param0.tableName
|
|
* @param {any[]} param0.data
|
|
*/ async function pruneTable({ dbName , tableName , data , userId }) {
|
|
const dbFullName = `${process.env.DSQL_USER_DB_PREFIX}${userId}_${dbName.replace(/ /g, "")}`;
|
|
const ids = data.map((dt)=>dt.id);
|
|
let query = `DELETE FROM ${dbFullName}.${tableName}`;
|
|
if (ids?.[0]) query += ` WHERE id NOT IN (${ids.join(",")})`;
|
|
const prune = await DB_HANDLER_default()(query);
|
|
}
|
|
|
|
|
|
/***/ })
|
|
|
|
};
|
|
;
|
|
|
|
// 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,3017,3403,7547,5886,5338,1352,6715,8164,5910,2630], () => (__webpack_exec__(6339)));
|
|
module.exports = __webpack_exports__;
|
|
|
|
})(); |