Some checks failed
farmcontrol/farmcontrol-api/pipeline/head There was a failure building this commit
162 lines
3.7 KiB
JavaScript
162 lines
3.7 KiB
JavaScript
import config from '../../config.js';
|
|
import { materialModel } from '../../database/schemas/management/material.schema.js';
|
|
import log4js from 'log4js';
|
|
import mongoose from 'mongoose';
|
|
import {
|
|
getObject,
|
|
listObjects,
|
|
listObjectsByProperties,
|
|
editObject,
|
|
newObject,
|
|
getModelStats,
|
|
getModelHistory,
|
|
} from '../../database/database.js';
|
|
|
|
const logger = log4js.getLogger('Materials');
|
|
logger.level = config.server.logLevel;
|
|
|
|
export const listMaterialsRouteHandler = async (
|
|
req,
|
|
res,
|
|
page = 1,
|
|
limit = 25,
|
|
property = '',
|
|
filter = {},
|
|
search = '',
|
|
sort = '',
|
|
order = 'ascend'
|
|
) => {
|
|
const result = await listObjects({
|
|
model: materialModel,
|
|
page,
|
|
limit,
|
|
property,
|
|
filter,
|
|
search,
|
|
sort,
|
|
order,
|
|
populate: [],
|
|
});
|
|
|
|
if (result?.error) {
|
|
logger.error('Error listing materials.');
|
|
res.status(result.code).send(result);
|
|
return;
|
|
}
|
|
|
|
logger.debug(`List of materials (Page ${page}, Limit ${limit}). Count: ${result.length}`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const listMaterialsByPropertiesRouteHandler = async (
|
|
req,
|
|
res,
|
|
properties = [],
|
|
filter = {}
|
|
) => {
|
|
const result = await listObjectsByProperties({
|
|
model: materialModel,
|
|
properties,
|
|
filter,
|
|
populate: [],
|
|
});
|
|
|
|
if (result?.error) {
|
|
logger.error('Error listing materials.');
|
|
res.status(result.code).send(result);
|
|
return;
|
|
}
|
|
|
|
logger.debug(`List of materials. Count: ${result.length}`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const getMaterialRouteHandler = async (req, res) => {
|
|
const id = req.params.id;
|
|
const result = await getObject({
|
|
model: materialModel,
|
|
id,
|
|
populate: [],
|
|
});
|
|
if (result?.error) {
|
|
logger.warn(`Material not found with supplied id.`);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
logger.debug(`Retrieved material with ID: ${id}`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const editMaterialRouteHandler = async (req, res) => {
|
|
const id = new mongoose.Types.ObjectId(req.params.id);
|
|
|
|
logger.trace(`Material with ID: ${id}`);
|
|
|
|
const updateData = {
|
|
updatedAt: new Date(),
|
|
name: req.body.name,
|
|
url: req.body.url,
|
|
tags: req.body.tags,
|
|
};
|
|
|
|
const result = await editObject({
|
|
model: materialModel,
|
|
id,
|
|
updateData,
|
|
user: req.user,
|
|
});
|
|
|
|
if (result.error) {
|
|
logger.error('Error editing material:', result.error);
|
|
res.status(result.code).send(result);
|
|
return;
|
|
}
|
|
|
|
logger.debug(`Edited material with ID: ${id}`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const newMaterialRouteHandler = async (req, res) => {
|
|
const newData = {
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
name: req.body.name,
|
|
url: req.body.url,
|
|
tags: req.body.tags,
|
|
};
|
|
|
|
const result = await newObject({
|
|
model: materialModel,
|
|
newData,
|
|
user: req.user,
|
|
});
|
|
if (result.error) {
|
|
logger.error('No material created:', result.error);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
|
|
logger.debug(`New material with ID: ${result._id}`);
|
|
res.send(result);
|
|
};
|
|
|
|
export const getMaterialStatsRouteHandler = async (req, res) => {
|
|
const result = await getModelStats({ model: materialModel });
|
|
if (result?.error) {
|
|
logger.error('Error fetching material stats:', result.error);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
logger.trace('Material stats:', result);
|
|
res.send(result);
|
|
};
|
|
|
|
export const getMaterialHistoryRouteHandler = async (req, res) => {
|
|
const from = req.query.from;
|
|
const to = req.query.to;
|
|
const result = await getModelHistory({ model: materialModel, from, to });
|
|
if (result?.error) {
|
|
logger.error('Error fetching material history:', result.error);
|
|
return res.status(result.code).send(result);
|
|
}
|
|
logger.trace('Material history:', result);
|
|
res.send(result);
|
|
};
|