Add createDocumentJobUserNotifier utility and integrate into document job handling
Some checks failed
farmcontrol/farmcontrol-api/pipeline/head There was a failure building this commit

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.
This commit is contained in:
Tom Butcher 2026-08-19 18:58:28 +01:00
parent 0fe1714e1a
commit f3391fb8f3
3 changed files with 32 additions and 0 deletions

View File

@ -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);
});
});

View File

@ -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);

View File

@ -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,