2024-12-06 10:31:24 +00:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
const fs = require("fs");
|
|
|
|
const path = require("path");
|
|
|
|
const { execSync } = require("child_process");
|
|
|
|
const serverError = require("./serverError");
|
|
|
|
const DB_HANDLER = require("../../utils/backend/global-db/DB_HANDLER");
|
|
|
|
const { default: grabUserSchemaData } = require("./grabUserSchemaData");
|
|
|
|
const { default: setUserSchemaData } = require("./setUserSchemaData");
|
|
|
|
const addDbEntry = require("./db/addDbEntry");
|
|
|
|
const createDbFromSchema = require("../../shell/createDbFromSchema");
|
2024-12-06 11:55:03 +00:00
|
|
|
const LOCAL_DB_HANDLER = require("../../utils/backend/global-db/LOCAL_DB_HANDLER");
|
2024-12-06 10:31:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* # Add User Table to Database
|
|
|
|
*
|
|
|
|
* @param {object} params
|
|
|
|
* @param {number} params.userId - user id
|
|
|
|
* @param {string} params.database
|
2024-12-06 11:55:03 +00:00
|
|
|
* @param {boolean} [params.useLocal]
|
2024-12-06 10:31:24 +00:00
|
|
|
*
|
|
|
|
* @returns {Promise<any>} new user auth object payload
|
|
|
|
*/
|
2024-12-06 11:55:03 +00:00
|
|
|
module.exports = async function addUsersTableToDb({
|
|
|
|
userId,
|
|
|
|
database,
|
|
|
|
useLocal,
|
|
|
|
}) {
|
2024-12-06 10:31:24 +00:00
|
|
|
/**
|
|
|
|
* Initialize
|
|
|
|
*
|
|
|
|
* @description Initialize
|
|
|
|
*/
|
|
|
|
const dbFullName = `datasquirel_user_${userId}_${database}`;
|
|
|
|
/** @type {import("../../types").DSQL_TableSchemaType} */
|
|
|
|
const userPreset = require("../../data/presets/users.json");
|
|
|
|
|
|
|
|
try {
|
|
|
|
/**
|
|
|
|
* Fetch user
|
|
|
|
*
|
|
|
|
* @description Fetch user from db
|
|
|
|
*/
|
|
|
|
const userSchemaData = grabUserSchemaData({ userId });
|
|
|
|
if (!userSchemaData) throw new Error("User schema data not found!");
|
|
|
|
|
|
|
|
let targetDatabase = userSchemaData.filter(
|
|
|
|
(db) => db.dbSlug === database
|
|
|
|
)[0];
|
|
|
|
|
|
|
|
let existingTableIndex;
|
|
|
|
// @ts-ignore
|
|
|
|
let existingTable = targetDatabase.tables.filter((table, index) => {
|
|
|
|
if (table.tableName === "users") {
|
|
|
|
existingTableIndex = index;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (existingTable && existingTable[0] && existingTableIndex) {
|
|
|
|
targetDatabase.tables[existingTableIndex] = userPreset;
|
|
|
|
} else {
|
|
|
|
targetDatabase.tables.push(userPreset);
|
|
|
|
}
|
|
|
|
|
|
|
|
setUserSchemaData({ schemaData: userSchemaData, userId });
|
|
|
|
|
2024-12-06 11:55:03 +00:00
|
|
|
const targetDb = useLocal
|
|
|
|
? await LOCAL_DB_HANDLER(
|
|
|
|
`SELECT id FROM user_databases WHERE user_id=? AND db_slug=?`,
|
|
|
|
[userId, database]
|
|
|
|
)
|
|
|
|
: await DB_HANDLER(
|
|
|
|
`SELECT id FROM user_databases WHERE user_id=? AND db_slug=?`,
|
|
|
|
[userId, database]
|
|
|
|
);
|
2024-12-06 10:31:24 +00:00
|
|
|
|
|
|
|
if (targetDb && targetDb[0]) {
|
|
|
|
const newTableEntry = await addDbEntry({
|
|
|
|
dbFullName: "datasquirel",
|
|
|
|
tableName: "user_database_tables",
|
|
|
|
data: {
|
|
|
|
user_id: userId,
|
|
|
|
db_id: targetDb[0].id,
|
|
|
|
db_slug: database,
|
|
|
|
table_name: "Users",
|
|
|
|
table_slug: "users",
|
|
|
|
},
|
2024-12-06 11:55:03 +00:00
|
|
|
useLocal,
|
2024-12-06 10:31:24 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const dbShellUpdate = await createDbFromSchema({
|
|
|
|
userId,
|
|
|
|
targetDatabase: dbFullName,
|
|
|
|
});
|
|
|
|
|
|
|
|
return `Done!`;
|
|
|
|
} catch (/** @type {any} */ error) {
|
|
|
|
serverError({
|
|
|
|
component: "addUsersTableToDb",
|
|
|
|
message: error.message,
|
|
|
|
user: { id: userId },
|
|
|
|
});
|
|
|
|
return error.message;
|
|
|
|
}
|
|
|
|
};
|