first implementation of fields
This commit is contained in:
@ -48,19 +48,17 @@ router.get('/samples', async (req, res, next) => {
|
||||
filters['to-page'] = 0;
|
||||
}
|
||||
|
||||
if (filters.sort[0].indexOf('material.') >= 0) { // need to populate materials, material supplier and group
|
||||
query.push(
|
||||
{$lookup: {from: 'materials', localField: 'material_id', foreignField: '_id', as: 'material'}},
|
||||
{$set: {material: { $arrayElemAt: ['$material', 0]}}},
|
||||
{$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.number': { $arrayElemAt: ['$material.numbers.number', {$indexOfArray: ['$material.numbers.color', '$color']}]}
|
||||
}
|
||||
query.push(
|
||||
{$lookup: {from: 'materials', localField: 'material_id', foreignField: '_id', as: 'material'}},
|
||||
{$set: {material: { $arrayElemAt: ['$material', 0]}}},
|
||||
{$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.number': { $arrayElemAt: ['$material.numbers.number', {$indexOfArray: ['$material.numbers.color', '$color']}]}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
if (filters['from-id']) { // from-id specified
|
||||
const fromSample = await SampleModel.findById(filters['from-id']).lean().exec().catch(err => {next(err);});
|
||||
@ -79,9 +77,9 @@ router.get('/samples', async (req, res, next) => {
|
||||
query.push({$sort: {[filters.sort[0]]: filters.sort[1], '_id': filters.sort[1]}}); // set _id as secondary sort
|
||||
}
|
||||
|
||||
if (filters.sort[0].indexOf('material.') >= 0) { // unpopulate materials again
|
||||
query.push({$unset: 'material'});
|
||||
}
|
||||
// if (filters.sort[0].indexOf('material.') >= 0) { // unpopulate materials again
|
||||
// query.push({$unset: 'material'});
|
||||
// }
|
||||
|
||||
if (filters['to-page']) {
|
||||
query.push({$skip: Math.abs(filters['to-page'] + Number(filters['to-page'] < 0)) * filters['page-size'] + Number(filters['to-page'] < 0)}) // number to skip, if going back pages, one page has to be skipped less but on sample more
|
||||
@ -90,6 +88,16 @@ router.get('/samples', async (req, res, next) => {
|
||||
if (filters['page-size']) {
|
||||
query.push({$limit: filters['page-size']});
|
||||
}
|
||||
console.log(filters.fields);
|
||||
const projection = filters.fields.reduce((s, e) => {s[e] = true; return s; }, {});
|
||||
if (filters.fields.indexOf('added') >= 0) { // add added date
|
||||
projection.added = {$toDate: '$_id'};
|
||||
}
|
||||
if (!(filters.fields.indexOf('_id') >= 0)) { // disable _id explicitly
|
||||
console.log('disable id');
|
||||
projection._id = false;
|
||||
}
|
||||
query.push({$project: projection});
|
||||
|
||||
SampleModel.aggregate(query).exec((err, data) => {
|
||||
if (err) return next(err);
|
||||
@ -97,14 +105,14 @@ router.get('/samples', async (req, res, next) => {
|
||||
data.reverse();
|
||||
}
|
||||
if (filters.csv) { // output as csv // TODO: csv example in OAS
|
||||
csv(_.compact(data.map(e => SampleValidate.output(e))), ['_id', 'number'], (err, data) => {
|
||||
csv(_.compact(data.map(e => SampleValidate.output(e, 'refs'))), ['_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
|
||||
res.json(_.compact(data.map(e => SampleValidate.output(e, 'refs')))); // validate all and filter null values from validation errors
|
||||
}
|
||||
})
|
||||
});
|
||||
|
Reference in New Issue
Block a user