2024-11-05 11:12:42 +00:00
|
|
|
// @ts-check
|
|
|
|
|
2024-11-05 14:18:40 +00:00
|
|
|
const path = require("path");
|
2024-11-05 11:12:42 +00:00
|
|
|
require("dotenv").config({ path: "../../../.env" });
|
2024-11-05 14:18:40 +00:00
|
|
|
const fs = require("fs");
|
2024-11-05 11:12:42 +00:00
|
|
|
const hashPassword = require("datasquirel/functions/hashPassword");
|
|
|
|
const updateDbEntry = require("../../../package-shared/functions/backend/db/updateDbEntry");
|
2024-11-05 14:18:40 +00:00
|
|
|
const EJSON = require("../../../package-shared/utils/ejson");
|
|
|
|
|
|
|
|
const tmpDir = process.argv[process.argv.length - 1];
|
2024-11-05 11:12:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* # Create New User
|
|
|
|
*/
|
|
|
|
async function createUser() {
|
|
|
|
/**
|
|
|
|
* Validate Form
|
|
|
|
*
|
|
|
|
* @description Check if request body is valid
|
|
|
|
*/
|
|
|
|
try {
|
2024-11-05 14:18:40 +00:00
|
|
|
const isTmpDir = Boolean(tmpDir?.match(/\.json$/));
|
|
|
|
const targetPath = isTmpDir
|
|
|
|
? path.resolve(process.cwd(), tmpDir)
|
|
|
|
: path.resolve(__dirname, "./update-user.json");
|
|
|
|
const updateUserObj = EJSON.parse(fs.readFileSync(targetPath, "utf-8"));
|
|
|
|
|
|
|
|
if (typeof updateUserObj !== "object" || Array.isArray(updateUserObj))
|
|
|
|
throw new Error("Update User Object Invalid!");
|
2024-11-05 11:12:42 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2024-11-05 14:18:40 +00:00
|
|
|
if (isTmpDir) {
|
|
|
|
try {
|
|
|
|
fs.unlinkSync(path.resolve(process.cwd(), tmpDir));
|
|
|
|
} catch (error) {}
|
|
|
|
}
|
|
|
|
|
2024-11-05 11:12:42 +00:00
|
|
|
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();
|
|
|
|
});
|