2024-12-06 13:24:26 +00:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
const updateDbEntry = require("../../backend/db/updateDbEntry");
|
2024-12-08 08:57:48 +00:00
|
|
|
const encrypt = require("../../dsql/encrypt");
|
|
|
|
const hashPassword = require("../../dsql/hashPassword");
|
|
|
|
const varDatabaseDbHandler = require("../../backend/varDatabaseDbHandler");
|
2024-12-06 13:24:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* # Update API User Function
|
|
|
|
*
|
|
|
|
* @param {object} params
|
2024-12-08 08:57:48 +00:00
|
|
|
* @param {Object<string, any>} params.payload
|
2024-12-06 13:24:26 +00:00
|
|
|
* @param {string} params.dbFullName
|
2024-12-08 08:57:48 +00:00
|
|
|
* @param {string | number} params.updatedUserId
|
2024-12-06 13:24:26 +00:00
|
|
|
* @param {boolean} [params.useLocal]
|
2024-12-08 08:57:48 +00:00
|
|
|
* @param {import("../../../types").DSQL_DatabaseSchemaType} [params.dbSchema]
|
2024-12-06 13:24:26 +00:00
|
|
|
*
|
2024-12-08 08:57:48 +00:00
|
|
|
* @returns {Promise<{ success: boolean, payload?: any, msg?: string }>}
|
2024-12-06 13:24:26 +00:00
|
|
|
*/
|
|
|
|
module.exports = async function apiUpdateUser({
|
|
|
|
payload,
|
|
|
|
dbFullName,
|
2024-12-08 08:57:48 +00:00
|
|
|
updatedUserId,
|
2024-12-06 13:24:26 +00:00
|
|
|
useLocal,
|
2024-12-08 08:57:48 +00:00
|
|
|
dbSchema,
|
2024-12-06 13:24:26 +00:00
|
|
|
}) {
|
2024-12-08 08:57:48 +00:00
|
|
|
const existingUserQuery = `SELECT * FROM users WHERE id = ?`;
|
|
|
|
const existingUserValues = [updatedUserId];
|
|
|
|
|
|
|
|
const existingUser = await varDatabaseDbHandler({
|
|
|
|
queryString: existingUserQuery,
|
|
|
|
queryValuesArray: existingUserValues,
|
|
|
|
database: dbFullName,
|
|
|
|
useLocal,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!existingUser?.[0]) {
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
msg: "User not found",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-12-06 13:24:26 +00:00
|
|
|
const data = (() => {
|
|
|
|
const reqBodyKeys = Object.keys(payload);
|
|
|
|
|
2024-12-08 08:57:48 +00:00
|
|
|
const targetTableSchema = (() => {
|
|
|
|
try {
|
|
|
|
const targetDatabaseSchema = dbSchema?.tables?.find(
|
|
|
|
(tbl) => tbl.tableName == "users"
|
|
|
|
);
|
|
|
|
return targetDatabaseSchema;
|
|
|
|
} catch (error) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
2024-12-06 13:24:26 +00:00
|
|
|
/** @type {any} */
|
|
|
|
const finalData = {};
|
|
|
|
|
|
|
|
reqBodyKeys.forEach((key) => {
|
2024-12-08 08:57:48 +00:00
|
|
|
const targetFieldSchema = targetTableSchema?.fields?.find(
|
|
|
|
(field) => field.fieldName == key
|
|
|
|
);
|
|
|
|
|
|
|
|
if (key?.match(/^date_|^id$|^uuid$/)) return;
|
|
|
|
let value = payload[key];
|
|
|
|
|
|
|
|
if (targetFieldSchema?.encrypted) {
|
|
|
|
value = encrypt({ data: value });
|
|
|
|
}
|
|
|
|
|
|
|
|
finalData[key] = value;
|
2024-12-06 13:24:26 +00:00
|
|
|
});
|
|
|
|
|
2024-12-08 08:57:48 +00:00
|
|
|
if (finalData.password && typeof finalData.password == "string") {
|
|
|
|
finalData.password = hashPassword({ password: finalData.password });
|
|
|
|
}
|
|
|
|
|
2024-12-06 13:24:26 +00:00
|
|
|
return finalData;
|
|
|
|
})();
|
|
|
|
|
|
|
|
const updateUser = await updateDbEntry({
|
|
|
|
dbContext: "Dsql User",
|
|
|
|
paradigm: "Full Access",
|
|
|
|
dbFullName,
|
|
|
|
tableName: "users",
|
|
|
|
identifierColumnName: "id",
|
2024-12-08 08:57:48 +00:00
|
|
|
identifierValue: updatedUserId,
|
2024-12-06 13:24:26 +00:00
|
|
|
data: data,
|
|
|
|
useLocal,
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
success: true,
|
|
|
|
payload: updateUser,
|
|
|
|
};
|
|
|
|
};
|