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.
All checks were successful
farmcontrol/farmcontrol-api/pipeline/head This commit looks good

This commit is contained in:
Tom Butcher 2026-07-28 02:51:09 +01:00
parent 8ca49e5a87
commit 3155733642

View File

@ -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`, {
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`, {
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,