2024-12-06 13:24:26 +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");
|
|
|
|
const LOCAL_DB_HANDLER = require("../../utils/backend/global-db/LOCAL_DB_HANDLER");
|
2024-12-08 08:57:48 +00:00
|
|
|
const grabNewUsersTableSchema = require("./grabNewUsersTableSchema");
|
2024-12-06 13:24:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* # Add User Table to Database
|
|
|
|
*
|
|
|
|
* @param {object} params
|
|
|
|
* @param {number} params.userId - user id
|
|
|
|
* @param {string} params.database
|
|
|
|
* @param {boolean} [params.useLocal]
|
2024-12-08 08:57:48 +00:00
|
|
|
* @param {Object<string, any>} [params.payload] - payload object
|
2024-12-06 13:24:26 +00:00
|
|
|
*
|
|
|
|
* @returns {Promise<any>} new user auth object payload
|
|
|
|
*/
|
|
|
|
module.exports = async function addUsersTableToDb({
|
|
|
|
userId,
|
|
|
|
database,
|
|
|
|
useLocal,
|
2024-12-08 08:57:48 +00:00
|
|
|
payload,
|
2024-12-06 13:24:26 +00:00
|
|
|
}) {
|
|
|
|
try {
|
2024-12-08 08:57:48 +00:00
|
|
|
const dbFullName = database;
|
|
|
|
|
|
|
|
const userPreset = grabNewUsersTableSchema({ payload });
|
|
|
|
if (!userPreset) throw new Error("Couldn't Get User Preset!");
|
|
|
|
|
2024-12-06 13:24:26 +00:00
|
|
|
const userSchemaData = grabUserSchemaData({ userId });
|
|
|
|
if (!userSchemaData) throw new Error("User schema data not found!");
|
|
|
|
|
2024-12-08 08:57:48 +00:00
|
|
|
let targetDatabase = userSchemaData.find(
|
|
|
|
(db) => db.dbFullName === database
|
|
|
|
);
|
2024-12-06 13:24:26 +00:00
|
|
|
|
2024-12-08 08:57:48 +00:00
|
|
|
if (!targetDatabase) {
|
|
|
|
throw new Error("Couldn't Find Target Database!");
|
|
|
|
}
|
|
|
|
|
|
|
|
let existingTableIndex = targetDatabase?.tables.findIndex(
|
|
|
|
(table) => table.tableName === "users"
|
|
|
|
);
|
2024-12-06 13:24:26 +00:00
|
|
|
|
2024-12-08 08:57:48 +00:00
|
|
|
if (typeof existingTableIndex == "number" && existingTableIndex > 0) {
|
2024-12-06 13:24:26 +00:00
|
|
|
targetDatabase.tables[existingTableIndex] = userPreset;
|
|
|
|
} else {
|
|
|
|
targetDatabase.tables.push(userPreset);
|
|
|
|
}
|
|
|
|
|
|
|
|
setUserSchemaData({ schemaData: userSchemaData, userId });
|
|
|
|
|
2024-12-08 08:57:48 +00:00
|
|
|
/** @type {any[] | null} */
|
2024-12-06 13:24:26 +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-08 08:57:48 +00:00
|
|
|
if (targetDb?.[0]) {
|
2024-12-06 13:24:26 +00:00
|
|
|
const newTableEntry = await addDbEntry({
|
|
|
|
dbFullName: "datasquirel",
|
|
|
|
tableName: "user_database_tables",
|
|
|
|
data: {
|
|
|
|
user_id: userId,
|
|
|
|
db_id: targetDb[0].id,
|
2024-12-08 08:57:48 +00:00
|
|
|
db_slug: targetDatabase.dbSlug,
|
2024-12-06 13:24:26 +00:00
|
|
|
table_name: "Users",
|
|
|
|
table_slug: "users",
|
|
|
|
},
|
|
|
|
useLocal,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const dbShellUpdate = await createDbFromSchema({
|
|
|
|
userId,
|
|
|
|
targetDatabase: dbFullName,
|
|
|
|
});
|
|
|
|
|
|
|
|
return `Done!`;
|
|
|
|
} catch (/** @type {any} */ error) {
|
2024-12-08 08:57:48 +00:00
|
|
|
console.log(`addUsersTableToDb.js ERROR: ${error.message}`);
|
|
|
|
|
2024-12-06 13:24:26 +00:00
|
|
|
serverError({
|
|
|
|
component: "addUsersTableToDb",
|
|
|
|
message: error.message,
|
|
|
|
user: { id: userId },
|
|
|
|
});
|
|
|
|
return error.message;
|
|
|
|
}
|
|
|
|
};
|