2020-04-29 12:10:27 +02:00
|
|
|
import express from 'express';
|
2020-05-12 17:15:36 +02:00
|
|
|
import _ from 'lodash';
|
2020-04-29 12:10:27 +02:00
|
|
|
|
|
|
|
import MaterialValidate from './validate/material';
|
|
|
|
import MaterialModel from '../models/material'
|
2020-05-08 09:58:12 +02:00
|
|
|
import SampleModel from '../models/sample';
|
2020-04-29 12:10:27 +02:00
|
|
|
import IdValidate from './validate/id';
|
2020-05-07 21:55:29 +02:00
|
|
|
import res400 from './validate/res400';
|
2020-05-08 09:58:12 +02:00
|
|
|
import mongoose from 'mongoose';
|
2020-04-29 12:10:27 +02:00
|
|
|
|
2020-05-13 17:28:18 +02:00
|
|
|
|
2020-04-29 12:10:27 +02:00
|
|
|
|
|
|
|
const router = express.Router();
|
|
|
|
|
|
|
|
router.get('/materials', (req, res, next) => {
|
|
|
|
if (!req.auth(res, ['read', 'write', 'maintain', 'dev', 'admin'], 'all')) return;
|
|
|
|
|
2020-05-13 14:18:15 +02:00
|
|
|
MaterialModel.find({status: 10}).lean().exec((err, data) => {
|
2020-05-06 14:39:04 +02:00
|
|
|
if (err) return next(err);
|
2020-05-12 17:15:36 +02:00
|
|
|
res.json(_.compact(data.map(e => MaterialValidate.output(e)))); // validate all and filter null values from validation errors
|
2020-04-29 12:10:27 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
router.get('/material/' + IdValidate.parameter(), (req, res, next) => {
|
|
|
|
if (!req.auth(res, ['read', 'write', 'maintain', 'dev', 'admin'], 'all')) return;
|
|
|
|
|
|
|
|
MaterialModel.findById(req.params.id).lean().exec((err, data) => {
|
2020-05-06 14:39:04 +02:00
|
|
|
if (err) return next(err);
|
2020-04-29 12:10:27 +02:00
|
|
|
if (data) {
|
|
|
|
res.json(MaterialValidate.output(data));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
res.status(404).json({status: 'Not found'});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-04-29 16:09:31 +02:00
|
|
|
router.put('/material/' + IdValidate.parameter(), (req, res, next) => {
|
|
|
|
if (!req.auth(res, ['write', 'maintain', 'dev', 'admin'], 'basic')) return;
|
|
|
|
|
|
|
|
const {error, value: material} = MaterialValidate.input(req.body, 'change');
|
2020-05-07 21:55:29 +02:00
|
|
|
if (error) return res400(error, res);
|
2020-04-29 16:09:31 +02:00
|
|
|
|
2020-05-13 14:18:15 +02:00
|
|
|
MaterialModel.findById(req.params.id).lean().exec(async (err, materialData: any) => {
|
|
|
|
if (!materialData) {
|
|
|
|
return res.status(404).json({status: 'Not found'});
|
|
|
|
}
|
|
|
|
if (material.hasOwnProperty('name') && material.name !== materialData.name) {
|
|
|
|
if (!await nameCheck(material, res, next)) return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for changes
|
|
|
|
if (!_.isEqual(_.pick(IdValidate.stringify(materialData), _.keys(material)), material)) {
|
|
|
|
material.status = 0;
|
|
|
|
}
|
2020-04-29 16:09:31 +02:00
|
|
|
|
2020-05-13 14:18:15 +02:00
|
|
|
await MaterialModel.findByIdAndUpdate(req.params.id, material, {new: true}).lean().exec((err, data) => {
|
2020-05-06 14:39:04 +02:00
|
|
|
if (err) return next(err);
|
2020-05-13 14:18:15 +02:00
|
|
|
res.json(MaterialValidate.output(data));
|
2020-04-29 16:09:31 +02:00
|
|
|
});
|
2020-05-13 14:18:15 +02:00
|
|
|
});
|
2020-04-29 16:09:31 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
router.delete('/material/' + IdValidate.parameter(), (req, res, next) => {
|
|
|
|
if (!req.auth(res, ['write', 'maintain', 'dev', 'admin'], 'basic')) return;
|
|
|
|
|
2020-05-08 09:58:12 +02:00
|
|
|
// check if there are still samples referencing this material
|
|
|
|
SampleModel.find({'material_id': new mongoose.Types.ObjectId(req.params.id)}).lean().exec((err, data) => {
|
2020-05-06 14:39:04 +02:00
|
|
|
if (err) return next(err);
|
2020-05-08 09:58:12 +02:00
|
|
|
if (data.length) {
|
|
|
|
return res.status(400).json({status: 'Material still in use'});
|
2020-04-29 16:09:31 +02:00
|
|
|
}
|
2020-05-13 14:18:15 +02:00
|
|
|
MaterialModel.findByIdAndUpdate(req.params.id, {status: -1}).lean().exec((err, data) => {
|
2020-05-08 09:58:12 +02:00
|
|
|
if (err) return next(err);
|
|
|
|
if (data) {
|
2020-05-08 15:12:36 +02:00
|
|
|
res.json({status: 'OK'});
|
2020-05-08 09:58:12 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
res.status(404).json({status: 'Not found'});
|
|
|
|
}
|
|
|
|
});
|
2020-04-29 16:09:31 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-13 14:18:15 +02:00
|
|
|
router.post('/material/new', async (req, res, next) => {
|
2020-04-29 12:10:27 +02:00
|
|
|
if (!req.auth(res, ['write', 'maintain', 'dev', 'admin'], 'basic')) return;
|
|
|
|
|
|
|
|
// validate input
|
|
|
|
const {error, value: material} = MaterialValidate.input(req.body, 'new');
|
2020-05-07 21:55:29 +02:00
|
|
|
if (error) return res400(error, res);
|
2020-04-29 12:10:27 +02:00
|
|
|
|
2020-05-13 14:18:15 +02:00
|
|
|
if (!await nameCheck(material, res, next)) return;
|
2020-04-29 12:10:27 +02:00
|
|
|
|
2020-05-13 14:18:15 +02:00
|
|
|
material.status = 0;
|
|
|
|
await new MaterialModel(material).save((err, data) => {
|
|
|
|
if (err) return next(err);
|
|
|
|
res.json(MaterialValidate.output(data.toObject()));
|
2020-04-29 16:09:31 +02:00
|
|
|
});
|
|
|
|
});
|
2020-04-29 12:10:27 +02:00
|
|
|
|
|
|
|
|
2020-05-13 14:18:15 +02:00
|
|
|
module.exports = router;
|
|
|
|
|
|
|
|
|
|
|
|
async function nameCheck (material, res, next) { // check if name was already taken
|
|
|
|
const materialData = await MaterialModel.findOne({name: material.name}).lean().exec().catch(err => {next(err); return false;}) as any;
|
2020-05-14 15:36:47 +02:00
|
|
|
if (materialData instanceof Error) return false;
|
2020-05-13 14:18:15 +02:00
|
|
|
if (materialData) { // could not find material_id
|
|
|
|
res.status(400).json({status: 'Material name already taken'});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|