adjusted condition
This commit is contained in:
		@@ -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);
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
}
 | 
			
		||||
@@ -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();
 | 
			
		||||
        });
 | 
			
		||||
 
 | 
			
		||||
@@ -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'});
 | 
			
		||||
    });
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user