datasquirel/engine/user/add-user.js

171 lines
5.3 KiB
JavaScript
Raw Normal View History

2023-09-21 14:00:04 +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");
/**
* Make a get request to Datasquirel API
* ==============================================================================
* @async
*
* @param {Object} params - Single object passed
2024-10-19 16:45:42 +00:00
* @param {import("../../package-shared/types").UserDataPayload} params.payload - SQL Query
* @param {import("../../package-shared/types").DSQL_DatabaseSchemaType | undefined} params.dbSchema - Name of the table to query
2024-08-22 11:59:50 +00:00
* @param {string} [params.encryptionKey]
* @param {string} [params.encryptionSalt]
2023-09-21 14:00:04 +00:00
*
2024-10-21 07:05:18 +00:00
* @returns { Promise<import("../../package-shared/types").AddUserFunctionReturn> } - Return Object
2023-09-21 14:00:04 +00:00
*/
2024-08-22 11:59:50 +00:00
async function localAddUser({
payload,
dbSchema,
encryptionKey,
encryptionSalt,
}) {
2023-09-21 14:00:04 +00:00
try {
/**
* Initialize Variables
*/
const dbFullName = process.env.DSQL_DB_NAME || "";
2024-08-22 11:59:50 +00:00
const encryptionKeyFinal = process.env.DSQL_ENCRYPTION_KEY || "";
const encryptionSaltFinal = process.env.DSQL_ENCRYPTION_SALT || "";
2023-09-21 14:00:04 +00:00
/**
* Hash Password
*
* @description Hash Password
*/
if (!payload?.password) {
2024-08-22 11:59:50 +00:00
return {
success: false,
2024-10-21 07:05:18 +00:00
msg: `Password is required to create an account`,
2024-08-22 11:59:50 +00:00
};
2023-09-21 14:00:04 +00:00
}
const hashedPassword = hashPassword({
password: payload.password,
2024-08-22 11:59:50 +00:00
encryptionKey: encryptionKeyFinal,
2023-09-21 14:00:04 +00:00
});
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,
2024-10-21 07:05:18 +00:00
msg: "Could not create users table",
2023-09-21 14:00:04 +00:00
};
}
2024-08-22 11:59:50 +00:00
const fieldsTitles = fields.map(
(/** @type {*} */ fieldObject) => fieldObject.Field
);
2023-09-21 14:00:04 +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)) {
invalidField = key;
break;
}
}
if (invalidField) {
2024-08-22 11:59:50 +00:00
return {
success: false,
2024-10-21 07:05:18 +00:00
msg: `${invalidField} is not a valid field!`,
2024-08-22 11:59:50 +00:00
};
2023-09-21 14:00:04 +00:00
}
2024-10-14 06:49:01 +00:00
if (!dbSchema) {
throw new Error("Db Schema not found!");
}
2024-08-22 11:59:50 +00:00
const tableSchema = dbSchema.tables.find(
(tb) => tb?.tableName === "users"
);
2023-09-21 14:00:04 +00:00
const existingUser = await varDatabaseDbHandler({
2024-08-22 11:59:50 +00:00
queryString: `SELECT * FROM users WHERE email = ?${
payload.username ? "OR username = ?" : ""
}}`,
queryValuesArray: payload.username
? [payload.email, payload.username]
: [payload.email],
2023-09-21 14:00:04 +00:00
database: dbFullName,
tableSchema: tableSchema,
});
if (existingUser && existingUser[0]) {
return {
success: false,
2024-10-21 07:05:18 +00:00
msg: "User Already Exists",
2023-09-21 14:00:04 +00:00
};
}
const addUser = await addDbEntry({
dbFullName: dbFullName,
tableName: "users",
data: {
...payload,
image: "/images/user_images/user-preset.png",
2024-08-22 11:59:50 +00:00
image_thumbnail:
"/images/user_images/user-preset-thumbnail.png",
2023-09-21 14:00:04 +00:00
},
2024-08-22 11:59:50 +00:00
encryptionKey: encryptionKeyFinal,
encryptionSalt: encryptionSaltFinal,
2023-09-21 14:00:04 +00:00
tableSchema,
});
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,
2024-10-21 07:05:18 +00:00
msg: "Could not create user",
2023-09-21 14:00:04 +00:00
};
}
////////////////////////////////////////
} catch (/** @type {*} */ error) {
////////////////////////////////////////
console.log("Error in local add-user Request =>", error.message);
return {
success: false,
msg: "Something went wrong!",
};
////////////////////////////////////////
}
}
module.exports = localAddUser;