datasquirel/engine/user/get-user.js

56 lines
1.5 KiB
JavaScript
Raw Normal View History

2023-09-21 14:00:04 +00:00
// @ts-check
const varDatabaseDbHandler = require("../engine/utils/varDatabaseDbHandler");
/**
*
* @param {object} param0
* @param {number} param0.userId
* @param {string[]} param0.fields
2024-10-14 06:49:01 +00:00
* @param {import("@/package-shared/types/database-schema.td").DSQL_DatabaseSchemaType | undefined} [param0.dbSchema]
2023-09-21 14:00:04 +00:00
* @returns
*/
async function getLocalUser({ userId, fields, dbSchema }) {
/**
* GRAB user
*
* @description GRAB user
*/
const sanitizedFields = fields.map((fld) => fld.replace(/[^a-z\_]/g, ""));
const query = `SELECT ${sanitizedFields.join(",")} FROM users WHERE id = ?`;
2024-10-14 06:49:01 +00:00
const tableSchema = dbSchema?.tables.find(
(tb) => tb?.tableName === "users"
);
2023-09-21 14:00:04 +00:00
let foundUser = await varDatabaseDbHandler({
queryString: query,
queryValuesArray: [userId.toString()],
database: process.env.DSQL_DB_NAME || "",
tableSchema,
});
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
if (!foundUser || !foundUser[0])
return {
success: false,
payload: null,
msg: "User not found!",
};
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/** ********************* Send Response */
return {
success: true,
payload: foundUser[0],
};
}
module.exports = getLocalUser;