From f77d39af3478afdae181ef1ef38ef6d4026e3e1c Mon Sep 17 00:00:00 2001 From: VLE2FE Date: Wed, 13 May 2020 09:56:44 +0200 Subject: [PATCH] adjusted condition --- api/measurement.yaml | 2 +- src/models/condition.ts | 3 +- src/models/measurement.ts | 4 +-- src/routes/condition.spec.ts | 51 ++++++++++++++++++++++++++++------ src/routes/condition.ts | 12 +++++--- src/routes/measurement.spec.ts | 18 ++++++++++++ src/routes/measurement.ts | 10 +++---- src/test/db.json | 4 +++ 8 files changed, 82 insertions(+), 22 deletions(-) diff --git a/api/measurement.yaml b/api/measurement.yaml index 4386a15..d9fcd40 100644 --- a/api/measurement.yaml +++ b/api/measurement.yaml @@ -2,7 +2,7 @@ parameters: - $ref: 'api.yaml#/components/parameters/Id' get: - summary: TODO measurement values by id + summary: measurement values by id description: 'Auth: all, levels: read, write, maintain, dev, admin' tags: - /measurement diff --git a/src/models/condition.ts b/src/models/condition.ts index 1e24daf..e0f79da 100644 --- a/src/models/condition.ts +++ b/src/models/condition.ts @@ -6,7 +6,8 @@ const ConditionSchema = new mongoose.Schema({ sample_id: {type: mongoose.Schema.Types.ObjectId, ref: SampleModel}, number: String, 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); \ No newline at end of file diff --git a/src/models/measurement.ts b/src/models/measurement.ts index 401103b..ac0ef20 100644 --- a/src/models/measurement.ts +++ b/src/models/measurement.ts @@ -5,8 +5,8 @@ import MeasurementTemplateModel from './measurement_template'; const MeasurementSchema = new mongoose.Schema({ condition_id: {type: mongoose.Schema.Types.ObjectId, ref: ConditionModel}, 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); \ No newline at end of file diff --git a/src/routes/condition.spec.ts b/src/routes/condition.spec.ts index ec71ac3..2967108 100644 --- a/src/routes/condition.spec.ts +++ b/src/routes/condition.spec.ts @@ -1,7 +1,7 @@ import should from 'should/as-function'; import ConditionModel from '../models/condition'; import TestHelper from "../test/helper"; -// TODO: status + describe('/condition', () => { let server; @@ -70,8 +70,32 @@ describe('/condition', () => { url: '/condition/700000000000000000000001', auth: {basic: 'janedoe'}, httpStatus: 200, - req: {parameters: {material: 'copper', weeks: 3}}, - res: {_id: '700000000000000000000001', sample_id: '400000000000000000000001', number: 'B1', treatment_template: '200000000000000000000001', parameters: {material: 'copper', weeks: 3}} + req: {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 => { @@ -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}}); ConditionModel.findById('700000000000000000000001').lean().exec((err, data: any) => { 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).have.property('number', 'B1'); should(data.treatment_template.toString()).be.eql('200000000000000000000001'); + should(data).have.property('status', 0); should(data).have.property('parameters'); should(data.parameters).have.property('material', 'hot air'); should(data.parameters).have.property('weeks', 10); @@ -205,7 +231,7 @@ describe('/condition', () => { // TODO: rewrite delete methods -> set status for every database collection describe('DELETE /condition/{id}', () => { - it('deletes the condition', done => { + it('sets the status to deleted', done => { TestHelper.request(server, done, { method: 'delete', url: '/condition/700000000000000000000002', @@ -214,14 +240,21 @@ describe('/condition', () => { }).end((err, res) => { if (err) return done(err); 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); - 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(); }); }); }); - 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 => { TestHelper.request(server, done, { method: 'delete', @@ -315,11 +348,11 @@ describe('/condition', () => { if (err) return done(err); ConditionModel.findById(res.body._id).lean().exec((err, data: any) => { if (err) return done(err); - should(data).have.only.keys('_id', 'sample_id', 'number', 'parameters', 'treatment_template', '__v'); - should(data).have.property('_id'); + 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', 'B2'); should(data.treatment_template.toString()).be.eql('200000000000000000000001'); + should(data).have.property('status', 0); should(data).have.property('parameters'); should(data.parameters).have.property('material', 'hot air'); should(data.parameters).have.property('weeks', 10); diff --git a/src/routes/condition.ts b/src/routes/condition.ts index a5639e6..8acc2f2 100644 --- a/src/routes/condition.ts +++ b/src/routes/condition.ts @@ -1,5 +1,6 @@ import express from 'express'; import mongoose from 'mongoose'; +import _ from 'lodash'; import ConditionValidate from './validate/condition'; 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; const {error, value: condition} = ConditionValidate.input(req.body, 'change'); - console.log(error); if (error) return res400(error, res); 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) { res.status(404).json({status: 'Not found'}); } + // add properties needed for sampleIdCheck condition.treatment_template = data.treatment_template; condition.sample_id = data.sample_id; if (!await sampleIdCheck(condition, req, res, next)) return; 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; @@ -63,7 +67,7 @@ router.delete('/condition/' + IdValidate.parameter(), (req, res, next) => { res.status(404).json({status: 'Not found'}); } 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); 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 treatmentCheck(condition, 'new', res, next)) return; + condition.status = 0; await new ConditionModel(condition).save((err, data) => { if (err) return next(err); res.json(ConditionValidate.output(data.toObject())); @@ -119,7 +124,6 @@ async function treatmentCheck (condition, param, res, next) { // validate parameters const {error, value: ignore} = ParametersValidate.input(condition.parameters, treatmentData.parameters, param); - console.log(error); if (error) {res400(error, res); return false;} return true; } \ No newline at end of file diff --git a/src/routes/measurement.spec.ts b/src/routes/measurement.spec.ts index bba7ca8..7a604d2 100644 --- a/src/routes/measurement.spec.ts +++ b/src/routes/measurement.spec.ts @@ -74,6 +74,24 @@ describe('/measurement', () => { 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'}); 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); done(); }); diff --git a/src/routes/measurement.ts b/src/routes/measurement.ts index 85bea0e..4aa89c1 100644 --- a/src/routes/measurement.ts +++ b/src/routes/measurement.ts @@ -41,12 +41,12 @@ router.put('/measurement/' + IdValidate.parameter(), async (req, res, next) => { // add properties needed for conditionIdCheck measurement.measurement_template = data.measurement_template; 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 (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; 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'}); } 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); res.json({status: 'OK'}); }); diff --git a/src/test/db.json b/src/test/db.json index 01b06b3..621b385 100644 --- a/src/test/db.json +++ b/src/test/db.json @@ -193,6 +193,7 @@ "weeks": 3 }, "treatment_template": {"$oid":"200000000000000000000001"}, + "status": 10, "__v": 0 }, { @@ -204,6 +205,7 @@ "weeks": 3 }, "treatment_template": {"$oid":"200000000000000000000001"}, + "status": 10, "__v": 0 }, { @@ -215,6 +217,7 @@ "weeks": 3 }, "treatment_template": {"$oid":"200000000000000000000001"}, + "status": 10, "__v": 0 }, { @@ -226,6 +229,7 @@ "weeks": 5 }, "treatment_template": {"$oid":"200000000000000000000001"}, + "status": 10, "__v": 0 } ],