"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = grabParsedValue; const sanitize_html_1 = __importDefault(require("sanitize-html")); const sanitizeHtmlOptions_1 = __importDefault(require("../html/sanitizeHtmlOptions")); const encrypt_1 = __importDefault(require("../../dsql/encrypt")); /** * # Update DB Function * @description */ function grabParsedValue({ value, tableSchema, encryptionKey, encryptionSalt, dataKey, }) { var _a, _b; let newValue = value; 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 (typeof newValue == "undefined") return; if (typeof newValue == "object" && !newValue) newValue = ""; const htmlRegex = /<[^>]+>/g; if ((targetFieldSchema === null || targetFieldSchema === void 0 ? void 0 : targetFieldSchema.richText) || String(newValue).match(htmlRegex)) { newValue = (0, sanitize_html_1.default)(newValue, sanitizeHtmlOptions_1.default); } if (((_b = targetFieldSchema === null || targetFieldSchema === void 0 ? void 0 : targetFieldSchema.dataType) === null || _b === void 0 ? void 0 : _b.match(/int$/i)) && typeof value == "string" && !(value === null || value === void 0 ? void 0 : value.match(/./))) { value = ""; } if (targetFieldSchema === null || targetFieldSchema === void 0 ? void 0 : targetFieldSchema.encrypted) { newValue = (0, encrypt_1.default)({ data: newValue, encryptionKey, encryptionSalt, }); } if (typeof newValue === "object") { newValue = JSON.stringify(newValue); } if (targetFieldSchema === null || targetFieldSchema === void 0 ? void 0 : targetFieldSchema.pattern) { const pattern = new RegExp(targetFieldSchema.pattern, targetFieldSchema.patternFlags || ""); if (!pattern.test(newValue)) { console.log("DSQL: Pattern not matched =>", newValue); newValue = ""; } } if (typeof newValue === "string" && newValue.match(/^null$/i)) { newValue = { toSqlString: function () { return "NULL"; }, }; } if (typeof newValue === "string" && !newValue.match(/./i)) { newValue = { toSqlString: function () { return "NULL"; }, }; } return newValue; }