dsql-admin/dsql-app/shell/mariadb-users/users/update-user.js
2024-11-05 12:12:42 +01:00

66 lines
2.1 KiB
JavaScript
Executable File

// @ts-check
const fs = require("fs");
require("dotenv").config({ path: "../../../.env" });
const hashPassword = require("datasquirel/functions/hashPassword");
const path = require("path");
const { execSync } = require("child_process");
const encrypt = require("../../../package-shared/functions/backend/encrypt");
const addMariadbUser = require("../../../package-shared/functions/backend/addMariadbUser");
const DB_HANDLER = require("../../../package-shared/utils/backend/global-db/DB_HANDLER");
const addDbEntry = require("../../../package-shared/functions/backend/db/addDbEntry");
const updateDbEntry = require("../../../package-shared/functions/backend/db/updateDbEntry");
/**
* # Create New User
*/
async function createUser() {
/**
* Validate Form
*
* @description Check if request body is valid
*/
try {
const updateUserObj = JSON.parse(
fs.readFileSync(path.resolve(__dirname, "./new-user.json"), "utf-8")
);
let hashedPassword = updateUserObj.password
? hashPassword({
encryptionKey: process.env.DSQL_ENCRYPTION_PASSWORD || "",
password: updateUserObj.password,
})
: undefined;
let updatePayload = { ...updateUserObj };
if (hashedPassword) {
updatePayload["password"] = hashedPassword;
}
/** @type {any} */
const newUser = await updateDbEntry({
dbFullName: "datasquirel",
tableName: "users",
data: { ...updatePayload, id: undefined },
identifierColumnName: "id",
identifierValue: updatePayload.id,
});
if (!newUser?.affectedRows) return false;
return true;
} catch (/** @type {any} */ error) {
console.log(`Error in creating user => ${error.message}`);
return false;
}
}
createUser().then((res) => {
if (res) {
console.log("User Update Success!!!");
} else {
console.log("User Update Failed!");
}
process.exit();
});