Normalize IDs in update distribution and subscription methods for consistency
All checks were successful
farmcontrol/farmcontrol-ws/pipeline/head This commit looks good

- 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.
This commit is contained in:
Tom Butcher 2026-11-09 00:46:46 +00:00
parent ce3b9befeb
commit 76c3aa82dd
2 changed files with 16 additions and 8 deletions

View File

@ -581,7 +581,11 @@ async function getAuditLogs(idOrIds) {
} }
async function distributeUpdate(value, id, type) { 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) { async function distributeStats(value, type) {

View File

@ -206,7 +206,8 @@ export class UpdateManager {
} }
getObjectUpdateSubscriptionKey(objectType, id) { getObjectUpdateSubscriptionKey(objectType, id) {
return `${objectType}:${id}`; const normalizedId = id == null ? id : String(id).toLowerCase();
return `${objectType}:${normalizedId}`;
} }
matchesObjectTypeFilter(objectType, filter, value) { matchesObjectTypeFilter(objectType, filter, value) {
@ -298,8 +299,9 @@ export class UpdateManager {
} }
async subscribeToObjectUpdate(id, objectType) { async subscribeToObjectUpdate(id, objectType) {
logger.debug('Subscribing to object update...', id, objectType); const normalizedId = id == null ? id : String(id).toLowerCase();
const subject = `${objectType}s.${id}.object`; logger.debug('Subscribing to object update...', normalizedId, objectType);
const subject = `${objectType}s.${normalizedId}.object`;
const owner = this.socketClient.socketId; const owner = this.socketClient.socketId;
await natsServer.subscribe(subject, owner, (key, value) => { await natsServer.subscribe(subject, owner, (key, value) => {
@ -307,7 +309,7 @@ export class UpdateManager {
}); });
this.objectUpdateSubscriptions.add( this.objectUpdateSubscriptions.add(
this.getObjectUpdateSubscriptionKey(objectType, id) this.getObjectUpdateSubscriptionKey(objectType, normalizedId)
); );
this.subscriptions.add(getSubscriptionKey(subject, owner)); this.subscriptions.add(getSubscriptionKey(subject, owner));
return { success: true }; return { success: true };
@ -321,9 +323,10 @@ export class UpdateManager {
} }
const { _id, ...object } = { ...expandedValue }; 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', { this.socketClient.socket.emit('objectUpdate', {
_id, _id: normalizedId,
objectType, objectType,
object object
}); });
@ -375,7 +378,8 @@ export class UpdateManager {
} }
async removeObjectUpdateListener(id, objectType) { 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; const owner = this.socketClient.socketId;
await natsServer.removeSubscription(subject, owner); await natsServer.removeSubscription(subject, owner);