2024-12-06 10:31:24 +00:00
// @ts-check
2025-01-10 19:10:28 +00:00
import { APICreateUserFunctionParams } from "../../../types" ;
import addUsersTableToDb from "../../backend/addUsersTableToDb" ;
import addDbEntry from "../../backend/db/addDbEntry" ;
import updateUsersTableSchema from "../../backend/updateUsersTableSchema" ;
import varDatabaseDbHandler from "../../backend/varDatabaseDbHandler" ;
import hashPassword from "../../dsql/hashPassword" ;
/ * *
* # API Create User
* /
export default async function apiCreateUser ( {
2024-12-06 10:31:24 +00:00
encryptionKey ,
payload ,
database ,
userId ,
2024-12-06 11:55:03 +00:00
useLocal ,
2025-01-10 19:10:28 +00:00
} : APICreateUserFunctionParams ) {
2024-12-06 10:31:24 +00:00
const dbFullName = database ;
2024-12-09 11:45:39 +00:00
const API_USER_ID = userId || process . env . DSQL_API_USER_ID ;
2024-12-06 10:31:24 +00:00
2024-12-06 11:55:03 +00:00
const finalEncryptionKey =
encryptionKey || process . env . DSQL_ENCRYPTION_PASSWORD ;
if ( ! finalEncryptionKey ) {
return {
success : false ,
msg : "No encryption key provided" ,
payload : null ,
} ;
}
if ( ! finalEncryptionKey ? . match ( /.{8,}/ ) ) {
return {
success : false ,
msg : "Encryption key must be at least 8 characters long" ,
payload : null ,
} ;
}
2024-12-06 10:31:24 +00:00
const hashedPassword = hashPassword ( {
2024-12-06 11:55:03 +00:00
encryptionKey : finalEncryptionKey ,
2024-12-06 10:31:24 +00:00
password : String ( payload . password ) ,
} ) ;
payload . password = hashedPassword ;
2024-12-08 08:58:57 +00:00
const fieldsQuery = ` SHOW COLUMNS FROM users ` ;
2024-12-06 10:31:24 +00:00
2024-12-08 08:58:57 +00:00
let fields = await varDatabaseDbHandler ( {
queryString : fieldsQuery ,
database : dbFullName ,
useLocal ,
} ) ;
if ( ! fields ? . [ 0 ] ) {
2024-12-06 10:31:24 +00:00
const newTable = await addUsersTableToDb ( {
2024-12-09 11:45:39 +00:00
userId : Number ( API_USER_ID ) ,
2024-12-08 08:58:57 +00:00
database : dbFullName ,
2024-12-06 11:55:03 +00:00
useLocal ,
2024-12-08 08:58:57 +00:00
payload : payload ,
2024-12-06 10:31:24 +00:00
} ) ;
fields = await varDatabaseDbHandler ( {
2024-12-08 08:58:57 +00:00
queryString : fieldsQuery ,
2024-12-06 10:31:24 +00:00
database : dbFullName ,
2024-12-08 08:58:57 +00:00
useLocal ,
2024-12-06 10:31:24 +00:00
} ) ;
}
2024-12-08 08:58:57 +00:00
if ( ! fields ? . [ 0 ] ) {
2024-12-06 10:31:24 +00:00
return {
success : false ,
msg : "Could not create users table" ,
} ;
}
2025-01-10 19:10:28 +00:00
const fieldsTitles = fields . map ( ( fieldObject : any ) = > fieldObject . Field ) ;
2024-12-06 10:31:24 +00:00
let invalidField = null ;
for ( let i = 0 ; i < Object . keys ( payload ) . length ; i ++ ) {
const key = Object . keys ( payload ) [ i ] ;
if ( ! fieldsTitles . includes ( key ) ) {
2024-12-08 08:58:57 +00:00
await updateUsersTableSchema ( {
2024-12-09 11:45:39 +00:00
userId : Number ( API_USER_ID ) ,
2024-12-08 08:58:57 +00:00
database : dbFullName ,
newPayload : {
[ key ] : payload [ key ] ,
} ,
} ) ;
2024-12-06 10:31:24 +00:00
}
}
if ( invalidField ) {
return {
success : false ,
msg : ` ${ invalidField } is not a valid field! ` ,
} ;
}
2024-12-08 08:58:57 +00:00
const existingUserQuery = ` SELECT * FROM users WHERE email = ? ${
payload . username ? " OR username = ?" : ""
} ` ;
const existingUserValues = payload . username
? [ payload . email , payload . username ]
: [ payload . email ] ;
2024-12-06 10:31:24 +00:00
const existingUser = await varDatabaseDbHandler ( {
2024-12-08 08:58:57 +00:00
queryString : existingUserQuery ,
queryValuesArray : existingUserValues ,
2024-12-06 10:31:24 +00:00
database : dbFullName ,
2024-12-08 08:58:57 +00:00
useLocal ,
2024-12-06 10:31:24 +00:00
} ) ;
if ( existingUser ? . [ 0 ] ) {
return {
success : false ,
msg : "User Already Exists" ,
payload : null ,
} ;
}
const addUser = await addDbEntry ( {
dbContext : "Dsql User" ,
paradigm : "Full Access" ,
dbFullName : dbFullName ,
tableName : "users" ,
data : {
. . . payload ,
2024-12-11 04:50:22 +00:00
image :
process . env . DSQL_DEFAULT_USER_IMAGE ||
"/images/user-preset.png" ,
image_thumbnail :
process . env . DSQL_DEFAULT_USER_IMAGE ||
"/images/user-preset-thumbnail.png" ,
2024-12-06 10:31:24 +00:00
} ,
2024-12-09 11:45:39 +00:00
useLocal ,
2024-12-06 10:31:24 +00:00
} ) ;
if ( addUser ? . insertId ) {
2024-12-08 08:58:57 +00:00
const newlyAddedUserQuery = ` 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 } ' ` ;
2024-12-06 10:31:24 +00:00
const newlyAddedUser = await varDatabaseDbHandler ( {
2024-12-08 08:58:57 +00:00
queryString : newlyAddedUserQuery ,
2024-12-06 10:31:24 +00:00
database : dbFullName ,
2024-12-08 08:58:57 +00:00
useLocal ,
2024-12-06 10:31:24 +00:00
} ) ;
return {
success : true ,
payload : newlyAddedUser [ 0 ] ,
} ;
} else {
return {
success : false ,
msg : "Could not create user" ,
sqlResult : addUser ,
payload : null ,
} ;
}
2025-01-10 19:10:28 +00:00
}