From 315573364238da2d01eb6ab1da14f7234ce63746 Mon Sep 17 00:00:00 2001 From: Tom Butcher Date: Tue, 28 Jul 2026 02:51:09 +0100 Subject: [PATCH] Add filterDistributeKeys function and DISTRIBUTE_KEYS constant to utils.js for improved data handling in distributed updates. Updated distribution functions to utilize these enhancements, ensuring only relevant keys are published. --- src/utils.js | 58 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 47 insertions(+), 11 deletions(-) diff --git a/src/utils.js b/src/utils.js index 4539240..b15751b 100644 --- a/src/utils.js +++ b/src/utils.js @@ -636,6 +636,16 @@ function getChangedValues(oldObj, newObj, old = false) { const AUDIT_EXCLUDED_MODELS = ['notification', 'userNotifier']; const SENSITIVE_KEYS = ['secret']; +const DISTRIBUTE_KEYS = { + _id: true, + _reference: true, + name: true, + tags: true, + state: true, + createdAt: true, + updatedAt: true, +}; + function omitSensitive(obj) { if (obj == null || typeof obj !== 'object') return obj; if (Array.isArray(obj)) return obj.map(omitSensitive); @@ -822,20 +832,38 @@ async function getAuditLogs(idOrIds) { } } +function filterDistributeKeys(value, keys = DISTRIBUTE_KEYS) { + if (value == null) return value; + + const obj = value?.toObject ? value.toObject() : value; + if (typeof obj !== 'object' || Array.isArray(obj)) return obj; + + const hasKeys = keys && Object.keys(keys).length > 0; + if (!hasKeys) return { ...obj }; + + const result = {}; + for (const [key, val] of Object.entries(obj)) { + if (keys[key] === true) { + result[key] = val; + } + } + return result; +} + async function distributeUpdate(value, id, type) { await natsServer.publish(`${type}s.${id}.object`, value); } async function distributeStats(value, type) { - await natsServer.publish(`${type}s.stats`, value); + await natsServer.publish(`${type}s.stats`, filterDistributeKeys(value)); } async function distributeNew(value, type) { - await natsServer.publish(`${type}s.new`, value); + await natsServer.publish(`${type}s.new`, filterDistributeKeys(value)); } async function distributeDelete(value, type) { - await natsServer.publish(`${type}s.delete`, value); + await natsServer.publish(`${type}s.delete`, filterDistributeKeys(value)); } function getReferenceId(value) { @@ -899,10 +927,13 @@ async function distributeChildDelete(value, id, model) { logger.debug( `Distributing child delete for ${populated.ref}s.${populatedId}.events.childDelete` ); - await natsServer.publish(`${populated.ref}s.${populatedId}.events.childDelete`, { - type: 'childDelete', - data: { parentId: id, parentType: model.modelName }, - }); + await natsServer.publish( + `${populated.ref}s.${populatedId}.events.childDelete`, + filterDistributeKeys({ + type: 'childDelete', + data: { parentId: id, parentType: model.modelName }, + }) + ); } } @@ -912,10 +943,13 @@ async function distributeChildNew(value, id, model) { const populatedId = getReferenceIdString(populated._id); if (!populatedId) continue; logger.debug(`Distributing child new for ${populated.ref}s.${populatedId}.events.childNew`); - await natsServer.publish(`${populated.ref}s.${populatedId}.events.childNew`, { - type: 'childNew', - data: { parentId: id, parentType: model.modelName }, - }); + await natsServer.publish( + `${populated.ref}s.${populatedId}.events.childNew`, + filterDistributeKeys({ + type: 'childNew', + data: { parentId: id, parentType: model.modelName }, + }) + ); } } @@ -1264,6 +1298,8 @@ export { flatternObjectIds, expandObjectIds, newNoteNotification, + filterDistributeKeys, + DISTRIBUTE_KEYS, distributeUpdate, distributeStats, distributeNew,