80 lines
2.9 KiB
TypeScript
80 lines
2.9 KiB
TypeScript
import express from 'express';
|
|
import _ from 'lodash';
|
|
|
|
import TemplateValidate from './validate/template';
|
|
import ConditionTemplateModel from '../models/condition_template';
|
|
import MeasurementTemplateModel from '../models/measurement_template';
|
|
import res400 from './validate/res400';
|
|
import IdValidate from './validate/id';
|
|
|
|
|
|
|
|
const router = express.Router();
|
|
|
|
router.get('/template/:collection(measurements|conditions)', (req, res, next) => {
|
|
if (!req.auth(res, ['read', 'write', 'maintain', 'dev', 'admin'], 'basic')) return;
|
|
|
|
req.params.collection = req.params.collection.replace(/s$/g, ''); // remove trailing s
|
|
model(req).find({}).lean().exec((err, data) => {
|
|
if (err) next (err);
|
|
res.json(_.compact(data.map(e => TemplateValidate.output(e)))); // validate all and filter null values from validation errors
|
|
});
|
|
});
|
|
|
|
router.get('/template/:collection(measurement|condition)/' + IdValidate.parameter(), (req, res, next) => {
|
|
if (!req.auth(res, ['read', 'write', 'maintain', 'dev', 'admin'], 'basic')) return;
|
|
|
|
model(req).findById(req.params.id).lean().exec((err, data) => {
|
|
if (err) next (err);
|
|
if (data) {
|
|
res.json(TemplateValidate.output(data));
|
|
}
|
|
else {
|
|
res.status(404).json({status: 'Not found'});
|
|
}
|
|
});
|
|
});
|
|
|
|
router.put('/template/:collection(measurement|condition)/' + IdValidate.parameter(), async (req, res, next) => {
|
|
if (!req.auth(res, ['maintain', 'admin'], 'basic')) return;
|
|
|
|
const {error, value: template} = TemplateValidate.input(req.body, 'change');
|
|
if (error) return res400(error, res);
|
|
|
|
const templateData = await model(req).findById(req.params.id).lean().exec().catch(err => {next(err);}) as any;
|
|
if (templateData instanceof Error) return;
|
|
if (!templateData) {
|
|
return res.status(404).json({status: 'Not found'});
|
|
}
|
|
|
|
if (!_.isEqual(_.pick(templateData, _.keys(template)), template)) { // data was changed
|
|
template.version = templateData.version + 1; // increase version
|
|
await new (model(req))(_.assign({}, _.omit(templateData, ['_id', '__v']), template)).save((err, data) => { // save new template, fill with old properties
|
|
if (err) next (err);
|
|
res.json(TemplateValidate.output(data.toObject()));
|
|
});
|
|
}
|
|
else {
|
|
res.json(TemplateValidate.output(templateData));
|
|
}
|
|
});
|
|
|
|
router.post('/template/:collection(measurement|condition)/new', async (req, res, next) => {
|
|
if (!req.auth(res, ['maintain', 'admin'], 'basic')) return;
|
|
|
|
const {error, value: template} = TemplateValidate.input(req.body, 'new');
|
|
if (error) return res400(error, res);
|
|
|
|
template.version = 1; // set template version
|
|
await new (model(req))(template).save((err, data) => {
|
|
if (err) next (err);
|
|
res.json(TemplateValidate.output(data.toObject()));
|
|
});
|
|
});
|
|
|
|
|
|
module.exports = router;
|
|
|
|
function model (req) { // return right template model
|
|
return req.params.collection === 'condition' ? ConditionTemplateModel : MeasurementTemplateModel;
|
|
} |