From 9447a3206ea74e89b50045f9f46e29d14d5654fe Mon Sep 17 00:00:00 2001 From: Tom Butcher Date: Thu, 3 Sep 2026 22:25:32 +0100 Subject: [PATCH] Refactor UpdateManager and enhance object update handling - Updated the `distributeUpdate` function to include the object ID in the published message, improving traceability. - Refactored the `UpdateManager` class to streamline object update emissions, consolidating logic into the new `emitObjectUpdate` method for better maintainability. - Enhanced test cases to ensure proper handling of object updates with the new structure, verifying that emitted events include the correct object ID and data. - Modified the `objectViewSchema` to remove the `_reference` field and added a new `viewMode` field for improved schema clarity. --- .../schemas/misc/objectview.schema.js | 6 ++- src/database/utils.js | 2 +- src/updates/__tests__/updatemanager.test.js | 13 +++--- src/updates/updatemanager.js | 40 +++++++++---------- 4 files changed, 31 insertions(+), 30 deletions(-) diff --git a/src/database/schemas/misc/objectview.schema.js b/src/database/schemas/misc/objectview.schema.js index af64771..4a99201 100644 --- a/src/database/schemas/misc/objectview.schema.js +++ b/src/database/schemas/misc/objectview.schema.js @@ -1,9 +1,7 @@ import mongoose from 'mongoose'; -import { generateId } from '../../utils.js'; const { Schema } = mongoose; const objectViewSchema = new mongoose.Schema({ - _reference: { type: String, default: () => generateId()() }, user: { type: Schema.Types.ObjectId, ref: 'user', @@ -35,6 +33,10 @@ const objectViewSchema = new mongoose.Schema({ type: Schema.Types.Mixed, default: () => ({}), }, + viewMode: { + type: Schema.Types.Mixed, + default: null, + }, createdAt: { type: Date, required: true, diff --git a/src/database/utils.js b/src/database/utils.js index 4254d3c..a5ee793 100644 --- a/src/database/utils.js +++ b/src/database/utils.js @@ -555,7 +555,7 @@ async function getAuditLogs(idOrIds) { } async function distributeUpdate(value, id, type) { - await natsServer.publish(`${type}s.${id}.object`, value); + await natsServer.publish(`${type}s.${id}.object`, { ...value, _id: id }); } async function distributeStats(value, type) { diff --git a/src/updates/__tests__/updatemanager.test.js b/src/updates/__tests__/updatemanager.test.js index 00d95d4..6aeaec2 100644 --- a/src/updates/__tests__/updatemanager.test.js +++ b/src/updates/__tests__/updatemanager.test.js @@ -184,7 +184,7 @@ describe('UpdateManager', () => { ); const natsCallback = natsServer.subscribe.mock.calls[0][2]; - const data = { status: 'idle' }; + const data = { _id: '123', status: 'idle' }; natsCallback('printers.123.object', data); expect(mockSocketClient.socket.emit).toHaveBeenCalledWith( @@ -192,7 +192,7 @@ describe('UpdateManager', () => { { _id: '123', objectType: 'printer', - object: data + object: { status: 'idle' } } ); }); @@ -209,7 +209,7 @@ describe('UpdateManager', () => { ); const natsCallback = natsServer.subscribe.mock.calls[0][2]; - const data = { status: 'idle' }; + const data = { _id: '456', status: 'idle' }; natsCallback('printers.456.object', data); expect(mockSocketClient.socket.emit).toHaveBeenCalledWith( @@ -217,7 +217,7 @@ describe('UpdateManager', () => { { _id: '456', objectType: 'printer', - object: data + object: { status: 'idle' } } ); }); @@ -227,7 +227,10 @@ describe('UpdateManager', () => { await updateManager.subscribeToAllObjectUpdates('printer'); const allUpdatesCallback = natsServer.subscribe.mock.calls[1][2]; - allUpdatesCallback('printers.123.object', { status: 'idle' }); + allUpdatesCallback('printers.123.object', { + _id: '123', + status: 'idle' + }); expect(mockSocketClient.socket.emit).toHaveBeenCalledTimes(0); }); diff --git a/src/updates/updatemanager.js b/src/updates/updatemanager.js index ae0669e..6939ff4 100644 --- a/src/updates/updatemanager.js +++ b/src/updates/updatemanager.js @@ -303,13 +303,7 @@ export class UpdateManager { const owner = this.socketClient.socketId; await natsServer.subscribe(subject, owner, (key, value) => { - const expandedValue = expandObjectIds(value); - logger.trace('Object update event:', id); - this.socketClient.socket.emit('objectUpdate', { - _id: id, - objectType: objectType, - object: { ...expandedValue } - }); + this.emitObjectUpdate(objectType, value); }); this.objectUpdateSubscriptions.add( @@ -319,12 +313,20 @@ export class UpdateManager { return { success: true }; } - extractIdFromUpdateSubject(subject) { - const parts = subject.split('.'); - if (parts.length < 3 || parts[parts.length - 1] !== 'object') { - return null; + emitObjectUpdate(objectType, value) { + const expandedValue = expandObjectIds(value); + if (!expandedValue || expandedValue._id == null) { + logger.warn('Object update missing _id:', objectType, value); + return; } - return parts[parts.length - 2]; + + const { _id, ...object } = { ...expandedValue }; + logger.trace('Object update event:', _id, objectType); + this.socketClient.socket.emit('objectUpdate', { + _id, + objectType, + object + }); } async subscribeToAllObjectUpdates(objectType) { @@ -333,9 +335,9 @@ export class UpdateManager { const owner = this.socketClient.socketId; await natsServer.subscribe(subject, owner, (key, value) => { - const id = this.extractIdFromUpdateSubject(key); - if (!id) { - logger.warn('Unable to extract id from update subject:', key); + const id = value?._id; + if (id == null) { + logger.warn('Object update missing _id:', objectType, value); return; } @@ -347,13 +349,7 @@ export class UpdateManager { return; } - const expandedValue = expandObjectIds(value); - logger.trace('All object update event:', id, objectType); - this.socketClient.socket.emit('objectUpdate', { - _id: id, - objectType: objectType, - object: { ...expandedValue } - }); + this.emitObjectUpdate(objectType, value); }); this.subscriptions.add(getSubscriptionKey(subject, owner));