import sanitizeHtml from "sanitize-html";
import sanitizeHtmlOptions from "../html/sanitizeHtmlOptions";
import encrypt from "../../dsql/encrypt";
import checkIfIsMaster from "../../../utils/check-if-is-master";
import connDbHandler from "../../../utils/db/conn-db-handler";
import _ from "lodash";
import purgeDefaultFields from "../../../utils/purge-default-fields";
/**
* # Update DB Function
* @description
*/
export default async function updateDbEntry({ dbContext, dbFullName, tableName, data, tableSchema, identifierColumnName, identifierValue, encryptionKey, encryptionSalt, forceLocal, }) {
var _a;
/**
* Check if data is valid
*/
if (!data || !Object.keys(data).length) {
return {
success: false,
payload: undefined,
msg: "No data provided",
};
}
const isMaster = forceLocal
? true
: checkIfIsMaster({ dbContext, dbFullName });
const DB_CONN = isMaster
? global.DSQL_DB_CONN
: global.DSQL_FULL_ACCESS_DB_CONN || global.DSQL_DB_CONN;
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
let newData = _.cloneDeep(data);
newData = purgeDefaultFields(newData);
/**
* Declare variables
*
* @description Declare "results" variable
*/
const dataKeys = Object.keys(newData);
let updateKeyValueArray = [];
let updateValues = [];
for (let i = 0; i < dataKeys.length; i++) {
try {
const dataKey = dataKeys[i];
let value = newData[dataKey];
const targetFieldSchemaArray = tableSchema
? (_a = tableSchema === null || tableSchema === void 0 ? void 0 : tableSchema.fields) === null || _a === void 0 ? void 0 : _a.filter((field) => field.fieldName === dataKey)
: null;
const targetFieldSchema = targetFieldSchemaArray && targetFieldSchemaArray[0]
? targetFieldSchemaArray[0]
: null;
if (value == null || value == undefined)
continue;
const htmlRegex = /<[^>]+>/g;
if ((targetFieldSchema === null || targetFieldSchema === void 0 ? void 0 : targetFieldSchema.richText) || String(value).match(htmlRegex)) {
value = sanitizeHtml(value, sanitizeHtmlOptions);
}
if (targetFieldSchema === null || targetFieldSchema === void 0 ? void 0 : targetFieldSchema.encrypted) {
value = encrypt({
data: value,
encryptionKey,
encryptionSalt,
});
}
if (typeof value === "object") {
value = JSON.stringify(value);
}
if (targetFieldSchema === null || targetFieldSchema === void 0 ? void 0 : targetFieldSchema.pattern) {
const pattern = new RegExp(targetFieldSchema.pattern, targetFieldSchema.patternFlags || "");
if (!pattern.test(value)) {
console.log("DSQL: Pattern not matched =>", value);
value = "";
}
}
if (typeof value === "string" && value.match(/^null$/i)) {
value = {
toSqlString: function () {
return "NULL";
},
};
}
if (typeof value === "string" && !value.match(/./i)) {
value = {
toSqlString: function () {
return "NULL";
},
};
}
updateKeyValueArray.push(`\`${dataKey}\`=?`);
if (typeof value == "number") {
updateValues.push(String(value));
}
else {
updateValues.push(value);
}
////////////////////////////////////////
////////////////////////////////////////
}
catch ( /** @type {any} */error) {
////////////////////////////////////////
////////////////////////////////////////
console.log("DSQL: Error in parsing data keys in update function =>", error.message);
continue;
}
}
////////////////////////////////////////
////////////////////////////////////////
updateKeyValueArray.push(`date_updated='${Date()}'`);
updateKeyValueArray.push(`date_updated_code='${Date.now()}'`);
////////////////////////////////////////
////////////////////////////////////////
const query = `UPDATE ${isMaster && !dbFullName ? "" : `\`${dbFullName}\`.`}\`${tableName}\` SET ${updateKeyValueArray.join(",")} WHERE \`${identifierColumnName}\`=?`;
updateValues.push(identifierValue);
const updatedEntry = await connDbHandler(DB_CONN, query, updateValues);
/**
* Return statement
*/
return {
success: Boolean(updatedEntry === null || updatedEntry === void 0 ? void 0 : updatedEntry.affectedRows),
payload: updatedEntry,
queryObject: {
sql: query,
params: updateValues,
},
};
}