small fixes
This commit is contained in:
@ -3,7 +3,6 @@ import MaterialModel from '../models/material';
|
||||
import MaterialGroupModel from '../models/material_groups';
|
||||
import MaterialSupplierModel from '../models/material_suppliers';
|
||||
import TestHelper from "../test/helper";
|
||||
import globals from '../globals';
|
||||
|
||||
|
||||
describe('/material', () => {
|
||||
|
@ -581,27 +581,13 @@ router.get('/sample/number/:number', (req, res, next) => {
|
||||
router.put('/sample/restore/' + IdValidate.parameter(), (req, res, next) => {
|
||||
if (!req.auth(res, ['dev', 'admin'], 'basic')) return;
|
||||
|
||||
SampleModel.findByIdAndUpdate(req.params.id, {status: globals.status.new}).log(req).lean().exec((err, data) => {
|
||||
if (err) return next(err);
|
||||
|
||||
if (!data) {
|
||||
return res.status(404).json({status: 'Not found'});
|
||||
}
|
||||
res.json({status: 'OK'});
|
||||
});
|
||||
setStatus(globals.status.new, req, res, next);
|
||||
});
|
||||
|
||||
router.put('/sample/validate/' + IdValidate.parameter(), (req, res, next) => {
|
||||
if (!req.auth(res, ['dev', 'admin'], 'basic')) return;
|
||||
|
||||
SampleModel.findByIdAndUpdate(req.params.id, {status: globals.status.val}).log(req).lean().exec((err, data) => {
|
||||
if (err) return next(err);
|
||||
if (!data) {
|
||||
return res.status(404).json({status: 'Not found'});
|
||||
}
|
||||
|
||||
res.json({status: 'OK'});
|
||||
});
|
||||
setStatus(globals.status.val, req, res, next);
|
||||
});
|
||||
|
||||
router.post('/sample/new', async (req, res, next) => {
|
||||
@ -899,4 +885,15 @@ async function sampleReturn (sampleData, req, res, next) {
|
||||
else {
|
||||
res.status(404).json({status: 'Not found'});
|
||||
}
|
||||
}
|
||||
|
||||
function setStatus (status, req, res, next) {
|
||||
SampleModel.findByIdAndUpdate(req.params.id, {status}).log(req).lean().exec((err, data) => {
|
||||
if (err) return next(err);
|
||||
|
||||
if (!data) {
|
||||
return res.status(404).json({status: 'Not found'});
|
||||
}
|
||||
res.json({status: 'OK'});
|
||||
});
|
||||
}
|
@ -1,7 +1,10 @@
|
||||
import Joi from 'joi';
|
||||
|
||||
export default class IdValidate {
|
||||
private static id = Joi.string().pattern(new RegExp('[0-9a-f]{24}')).length(24).messages({'string.pattern.base': 'Invalid object id'});
|
||||
private static id = Joi.string()
|
||||
.pattern(new RegExp('[0-9a-f]{24}'))
|
||||
.length(24)
|
||||
.messages({'string.pattern.base': 'Invalid object id'});
|
||||
|
||||
static get () { // return joi validation
|
||||
return this.id;
|
||||
@ -17,7 +20,8 @@ export default class IdValidate {
|
||||
|
||||
static stringify (data) { // convert all ObjectID objects to plain strings
|
||||
Object.keys(data).forEach(key => {
|
||||
if (data[key] !== null && data[key].hasOwnProperty('_bsontype') && data[key]._bsontype === 'ObjectID') { // stringify id
|
||||
// stringify id
|
||||
if (data[key] !== null && data[key].hasOwnProperty('_bsontype') && data[key]._bsontype === 'ObjectID') {
|
||||
data[key] = data[key].toString();
|
||||
}
|
||||
else if (typeof data[key] === 'object' && data[key] !== null) { // deeper into recursion
|
||||
|
@ -1,7 +1,8 @@
|
||||
import Joi from 'joi';
|
||||
|
||||
export default class ParametersValidate {
|
||||
static input (data, parameters, param) { // data to validate, parameters from template, param: 'new', 'change', 'null'(null values are allowed)
|
||||
// data to validate, parameters from template, param: 'new', 'change', 'null'(null values are allowed)
|
||||
static input (data, parameters, param) {
|
||||
let joiObject = {};
|
||||
parameters.forEach(parameter => {
|
||||
if (parameter.range.hasOwnProperty('values')) { // append right validation method according to parameter
|
||||
|
Reference in New Issue
Block a user