2020-05-12 12:15:36 +02:00
import express from 'express' ;
2020-05-12 17:15:36 +02:00
import _ from 'lodash' ;
2020-05-12 12:15:36 +02:00
import MeasurementModel from '../models/measurement' ;
import MeasurementTemplateModel from '../models/measurement_template' ;
import MeasurementValidate from './validate/measurement' ;
import IdValidate from './validate/id' ;
import res400 from './validate/res400' ;
import ParametersValidate from './validate/parameters' ;
2020-05-27 14:31:17 +02:00
import globals from '../globals' ;
2020-05-12 12:15:36 +02:00
const router = express . Router ( ) ;
router . get ( '/measurement/' + IdValidate . parameter ( ) , ( req , res , next ) = > {
if ( ! req . auth ( res , [ 'read' , 'write' , 'maintain' , 'dev' , 'admin' ] , 'all' ) ) return ;
MeasurementModel . findById ( req . params . id ) . lean ( ) . exec ( ( err , data ) = > {
if ( err ) return next ( err ) ;
if ( ! data ) {
return res . status ( 404 ) . json ( { status : 'Not found' } ) ;
}
res . json ( MeasurementValidate . output ( data ) ) ;
} ) ;
} ) ;
2020-05-12 17:15:36 +02:00
router . put ( '/measurement/' + IdValidate . parameter ( ) , async ( req , res , next ) = > {
if ( ! req . auth ( res , [ 'write' , 'maintain' , 'dev' , 'admin' ] , 'basic' ) ) return ;
const { error , value : measurement } = MeasurementValidate . input ( req . body , 'change' ) ;
if ( error ) return res400 ( error , res ) ;
const data = await MeasurementModel . findById ( req . params . id ) . lean ( ) . exec ( ) . catch ( err = > { next ( err ) ; } ) as any ;
2020-05-14 15:36:47 +02:00
if ( data instanceof Error ) return ;
2020-05-12 17:15:36 +02:00
if ( ! data ) {
res . status ( 404 ) . json ( { status : 'Not found' } ) ;
}
2020-05-18 14:47:22 +02:00
2020-05-12 17:15:36 +02:00
// add properties needed for conditionIdCheck
measurement . measurement_template = data . measurement_template ;
measurement . condition_id = data . condition_id ;
if ( ! await conditionIdCheck ( measurement , req , res , next ) ) return ;
2020-05-18 14:47:22 +02:00
// check for changes
2020-05-12 17:15:36 +02:00
if ( measurement . values ) {
2020-05-13 09:56:44 +02:00
measurement . values = _ . assign ( { } , data . values , measurement . values ) ;
if ( ! _ . isEqual ( measurement . values , data . values ) ) {
2020-05-27 14:31:17 +02:00
measurement . status = globals . status . new ; // set status to new
2020-05-13 09:56:44 +02:00
}
2020-05-12 17:15:36 +02:00
}
2020-05-18 14:47:22 +02:00
2020-05-12 17:15:36 +02:00
if ( ! await templateCheck ( measurement , 'change' , res , next ) ) return ;
await MeasurementModel . findByIdAndUpdate ( req . params . id , measurement , { new : true } ) . lean ( ) . exec ( ( err , data ) = > {
if ( err ) return next ( err ) ;
res . json ( MeasurementValidate . output ( data ) ) ;
} ) ;
} ) ;
2020-05-12 17:37:01 +02:00
router . delete ( '/measurement/' + IdValidate . parameter ( ) , ( req , res , next ) = > {
if ( ! req . auth ( res , [ 'write' , 'maintain' , 'dev' , 'admin' ] , 'basic' ) ) return ;
MeasurementModel . findById ( req . params . id ) . lean ( ) . exec ( async ( err , data ) = > {
if ( err ) return next ( err ) ;
if ( ! data ) {
res . status ( 404 ) . json ( { status : 'Not found' } ) ;
}
if ( ! await conditionIdCheck ( data , req , res , next ) ) return ;
2020-05-27 14:31:17 +02:00
await MeasurementModel . findByIdAndUpdate ( req . params . id , { status :globals.status.deleted } ) . lean ( ) . exec ( err = > {
2020-05-12 17:37:01 +02:00
if ( err ) return next ( err ) ;
res . json ( { status : 'OK' } ) ;
} ) ;
} ) ;
} ) ;
2020-05-12 12:15:36 +02:00
router . post ( '/measurement/new' , async ( req , res , next ) = > {
if ( ! req . auth ( res , [ 'write' , 'maintain' , 'dev' , 'admin' ] , 'basic' ) ) return ;
const { error , value : measurement } = MeasurementValidate . input ( req . body , 'new' ) ;
if ( error ) return res400 ( error , res ) ;
if ( ! await conditionIdCheck ( measurement , req , res , next ) ) return ;
if ( ! await templateCheck ( measurement , 'new' , res , next ) ) return ;
2020-05-12 17:15:36 +02:00
measurement . status = 0 ;
2020-05-12 12:15:36 +02:00
await new MeasurementModel ( measurement ) . save ( ( err , data ) = > {
if ( err ) return next ( err ) ;
res . json ( MeasurementValidate . output ( data . toObject ( ) ) ) ;
} ) ;
} ) ;
module . exports = router ;
2020-05-27 14:31:17 +02:00
async function conditionIdCheck ( measurement , req , res , next ) { // validate condition_id, returns false if invalid // TODO
// const sampleData = await ConditionModel.findById(measurement.condition_id).populate('sample_id').lean().exec().catch(err => {next(err); return false;}) as any;
// if (!sampleData) { // sample_id not found
// res.status(400).json({status: 'Condition id not available'});
2020-05-12 12:15:36 +02:00
return false
2020-05-27 14:31:17 +02:00
// }
// if (sampleData.sample_id.user_id.toString() !== req.authDetails.id && !req.auth(res, ['maintain', 'admin'], 'basic')) return false; // sample does not belong to user
// return true;
2020-05-12 12:15:36 +02:00
}
2020-05-18 14:47:22 +02:00
async function templateCheck ( measurement , param , res , next ) { // validate measurement_template and values, param for new/change
2020-05-12 12:15:36 +02:00
const templateData = await MeasurementTemplateModel . findById ( measurement . measurement_template ) . lean ( ) . exec ( ) . catch ( err = > { next ( err ) ; return false ; } ) as any ;
if ( ! templateData ) { // template not found
res . status ( 400 ) . json ( { status : 'Measurement template not available' } ) ;
return false
}
// validate values
const { error , value : ignore } = ParametersValidate . input ( measurement . values , templateData . parameters , param ) ;
if ( error ) { res400 ( error , res ) ; return false ; }
return true ;
}