added status filter for materials
This commit is contained in:
		@@ -72,6 +72,43 @@ describe('/material', () => {
 | 
			
		||||
        done();
 | 
			
		||||
      });
 | 
			
		||||
    });
 | 
			
		||||
    it('allows filtering by state', done => {
 | 
			
		||||
      TestHelper.request(server, done, {
 | 
			
		||||
        method: 'get',
 | 
			
		||||
        url: '/materials?status=new',
 | 
			
		||||
        auth: {basic: 'janedoe'},
 | 
			
		||||
        httpStatus: 200
 | 
			
		||||
      }).end((err, res) => {
 | 
			
		||||
        if (err) return done(err);
 | 
			
		||||
        const json = require('../test/db.json');
 | 
			
		||||
        should(res.body).have.lengthOf(json.collections.materials.filter(e => e.status === globals.status.new).length);
 | 
			
		||||
        should(res.body).matchEach(material => {
 | 
			
		||||
          should(material).have.only.keys('_id', 'name', 'supplier', 'group', 'mineral', 'glass_fiber', 'carbon_fiber', 'numbers');
 | 
			
		||||
          should(material).have.property('_id').be.type('string');
 | 
			
		||||
          should(material).have.property('name').be.type('string');
 | 
			
		||||
          should(material).have.property('supplier').be.type('string');
 | 
			
		||||
          should(material).have.property('group').be.type('string');
 | 
			
		||||
          should(material).have.property('mineral').be.type('number');
 | 
			
		||||
          should(material).have.property('glass_fiber').be.type('number');
 | 
			
		||||
          should(material).have.property('carbon_fiber').be.type('number');
 | 
			
		||||
          should(material.numbers).matchEach(number => {
 | 
			
		||||
            should(number).have.only.keys('color', 'number');
 | 
			
		||||
            should(number).have.property('color').be.type('string');
 | 
			
		||||
            should(number).have.property('number').be.type('string');
 | 
			
		||||
          });
 | 
			
		||||
        });
 | 
			
		||||
        done();
 | 
			
		||||
      });
 | 
			
		||||
    });
 | 
			
		||||
    it('rejects an invalid state name', done => {
 | 
			
		||||
      TestHelper.request(server, done, {
 | 
			
		||||
        method: 'get',
 | 
			
		||||
        url: '/materials?status=xxx',
 | 
			
		||||
        auth: {basic: 'janedoe'},
 | 
			
		||||
        httpStatus: 400,
 | 
			
		||||
        res: {status: 'Invalid body format', details: '"status" must be one of [validated, new, all]'}
 | 
			
		||||
      });
 | 
			
		||||
    });
 | 
			
		||||
    it('rejects unauthorized requests', done => {
 | 
			
		||||
      TestHelper.request(server, done, {
 | 
			
		||||
        method: 'get',
 | 
			
		||||
 
 | 
			
		||||
@@ -19,7 +19,24 @@ const router = express.Router();
 | 
			
		||||
router.get('/materials', (req, res, next) => {
 | 
			
		||||
  if (!req.auth(res, ['read', 'write', 'maintain', 'dev', 'admin'], 'all')) return;
 | 
			
		||||
 | 
			
		||||
  MaterialModel.find({status:globals.status.validated}).populate('group_id').populate('supplier_id').lean().exec((err, data) => {
 | 
			
		||||
  const {error, value: filters} = MaterialValidate.query(req.query);
 | 
			
		||||
  if (error) return res400(error, res);
 | 
			
		||||
 | 
			
		||||
  let conditions;
 | 
			
		||||
 | 
			
		||||
  if (filters.hasOwnProperty('status')) {
 | 
			
		||||
    if(filters.status === 'all') {
 | 
			
		||||
      conditions = {$or: [{status: globals.status.validated}, {status: globals.status.new}]}
 | 
			
		||||
    }
 | 
			
		||||
    else {
 | 
			
		||||
      conditions = {status: globals.status[filters.status]};
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  else {  // default
 | 
			
		||||
    conditions = {status: globals.status.validated};
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  MaterialModel.find(conditions).populate('group_id').populate('supplier_id').lean().exec((err, data) => {
 | 
			
		||||
    if (err) return next(err);
 | 
			
		||||
 | 
			
		||||
    res.json(_.compact(data.map(e => MaterialValidate.output(e))));  // validate all and filter null values from validation errors
 | 
			
		||||
 
 | 
			
		||||
@@ -107,4 +107,10 @@ export default class MaterialValidate {  // validate input for material
 | 
			
		||||
      numbers: this.material.numbers
 | 
			
		||||
    });
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static query (data) {
 | 
			
		||||
    return Joi.object({
 | 
			
		||||
      status: Joi.string().valid('validated', 'new', 'all')
 | 
			
		||||
    }).validate(data);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user