Browse Source

Merge a49d20fa24 into 7628e40f4b

pull/5150/merge
Ananta Podder 3 days ago
committed by GitHub
parent
commit
4659dd6daf
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 70
      apps/api/src/helper/object.helper.ts

70
apps/api/src/helper/object.helper.ts

@ -44,45 +44,51 @@ export function redactAttributes({
return object; return object;
} }
// Create deep clone // Use efficient deep clone from lodash only on first run
const redactedObject = isFirstRun const redactedObject = isFirstRun ? cloneDeep(object) : object;
? JSON.parse(JSON.stringify(object))
: object;
for (const option of options) { // Create a Map for faster attribute lookup
if (redactedObject.hasOwnProperty(option.attribute)) { const attributeMap = new Map(
if (option.valueMap['*'] || option.valueMap['*'] === null) { options.map((opt) => [opt.attribute, opt.valueMap])
redactedObject[option.attribute] = option.valueMap['*']; );
} else if (option.valueMap[redactedObject[option.attribute]]) {
redactedObject[option.attribute] = // Process current level attributes first
option.valueMap[redactedObject[option.attribute]]; for (const [attribute, valueMap] of attributeMap) {
if (attribute in redactedObject) {
const currentValue = redactedObject[attribute];
// Check for wildcard first, then specific value
if ('*' in valueMap) {
redactedObject[attribute] = valueMap['*'];
} else if (currentValue in valueMap) {
redactedObject[attribute] = valueMap[currentValue];
} }
} else { }
// If the attribute is not present on the current object, }
// check if it exists on any nested objects
for (const property in redactedObject) { // Process nested objects and arrays efficiently
if (isArray(redactedObject[property])) { const propertyNames = Object.keys(redactedObject);
redactedObject[property] = redactedObject[property].map( for (const property of propertyNames) {
(currentObject) => { const redactedField = redactedObject[property];
return redactAttributes({
options, if (isArray(redactedField)) {
isFirstRun: false, // Process arrays in-place to avoid creating new arrays
object: currentObject for (let innerField of redactedField) {
}); if (isObject(innerField)) {
} innerField = redactAttributes({
);
} else if (
isObject(redactedObject[property]) &&
!(redactedObject[property] instanceof Big)
) {
// Recursively call the function on the nested object
redactedObject[property] = redactAttributes({
options, options,
isFirstRun: false, isFirstRun: false,
object: redactedObject[property] object: innerField
}); });
} }
} }
} else if (isObject(redactedField) && !(redactedField instanceof Big)) {
// Recursively process nested objects
redactedObject[property] = redactAttributes({
options,
isFirstRun: false,
object: redactedField
});
} }
} }

Loading…
Cancel
Save