base for csv export
This commit is contained in:
		@@ -244,6 +244,21 @@ describe('/sample', () => {
 | 
			
		||||
        done();
 | 
			
		||||
      });
 | 
			
		||||
    });
 | 
			
		||||
    it('returns a correct csv file if specified', done => {
 | 
			
		||||
      TestHelper.request(server, done, {
 | 
			
		||||
        method: 'get',
 | 
			
		||||
        url: '/samples?status=all&page-size=2&csv=true',
 | 
			
		||||
        contentType: /text\/csv/,
 | 
			
		||||
        auth: {basic: 'janedoe'},
 | 
			
		||||
        httpStatus: 200
 | 
			
		||||
      }).end((err, res) => {
 | 
			
		||||
        if (err) return done(err);
 | 
			
		||||
        should(res.text).be.eql('"_id","number","type","color","batch","condition","material_id","note_id","user_id","added"\r\n' +
 | 
			
		||||
          '"400000000000000000000001","1","granulate","black","","{""material"":""copper"",""weeks"":3,""condition_template"":""200000000000000000000001""}","100000000000000000000004",,"000000000000000000000002","2004-01-10T13:37:04.000Z"\r\n' +
 | 
			
		||||
          '"400000000000000000000002","21","granulate","natural","1560237365","{""material"":""copper"",""weeks"":3,""condition_template"":""200000000000000000000001""}","100000000000000000000001","500000000000000000000001","000000000000000000000002","2004-01-10T13:37:04.000Z"');
 | 
			
		||||
        done();
 | 
			
		||||
      });
 | 
			
		||||
    });
 | 
			
		||||
    it('rejects a negative page size', done => {
 | 
			
		||||
      TestHelper.request(server, done, {
 | 
			
		||||
        method: 'get',
 | 
			
		||||
 
 | 
			
		||||
@@ -15,6 +15,7 @@ import ConditionTemplateModel from '../models/condition_template';
 | 
			
		||||
import ParametersValidate from './validate/parameters';
 | 
			
		||||
import globals from '../globals';
 | 
			
		||||
import db from '../db';
 | 
			
		||||
import csv from '../helpers/csv';
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
const router = express.Router();
 | 
			
		||||
@@ -54,7 +55,10 @@ router.get('/samples', async (req, res, next) => {
 | 
			
		||||
      {$lookup: { from: 'material_groups', localField: 'material.group_id', foreignField: '_id', as: 'material.group' }},
 | 
			
		||||
      {$set: {'material.group': { $arrayElemAt: ['$material.group.name', 0]}}},
 | 
			
		||||
      {$lookup: { from: 'material_suppliers', localField: 'material.supplier_id', foreignField: '_id', as: 'material.supplier'}},
 | 
			
		||||
      {$set: {'material.supplier': {$arrayElemAt: ['$material.supplier.name', 0]}}}
 | 
			
		||||
      {$set: {'material.supplier': {$arrayElemAt: ['$material.supplier.name', 0]}}},
 | 
			
		||||
      {$set: {'material.number': { $arrayElemAt: ['$material.numbers.number', {$indexOfArray: ['$material.numbers.color', '$color']}]}
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -92,7 +96,16 @@ router.get('/samples', async (req, res, next) => {
 | 
			
		||||
    if (filters['to-page'] < 0) {
 | 
			
		||||
      data.reverse();
 | 
			
		||||
    }
 | 
			
		||||
    res.json(_.compact(data.map(e => SampleValidate.output(e))));  // validate all and filter null values from validation errors
 | 
			
		||||
    if (filters.csv) {  // output as csv  // TODO: csv example in OAS
 | 
			
		||||
      csv(_.compact(data.map(e => SampleValidate.output(e))), ['_id', 'number'], (err, data) => {
 | 
			
		||||
        if (err) return next(err);
 | 
			
		||||
        res.set('Content-Type', 'text/csv');
 | 
			
		||||
        res.send(data);
 | 
			
		||||
      });
 | 
			
		||||
    }
 | 
			
		||||
    else {
 | 
			
		||||
      res.json(_.compact(data.map(e => SampleValidate.output(e))));  // validate all and filter null values from validation errors
 | 
			
		||||
    }
 | 
			
		||||
  })
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -133,7 +133,8 @@ export default class SampleValidate {
 | 
			
		||||
      'from-id': IdValidate.get(),
 | 
			
		||||
      'to-page': Joi.number().integer(),
 | 
			
		||||
      'page-size': Joi.number().integer().min(1),
 | 
			
		||||
      sort: Joi.string().pattern(/^(_id|color|number|type|batch|added|material\.name|material\.supplier|material\.group|material\.mineral|material\.glass_fiber|material\.carbon_fiber)-(asc|desc)$/m).default('_id-asc')  // TODO: material keys
 | 
			
		||||
      sort: Joi.string().pattern(/^(_id|color|number|type|batch|added|material\.name|material\.supplier|material\.group|material\.mineral|material\.glass_fiber|material\.carbon_fiber|material\.number)-(asc|desc)$/m).default('_id-asc'),
 | 
			
		||||
      csv: Joi.boolean().default(false)
 | 
			
		||||
    }).with('to-page', 'page-size').validate(data);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user