Add createDocumentJobUserNotifier utility and integrate into document job handling
Some checks failed
farmcontrol/farmcontrol-api/pipeline/head There was a failure building this commit
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:
parent
0fe1714e1a
commit
f3391fb8f3
@ -18,6 +18,10 @@ jest.unstable_mockModule('../../../database/schemas/management/documentjob.schem
|
|||||||
documentJobModel: { modelName: 'DocumentJob' },
|
documentJobModel: { modelName: 'DocumentJob' },
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
jest.unstable_mockModule('../../../utils.js', () => ({
|
||||||
|
createDocumentJobUserNotifier: jest.fn(),
|
||||||
|
}));
|
||||||
|
|
||||||
jest.unstable_mockModule('log4js', () => ({
|
jest.unstable_mockModule('log4js', () => ({
|
||||||
default: {
|
default: {
|
||||||
getLogger: () => ({
|
getLogger: () => ({
|
||||||
@ -37,6 +41,7 @@ const {
|
|||||||
} = await import('../documentjobs.js');
|
} = await import('../documentjobs.js');
|
||||||
|
|
||||||
const { listObjects, getObject, newObject } = await import('../../../database/database.js');
|
const { listObjects, getObject, newObject } = await import('../../../database/database.js');
|
||||||
|
const { createDocumentJobUserNotifier } = await import('../../../utils.js');
|
||||||
const { documentJobModel } = await import(
|
const { documentJobModel } = await import(
|
||||||
'../../../database/schemas/management/documentjob.schema.js'
|
'../../../database/schemas/management/documentjob.schema.js'
|
||||||
);
|
);
|
||||||
@ -81,6 +86,7 @@ describe('Document Job Service Route Handlers', () => {
|
|||||||
await newDocumentJobRouteHandler(req, res);
|
await newDocumentJobRouteHandler(req, res);
|
||||||
|
|
||||||
expect(newObject).toHaveBeenCalled();
|
expect(newObject).toHaveBeenCalled();
|
||||||
|
expect(createDocumentJobUserNotifier).toHaveBeenCalledWith('456', req.user);
|
||||||
expect(res.send).toHaveBeenCalledWith(mockJob);
|
expect(res.send).toHaveBeenCalledWith(mockJob);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -15,6 +15,7 @@ import {
|
|||||||
getPropertyValues,
|
getPropertyValues,
|
||||||
getObjectNeighbors,
|
getObjectNeighbors,
|
||||||
} from '../../database/database.js';
|
} from '../../database/database.js';
|
||||||
|
import { createDocumentJobUserNotifier } from '../../utils.js';
|
||||||
const logger = log4js.getLogger('Document Jobs');
|
const logger = log4js.getLogger('Document Jobs');
|
||||||
logger.level = config.server.logLevel;
|
logger.level = config.server.logLevel;
|
||||||
|
|
||||||
@ -155,6 +156,8 @@ export const newDocumentJobRouteHandler = async (req, res) => {
|
|||||||
return res.status(result.code).send(result);
|
return res.status(result.code).send(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await createDocumentJobUserNotifier(result._id, req.user);
|
||||||
|
|
||||||
logger.debug(`New document job with ID: ${result._id}`);
|
logger.debug(`New document job with ID: ${result._id}`);
|
||||||
|
|
||||||
res.send(result);
|
res.send(result);
|
||||||
|
|||||||
23
src/utils.js
23
src/utils.js
@ -1390,6 +1390,28 @@ async function createNotification(user, title, message, type = 'info', metadata)
|
|||||||
return notification;
|
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;
|
let mailWorker = null;
|
||||||
|
|
||||||
function getMailWorker() {
|
function getMailWorker() {
|
||||||
@ -1736,6 +1758,7 @@ export {
|
|||||||
distributeChildNew,
|
distributeChildNew,
|
||||||
notfiyObjectUserNotifiers,
|
notfiyObjectUserNotifiers,
|
||||||
createNotification,
|
createNotification,
|
||||||
|
createDocumentJobUserNotifier,
|
||||||
sendEmailNotification,
|
sendEmailNotification,
|
||||||
getFilter,
|
getFilter,
|
||||||
getSort,
|
getSort,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user