small fixes
This commit is contained in:
		@@ -67,6 +67,7 @@ function resolveXDoc (doc) {  // resolve x-doc properties recursively
 | 
			
		||||
 | 
			
		||||
// templates
 | 
			
		||||
 | 
			
		||||
// noinspection LongLine
 | 
			
		||||
const htmlTplString = `
 | 
			
		||||
<!DOCTYPE html>
 | 
			
		||||
<html lang="en">
 | 
			
		||||
 
 | 
			
		||||
@@ -10,7 +10,7 @@
 | 
			
		||||
/* =================== USAGE ===================
 | 
			
		||||
 | 
			
		||||
    import * as express from "express";
 | 
			
		||||
    var app = express();
 | 
			
		||||
    let app = express();
 | 
			
		||||
 | 
			
		||||
 =============================================== */
 | 
			
		||||
 | 
			
		||||
@@ -29,40 +29,44 @@ declare function e(): core.Express;
 | 
			
		||||
 | 
			
		||||
declare namespace e {
 | 
			
		||||
    /**
 | 
			
		||||
     * This is a built-in middleware function in Express. It parses incoming requests with JSON payloads and is based on body-parser.
 | 
			
		||||
     * This is a built-in middleware function in Express. It parses incoming requests with JSON payloads and is based on
 | 
			
		||||
     * body-parser.
 | 
			
		||||
     * @since 4.16.0
 | 
			
		||||
     */
 | 
			
		||||
    var json: typeof bodyParser.json;
 | 
			
		||||
    let json: typeof bodyParser.json;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * This is a built-in middleware function in Express. It parses incoming requests with Buffer payloads and is based on body-parser.
 | 
			
		||||
     * This is a built-in middleware function in Express. It parses incoming requests with Buffer payloads and is based 
 | 
			
		||||
     * on body-parser.
 | 
			
		||||
     * @since 4.17.0
 | 
			
		||||
     */
 | 
			
		||||
    var raw: typeof bodyParser.raw;
 | 
			
		||||
    let raw: typeof bodyParser.raw;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * This is a built-in middleware function in Express. It parses incoming requests with text payloads and is based on body-parser.
 | 
			
		||||
     * This is a built-in middleware function in Express. It parses incoming requests with text payloads and is based on
 | 
			
		||||
     * body-parser.
 | 
			
		||||
     * @since 4.17.0
 | 
			
		||||
     */
 | 
			
		||||
    var text: typeof bodyParser.text;
 | 
			
		||||
    let text: typeof bodyParser.text;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * These are the exposed prototypes.
 | 
			
		||||
     */
 | 
			
		||||
    var application: Application;
 | 
			
		||||
    var request: Request;
 | 
			
		||||
    var response: Response;
 | 
			
		||||
    let application: Application;
 | 
			
		||||
    let request: Request;
 | 
			
		||||
    let response: Response;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * This is a built-in middleware function in Express. It serves static files and is based on serve-static.
 | 
			
		||||
     */
 | 
			
		||||
    var static: typeof serveStatic;
 | 
			
		||||
    let static: typeof serveStatic;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * This is a built-in middleware function in Express. It parses incoming requests with urlencoded payloads and is based on body-parser.
 | 
			
		||||
     * This is a built-in middleware function in Express. It parses incoming requests with urlencoded payloads and is 
 | 
			
		||||
     * based on body-parser.
 | 
			
		||||
     * @since 4.16.0
 | 
			
		||||
     */
 | 
			
		||||
    var urlencoded: typeof bodyParser.urlencoded;
 | 
			
		||||
    let urlencoded: typeof bodyParser.urlencoded;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * This is a built-in middleware function in Express. It parses incoming request query parameters.
 | 
			
		||||
@@ -95,7 +99,8 @@ declare namespace e {
 | 
			
		||||
    interface Application extends core.Application { }
 | 
			
		||||
    interface CookieOptions extends core.CookieOptions { }
 | 
			
		||||
    interface Errback extends core.Errback { }
 | 
			
		||||
    interface ErrorRequestHandler<P extends core.Params = core.ParamsDictionary, ResBody = any, ReqBody = any, ReqQuery = core.Query>
 | 
			
		||||
    interface ErrorRequestHandler<P extends core.Params = core.ParamsDictionary, ResBody = any, ReqBody = any,
 | 
			
		||||
      ReqQuery = core.Query>
 | 
			
		||||
        extends core.ErrorRequestHandler<P, ResBody, ReqBody, ReqQuery> { }
 | 
			
		||||
    interface Express extends core.Express { }
 | 
			
		||||
    interface Handler extends core.Handler { }
 | 
			
		||||
@@ -105,8 +110,10 @@ declare namespace e {
 | 
			
		||||
    interface IRouterMatcher<T> extends core.IRouterMatcher<T> { }
 | 
			
		||||
    interface MediaType extends core.MediaType { }
 | 
			
		||||
    interface NextFunction extends core.NextFunction { }
 | 
			
		||||
    interface Request<P extends core.Params = core.ParamsDictionary, ResBody = any, ReqBody = any, ReqQuery = core.Query> extends core.Request<P, ResBody, ReqBody, ReqQuery> { }
 | 
			
		||||
    interface RequestHandler<P extends core.Params = core.ParamsDictionary, ResBody = any, ReqBody = any, ReqQuery = core.Query> extends core.RequestHandler<P, ResBody, ReqBody, ReqQuery> { }
 | 
			
		||||
    interface Request<P extends core.Params = core.ParamsDictionary, ResBody = any, ReqBody = any, 
 | 
			
		||||
      ReqQuery = core.Query> extends core.Request<P, ResBody, ReqBody, ReqQuery> { }
 | 
			
		||||
    interface RequestHandler<P extends core.Params = core.ParamsDictionary, ResBody = any, ReqBody = any, 
 | 
			
		||||
      ReqQuery = core.Query> extends core.RequestHandler<P, ResBody, ReqBody, ReqQuery> { }
 | 
			
		||||
    interface RequestParamHandler extends core.RequestParamHandler { }
 | 
			
		||||
    export interface Response<ResBody = any> extends core.Response<ResBody> { }
 | 
			
		||||
    interface Router extends core.Router { }
 | 
			
		||||
 
 | 
			
		||||
@@ -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