All checks were successful
farmcontrol/farmcontrol-api/pipeline/head This commit looks good
153 lines
3.7 KiB
JavaScript
153 lines
3.7 KiB
JavaScript
import config from '../../config.js';
|
|
import { userNotifierModel } from '../../database/schemas/misc/usernotifier.schema.js';
|
|
import log4js from 'log4js';
|
|
import {
|
|
deleteObject,
|
|
editObject,
|
|
getObject,
|
|
listObjects,
|
|
newObject,
|
|
} from '../../database/database.js';
|
|
import { subscribeAuditLog, unsubscribeAuditLog } from '../../utils.js';
|
|
|
|
const logger = log4js.getLogger('UserNotifiers');
|
|
logger.level = config.server.logLevel;
|
|
|
|
export const listUserNotifiersRouteHandler = async (
|
|
req,
|
|
res,
|
|
page = 1,
|
|
limit = 25,
|
|
property = '',
|
|
filter = {},
|
|
search = '',
|
|
sort = '',
|
|
order = 'ascend'
|
|
) => {
|
|
const result = await listObjects({
|
|
model: userNotifierModel,
|
|
page,
|
|
limit,
|
|
property,
|
|
filter,
|
|
search,
|
|
sort,
|
|
order,
|
|
populate: ['user', 'object'],
|
|
});
|
|
|
|
if (result?.error) {
|
|
logger.error('Error listing user notifiers.');
|
|
res.status(result.code).send(result);
|
|
return;
|
|
}
|
|
|
|
logger.debug(`List of user notifiers (Page ${page}, Limit ${limit}). Count: ${result.length}`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const getUserNotifierRouteHandler = async (req, res) => {
|
|
const id = req.params.id;
|
|
const result = await getObject({
|
|
model: userNotifierModel,
|
|
id,
|
|
populate: ['user', 'object'],
|
|
});
|
|
if (result?.error) {
|
|
logger.warn(`User notifier not found with supplied id.`);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
logger.debug(`Retrieved user notifier with ID: ${id}`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const newUserNotifierRouteHandler = async (req, res) => {
|
|
const newData = {
|
|
user: req.user._id,
|
|
object: req.body.object,
|
|
objectType: req.body.objectType,
|
|
};
|
|
const result = await newObject({
|
|
model: userNotifierModel,
|
|
newData,
|
|
user: req.user,
|
|
});
|
|
if (result.error) {
|
|
logger.error('No user notifier created:', result.error);
|
|
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);
|
|
};
|
|
|
|
export const editUserNotifierRouteHandler = async (req, res) => {
|
|
const id = req.params.id;
|
|
|
|
const existing = await getObject({
|
|
model: userNotifierModel,
|
|
id,
|
|
});
|
|
if (existing?.error) {
|
|
return res.status(existing.code).send(existing);
|
|
}
|
|
if (String(existing.user._id) !== String(req.user._id)) {
|
|
return res.status(403).send({ error: 'Forbidden: you can only edit your own notifiers' });
|
|
}
|
|
|
|
const updateData = {
|
|
updatedAt: new Date(),
|
|
email: req.body.email,
|
|
};
|
|
const result = await editObject({
|
|
model: userNotifierModel,
|
|
id,
|
|
updateData,
|
|
user: req.user,
|
|
populate: ['user', 'object'],
|
|
});
|
|
|
|
if (result.error) {
|
|
logger.error('Error editing user notifier:', result.error);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
|
|
logger.debug(`Edited user notifier with ID: ${id}`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const deleteUserNotifierRouteHandler = async (req, res) => {
|
|
const id = req.params.id;
|
|
|
|
const existing = await getObject({
|
|
model: userNotifierModel,
|
|
id,
|
|
});
|
|
if (existing?.error) {
|
|
return res.status(existing.code).send(existing);
|
|
}
|
|
if (String(existing.user._id) !== String(req.user._id)) {
|
|
return res.status(403).send({ error: 'Forbidden: you can only delete your own notifiers' });
|
|
}
|
|
|
|
const result = await deleteObject({
|
|
model: userNotifierModel,
|
|
id,
|
|
user: req.user,
|
|
});
|
|
|
|
if (result?.error) {
|
|
logger.error('No user notifier deleted:', result.error);
|
|
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',
|
|
});
|
|
};
|