base for csv export
This commit is contained in:
		@@ -89,7 +89,9 @@ function key (req, next): any {  // checks API key and returns changed user obje
 | 
			
		||||
        if (err) return next(err);
 | 
			
		||||
        if (data.length === 1) {  // one user found
 | 
			
		||||
          resolve({level: data[0].level, name: data[0].name, id: data[0]._id.toString(), location: data[0].location});
 | 
			
		||||
          delete req.query.key;  // delete query parameter to avoid interference with later validation
 | 
			
		||||
          if (!/^\/api/m.test(req.url)){
 | 
			
		||||
            delete req.query.key;  // delete query parameter to avoid interference with later validation
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
        else {
 | 
			
		||||
          resolve(null);
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										7
									
								
								src/helpers/csv.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								src/helpers/csv.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,7 @@
 | 
			
		||||
import {parseAsync} from 'json2csv';
 | 
			
		||||
 | 
			
		||||
export default function csv(input: object, fields: string[], f: (err, data) => void) {
 | 
			
		||||
  parseAsync(input)
 | 
			
		||||
    .then(csv => f(null, csv))
 | 
			
		||||
    .catch(err => f(err, null));
 | 
			
		||||
}
 | 
			
		||||
@@ -48,9 +48,8 @@ app.use(require('./helpers/authorize'));  // handle authentication
 | 
			
		||||
 | 
			
		||||
// redirect /api routes for Angular proxy in development
 | 
			
		||||
if (process.env.NODE_ENV !== 'production') {
 | 
			
		||||
  app.use('/api/:url([^]+)', (req, res) => {
 | 
			
		||||
  app.use('/api/:url([^]+)', (req, ignore) => {
 | 
			
		||||
    req.url = '/' + req.params.url;
 | 
			
		||||
    app.handle(req, res);
 | 
			
		||||
  });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -38,15 +38,7 @@ export default class TestHelper {
 | 
			
		||||
    return server
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static afterEach (server, done) {
 | 
			
		||||
    server.close(done);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static after(done) {
 | 
			
		||||
    db.disconnect(done);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static request (server, done, options) {  // options in form: {method, url, auth: {key/basic: 'name' or 'key'/{name, pass}}, httpStatus, req, res, default (set to false if you want to dismiss default .end handling)}
 | 
			
		||||
  static request (server, done, options) {  // options in form: {method, url, contentType, auth: {key/basic: 'name' or 'key'/{name, pass}}, httpStatus, req, res, default (set to false if you want to dismiss default .end handling)}
 | 
			
		||||
    let st = supertest(server);
 | 
			
		||||
    if (options.hasOwnProperty('auth') && options.auth.hasOwnProperty('key')) {  // resolve API key
 | 
			
		||||
      options.url += '?key=' + (this.auth.hasOwnProperty(options.auth.key)? this.auth[options.auth.key].key : options.auth.key);
 | 
			
		||||
@@ -79,8 +71,12 @@ export default class TestHelper {
 | 
			
		||||
        st = st.auth(options.auth.basic.name, options.auth.basic.pass)
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    st = st.expect('Content-type', /json/)
 | 
			
		||||
      .expect(options.httpStatus);
 | 
			
		||||
    if (options.hasOwnProperty('contentType')) {
 | 
			
		||||
      st = st.expect('Content-type', options.contentType).expect(options.httpStatus);
 | 
			
		||||
    }
 | 
			
		||||
    else {
 | 
			
		||||
      st = st.expect('Content-type', /json/).expect(options.httpStatus);
 | 
			
		||||
    }
 | 
			
		||||
    if (options.hasOwnProperty('res')) {  // evaluate result
 | 
			
		||||
      return st.end((err, res) => {
 | 
			
		||||
        if (err) return done (err);
 | 
			
		||||
@@ -128,4 +124,12 @@ export default class TestHelper {
 | 
			
		||||
      return st;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static afterEach (server, done) {
 | 
			
		||||
    server.close(done);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static after(done) {
 | 
			
		||||
    db.disconnect(done);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user