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
All checks were successful
farmcontrol/farmcontrol-api/pipeline/head This commit looks good
This commit is contained in:
parent
367891eb33
commit
bf3e23ddf1
@ -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',
|
||||
|
||||
28
src/utils.js
28
src/utils.js
@ -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,
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user