base for csv export
This commit is contained in:
@ -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
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
|
Reference in New Issue
Block a user