adapted existing /sample methods to condition, removed /condition route
This commit is contained in:
@ -5,10 +5,15 @@ import SampleValidate from './validate/sample';
|
||||
import NoteFieldValidate from './validate/note_field';
|
||||
import res400 from './validate/res400';
|
||||
import SampleModel from '../models/sample'
|
||||
import MeasurementModel from '../models/measurement';
|
||||
import MaterialModel from '../models/material';
|
||||
import NoteModel from '../models/note';
|
||||
import NoteFieldModel from '../models/note_field';
|
||||
import IdValidate from './validate/id';
|
||||
import mongoose from "mongoose";
|
||||
import ConditionTemplateModel from '../models/condition_template';
|
||||
import ParametersValidate from './validate/parameters';
|
||||
import globals from '../globals';
|
||||
|
||||
|
||||
const router = express.Router();
|
||||
@ -16,7 +21,7 @@ const router = express.Router();
|
||||
router.get('/samples', (req, res, next) => {
|
||||
if (!req.auth(res, ['read', 'write', 'maintain', 'dev', 'admin'], 'all')) return;
|
||||
|
||||
SampleModel.find({status: 10}).lean().exec((err, data) => {
|
||||
SampleModel.find({status: globals.status.validated}).lean().exec((err, data) => {
|
||||
if (err) return next(err);
|
||||
res.json(_.compact(data.map(e => SampleValidate.output(e)))); // validate all and filter null values from validation errors
|
||||
})
|
||||
@ -25,17 +30,32 @@ router.get('/samples', (req, res, next) => {
|
||||
router.get('/samples/:group(new|deleted)', (req, res, next) => {
|
||||
if (!req.auth(res, ['maintain', 'admin'], 'basic')) return;
|
||||
|
||||
let status;
|
||||
switch (req.params.group) {
|
||||
case 'new': status = 0;
|
||||
break;
|
||||
case 'deleted': status = -1;
|
||||
break;
|
||||
}
|
||||
SampleModel.find({status: status}).lean().exec((err, data) => {
|
||||
SampleModel.find({status: globals.status[req.params.group]}).lean().exec((err, data) => {
|
||||
if (err) return next(err);
|
||||
res.json(_.compact(data.map(e => SampleValidate.output(e)))); // validate all and filter null values from validation errors
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
router.get('/sample/' + IdValidate.parameter(), (req, res, next) => {
|
||||
if (!req.auth(res, ['read', 'write', 'maintain', 'dev', 'admin'], 'all')) return;
|
||||
|
||||
SampleModel.findById(req.params.id).populate('material_id').populate('user_id', 'name').populate('note_id').lean().exec((err, sampleData: any) => {
|
||||
if (err) return next(err);
|
||||
|
||||
if (sampleData) {
|
||||
if (sampleData.status ===globals.status.deleted && !req.auth(res, ['maintain', 'admin'], 'all')) return; // deleted samples only available for maintain/admin
|
||||
sampleData.material = sampleData.material_id; // map data to right keys
|
||||
sampleData.user = sampleData.user_id.name;
|
||||
sampleData.notes = sampleData.note_id ? sampleData.note_id : {};
|
||||
MeasurementModel.find({sample_id: mongoose.Types.ObjectId(req.params.id)}).lean().exec((err, data) => {
|
||||
sampleData.measurements = data;
|
||||
res.json(SampleValidate.output(sampleData, 'details'));
|
||||
});
|
||||
}
|
||||
else {
|
||||
res.status(404).json({status: 'Not found'});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
router.put('/sample/' + IdValidate.parameter(), (req, res, next) => {
|
||||
@ -60,6 +80,10 @@ router.put('/sample/' + IdValidate.parameter(), (req, res, next) => {
|
||||
if (!await materialCheck(sample, res, next, sampleData.material_id)) return;
|
||||
}
|
||||
|
||||
if (sample.hasOwnProperty('condition') && !(_.isEmpty(sample.condition) && _.isEmpty(sampleData.condition))) { // do not execute check if condition is and was empty
|
||||
if (!await conditionCheck(sample.condition, 'change', res, next)) return;
|
||||
}
|
||||
|
||||
if (sample.hasOwnProperty('notes')) {
|
||||
let newNotes = true;
|
||||
if (sampleData.note_id !== null) { // old notes data exists
|
||||
@ -89,10 +113,10 @@ router.put('/sample/' + IdValidate.parameter(), (req, res, next) => {
|
||||
|
||||
// check for changes
|
||||
if (!_.isEqual(_.pick(IdValidate.stringify(sampleData), _.keys(sample)), _.omit(sample, ['notes']))) {
|
||||
sample.status = 0;
|
||||
sample.status = globals.status.new;
|
||||
}
|
||||
|
||||
await SampleModel.findByIdAndUpdate(req.params.id, sample, {new: true}).lean().exec((err, data) => {
|
||||
await SampleModel.findByIdAndUpdate(req.params.id, sample, {new: true}).lean().exec((err, data: any) => {
|
||||
if (err) return next(err);
|
||||
res.json(SampleValidate.output(data));
|
||||
});
|
||||
@ -112,7 +136,7 @@ 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: -1}).lean().exec(err => { // set sample status
|
||||
await SampleModel.findByIdAndUpdate(req.params.id, {status:globals.status.deleted}).lean().exec(err => { // set sample status
|
||||
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
|
||||
@ -133,6 +157,10 @@ router.delete('/sample/' + IdValidate.parameter(), (req, res, next) => {
|
||||
router.post('/sample/new', async (req, res, next) => {
|
||||
if (!req.auth(res, ['write', 'maintain', 'dev', 'admin'], 'basic')) return;
|
||||
|
||||
if (!req.body.hasOwnProperty('condition')) { // add empty condition if not specified
|
||||
req.body.condition = {};
|
||||
}
|
||||
|
||||
const {error, value: sample} = SampleValidate.input(req.body, 'new');
|
||||
if (error) return res400(error, res);
|
||||
|
||||
@ -143,7 +171,11 @@ router.post('/sample/new', async (req, res, next) => {
|
||||
customFieldsChange(Object.keys(sample.notes.custom_fields), 1);
|
||||
}
|
||||
|
||||
sample.status = 0; // set status to new
|
||||
if (!_.isEmpty(sample.condition)) { // do not execute check if condition is empty
|
||||
if (!await conditionCheck(sample.condition, 'change', res, next)) return;
|
||||
}
|
||||
|
||||
sample.status = globals.status.new; // set status to new
|
||||
sample.number = await numberGenerate(sample, req, res, next);
|
||||
if (!sample.number) return;
|
||||
|
||||
@ -152,6 +184,7 @@ router.post('/sample/new', async (req, res, next) => {
|
||||
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);
|
||||
res.json(SampleValidate.output(data.toObject()));
|
||||
@ -172,14 +205,15 @@ router.get('/sample/notes/fields', (req, res, next) => {
|
||||
module.exports = router;
|
||||
|
||||
|
||||
async function numberGenerate (sample, req, res, next) { // generate number, returns false on error
|
||||
async function numberGenerate (sample, req, res, next) { // generate number in format Location32, returns false on error
|
||||
const sampleData = await SampleModel
|
||||
.find({number: new RegExp('^' + req.authDetails.location + '[0-9]+$', 'm')})
|
||||
.findOne({number: new RegExp('^' + req.authDetails.location + '[0-9]+$', 'm')})
|
||||
.sort({number: -1})
|
||||
.lean()
|
||||
.exec()
|
||||
.catch(err => next(err));
|
||||
if (sampleData instanceof Error) return false;
|
||||
return req.authDetails.location + (sampleData.length > 0 ? Number(sampleData[0].number.replace(/[^0-9]+/g, '')) + 1 : 1);
|
||||
return req.authDetails.location + (sampleData ? Number(sampleData.number.replace(/[^0-9]+/g, '')) + 1 : 1);
|
||||
}
|
||||
|
||||
async function materialCheck (sample, res, next, id = sample.material_id) { // validate material_id and color, returns false if invalid
|
||||
@ -196,13 +230,31 @@ async function materialCheck (sample, res, next, id = sample.material_id) { //
|
||||
return true;
|
||||
}
|
||||
|
||||
async function conditionCheck (condition, param, res, next) { // validate treatment template, returns false if invalid, otherwise template data
|
||||
if (!condition.condition_template || !IdValidate.valid(condition.condition_template)) { // template id not found
|
||||
res.status(400).json({status: 'Condition template not available'});
|
||||
return false;
|
||||
}
|
||||
const conditionData = await ConditionTemplateModel.findById(condition.condition_template).lean().exec().catch(err => next(err)) as any;
|
||||
if (conditionData instanceof Error) return false;
|
||||
if (!conditionData) { // template not found
|
||||
res.status(400).json({status: 'Condition template not available'});
|
||||
return false;
|
||||
}
|
||||
|
||||
// validate parameters
|
||||
const {error, value: ignore} = ParametersValidate.input(_.omit(condition, 'condition_template'), conditionData.parameters, param);
|
||||
if (error) {res400(error, res); return false;}
|
||||
return conditionData;
|
||||
}
|
||||
|
||||
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
|
||||
let referencesCount = sample.notes.sample_references.length; // count to keep track of running async operations
|
||||
|
||||
sample.notes.sample_references.forEach(reference => {
|
||||
SampleModel.findById(reference.id).lean().exec((err, data) => {
|
||||
SampleModel.findById(reference.sample_id).lean().exec((err, data) => {
|
||||
if (err) {next(err); resolve(false)}
|
||||
if (!data) {
|
||||
res.status(400).json({status: 'Sample reference not available'});
|
||||
@ -230,7 +282,7 @@ function customFieldsChange (fields, amount) { // update custom_fields and resp
|
||||
if (err) return console.error(err);
|
||||
})
|
||||
}
|
||||
else if (data.qty <= 0) {
|
||||
else if (data.qty <= 0) { // delete document if field is not used anymore
|
||||
NoteFieldModel.findOneAndDelete({name: field}).lean().exec(err => {
|
||||
if (err) return console.error(err);
|
||||
});
|
||||
|
Reference in New Issue
Block a user