2020-05-04 15:48:07 +02:00
|
|
|
import express from 'express';
|
2020-05-12 17:15:36 +02:00
|
|
|
import _ from 'lodash';
|
2020-05-04 15:48:07 +02:00
|
|
|
|
|
|
|
import TemplateValidate from './validate/template';
|
2020-05-27 14:31:17 +02:00
|
|
|
import ConditionTemplateModel from '../models/condition_template';
|
|
|
|
import MeasurementTemplateModel from '../models/measurement_template';
|
2020-05-07 21:55:29 +02:00
|
|
|
import res400 from './validate/res400';
|
2020-05-14 12:31:57 +02:00
|
|
|
import IdValidate from './validate/id';
|
2020-05-29 15:24:24 +02:00
|
|
|
import mongoose from "mongoose";
|
2020-06-05 08:50:06 +02:00
|
|
|
import db from '../db';
|
2020-05-04 15:48:07 +02:00
|
|
|
|
2020-05-15 11:16:17 +02:00
|
|
|
|
2020-05-04 15:48:07 +02:00
|
|
|
|
|
|
|
const router = express.Router();
|
|
|
|
|
2020-05-27 14:31:17 +02:00
|
|
|
router.get('/template/:collection(measurements|conditions)', (req, res, next) => {
|
2020-05-04 15:48:07 +02:00
|
|
|
if (!req.auth(res, ['read', 'write', 'maintain', 'dev', 'admin'], 'basic')) return;
|
|
|
|
|
2020-05-18 14:47:22 +02:00
|
|
|
req.params.collection = req.params.collection.replace(/s$/g, ''); // remove trailing s
|
2020-05-14 12:31:57 +02:00
|
|
|
model(req).find({}).lean().exec((err, data) => {
|
2020-05-04 15:48:07 +02:00
|
|
|
if (err) next (err);
|
2020-05-27 14:31:17 +02:00
|
|
|
res.json(_.compact(data.map(e => TemplateValidate.output(e)))); // validate all and filter null values from validation errors
|
2020-05-04 15:48:07 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-27 14:31:17 +02:00
|
|
|
router.get('/template/:collection(measurement|condition)/' + IdValidate.parameter(), (req, res, next) => {
|
2020-05-04 15:48:07 +02:00
|
|
|
if (!req.auth(res, ['read', 'write', 'maintain', 'dev', 'admin'], 'basic')) return;
|
|
|
|
|
2020-05-14 12:31:57 +02:00
|
|
|
model(req).findById(req.params.id).lean().exec((err, data) => {
|
2020-05-04 15:48:07 +02:00
|
|
|
if (err) next (err);
|
|
|
|
if (data) {
|
2020-05-27 14:31:17 +02:00
|
|
|
res.json(TemplateValidate.output(data));
|
2020-05-04 15:48:07 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
res.status(404).json({status: 'Not found'});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-27 14:31:17 +02:00
|
|
|
router.put('/template/:collection(measurement|condition)/' + IdValidate.parameter(), async (req, res, next) => {
|
2020-05-04 15:48:07 +02:00
|
|
|
if (!req.auth(res, ['maintain', 'admin'], 'basic')) return;
|
|
|
|
|
2020-05-27 14:31:17 +02:00
|
|
|
const {error, value: template} = TemplateValidate.input(req.body, 'change');
|
2020-05-14 15:36:47 +02:00
|
|
|
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) {
|
2020-05-28 11:47:51 +02:00
|
|
|
return res.status(404).json({status: 'Not found'});
|
2020-05-14 15:36:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!_.isEqual(_.pick(templateData, _.keys(template)), template)) { // data was changed
|
2020-05-18 14:47:22 +02:00
|
|
|
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
|
2020-05-14 15:36:47 +02:00
|
|
|
if (err) next (err);
|
2020-06-05 08:50:06 +02:00
|
|
|
db.log(req, req.params.collection + '_templates', {_id: data._id}, data.toObject());
|
2020-05-27 14:31:17 +02:00
|
|
|
res.json(TemplateValidate.output(data.toObject()));
|
2020-05-14 15:36:47 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
2020-05-27 14:31:17 +02:00
|
|
|
res.json(TemplateValidate.output(templateData));
|
2020-05-14 15:36:47 +02:00
|
|
|
}
|
2020-05-04 15:48:07 +02:00
|
|
|
});
|
|
|
|
|
2020-05-27 14:31:17 +02:00
|
|
|
router.post('/template/:collection(measurement|condition)/new', async (req, res, next) => {
|
2020-05-14 16:42:47 +02:00
|
|
|
if (!req.auth(res, ['maintain', 'admin'], 'basic')) return;
|
|
|
|
|
2020-05-27 14:31:17 +02:00
|
|
|
const {error, value: template} = TemplateValidate.input(req.body, 'new');
|
2020-05-14 16:42:47 +02:00
|
|
|
if (error) return res400(error, res);
|
|
|
|
|
2020-05-29 15:24:24 +02:00
|
|
|
template._id = mongoose.Types.ObjectId(); // set reference to itself for first version of template
|
|
|
|
template.first_id = template._id;
|
2020-05-18 14:47:22 +02:00
|
|
|
template.version = 1; // set template version
|
2020-05-14 16:42:47 +02:00
|
|
|
await new (model(req))(template).save((err, data) => {
|
|
|
|
if (err) next (err);
|
2020-06-05 08:50:06 +02:00
|
|
|
db.log(req, req.params.collection + '_templates', {_id: data._id}, data.toObject());
|
2020-05-27 14:31:17 +02:00
|
|
|
res.json(TemplateValidate.output(data.toObject()));
|
2020-05-14 16:42:47 +02:00
|
|
|
});
|
|
|
|
});
|
2020-05-04 15:48:07 +02:00
|
|
|
|
|
|
|
|
2020-05-14 12:31:57 +02:00
|
|
|
module.exports = router;
|
|
|
|
|
2020-05-18 14:47:22 +02:00
|
|
|
function model (req) { // return right template model
|
2020-05-27 14:31:17 +02:00
|
|
|
return req.params.collection === 'condition' ? ConditionTemplateModel : MeasurementTemplateModel;
|
2020-05-14 12:31:57 +02:00
|
|
|
}
|