diff --git a/engine/engine/recoverMainJsonFromDb.js b/engine/engine/recoverMainJsonFromDb.js index e5b4d8f..854ef5b 100644 --- a/engine/engine/recoverMainJsonFromDb.js +++ b/engine/engine/recoverMainJsonFromDb.js @@ -1,13 +1,12 @@ +// @ts-check + const fs = require("fs"); require("dotenv").config({ path: "./../.env" }); //////////////////////////////////////// -const noDatabaseDbHandler = require("../functions/backend/noDatabaseDbHandler"); -const varDatabaseDbHandler = require("../functions/backend/varDatabaseDbHandler"); -const createTable = require("./utils/createTable"); -const slugToCamelTitle = require("./utils/slugToCamelTitle"); -const updateTable = require("./utils/updateTable"); +const dbHandler = require("./utils/dbHandler"); +const varDatabaseDbHandler = require("./utils/varDatabaseDbHandler"); /** ****************************************************************************** */ @@ -31,8 +30,12 @@ async function recoverMainJsonFromDb() { return; } - const databases = await global.DB_HANDLER(`SELECT * FROM user_databases WHERE user_id='${userId}'`); + const databases = await dbHandler({ + query: `SELECT * FROM user_databases WHERE user_id = ?`, + values: [userId], + }); + /** @type {*[]} */ const dbWrite = []; for (let i = 0; i < databases.length; i++) { @@ -44,10 +47,14 @@ async function recoverMainJsonFromDb() { dbFullName: db_full_name, dbDescription: db_description, dbImage: db_image, + /** @type {*[]} */ tables: [], }; - const tables = await global.DB_HANDLER(`SELECT * FROM user_database_tables WHERE user_id='${userId}' AND db_id='${id}'`); + const tables = await dbHandler({ + query: `SELECT * FROM user_database_tables WHERE user_id = ? AND db_id = ?`, + values: [userId, id], + }); for (let j = 0; j < tables.length; j++) { const { table_name, table_slug, table_description } = tables[j]; @@ -55,7 +62,9 @@ async function recoverMainJsonFromDb() { const tableObject = { tableName: table_slug, tableFullName: table_name, + /** @type {*[]} */ fields: [], + /** @type {*[]} */ indexes: [], }; @@ -64,24 +73,26 @@ async function recoverMainJsonFromDb() { queryString: `SHOW COLUMNS FROM ${table_slug}`, }); - for (let k = 0; k < tableFields.length; k++) { - const { Field, Type, Null, Default, Key } = tableFields[k]; + if (tableFields) { + for (let k = 0; k < tableFields.length; k++) { + const { Field, Type, Null, Default, Key } = tableFields[k]; - const fieldObject = { - fieldName: Field, - dataType: Type.toUpperCase(), - }; + const fieldObject = { + fieldName: Field, + dataType: Type.toUpperCase(), + }; - if (Default?.match(/./) && !Default?.match(/timestamp/i)) fieldObject["defaultValue"] = Default; - if (Key?.match(/pri/i)) { - fieldObject["primaryKey"] = true; - fieldObject["autoIncrement"] = true; + if (Default?.match(/./) && !Default?.match(/timestamp/i)) fieldObject["defaultValue"] = Default; + if (Key?.match(/pri/i)) { + fieldObject["primaryKey"] = true; + fieldObject["autoIncrement"] = true; + } + if (Default?.match(/timestamp/i)) fieldObject["defaultValueLiteral"] = Default; + if (Null?.match(/yes/i)) fieldObject["nullValue"] = true; + if (Null?.match(/no/i)) fieldObject["notNullValue"] = true; + + tableObject.fields.push(fieldObject); } - if (Default?.match(/timestamp/i)) fieldObject["defaultValueLiteral"] = Default; - if (Null?.match(/yes/i)) fieldObject["nullValue"] = true; - if (Null?.match(/no/i)) fieldObject["notNullValue"] = true; - - tableObject.fields.push(fieldObject); } dbObject.tables.push(tableObject); diff --git a/engine/engine/updateChildrenTablesOnDb.js b/engine/engine/updateChildrenTablesOnDb.js deleted file mode 100644 index 023ada8..0000000 --- a/engine/engine/updateChildrenTablesOnDb.js +++ /dev/null @@ -1,81 +0,0 @@ -const fs = require("fs"); -require("dotenv").config({ path: "./../.env" }); - -//////////////////////////////////////// - -const noDatabaseDbHandler = require("../functions/backend/noDatabaseDbHandler"); -const serverError = require("../functions/backend/serverError"); - -const varDatabaseDbHandler = require("../functions/backend/varDatabaseDbHandler"); - -/** ****************************************************************************** */ -/** ****************************************************************************** */ -/** ****************************************************************************** */ -/** ****************************************************************************** */ -/** ****************************************************************************** */ -/** ****************************************************************************** */ - -async function updateChildrenTablesOnDb() { - /** - * Grab Schema - * - * @description Grab Schema - */ - try { - const rootDir = "./../jsonData/dbSchemas/users"; - const userFolders = fs.readdirSync(rootDir); - - for (let i = 0; i < userFolders.length; i++) { - const folder = userFolders[i]; - const userId = folder.replace(/user-/, ""); - const databases = JSON.parse(fs.readFileSync(`${rootDir}/${folder}/main.json`)); - - for (let j = 0; j < databases.length; j++) { - const db = databases[j]; - const dbTables = db.tables; - for (let k = 0; k < dbTables.length; k++) { - const table = dbTables[k]; - - if (table?.childTable) { - const originTableName = table.childTableName; - const originDbName = table.childTableDbFullName; - - const WHERE_CLAUSE = `WHERE user_id='${userId}' AND db_slug='${db.dbSlug}' AND table_slug='${table.tableName}'`; - - const existingTableInDb = await global.DB_HANDLER(`SELECT * FROM user_database_tables ${WHERE_CLAUSE}`); - console.log(existingTableInDb); - - if (existingTableInDb && existingTableInDb[0]) { - const updateChildrenTablesInfo = await global.DB_HANDLER(`UPDATE user_database_tables SET child_table='1',child_table_parent_database='${originDbName}',child_table_parent_table='${originTableName}' WHERE id='${existingTableInDb[0].id}'`); - - console.log(updateChildrenTablesInfo); - } - } - } - } - } - } catch (error) { - console.log(error); - } - - //////////////////////////////////////// - //////////////////////////////////////// - //////////////////////////////////////// - - process.exit(); - - //////////////////////////////////////// - //////////////////////////////////////// - //////////////////////////////////////// -} - -/** ****************************************************************************** */ -/** ****************************************************************************** */ -/** ****************************************************************************** */ -/** ****************************************************************************** */ -/** ****************************************************************************** */ - -// const userArg = process.argv[process.argv.indexOf("--user")]; -// const externalUser = process.argv[process.argv.indexOf("--user") + 1]; - -updateChildrenTablesOnDb(); diff --git a/engine/user/social/utils/githubLogin.js b/engine/user/social/utils/githubLogin.js index 580392d..a01cd28 100644 --- a/engine/user/social/utils/githubLogin.js +++ b/engine/user/social/utils/githubLogin.js @@ -7,6 +7,7 @@ */ const fs = require("fs"); const httpsRequest = require("./httpsRequest"); +const dbHandler = require("../../../engine/utils/dbHandler"); ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// @@ -122,7 +123,10 @@ async function githubLogin({ code, clientId, clientSecret }) { //////////////////////////////////////////////// if (!gitHubUser?.email) { - const existingGithubUser = await global.DB_HANDLER(`SELECT email FROM users WHERE social_login='1' AND social_platform='github' AND social_id='${gitHubUser?.id || ""}'`); + const existingGithubUser = await dbHandler({ + query: `SELECT email FROM users WHERE social_login='1' AND social_platform='github' AND social_id= ?`, + values: [gitHubUser?.id || ""], + }); if (existingGithubUser && existingGithubUser[0] && gitHubUser) { gitHubUser.email = existingGithubUser[0].email; diff --git a/engine/user/social/utils/googleLogin.js b/engine/user/social/utils/googleLogin.js index 1081d55..6060e22 100644 --- a/engine/user/social/utils/googleLogin.js +++ b/engine/user/social/utils/googleLogin.js @@ -1,3 +1,5 @@ +// @ts-check + /** * ============================================================================== * Imports @@ -11,8 +13,8 @@ const fs = require("fs"); const { OAuth2Client } = require("google-auth-library"); -const { hashPassword } = require("../passwordHash"); -const serverError = require("../serverError"); +const dbHandler = require("../../../engine/utils/dbHandler"); +const hashPassword = require("../../../../functions/hashPassword"); ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// @@ -44,6 +46,7 @@ module.exports = async function googleLogin({ usertype, foundUser, isSocialValid //[CLIENT_ID_1, CLIENT_ID_2, CLIENT_ID_3] }); + // @ts-ignore const payload = ticket.payload; const userid = payload["sub"]; @@ -63,7 +66,10 @@ module.exports = async function googleLogin({ usertype, foundUser, isSocialValid //////////////////////////////////////////////// //////////////////////////////////////////////// - let existinEmail = await global.DB_HANDLER(`SELECT * FROM ${usertype} WHERE email='${payload.email}' AND social_login!='1' AND social_platform!='google'`); + let existinEmail = await dbHandler({ + query: `SELECT * FROM ${usertype} WHERE email = ? AND social_login!='1' AND social_platform!='google'`, + values: [payload.email], + }); if (existinEmail && existinEmail[0]) { loginFailureReason = "Email Exists Already"; @@ -73,7 +79,10 @@ module.exports = async function googleLogin({ usertype, foundUser, isSocialValid //////////////////////////////////////// - foundUser = await global.DB_HANDLER(`SELECT * FROM ${usertype} WHERE email='${payload.email}' AND social_login='1' AND social_platform='google'`); + foundUser = await dbHandler({ + query: `SELECT * FROM ${usertype} WHERE email = ? AND social_login='1' AND social_platform='google'`, + values: [payload.email], + }); if (foundUser && foundUser[0]) { newFoundUser = foundUser; @@ -84,50 +93,49 @@ module.exports = async function googleLogin({ usertype, foundUser, isSocialValid //////////////////////////////////////////////// //////////////////////////////////////////////// - let newUser = await global.DB_HANDLER(`INSERT INTO ${usertype} ( - first_name, - last_name, - social_platform, - social_name, - social_id, - email, - image, - image_thumbnail, - password, - verification_status, - social_login, - terms_agreement, - date_created, - date_code - ) VALUES ( - '${payload.given_name}', - '${payload.family_name}', - 'google', - 'google_${payload.email.replace(/@.*/, "")}', - '${payload.sub}', - '${payload.email}', - '${payload.picture}', - '${payload.picture}', - '${socialHashedPassword}', - '1', - '1', - '1', - '${Date()}', - '${Date.now()}' - )`); + let newUser = await dbHandler({ + query: `INSERT INTO ${usertype} ( + first_name, + last_name, + social_platform, + social_name, + social_id, + email, + image, + image_thumbnail, + password, + verification_status, + social_login, + terms_agreement, + date_created, + date_code + ) VALUES ( + '${payload.given_name}', + '${payload.family_name}', + 'google', + 'google_${payload.email.replace(/@.*/, "")}', + '${payload.sub}', + '${payload.email}', + '${payload.picture}', + '${payload.picture}', + '${socialHashedPassword}', + '1', + '1', + '1', + '${Date()}', + '${Date.now()}' + )`, + }); - newFoundUser = await global.DB_HANDLER(`SELECT * FROM ${usertype} WHERE id='${newUser.insertId}'`); + newFoundUser = await dbHandler({ + query: `SELECT * FROM ${usertype} WHERE id = ?`, + values: [newUser.insertId], + }); //////////////////////////////////////////////// //////////////////////////////////////////////// //////////////////////////////////////////////// } catch (error) { - serverError({ - component: "googleLogin", - message: error.message, - user: {}, - }); - loginFailureReason = error; isUserValid = false; diff --git a/package.json b/package.json index c026b74..9fd9ade 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "datasquirel", - "version": "1.6.9", + "version": "1.7.0", "description": "Cloud-based SQL data management tool", "main": "index.js", "bin": {