Archived
2

implemented changelog

This commit is contained in:
VLE2FE
2020-06-05 08:50:06 +02:00
parent 74080d0902
commit ca29cef48c
33 changed files with 905 additions and 112 deletions

View File

@ -14,6 +14,7 @@ import mongoose from 'mongoose';
import ConditionTemplateModel from '../models/condition_template';
import ParametersValidate from './validate/parameters';
import globals from '../globals';
import db from '../db';
const router = express.Router();
@ -101,9 +102,9 @@ router.put('/sample/' + IdValidate.parameter(), (req, res, next) => {
newNotes = !_.isEqual(_.pick(IdValidate.stringify(data), _.keys(sample.notes)), sample.notes); // check if notes were changed
if (newNotes) {
if (data.hasOwnProperty('custom_fields')) { // update note_fields
customFieldsChange(Object.keys(data.custom_fields), -1);
customFieldsChange(Object.keys(data.custom_fields), -1, req);
}
await NoteModel.findByIdAndDelete(sampleData.note_id).lean().exec(err => { // delete old notes
await NoteModel.findByIdAndDelete(sampleData.note_id).log(req).lean().exec(err => { // delete old notes
if (err) return console.error(err);
});
}
@ -112,9 +113,10 @@ router.put('/sample/' + IdValidate.parameter(), (req, res, next) => {
if (_.keys(sample.notes).length > 0 && newNotes) { // save new notes
if (!await sampleRefCheck(sample, res, next)) return;
if (sample.notes.hasOwnProperty('custom_fields') && Object.keys(sample.notes.custom_fields).length > 0) { // new custom_fields
customFieldsChange(Object.keys(sample.notes.custom_fields), 1);
customFieldsChange(Object.keys(sample.notes.custom_fields), 1, req);
}
let data = await new NoteModel(sample.notes).save().catch(err => { return next(err)}); // save new notes
db.log(req, 'notes', {_id: data._id}, data.toObject());
delete sample.notes;
sample.note_id = data._id;
}
@ -125,7 +127,7 @@ router.put('/sample/' + IdValidate.parameter(), (req, res, next) => {
sample.status = globals.status.new;
}
await SampleModel.findByIdAndUpdate(req.params.id, sample, {new: true}).lean().exec((err, data: any) => {
await SampleModel.findByIdAndUpdate(req.params.id, sample, {new: true}).log(req).lean().exec((err, data: any) => {
if (err) return next(err);
res.json(SampleValidate.output(data));
});
@ -145,18 +147,18 @@ router.delete('/sample/' + IdValidate.parameter(), (req, res, next) => {
// only maintain and admin are allowed to edit other user's data
if (sampleData.user_id.toString() !== req.authDetails.id && !req.auth(res, ['maintain', 'admin'], 'basic')) return;
await SampleModel.findByIdAndUpdate(req.params.id, {status:globals.status.deleted}).lean().exec(err => { // set sample status
await SampleModel.findByIdAndUpdate(req.params.id, {status:globals.status.deleted}).log(req).lean().exec(err => { // set sample status
if (err) return next(err);
// set status of associated measurements also to deleted
MeasurementModel.updateMany({sample_id: mongoose.Types.ObjectId(req.params.id)}, {status: -1}).lean().exec(err => {
MeasurementModel.updateMany({sample_id: mongoose.Types.ObjectId(req.params.id)}, {status: -1}).log(req).lean().exec(err => {
if (err) return next(err);
if (sampleData.note_id !== null) { // handle notes
NoteModel.findById(sampleData.note_id).lean().exec((err, data: any) => { // find notes to update note_fields
if (err) return next(err);
if (data.hasOwnProperty('custom_fields')) { // update note_fields
customFieldsChange(Object.keys(data.custom_fields), -1);
customFieldsChange(Object.keys(data.custom_fields), -1, req);
}
res.json({status: 'OK'});
});
@ -172,7 +174,7 @@ router.delete('/sample/' + IdValidate.parameter(), (req, res, next) => {
router.put('/sample/restore/' + IdValidate.parameter(), (req, res, next) => {
if (!req.auth(res, ['maintain', 'admin'], 'basic')) return;
SampleModel.findByIdAndUpdate(req.params.id, {status: globals.status.new}).lean().exec((err, data) => {
SampleModel.findByIdAndUpdate(req.params.id, {status: globals.status.new}).log(req).lean().exec((err, data) => {
if (err) return next(err);
if (!data) {
@ -202,7 +204,7 @@ router.put('/sample/validate/' + IdValidate.parameter(), (req, res, next) => {
return res.status(400).json({status: 'Sample without measurements cannot be valid'});
}
SampleModel.findByIdAndUpdate(req.params.id, {status: globals.status.validated}).lean().exec(err => {
SampleModel.findByIdAndUpdate(req.params.id, {status: globals.status.validated}).log(req).lean().exec(err => {
if (err) return next(err);
res.json({status: 'OK'});
});
@ -224,7 +226,7 @@ router.post('/sample/new', async (req, res, next) => {
if (!await sampleRefCheck(sample, res, next)) return;
if (sample.notes.hasOwnProperty('custom_fields') && Object.keys(sample.notes.custom_fields).length > 0) { // new custom_fields
customFieldsChange(Object.keys(sample.notes.custom_fields), 1);
customFieldsChange(Object.keys(sample.notes.custom_fields), 1, req);
}
if (!_.isEmpty(sample.condition)) { // do not execute check if condition is empty
@ -242,12 +244,14 @@ router.post('/sample/new', async (req, res, next) => {
await new NoteModel(sample.notes).save((err, data) => { // save notes
if (err) return next(err);
db.log(req, 'notes', {_id: data._id}, data.toObject());
delete sample.notes;
sample.note_id = data._id;
sample.user_id = req.authDetails.id;
new SampleModel(sample).save((err, data) => {
if (err) return next(err);
db.log(req, 'samples', {_id: data._id}, data.toObject());
res.json(SampleValidate.output(data.toObject()));
});
});
@ -330,7 +334,7 @@ async function conditionCheck (condition, param, res, next, checkVersion = true)
function sampleRefCheck (sample, res, next) { // validate sample_references, resolves false for invalid reference
return new Promise(resolve => {
if (sample.notes.sample_references.length > 0) { // there are sample_references
if (sample.notes.hasOwnProperty('sample_references') && sample.notes.sample_references.length > 0) { // there are sample_references
let referencesCount = sample.notes.sample_references.length; // count to keep track of running async operations
sample.notes.sample_references.forEach(reference => {
@ -353,17 +357,18 @@ function sampleRefCheck (sample, res, next) { // validate sample_references, re
});
}
function customFieldsChange (fields, amount) { // update custom_fields and respective quantities
function customFieldsChange (fields, amount, req) { // update custom_fields and respective quantities
fields.forEach(field => {
NoteFieldModel.findOneAndUpdate({name: field}, {$inc: {qty: amount}}, {new: true}).lean().exec((err, data: any) => { // check if field exists
NoteFieldModel.findOneAndUpdate({name: field}, {$inc: {qty: amount}} as any, {new: true}).log(req).lean().exec((err, data: any) => { // check if field exists
if (err) return console.error(err);
if (!data) { // new field
new NoteFieldModel({name: field, qty: 1}).save(err => {
new NoteFieldModel({name: field, qty: 1}).save((err, data) => {
if (err) return console.error(err);
db.log(req, 'note_fields', {_id: data._id}, data.toObject());
})
}
else if (data.qty <= 0) { // delete document if field is not used anymore
NoteFieldModel.findOneAndDelete({name: field}).lean().exec(err => {
NoteFieldModel.findOneAndDelete({name: field}).log(req).lean().exec(err => {
if (err) return console.error(err);
});
}