From 76c3aa82dd49986f721a3b6b1e0c271a45b4a1aa Mon Sep 17 00:00:00 2001 From: Tom Butcher Date: Mon, 9 Nov 2026 00:46:46 +0000 Subject: [PATCH] Normalize IDs in update distribution and subscription methods for consistency - Updated the `distributeUpdate` and `subscribeToObjectUpdate` functions to normalize IDs by converting them to lowercase, ensuring consistent handling of object identifiers. - Modified the `getObjectUpdateSubscriptionKey` method to utilize the normalized ID, improving subscription key generation. - Enhanced logging to reflect normalized IDs during object update events, aiding in debugging and traceability. --- src/database/utils.js | 6 +++++- src/updates/updatemanager.js | 18 +++++++++++------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/database/utils.js b/src/database/utils.js index 162c746..6002192 100644 --- a/src/database/utils.js +++ b/src/database/utils.js @@ -581,7 +581,11 @@ async function getAuditLogs(idOrIds) { } async function distributeUpdate(value, id, type) { - await natsServer.publish(`${type}s.${id}.object`, { ...value, _id: id }); + const normalizedId = id == null ? id : String(id).toLowerCase(); + await natsServer.publish(`${type}s.${normalizedId}.object`, { + ...value, + _id: normalizedId, + }); } async function distributeStats(value, type) { diff --git a/src/updates/updatemanager.js b/src/updates/updatemanager.js index 6939ff4..4a36539 100644 --- a/src/updates/updatemanager.js +++ b/src/updates/updatemanager.js @@ -206,7 +206,8 @@ export class UpdateManager { } getObjectUpdateSubscriptionKey(objectType, id) { - return `${objectType}:${id}`; + const normalizedId = id == null ? id : String(id).toLowerCase(); + return `${objectType}:${normalizedId}`; } matchesObjectTypeFilter(objectType, filter, value) { @@ -298,8 +299,9 @@ export class UpdateManager { } async subscribeToObjectUpdate(id, objectType) { - logger.debug('Subscribing to object update...', id, objectType); - const subject = `${objectType}s.${id}.object`; + const normalizedId = id == null ? id : String(id).toLowerCase(); + logger.debug('Subscribing to object update...', normalizedId, objectType); + const subject = `${objectType}s.${normalizedId}.object`; const owner = this.socketClient.socketId; await natsServer.subscribe(subject, owner, (key, value) => { @@ -307,7 +309,7 @@ export class UpdateManager { }); this.objectUpdateSubscriptions.add( - this.getObjectUpdateSubscriptionKey(objectType, id) + this.getObjectUpdateSubscriptionKey(objectType, normalizedId) ); this.subscriptions.add(getSubscriptionKey(subject, owner)); return { success: true }; @@ -321,9 +323,10 @@ export class UpdateManager { } const { _id, ...object } = { ...expandedValue }; - logger.trace('Object update event:', _id, objectType); + const normalizedId = _id == null ? _id : String(_id).toLowerCase(); + logger.trace('Object update event:', normalizedId, objectType); this.socketClient.socket.emit('objectUpdate', { - _id, + _id: normalizedId, objectType, object }); @@ -375,7 +378,8 @@ export class UpdateManager { } async removeObjectUpdateListener(id, objectType) { - const subject = `${objectType}s.${id}.object`; + const normalizedId = id == null ? id : String(id).toLowerCase(); + const subject = `${objectType}s.${normalizedId}.object`; const owner = this.socketClient.socketId; await natsServer.removeSubscription(subject, owner);