Archived
2
This repository has been archived on 2023-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
definma-api/src/routes/template.ts

86 lines
3.3 KiB
TypeScript
Raw Normal View History

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';
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';
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();
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);
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
});
});
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) {
res.json(TemplateValidate.output(data));
2020-05-04 15:48:07 +02:00
}
else {
res.status(404).json({status: 'Not found'});
}
});
});
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;
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());
res.json(TemplateValidate.output(data.toObject()));
2020-05-14 15:36:47 +02:00
});
}
else {
res.json(TemplateValidate.output(templateData));
2020-05-14 15:36:47 +02:00
}
2020-05-04 15:48:07 +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;
const {error, value: template} = TemplateValidate.input(req.body, 'new');
2020-05-14 16:42:47 +02:00
if (error) return res400(error, res);
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());
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
return req.params.collection === 'condition' ? ConditionTemplateModel : MeasurementTemplateModel;
2020-05-14 12:31:57 +02:00
}