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 MaterialTemplateModel from '../models/material_template'; import SampleModel from '../models/sample'; import MaterialModel from '../models/material'; import MeasurementModel from '../models/measurement'; import res400 from './validate/res400'; import IdValidate from './validate/id'; import mongoose from "mongoose"; import db from '../db'; const router = express.Router(); router.get('/template/:collection(measurements|conditions|materials)', (req, res, next) => { if (!req.auth(res, ['read', 'write', '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); // validate all and filter null values from validation errors res.json(_.compact(data.map(e => TemplateValidate.output(e)))); }); }); router.get('/template/:collection(measurement|condition|material)/' + IdValidate.parameter(), (req, res, next) => { if (!req.auth(res, ['read', 'write', '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|material)/' + IdValidate.parameter(), async (req, res, next) => { if (!req.auth(res, ['dev', 'admin'], 'basic')) return; const {error, value: template} = TemplateValidate.input(req.body, 'change'); if (error) return res400(error, res); // find given template const templateRef = await model(req).findById(req.params.id).lean().exec().catch(err => {next(err);}) as any; if (templateRef instanceof Error) return; if (!templateRef) { return res.status(404).json({status: 'Not found'}); } // find latest version const templateData = await model(req).findOne({first_id: templateRef.first_id}).sort({version: -1}) .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 if (!template.parameters || _.isEqual(templateData.parameters, template.parameters)) { // only name was changed model(req).findByIdAndUpdate(req.params.id, {name: template.name}, {new: true}) .log(req).lean().exec((err, data) => { if (err) next (err); res.json(TemplateValidate.output(data)); }); } else if (template.parameters.filter((e, i) => _.isEqual(e.range, templateData.parameters[i].range)).length === templateData.parameters.length) { // only names changed const changedParameterNames = template.parameters.map((e, i) => ( // list of new names {name: e.name, index: i, oldName: templateData.parameters[i].name} )).filter(e => e.name !== e.oldName); // custom mappings for different collections let targetModel; // model of the collection where the template is used let pathPrefix; // path to the parameters in use let templatePath; // complete path of the template property switch (req.params.collection) { case 'condition': targetModel = SampleModel; pathPrefix = 'condition.'; templatePath = 'condition.condition_template'; break; case 'measurement': targetModel = MeasurementModel; pathPrefix = 'values.'; templatePath = 'measurement_template'; break; case 'material': targetModel = MaterialModel; pathPrefix = 'properties.'; templatePath = 'properties.material_template'; break; } targetModel.updateMany({[templatePath]: mongoose.Types.ObjectId(templateData._id)}, {$rename: changedParameterNames.reduce((s, e) => {s[pathPrefix + e.oldName] = pathPrefix + e.name; return s;}, {}) }) .log(req).lean().exec(err => { if (err) return next(err); model(req).findByIdAndUpdate(req.params.id, {$set: changedParameterNames.reduce( (s, e) => {s[`parameters.${e.index}.name`] = e.name; return s;}, {name: template.name} ), },{new: true}).log(req).lean().exec((err, data) => { if (err) next (err); res.json(TemplateValidate.output(data)); }); }); } else { template.version = templateData.version + 1; // increase version // save new template, fill with old properties await new (model(req))(_.assign({}, _.omit(templateData, ['_id', '__v']), template)).save((err, data) => { if (err) next (err); db.log(req, req.params.collection + '_templates', {_id: data._id}, data.toObject()); res.json(TemplateValidate.output(data.toObject())); }); } } else { res.json(TemplateValidate.output(templateData)); } }); router.post('/template/:collection(measurement|condition|material)/new', async (req, res, next) => { if (!req.auth(res, ['dev', 'admin'], 'basic')) return; const {error, value: template} = TemplateValidate.input(req.body, 'new'); 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; template.version = 1; // set template version await new (model(req))(template).save((err, data) => { if (err) next (err); db.log(req, req.params.collection + '_templates', {_id: data._id}, data.toObject()); res.json(TemplateValidate.output(data.toObject())); }); }); module.exports = router; function model (req) { // return right template model switch (req.params.collection) { case 'condition': return ConditionTemplateModel case 'measurement': return MeasurementTemplateModel case 'material': return MaterialTemplateModel } }