Browse Source

Reimplement redactObject()

pull/2760/head
Thomas Kaul 2 years ago
parent
commit
e4dbc26fa2
  1. 61
      apps/api/src/interceptors/redact-values-in-response.interceptor.ts

61
apps/api/src/interceptors/redact-values-in-response.interceptor.ts

@ -30,9 +30,7 @@ export class RedactValuesInResponseInterceptor<T>
hasImpersonationId ||
this.userService.isRestrictedView(request.user)
) {
data = redactAttributes({
object: data,
options: [
const attributes = [
'balance',
'balanceInBaseCurrency',
'comment',
@ -51,7 +49,13 @@ export class RedactValuesInResponseInterceptor<T>
'unitPrice',
'value',
'valueInBaseCurrency'
].map((attribute) => {
];
console.time('oldExecutionTime');
data = redactAttributes({
object: data,
options: attributes.map((attribute) => {
return {
attribute,
valueMap: {
@ -60,10 +64,59 @@ export class RedactValuesInResponseInterceptor<T>
};
})
});
console.timeLog('oldExecutionTime');
data = this.redactObject({
attributes,
data,
valueMap: {
'*': null
}
});
}
return data;
})
);
}
private redactObject({
attributes,
data,
valueMap
}: {
attributes: string[];
data: any;
valueMap: { [key: string]: any };
}) {
console.time('newExecutionTime');
// Stringify the JSON object
let jsonString = JSON.stringify(data);
// Nullify occurrences of attributes in the string
for (const attribute of attributes) {
const regex = new RegExp(`"${attribute}"\\s*:\\s*"[^"]*"`, 'g');
if (valueMap['*'] || valueMap['*'] === null) {
jsonString = jsonString.replace(
regex,
`"${attribute}":${valueMap['*']}`
);
} else if (valueMap[attribute]) {
jsonString = jsonString.replace(
regex,
`"${attribute}":${valueMap[attribute]}`
);
}
}
// Transform the stringified JSON back to an object
const transformedObject = JSON.parse(jsonString);
console.timeLog('newExecutionTime');
return transformedObject;
}
}

Loading…
Cancel
Save