import path from "path"; require("dotenv").config({ path: "../../../.env" }); import fs from "fs"; import EJSON from "../../../utils/ejson"; import hashPassword from "../../../functions/dsql/hashPassword"; import updateDbEntry from "../../../functions/backend/db/updateDbEntry"; const tmpDir = process.argv[process.argv.length - 1]; /** * # Create New User */ async function createUser() { var _a; /** * Validate Form * * @description Check if request body is valid */ try { const isTmpDir = Boolean(tmpDir === null || tmpDir === void 0 ? void 0 : 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!"); let hashedPassword = updateUserObj.password ? hashPassword({ encryptionKey: process.env.DSQL_ENCRYPTION_PASSWORD || "", password: updateUserObj.password, }) : undefined; let updatePayload = Object.assign({}, updateUserObj); if (hashedPassword) { updatePayload["password"] = hashedPassword; } const newUser = await updateDbEntry({ dbFullName: "datasquirel", tableName: "users", data: Object.assign(Object.assign({}, updatePayload), { id: undefined }), identifierColumnName: "id", identifierValue: updatePayload.id, }); if (!(newUser === null || newUser === void 0 ? void 0 : newUser.affectedRows)) return false; if (isTmpDir) { try { fs.unlinkSync(path.resolve(process.cwd(), tmpDir)); } catch (error) { } } return true; } catch (error) { (_a = global.ERROR_CALLBACK) === null || _a === void 0 ? void 0 : _a.call(global, `Error Updating User`, error); return false; } } createUser().then((res) => { if (res) { console.log("User Update Success!!!"); } else { console.log("User Update Failed!"); } process.exit(); });