2023-08-12 15:46:00 +00:00
// @ts-check
const hashPassword = require ( "../../functions/hashPassword" ) ;
const addUsersTableToDb = require ( "../engine/addUsersTableToDb" ) ;
const varDatabaseDbHandler = require ( "../engine/utils/varDatabaseDbHandler" ) ;
const addDbEntry = require ( "../query/utils/addDbEntry" ) ;
const runQuery = require ( "../query/utils/runQuery" ) ;
/ * *
* @ typedef { Object } LocalPostReturn
* @ property { boolean } success - Did the function run successfully ?
* @ property { * } [ payload ] - GET request results
* @ property { string } [ msg ] - Message
* @ property { string } [ error ] - Error Message
* /
/ * *
* Make a get request to Datasquirel API
* === === === === === === === === === === === === === === === === === === === === === === === === === ===
* @ async
*
* @ param { Object } params - Single object passed
* @ param { import ( "../../users/add-user" ) . UserDataPayload } params . payload - SQL Query
* @ param { import ( "../../types/database-schema.td" ) . DSQL _DatabaseSchemaType } params . dbSchema - Name of the table to query
*
* @ returns { Promise < LocalPostReturn > } - Return Object
* /
async function localAddUser ( { payload , dbSchema } ) {
try {
/ * *
* Initialize Variables
* /
const dbFullName = process . env . DSQL _DB _NAME || "" ;
2023-08-12 16:35:59 +00:00
const encryptionKey = process . env . DSQL _ENCRYPTION _KEY || "" ;
const encryptionSalt = process . env . DSQL _ENCRYPTION _SALT || "" ;
2023-08-12 15:46:00 +00:00
/ * *
* Hash Password
*
* @ description Hash Password
* /
if ( ! payload ? . password ) {
return { success : false , payload : ` Password is required to create an account ` } ;
}
const hashedPassword = hashPassword ( payload . password ) ;
payload . password = hashedPassword ;
let fields = await varDatabaseDbHandler ( {
queryString : ` SHOW COLUMNS FROM users ` ,
database : dbFullName ,
} ) ;
if ( ! fields ) {
const newTable = await addUsersTableToDb ( { dbSchema } ) ;
console . log ( newTable ) ;
fields = await varDatabaseDbHandler ( {
queryString : ` SHOW COLUMNS FROM users ` ,
database : dbFullName ,
} ) ;
}
if ( ! fields ) {
return {
success : false ,
payload : "Could not create users table" ,
} ;
}
const fieldsTitles = fields . map ( ( fieldObject ) => fieldObject . Field ) ;
let invalidField = null ;
for ( let i = 0 ; i < Object . keys ( payload ) . length ; i ++ ) {
const key = Object . keys ( payload ) [ i ] ;
if ( ! fieldsTitles . includes ( key ) ) {
invalidField = key ;
break ;
}
}
if ( invalidField ) {
return { success : false , payload : ` ${ invalidField } is not a valid field! ` } ;
}
2023-08-13 13:00:04 +00:00
const tableSchema = dbSchema . tables . find ( ( tb ) => tb ? . tableName === "users" ) ;
2023-08-12 15:46:00 +00:00
const existingUser = await varDatabaseDbHandler ( {
queryString : ` SELECT * FROM users WHERE email = ? ${ payload . username ? "OR username = ?" : "" } } ` ,
queryValuesArray : payload . username ? [ payload . email , payload . username ] : [ payload . email ] ,
database : dbFullName ,
2023-08-13 13:00:04 +00:00
tableSchema : tableSchema ,
2023-08-12 15:46:00 +00:00
} ) ;
if ( existingUser && existingUser [ 0 ] ) {
return {
success : false ,
payload : "User Already Exists" ,
} ;
}
const addUser = await addDbEntry ( {
dbContext : "Dsql User" ,
paradigm : "Full Access" ,
dbFullName : dbFullName ,
tableName : "users" ,
data : {
... payload ,
image : "/images/user_images/user-preset.png" ,
image _thumbnail : "/images/user_images/user-preset-thumbnail.png" ,
} ,
2023-08-12 16:35:59 +00:00
encryptionKey ,
encryptionSalt ,
2023-08-13 13:00:04 +00:00
tableSchema ,
2023-08-12 15:46:00 +00:00
} ) ;
if ( addUser ? . insertId ) {
const newlyAddedUser = await varDatabaseDbHandler ( {
queryString : ` SELECT id,first_name,last_name,email,username,phone,image,image_thumbnail,city,state,country,zip_code,address,verification_status,more_user_data FROM users WHERE id=' ${ addUser . insertId } ' ` ,
database : dbFullName ,
} ) ;
return {
success : true ,
payload : newlyAddedUser ? . [ 0 ] ,
} ;
} else {
return {
success : false ,
payload : "Could not create user" ,
} ;
}
////////////////////////////////////////
2023-08-13 13:56:56 +00:00
} catch ( /** @type {*} */ error ) {
2023-08-12 15:46:00 +00:00
////////////////////////////////////////
console . log ( "Error in local add-user Request =>" , error . message ) ;
return {
success : false ,
payload : null ,
msg : "Something went wrong!" ,
} ;
////////////////////////////////////////
}
}
module . exports = localAddUser ;