Refactor model entry retrieval in database utilities
All checks were successful
farmcontrol/farmcontrol-ws/pipeline/head This commit looks good

- Changed the getModelEntryByType function to use dynamic import for models, caching the result to improve performance.
- Updated the editNotification function to await the model entry retrieval, ensuring proper asynchronous handling of model data.
This commit is contained in:
Tom Butcher 2026-08-19 19:03:34 +01:00
parent c36765cc47
commit 7e8879164a

View File

@ -2,7 +2,6 @@ import { ObjectId } from 'mongodb';
import { auditLogModel } from './schemas/management/auditlog.schema.js';
import { notificationModel } from './schemas/misc/notification.schema.js';
import { userNotifierModel } from './schemas/misc/usernotifier.schema.js';
import { models } from './schemas/models.js';
import { natsServer } from './nats.js';
import { customAlphabet } from 'nanoid';
@ -21,8 +20,13 @@ function omitSensitive(obj) {
return result;
}
function getModelEntryByType(parentType) {
return Object.values(models).find(
let modelsCache = null;
async function getModelEntryByType(parentType) {
if (!modelsCache) {
modelsCache = (await import('./schemas/models.js')).models;
}
return Object.values(modelsCache).find(
entry => entry.type === parentType || entry.model?.modelName === parentType
);
}
@ -496,7 +500,7 @@ async function editNotification(
) {
if (NOTIFICATION_EXCLUDED_MODELS.includes(parentType)) return;
const modelEntry = getModelEntryByType(parentType);
const modelEntry = await getModelEntryByType(parentType);
const user = notificationUserFromOwner(owner, ownerType);
const objectName =
oldValue?.name ?? newValue?.name ?? modelEntry?.label ?? parentType;