From f3391fb8f3bfa4164c0bd83eef2a4685252d43c5 Mon Sep 17 00:00:00 2001 From: Tom Butcher Date: Wed, 19 Aug 2026 18:58:28 +0100 Subject: [PATCH] Add createDocumentJobUserNotifier utility and integrate into document job handling This update introduces the `createDocumentJobUserNotifier` function in `utils.js`, which creates a user notifier for document jobs if it doesn't already exist. The function is then integrated into the `newDocumentJobRouteHandler` in `documentjobs.js`, ensuring that user notifications are properly managed upon the creation of new document jobs. Additionally, tests have been updated to verify the correct invocation of the new utility function, enhancing the overall notification system for document jobs. --- .../management/__tests__/documentjobs.test.js | 6 +++++ src/services/management/documentjobs.js | 3 +++ src/utils.js | 23 +++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/src/services/management/__tests__/documentjobs.test.js b/src/services/management/__tests__/documentjobs.test.js index 16b1a51..c70f957 100644 --- a/src/services/management/__tests__/documentjobs.test.js +++ b/src/services/management/__tests__/documentjobs.test.js @@ -18,6 +18,10 @@ jest.unstable_mockModule('../../../database/schemas/management/documentjob.schem documentJobModel: { modelName: 'DocumentJob' }, })); +jest.unstable_mockModule('../../../utils.js', () => ({ + createDocumentJobUserNotifier: jest.fn(), +})); + jest.unstable_mockModule('log4js', () => ({ default: { getLogger: () => ({ @@ -37,6 +41,7 @@ const { } = await import('../documentjobs.js'); const { listObjects, getObject, newObject } = await import('../../../database/database.js'); +const { createDocumentJobUserNotifier } = await import('../../../utils.js'); const { documentJobModel } = await import( '../../../database/schemas/management/documentjob.schema.js' ); @@ -81,6 +86,7 @@ describe('Document Job Service Route Handlers', () => { await newDocumentJobRouteHandler(req, res); expect(newObject).toHaveBeenCalled(); + expect(createDocumentJobUserNotifier).toHaveBeenCalledWith('456', req.user); expect(res.send).toHaveBeenCalledWith(mockJob); }); }); diff --git a/src/services/management/documentjobs.js b/src/services/management/documentjobs.js index 9cf8606..f84d967 100644 --- a/src/services/management/documentjobs.js +++ b/src/services/management/documentjobs.js @@ -15,6 +15,7 @@ import { getPropertyValues, getObjectNeighbors, } from '../../database/database.js'; +import { createDocumentJobUserNotifier } from '../../utils.js'; const logger = log4js.getLogger('Document Jobs'); logger.level = config.server.logLevel; @@ -155,6 +156,8 @@ export const newDocumentJobRouteHandler = async (req, res) => { return res.status(result.code).send(result); } + await createDocumentJobUserNotifier(result._id, req.user); + logger.debug(`New document job with ID: ${result._id}`); res.send(result); diff --git a/src/utils.js b/src/utils.js index 416f239..6378655 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1390,6 +1390,28 @@ async function createNotification(user, title, message, type = 'info', metadata) return notification; } +async function createDocumentJobUserNotifier(documentJobId, user) { + const userId = user?._id ?? user; + if (!userId) return null; + + const existing = await userNotifierModel.findOne({ + user: userId, + object: documentJobId, + objectType: 'documentJob', + }); + if (existing) return existing; + + const userNotifier = await userNotifierModel.create({ + user: userId, + object: documentJobId, + objectType: 'documentJob', + email: false, + }); + + await distributeNew(userNotifier, 'userNotifier'); + return userNotifier; +} + let mailWorker = null; function getMailWorker() { @@ -1736,6 +1758,7 @@ export { distributeChildNew, notfiyObjectUserNotifiers, createNotification, + createDocumentJobUserNotifier, sendEmailNotification, getFilter, getSort,