Archived
2

adjusted condition

This commit is contained in:
VLE2FE 2020-05-13 09:56:44 +02:00
parent 63a7a302b6
commit f77d39af34
8 changed files with 82 additions and 22 deletions

View File

@ -2,7 +2,7 @@
parameters: parameters:
- $ref: 'api.yaml#/components/parameters/Id' - $ref: 'api.yaml#/components/parameters/Id'
get: get:
summary: TODO measurement values by id summary: measurement values by id
description: 'Auth: all, levels: read, write, maintain, dev, admin' description: 'Auth: all, levels: read, write, maintain, dev, admin'
tags: tags:
- /measurement - /measurement

View File

@ -6,7 +6,8 @@ const ConditionSchema = new mongoose.Schema({
sample_id: {type: mongoose.Schema.Types.ObjectId, ref: SampleModel}, sample_id: {type: mongoose.Schema.Types.ObjectId, ref: SampleModel},
number: String, number: String,
parameters: mongoose.Schema.Types.Mixed, parameters: mongoose.Schema.Types.Mixed,
treatment_template: {type: mongoose.Schema.Types.ObjectId, ref: TreatmentTemplateModel} treatment_template: {type: mongoose.Schema.Types.ObjectId, ref: TreatmentTemplateModel},
status: Number
}); });
export default mongoose.model('condition', ConditionSchema); export default mongoose.model('condition', ConditionSchema);

View File

@ -5,8 +5,8 @@ import MeasurementTemplateModel from './measurement_template';
const MeasurementSchema = new mongoose.Schema({ const MeasurementSchema = new mongoose.Schema({
condition_id: {type: mongoose.Schema.Types.ObjectId, ref: ConditionModel}, condition_id: {type: mongoose.Schema.Types.ObjectId, ref: ConditionModel},
values: mongoose.Schema.Types.Mixed, values: mongoose.Schema.Types.Mixed,
status: Number, measurement_template: {type: mongoose.Schema.Types.ObjectId, ref: MeasurementTemplateModel},
measurement_template: {type: mongoose.Schema.Types.ObjectId, ref: MeasurementTemplateModel} status: Number
}); });
export default mongoose.model('measurement', MeasurementSchema); export default mongoose.model('measurement', MeasurementSchema);

View File

@ -1,7 +1,7 @@
import should from 'should/as-function'; import should from 'should/as-function';
import ConditionModel from '../models/condition'; import ConditionModel from '../models/condition';
import TestHelper from "../test/helper"; import TestHelper from "../test/helper";
// TODO: status
describe('/condition', () => { describe('/condition', () => {
let server; let server;
@ -70,8 +70,32 @@ describe('/condition', () => {
url: '/condition/700000000000000000000001', url: '/condition/700000000000000000000001',
auth: {basic: 'janedoe'}, auth: {basic: 'janedoe'},
httpStatus: 200, httpStatus: 200,
req: {parameters: {material: 'copper', weeks: 3}}, req: {parameters: {material: 'copper', weeks: 3}}
res: {_id: '700000000000000000000001', sample_id: '400000000000000000000001', number: 'B1', treatment_template: '200000000000000000000001', parameters: {material: 'copper', weeks: 3}} }).end((err, res) => {
if (err) return done(err);
should(res.body).be.eql({_id: '700000000000000000000001', sample_id: '400000000000000000000001', number: 'B1', treatment_template: '200000000000000000000001', parameters: {material: 'copper', weeks: 3}});
ConditionModel.findById('700000000000000000000001').lean().exec((err, data) => {
if (err) return done(err);
should(data).have.property('status', 10);
done();
});
});
});
it('keeps only one unchanged parameter', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/condition/700000000000000000000001',
auth: {basic: 'janedoe'},
httpStatus: 200,
req: {parameters: {material: 'copper'}}
}).end((err, res) => {
if (err) return done(err);
should(res.body).be.eql({_id: '700000000000000000000001', sample_id: '400000000000000000000001', number: 'B1', treatment_template: '200000000000000000000001', parameters: {material: 'copper', weeks: 3}});
ConditionModel.findById('700000000000000000000001').lean().exec((err, data) => {
if (err) return done(err);
should(data).have.property('status', 10);
done();
});
}); });
}); });
it('changes the given properties', done => { it('changes the given properties', done => {
@ -86,9 +110,11 @@ describe('/condition', () => {
should(res.body).be.eql({_id: '700000000000000000000001', sample_id: '400000000000000000000001', number: 'B1', treatment_template: '200000000000000000000001', parameters: {material: 'hot air', weeks: 10}}); should(res.body).be.eql({_id: '700000000000000000000001', sample_id: '400000000000000000000001', number: 'B1', treatment_template: '200000000000000000000001', parameters: {material: 'hot air', weeks: 10}});
ConditionModel.findById('700000000000000000000001').lean().exec((err, data: any) => { ConditionModel.findById('700000000000000000000001').lean().exec((err, data: any) => {
if (err) return done(err); if (err) return done(err);
should(data).have.only.keys('_id', 'sample_id', 'number', 'parameters', 'treatment_template', 'status', '__v');
should(data.sample_id.toString()).be.eql('400000000000000000000001'); should(data.sample_id.toString()).be.eql('400000000000000000000001');
should(data).have.property('number', 'B1'); should(data).have.property('number', 'B1');
should(data.treatment_template.toString()).be.eql('200000000000000000000001'); should(data.treatment_template.toString()).be.eql('200000000000000000000001');
should(data).have.property('status', 0);
should(data).have.property('parameters'); should(data).have.property('parameters');
should(data.parameters).have.property('material', 'hot air'); should(data.parameters).have.property('material', 'hot air');
should(data.parameters).have.property('weeks', 10); should(data.parameters).have.property('weeks', 10);
@ -205,7 +231,7 @@ describe('/condition', () => {
// TODO: rewrite delete methods -> set status for every database collection // TODO: rewrite delete methods -> set status for every database collection
describe('DELETE /condition/{id}', () => { describe('DELETE /condition/{id}', () => {
it('deletes the condition', done => { it('sets the status to deleted', done => {
TestHelper.request(server, done, { TestHelper.request(server, done, {
method: 'delete', method: 'delete',
url: '/condition/700000000000000000000002', url: '/condition/700000000000000000000002',
@ -214,14 +240,21 @@ describe('/condition', () => {
}).end((err, res) => { }).end((err, res) => {
if (err) return done(err); if (err) return done(err);
should(res.body).be.eql({status: 'OK'}); should(res.body).be.eql({status: 'OK'});
ConditionModel.findById('700000000000000000000002').lean().exec((err, data) => { ConditionModel.findById('700000000000000000000002').lean().exec((err, data: any) => {
if (err) return done(err); if (err) return done(err);
should(data).be.null(); should(data).have.only.keys('_id', 'sample_id', 'number', 'parameters', 'treatment_template', 'status', '__v');
should(data.sample_id.toString()).be.eql('400000000000000000000002');
should(data).have.property('number', 'B1');
should(data.treatment_template.toString()).be.eql('200000000000000000000001');
should(data).have.property('status', -1);
should(data).have.property('parameters');
should(data.parameters).have.property('material', 'copper');
should(data.parameters).have.property('weeks', 3);
done(); done();
}); });
}); });
}); });
it('rejects a deleting a condition referenced by measurements'); it('rejects a deleting a condition referenced by measurements'); // TODO
it('rejects an invalid id', done => { it('rejects an invalid id', done => {
TestHelper.request(server, done, { TestHelper.request(server, done, {
method: 'delete', method: 'delete',
@ -315,11 +348,11 @@ describe('/condition', () => {
if (err) return done(err); if (err) return done(err);
ConditionModel.findById(res.body._id).lean().exec((err, data: any) => { ConditionModel.findById(res.body._id).lean().exec((err, data: any) => {
if (err) return done(err); if (err) return done(err);
should(data).have.only.keys('_id', 'sample_id', 'number', 'parameters', 'treatment_template', '__v'); should(data).have.only.keys('_id', 'sample_id', 'number', 'parameters', 'treatment_template', 'status', '__v');
should(data).have.property('_id');
should(data.sample_id.toString()).be.eql('400000000000000000000002'); should(data.sample_id.toString()).be.eql('400000000000000000000002');
should(data).have.property('number', 'B2'); should(data).have.property('number', 'B2');
should(data.treatment_template.toString()).be.eql('200000000000000000000001'); should(data.treatment_template.toString()).be.eql('200000000000000000000001');
should(data).have.property('status', 0);
should(data).have.property('parameters'); should(data).have.property('parameters');
should(data.parameters).have.property('material', 'hot air'); should(data.parameters).have.property('material', 'hot air');
should(data.parameters).have.property('weeks', 10); should(data.parameters).have.property('weeks', 10);

View File

@ -1,5 +1,6 @@
import express from 'express'; import express from 'express';
import mongoose from 'mongoose'; import mongoose from 'mongoose';
import _ from 'lodash';
import ConditionValidate from './validate/condition'; import ConditionValidate from './validate/condition';
import ParametersValidate from './validate/parameters'; import ParametersValidate from './validate/parameters';
@ -30,7 +31,6 @@ router.put('/condition/' + IdValidate.parameter(), async (req, res, next) => {
if (!req.auth(res, ['write', 'maintain', 'dev', 'admin'], 'basic')) return; if (!req.auth(res, ['write', 'maintain', 'dev', 'admin'], 'basic')) return;
const {error, value: condition} = ConditionValidate.input(req.body, 'change'); const {error, value: condition} = ConditionValidate.input(req.body, 'change');
console.log(error);
if (error) return res400(error, res); if (error) return res400(error, res);
const data = await ConditionModel.findById(req.params.id).lean().exec().catch(err => {next(err);}) as any; const data = await ConditionModel.findById(req.params.id).lean().exec().catch(err => {next(err);}) as any;
@ -40,11 +40,15 @@ router.put('/condition/' + IdValidate.parameter(), async (req, res, next) => {
if (!data) { if (!data) {
res.status(404).json({status: 'Not found'}); res.status(404).json({status: 'Not found'});
} }
// add properties needed for sampleIdCheck
condition.treatment_template = data.treatment_template; condition.treatment_template = data.treatment_template;
condition.sample_id = data.sample_id; condition.sample_id = data.sample_id;
if (!await sampleIdCheck(condition, req, res, next)) return; if (!await sampleIdCheck(condition, req, res, next)) return;
if (condition.parameters) { if (condition.parameters) {
condition.parameters = Object.assign(data.parameters, condition.parameters); condition.parameters = _.assign({}, data.parameters, condition.parameters);
if (!_.isEqual(condition.parameters, data.parameters)) {
condition.status = 0;
}
} }
if (!await treatmentCheck(condition, 'change', res, next)) return; if (!await treatmentCheck(condition, 'change', res, next)) return;
@ -63,7 +67,7 @@ router.delete('/condition/' + IdValidate.parameter(), (req, res, next) => {
res.status(404).json({status: 'Not found'}); res.status(404).json({status: 'Not found'});
} }
if (!await sampleIdCheck(data, req, res, next)) return; if (!await sampleIdCheck(data, req, res, next)) return;
await ConditionModel.findByIdAndDelete(req.params.id).lean().exec(async err => { await ConditionModel.findByIdAndUpdate(req.params.id, {status: -1}).lean().exec(err => {
if (err) return next(err); if (err) return next(err);
res.json({status: 'OK'}); res.json({status: 'OK'});
}); });
@ -80,6 +84,7 @@ router.post('/condition/new', async (req, res, next) => {
if (!await numberCheck(condition, res, next)) return; if (!await numberCheck(condition, res, next)) return;
if (!await treatmentCheck(condition, 'new', res, next)) return; if (!await treatmentCheck(condition, 'new', res, next)) return;
condition.status = 0;
await new ConditionModel(condition).save((err, data) => { await new ConditionModel(condition).save((err, data) => {
if (err) return next(err); if (err) return next(err);
res.json(ConditionValidate.output(data.toObject())); res.json(ConditionValidate.output(data.toObject()));
@ -119,7 +124,6 @@ async function treatmentCheck (condition, param, res, next) {
// validate parameters // validate parameters
const {error, value: ignore} = ParametersValidate.input(condition.parameters, treatmentData.parameters, param); const {error, value: ignore} = ParametersValidate.input(condition.parameters, treatmentData.parameters, param);
console.log(error);
if (error) {res400(error, res); return false;} if (error) {res400(error, res); return false;}
return true; return true;
} }

View File

@ -74,6 +74,24 @@ describe('/measurement', () => {
if (err) return done(err); if (err) return done(err);
should(res.body).be.eql({_id: '800000000000000000000001', condition_id: '700000000000000000000001', values: {dpt: [[3997.12558,98.00555],[3995.08519,98.03253],[3993.04480,98.02657]]}, measurement_template: '300000000000000000000001'}); should(res.body).be.eql({_id: '800000000000000000000001', condition_id: '700000000000000000000001', values: {dpt: [[3997.12558,98.00555],[3995.08519,98.03253],[3993.04480,98.02657]]}, measurement_template: '300000000000000000000001'});
MeasurementModel.findById('800000000000000000000001').lean().exec((err, data: any) => { MeasurementModel.findById('800000000000000000000001').lean().exec((err, data: any) => {
if (err) return done(err);
should(data).have.property('status', 10);
done();
});
});
});
it('keeps only one unchanged value', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/measurement/800000000000000000000002',
auth: {basic: 'janedoe'},
httpStatus: 200,
req: {values: {'weight %': 0.5}}
}).end((err, res) => {
if (err) return done(err);
should(res.body).be.eql({_id: '800000000000000000000002', condition_id: '700000000000000000000002', values: {'weight %': 0.5, 'standard deviation': 0.2}, measurement_template: '300000000000000000000002'});
MeasurementModel.findById('800000000000000000000002').lean().exec((err, data: any) => {
if (err) return done(err);
should(data).have.property('status', 10); should(data).have.property('status', 10);
done(); done();
}); });

View File

@ -41,12 +41,12 @@ router.put('/measurement/' + IdValidate.parameter(), async (req, res, next) => {
// add properties needed for conditionIdCheck // add properties needed for conditionIdCheck
measurement.measurement_template = data.measurement_template; measurement.measurement_template = data.measurement_template;
measurement.condition_id = data.condition_id; measurement.condition_id = data.condition_id;
if (measurement.hasOwnProperty('values') && !_.isEqual(measurement.values, data.values)) {
measurement.status = 0;
}
if (!await conditionIdCheck(measurement, req, res, next)) return; if (!await conditionIdCheck(measurement, req, res, next)) return;
if (measurement.values) { if (measurement.values) {
measurement.values = Object.assign(data.values, measurement.values); measurement.values = _.assign({}, data.values, measurement.values);
if (!_.isEqual(measurement.values, data.values)) {
measurement.status = 0;
}
} }
if (!await templateCheck(measurement, 'change', res, next)) return; if (!await templateCheck(measurement, 'change', res, next)) return;
await MeasurementModel.findByIdAndUpdate(req.params.id, measurement, {new: true}).lean().exec((err, data) => { await MeasurementModel.findByIdAndUpdate(req.params.id, measurement, {new: true}).lean().exec((err, data) => {
@ -64,7 +64,7 @@ router.delete('/measurement/' + IdValidate.parameter(), (req, res, next) => {
res.status(404).json({status: 'Not found'}); res.status(404).json({status: 'Not found'});
} }
if (!await conditionIdCheck(data, req, res, next)) return; if (!await conditionIdCheck(data, req, res, next)) return;
await MeasurementModel.findByIdAndUpdate(req.params.id, {status: -1}).lean().exec(async err => { await MeasurementModel.findByIdAndUpdate(req.params.id, {status: -1}).lean().exec(err => {
if (err) return next(err); if (err) return next(err);
res.json({status: 'OK'}); res.json({status: 'OK'});
}); });

View File

@ -193,6 +193,7 @@
"weeks": 3 "weeks": 3
}, },
"treatment_template": {"$oid":"200000000000000000000001"}, "treatment_template": {"$oid":"200000000000000000000001"},
"status": 10,
"__v": 0 "__v": 0
}, },
{ {
@ -204,6 +205,7 @@
"weeks": 3 "weeks": 3
}, },
"treatment_template": {"$oid":"200000000000000000000001"}, "treatment_template": {"$oid":"200000000000000000000001"},
"status": 10,
"__v": 0 "__v": 0
}, },
{ {
@ -215,6 +217,7 @@
"weeks": 3 "weeks": 3
}, },
"treatment_template": {"$oid":"200000000000000000000001"}, "treatment_template": {"$oid":"200000000000000000000001"},
"status": 10,
"__v": 0 "__v": 0
}, },
{ {
@ -226,6 +229,7 @@
"weeks": 5 "weeks": 5
}, },
"treatment_template": {"$oid":"200000000000000000000001"}, "treatment_template": {"$oid":"200000000000000000000001"},
"status": 10,
"__v": 0 "__v": 0
} }
], ],