159 lines
3.5 KiB
JavaScript
159 lines
3.5 KiB
JavaScript
import dotenv from 'dotenv';
|
|
import { documentJobModel } from '../../schemas/management/documentjob.schema.js';
|
|
import log4js from 'log4js';
|
|
import mongoose from 'mongoose';
|
|
import {
|
|
deleteObject,
|
|
listObjects,
|
|
getObject,
|
|
editObject,
|
|
newObject,
|
|
listObjectsByProperties,
|
|
} from '../../database/database.js';
|
|
dotenv.config();
|
|
|
|
const logger = log4js.getLogger('Document Jobs');
|
|
logger.level = process.env.LOG_LEVEL;
|
|
|
|
export const listDocumentJobsRouteHandler = async (
|
|
req,
|
|
res,
|
|
page = 1,
|
|
limit = 25,
|
|
property = '',
|
|
filter = {},
|
|
search = '',
|
|
sort = '',
|
|
order = 'ascend'
|
|
) => {
|
|
const result = await listObjects({
|
|
model: documentJobModel,
|
|
page,
|
|
limit,
|
|
property,
|
|
filter,
|
|
search,
|
|
sort,
|
|
order,
|
|
});
|
|
|
|
if (result?.error) {
|
|
logger.error('Error listing document jobs.');
|
|
res.status(result.code).send(result);
|
|
return;
|
|
}
|
|
|
|
logger.debug(`List of document jobs (Page ${page}, Limit ${limit}). Count: ${result.length}`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const listDocumentJobsByPropertiesRouteHandler = async (
|
|
req,
|
|
res,
|
|
properties = '',
|
|
filter = {}
|
|
) => {
|
|
const result = await listObjectsByProperties({
|
|
model: documentJobModel,
|
|
properties,
|
|
filter,
|
|
});
|
|
|
|
if (result?.error) {
|
|
logger.error('Error listing document jobs.');
|
|
res.status(result.code).send(result);
|
|
return;
|
|
}
|
|
|
|
logger.debug(`List of document jobs. Count: ${result.length}`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const getDocumentJobRouteHandler = async (req, res) => {
|
|
const id = req.params.id;
|
|
const result = await getObject({
|
|
model: documentJobModel,
|
|
id,
|
|
});
|
|
if (result?.error) {
|
|
logger.warn(`Document Job not found with supplied id.`);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
logger.debug(`Retreived document job with ID: ${id}`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const editDocumentJobRouteHandler = async (req, res) => {
|
|
// Get ID from params
|
|
const id = new mongoose.Types.ObjectId(req.params.id);
|
|
|
|
logger.trace(`Document Job with ID: ${id}`);
|
|
|
|
const updateData = {
|
|
updatedAt: new Date(),
|
|
name: req.body.name,
|
|
width: req.body.width,
|
|
height: req.body.height,
|
|
};
|
|
// Create audit log before updating
|
|
const result = await editObject({
|
|
model: documentJobModel,
|
|
id,
|
|
updateData,
|
|
user: req.user,
|
|
});
|
|
|
|
if (result.error) {
|
|
logger.error('Error editing document job:', result.error);
|
|
res.status(result).send(result);
|
|
return;
|
|
}
|
|
|
|
logger.debug(`Edited document job with ID: ${id}`);
|
|
|
|
res.send(result);
|
|
};
|
|
|
|
export const newDocumentJobRouteHandler = async (req, res) => {
|
|
const newData = {
|
|
updatedAt: new Date(),
|
|
name: req.body.name,
|
|
width: req.body.width,
|
|
height: req.body.height,
|
|
};
|
|
const result = await newObject({
|
|
model: documentJobModel,
|
|
newData,
|
|
user: req.user,
|
|
});
|
|
if (result.error) {
|
|
logger.error('No document job created:', result.error);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
|
|
logger.debug(`New document job with ID: ${result._id}`);
|
|
|
|
res.send(result);
|
|
};
|
|
|
|
export const deleteDocumentJobRouteHandler = async (req, res) => {
|
|
// Get ID from params
|
|
const id = new mongoose.Types.ObjectId(req.params.id);
|
|
|
|
logger.trace(`Document Job with ID: ${id}`);
|
|
|
|
const result = await deleteObject({
|
|
model: documentJobModel,
|
|
id,
|
|
user: req.user,
|
|
});
|
|
if (result.error) {
|
|
logger.error('No document job deleted:', result.error);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
|
|
logger.debug(`Deleted document job with ID: ${result._id}`);
|
|
|
|
res.send(result);
|
|
};
|