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
All checks were successful
farmcontrol/farmcontrol-api/pipeline/head This commit looks good
This commit is contained in:
parent
8ca49e5a87
commit
3155733642
58
src/utils.js
58
src/utils.js
@ -636,6 +636,16 @@ function getChangedValues(oldObj, newObj, old = false) {
|
|||||||
const AUDIT_EXCLUDED_MODELS = ['notification', 'userNotifier'];
|
const AUDIT_EXCLUDED_MODELS = ['notification', 'userNotifier'];
|
||||||
const SENSITIVE_KEYS = ['secret'];
|
const SENSITIVE_KEYS = ['secret'];
|
||||||
|
|
||||||
|
const DISTRIBUTE_KEYS = {
|
||||||
|
_id: true,
|
||||||
|
_reference: true,
|
||||||
|
name: true,
|
||||||
|
tags: true,
|
||||||
|
state: true,
|
||||||
|
createdAt: true,
|
||||||
|
updatedAt: true,
|
||||||
|
};
|
||||||
|
|
||||||
function omitSensitive(obj) {
|
function omitSensitive(obj) {
|
||||||
if (obj == null || typeof obj !== 'object') return obj;
|
if (obj == null || typeof obj !== 'object') return obj;
|
||||||
if (Array.isArray(obj)) return obj.map(omitSensitive);
|
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) {
|
async function distributeUpdate(value, id, type) {
|
||||||
await natsServer.publish(`${type}s.${id}.object`, value);
|
await natsServer.publish(`${type}s.${id}.object`, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function distributeStats(value, type) {
|
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) {
|
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) {
|
async function distributeDelete(value, type) {
|
||||||
await natsServer.publish(`${type}s.delete`, value);
|
await natsServer.publish(`${type}s.delete`, filterDistributeKeys(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
function getReferenceId(value) {
|
function getReferenceId(value) {
|
||||||
@ -899,10 +927,13 @@ async function distributeChildDelete(value, id, model) {
|
|||||||
logger.debug(
|
logger.debug(
|
||||||
`Distributing child delete for ${populated.ref}s.${populatedId}.events.childDelete`
|
`Distributing child delete for ${populated.ref}s.${populatedId}.events.childDelete`
|
||||||
);
|
);
|
||||||
await natsServer.publish(`${populated.ref}s.${populatedId}.events.childDelete`, {
|
await natsServer.publish(
|
||||||
type: 'childDelete',
|
`${populated.ref}s.${populatedId}.events.childDelete`,
|
||||||
data: { parentId: id, parentType: model.modelName },
|
filterDistributeKeys({
|
||||||
});
|
type: 'childDelete',
|
||||||
|
data: { parentId: id, parentType: model.modelName },
|
||||||
|
})
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -912,10 +943,13 @@ async function distributeChildNew(value, id, model) {
|
|||||||
const populatedId = getReferenceIdString(populated._id);
|
const populatedId = getReferenceIdString(populated._id);
|
||||||
if (!populatedId) continue;
|
if (!populatedId) continue;
|
||||||
logger.debug(`Distributing child new for ${populated.ref}s.${populatedId}.events.childNew`);
|
logger.debug(`Distributing child new for ${populated.ref}s.${populatedId}.events.childNew`);
|
||||||
await natsServer.publish(`${populated.ref}s.${populatedId}.events.childNew`, {
|
await natsServer.publish(
|
||||||
type: 'childNew',
|
`${populated.ref}s.${populatedId}.events.childNew`,
|
||||||
data: { parentId: id, parentType: model.modelName },
|
filterDistributeKeys({
|
||||||
});
|
type: 'childNew',
|
||||||
|
data: { parentId: id, parentType: model.modelName },
|
||||||
|
})
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1264,6 +1298,8 @@ export {
|
|||||||
flatternObjectIds,
|
flatternObjectIds,
|
||||||
expandObjectIds,
|
expandObjectIds,
|
||||||
newNoteNotification,
|
newNoteNotification,
|
||||||
|
filterDistributeKeys,
|
||||||
|
DISTRIBUTE_KEYS,
|
||||||
distributeUpdate,
|
distributeUpdate,
|
||||||
distributeStats,
|
distributeStats,
|
||||||
distributeNew,
|
distributeNew,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user