Implemented subscribe and unsubscribe audit log functions in utils.js and integrated them into user notifier route handlers for tracking subscription changes. This enhances audit capabilities and maintains user activity logs.
All checks were successful
farmcontrol/farmcontrol-api/pipeline/head This commit looks good

This commit is contained in:
Tom Butcher 2026-07-13 03:12:48 +01:00
parent 367891eb33
commit bf3e23ddf1
2 changed files with 33 additions and 0 deletions

View File

@ -8,6 +8,7 @@ import {
listObjects,
newObject,
} from '../../database/database.js';
import { subscribeAuditLog, unsubscribeAuditLog } from '../../utils.js';
const logger = log4js.getLogger('UserNotifiers');
logger.level = config.server.logLevel;
@ -76,6 +77,8 @@ export const newUserNotifierRouteHandler = async (req, res) => {
return res.status(result.code).send(result);
}
await subscribeAuditLog(result.object._id, result.objectType, req.user);
logger.debug(`New user notifier with ID: ${result._id}`);
res.send(result);
};
@ -140,6 +143,8 @@ export const deleteUserNotifierRouteHandler = async (req, res) => {
return res.status(result.code).send(result);
}
await unsubscribeAuditLog(existing.object._id, existing.objectType, req.user);
logger.info(`Successfully deleted user notifier ${id}`);
res.send({
status: 'ok',

View File

@ -790,6 +790,32 @@ async function newNoteNotification(note, user) {
);
}
async function subscribeAuditLog(parentId, parentType, user) {
const auditLog = new auditLogModel({
parent: parentId,
parentType,
owner: user._id,
ownerType: 'user',
operation: 'subscribe',
});
await auditLog.save();
await distributeNew(auditLog._id, 'auditLog');
return auditLog;
}
async function unsubscribeAuditLog(parentId, parentType, user) {
const auditLog = new auditLogModel({
parent: parentId,
parentType,
owner: user._id,
ownerType: 'user',
operation: 'unsubscribe',
});
await auditLog.save();
await distributeNew(auditLog._id, 'auditLog');
return auditLog;
}
async function getAuditLogs(idOrIds) {
if (Array.isArray(idOrIds)) {
return auditLogModel.find({ parent: { $in: idOrIds } }).populate('owner');
@ -1256,4 +1282,6 @@ export {
modelHasRef,
getFieldsByRef,
jsonToCacheKey,
subscribeAuditLog,
unsubscribeAuditLog,
};