2020-05-06 14:39:04 +02:00
|
|
|
import express from 'express';
|
2020-05-12 17:15:36 +02:00
|
|
|
import _ from 'lodash';
|
2020-05-06 14:39:04 +02:00
|
|
|
|
|
|
|
import SampleValidate from './validate/sample';
|
|
|
|
import NoteFieldValidate from './validate/note_field';
|
2020-05-07 21:55:29 +02:00
|
|
|
import res400 from './validate/res400';
|
2020-05-06 14:39:04 +02:00
|
|
|
import SampleModel from '../models/sample'
|
|
|
|
import MaterialModel from '../models/material';
|
|
|
|
import NoteModel from '../models/note';
|
|
|
|
import NoteFieldModel from '../models/note_field';
|
2020-05-07 21:55:29 +02:00
|
|
|
import IdValidate from './validate/id';
|
2020-05-06 14:39:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
const router = express.Router();
|
|
|
|
|
|
|
|
router.get('/samples', (req, res, next) => {
|
|
|
|
if (!req.auth(res, ['read', 'write', 'maintain', 'dev', 'admin'], 'all')) return;
|
|
|
|
|
2020-05-13 12:06:28 +02:00
|
|
|
SampleModel.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 => SampleValidate.output(e)))); // validate all and filter null values from validation errors
|
2020-05-06 14:39:04 +02:00
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2020-05-07 21:55:29 +02:00
|
|
|
router.put('/sample/' + IdValidate.parameter(), (req, res, next) => {
|
2020-05-06 14:39:04 +02:00
|
|
|
if (!req.auth(res, ['write', 'maintain', 'dev', 'admin'], 'basic')) return;
|
|
|
|
|
2020-05-07 21:55:29 +02:00
|
|
|
const {error, value: sample} = SampleValidate.input(req.body, 'change');
|
|
|
|
if (error) return res400(error, res);
|
2020-05-06 14:39:04 +02:00
|
|
|
|
2020-05-07 21:55:29 +02:00
|
|
|
SampleModel.findById(req.params.id).lean().exec(async (err, sampleData: any) => { // check if id exists
|
2020-05-06 14:39:04 +02:00
|
|
|
if (err) return next(err);
|
2020-05-07 21:55:29 +02:00
|
|
|
if (!sampleData) {
|
|
|
|
return res.status(404).json({status: 'Not found'});
|
2020-05-06 14:39:04 +02:00
|
|
|
}
|
2020-05-07 21:55:29 +02:00
|
|
|
// 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;
|
|
|
|
|
|
|
|
if (sample.hasOwnProperty('material_id')) {
|
|
|
|
if (!await materialCheck(sample, res, next)) return;
|
|
|
|
}
|
|
|
|
else if (sample.hasOwnProperty('color')) {
|
|
|
|
if (!await materialCheck(sample, res, next, sampleData.material_id)) return;
|
|
|
|
}
|
|
|
|
|
2020-05-14 15:36:47 +02:00
|
|
|
if (sample.hasOwnProperty('notes')) {
|
|
|
|
let newNotes = true;
|
|
|
|
if (sampleData.note_id !== null) { // old notes data exists
|
|
|
|
const data = await NoteModel.findById(sampleData.note_id).lean().exec().catch(err => {next(err);}) as any;
|
|
|
|
if (data instanceof Error) return;
|
|
|
|
newNotes = !_.isEqual(_.pick(IdValidate.stringify(data), _.keys(sample.notes)), sample.notes);
|
|
|
|
if (newNotes) {
|
|
|
|
if (data.hasOwnProperty('custom_fields')) { // update note_fields
|
|
|
|
customFieldsChange(Object.keys(data.custom_fields), -1);
|
|
|
|
}
|
|
|
|
NoteModel.findByIdAndDelete(sampleData.note_id).lean().exec(err => { // delete old notes
|
|
|
|
if (err) return console.error(err);
|
|
|
|
});
|
2020-05-07 21:55:29 +02:00
|
|
|
}
|
2020-05-06 14:39:04 +02:00
|
|
|
}
|
2020-05-14 15:36:47 +02:00
|
|
|
|
|
|
|
if (_.keys(sample.notes).length > 0 && newNotes) { // save new notes
|
|
|
|
if (!await sampleRefCheck(sample, res, next)) return;
|
|
|
|
if (sample.notes.hasOwnProperty('custom_fields') && Object.keys(sample.notes.custom_fields).length > 0) { // new custom_fields
|
|
|
|
customFieldsChange(Object.keys(sample.notes.custom_fields), 1);
|
|
|
|
}
|
|
|
|
let data = await new NoteModel(sample.notes).save().catch(err => { return next(err)}); // save new notes
|
|
|
|
delete sample.notes;
|
|
|
|
sample.note_id = data._id;
|
|
|
|
}
|
2020-05-07 21:55:29 +02:00
|
|
|
}
|
2020-05-13 12:06:28 +02:00
|
|
|
|
|
|
|
// check for changes
|
|
|
|
if (!_.isEqual(_.pick(IdValidate.stringify(sampleData), _.keys(sample)), _.omit(sample, ['notes']))) {
|
|
|
|
sample.status = 0;
|
|
|
|
}
|
2020-05-07 21:55:29 +02:00
|
|
|
SampleModel.findByIdAndUpdate(req.params.id, sample, {new: true}).lean().exec((err, data) => {
|
|
|
|
if (err) return next(err);
|
|
|
|
res.json(SampleValidate.output(data));
|
|
|
|
});
|
2020-05-06 14:39:04 +02:00
|
|
|
|
2020-05-07 21:55:29 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
router.delete('/sample/' + IdValidate.parameter(), (req, res, next) => {
|
|
|
|
if (!req.auth(res, ['write', 'maintain', 'dev', 'admin'], 'basic')) return;
|
|
|
|
|
|
|
|
SampleModel.findById(req.params.id).lean().exec(async (err, sampleData: any) => { // check if id exists
|
|
|
|
if (err) return next(err);
|
|
|
|
if (!sampleData) {
|
|
|
|
return res.status(404).json({status: 'Not found'});
|
|
|
|
}
|
|
|
|
// 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;
|
|
|
|
|
2020-05-13 12:06:28 +02:00
|
|
|
SampleModel.findByIdAndUpdate(req.params.id, {status: -1}).lean().exec(err => { // set sample status
|
2020-05-07 21:55:29 +02:00
|
|
|
if (err) return next(err);
|
|
|
|
if (sampleData.note_id !== null) {
|
2020-05-13 12:06:28 +02:00
|
|
|
NoteModel.findById(sampleData.note_id).lean().exec((err, data: any) => { // find notes to update note_fields
|
2020-05-07 21:55:29 +02:00
|
|
|
if (err) return next(err);
|
|
|
|
if (data.hasOwnProperty('custom_fields')) { // update note_fields
|
|
|
|
customFieldsChange(Object.keys(data.custom_fields), -1);
|
|
|
|
}
|
|
|
|
res.json({status: 'OK'});
|
2020-05-06 14:39:04 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
2020-05-07 21:55:29 +02:00
|
|
|
res.json({status: 'OK'});
|
2020-05-06 14:39:04 +02:00
|
|
|
}
|
2020-05-07 21:55:29 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-05-06 14:39:04 +02:00
|
|
|
|
2020-05-07 21:55:29 +02:00
|
|
|
router.post('/sample/new', async (req, res, next) => {
|
|
|
|
if (!req.auth(res, ['write', 'maintain', 'dev', 'admin'], 'basic')) return;
|
2020-05-06 14:39:04 +02:00
|
|
|
|
2020-05-07 21:55:29 +02:00
|
|
|
const {error, value: sample} = SampleValidate.input(req.body, 'new');
|
|
|
|
if (error) return res400(error, res);
|
|
|
|
|
|
|
|
if (!await materialCheck(sample, res, next)) return;
|
|
|
|
if (!await sampleRefCheck(sample, res, next)) return;
|
|
|
|
|
|
|
|
if (sample.notes.hasOwnProperty('custom_fields') && Object.keys(sample.notes.custom_fields).length > 0) { // new custom_fields
|
|
|
|
customFieldsChange(Object.keys(sample.notes.custom_fields), 1);
|
|
|
|
}
|
|
|
|
|
2020-05-13 12:06:28 +02:00
|
|
|
sample.status = 0;
|
2020-05-18 09:58:15 +02:00
|
|
|
sample.number = await numberGenerate(sample, req, res, next);
|
|
|
|
if (!sample.number) return;
|
2020-05-07 21:55:29 +02:00
|
|
|
new NoteModel(sample.notes).save((err, data) => {
|
|
|
|
if (err) return next(err);
|
|
|
|
delete sample.notes;
|
|
|
|
sample.note_id = data._id;
|
|
|
|
sample.user_id = req.authDetails.id;
|
2020-05-08 09:58:12 +02:00
|
|
|
console.log(sample);
|
2020-05-07 21:55:29 +02:00
|
|
|
new SampleModel(sample).save((err, data) => {
|
|
|
|
if (err) return next(err);
|
|
|
|
res.json(SampleValidate.output(data.toObject()));
|
2020-05-06 14:39:04 +02:00
|
|
|
});
|
2020-05-07 21:55:29 +02:00
|
|
|
});
|
2020-05-06 14:39:04 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
router.get('/sample/notes/fields', (req, res, next) => {
|
|
|
|
if (!req.auth(res, ['read', 'write', 'maintain', 'dev', 'admin'], 'all')) return;
|
|
|
|
|
|
|
|
NoteFieldModel.find({}).lean().exec((err, data) => {
|
|
|
|
if (err) return next(err);
|
2020-05-12 17:15:36 +02:00
|
|
|
res.json(_.compact(data.map(e => NoteFieldValidate.output(e)))); // validate all and filter null values from validation errors
|
2020-05-06 14:39:04 +02:00
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = router;
|
|
|
|
|
|
|
|
|
2020-05-18 09:58:15 +02:00
|
|
|
async function numberGenerate (sample, req, res, next) { // validate number, returns false if invalid
|
|
|
|
const sampleData = await SampleModel
|
|
|
|
.find({number: new RegExp('^' + req.authDetails.location + '[0-9]+$', 'm')})
|
|
|
|
.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);
|
2020-05-07 21:55:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async function materialCheck (sample, res, next, id = sample.material_id) { // validate material_id and color, returns false if invalid
|
2020-05-18 09:58:15 +02:00
|
|
|
const materialData = await MaterialModel.findById(id).lean().exec().catch(err => next(err)) as any;
|
2020-05-14 15:36:47 +02:00
|
|
|
if (materialData instanceof Error) return false;
|
2020-05-07 21:55:29 +02:00
|
|
|
if (!materialData) { // could not find material_id
|
|
|
|
res.status(400).json({status: 'Material not available'});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (sample.hasOwnProperty('color') && !materialData.numbers.find(e => e.color === sample.color)) { // color for material not specified
|
|
|
|
res.status(400).json({status: 'Color not available for material'});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
sample.notes.sample_references.forEach(reference => {
|
|
|
|
SampleModel.findById(reference.id).lean().exec((err, data) => {
|
|
|
|
if (err) {next(err); resolve(false)}
|
|
|
|
if (!data) {
|
|
|
|
res.status(400).json({status: 'Sample reference not available'});
|
|
|
|
return resolve(false);
|
|
|
|
}
|
|
|
|
referencesCount --;
|
|
|
|
if (referencesCount <= 0) {
|
|
|
|
resolve(true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
resolve(true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function customFieldsChange (fields, amount) {
|
2020-05-06 14:39:04 +02:00
|
|
|
fields.forEach(field => {
|
2020-05-07 21:55:29 +02:00
|
|
|
NoteFieldModel.findOneAndUpdate({name: field}, {$inc: {qty: amount}}, {new: true}).lean().exec((err, data: any) => { // check if field exists
|
2020-05-06 14:39:04 +02:00
|
|
|
if (err) return console.error(err);
|
|
|
|
if (!data) { // new field
|
|
|
|
new NoteFieldModel({name: field, qty: 1}).save(err => {
|
|
|
|
if (err) return console.error(err);
|
|
|
|
})
|
|
|
|
}
|
2020-05-07 21:55:29 +02:00
|
|
|
else if (data.qty <= 0) {
|
|
|
|
NoteFieldModel.findOneAndDelete({name: field}).lean().exec(err => {
|
|
|
|
if (err) return console.error(err);
|
|
|
|
});
|
|
|
|
}
|
2020-05-06 14:39:04 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|