|
|
|
@ -22,13 +22,6 @@ import globals from '../globals';
|
|
|
|
|
|
|
|
|
|
const router = express.Router();
|
|
|
|
|
|
|
|
|
|
// TODO: check added filter
|
|
|
|
|
// TODO: convert filter value to number according to table model
|
|
|
|
|
// TODO: validation for filter parameters
|
|
|
|
|
// TODO: location/device sort/filter
|
|
|
|
|
|
|
|
|
|
// TODO: think about filter keys with measurement template versions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
router.get('/samples', async (req, res, next) => {
|
|
|
|
|
if (!req.auth(res, ['read', 'write', 'dev', 'admin'], 'all')) return;
|
|
|
|
@ -40,12 +33,6 @@ router.get('/samples', async (req, res, next) => {
|
|
|
|
|
if ((filters.fields.find(e => e.indexOf('.' + globals.spectrum.dpt) >= 0) || filters.output !== 'json') &&
|
|
|
|
|
!req.auth(res, ['dev', 'admin'], 'all')) return;
|
|
|
|
|
|
|
|
|
|
// TODO: find a better place for these
|
|
|
|
|
const sampleKeys = ['_id', 'color', 'number', 'type', 'batch', 'added', 'condition', 'material_id', 'note_id',
|
|
|
|
|
'user_id'];
|
|
|
|
|
|
|
|
|
|
// TODO find further optimizations from bachelor thesis
|
|
|
|
|
|
|
|
|
|
// evaluate sort parameter from 'color-asc' to ['color', 1]
|
|
|
|
|
filters.sort = filters.sort.split('-');
|
|
|
|
|
filters.sort[0] = filters.sort[0] === 'added' ? '_id' : filters.sort[0]; // route added sorting criteria to _id
|
|
|
|
@ -123,7 +110,7 @@ router.get('/samples', async (req, res, next) => {
|
|
|
|
|
let sortStartValue = null;
|
|
|
|
|
if (filters['from-id']) { // from-id specified, fetch values for sorting
|
|
|
|
|
const fromSample = await MeasurementModel.findOne({sample_id: mongoose.Types.ObjectId(filters['from-id'])})
|
|
|
|
|
.lean().exec().catch(err => {next(err);}); // TODO: what if more than one measurement for sample?
|
|
|
|
|
.lean().exec().catch(err => {next(err);});
|
|
|
|
|
if (fromSample instanceof Error) return;
|
|
|
|
|
if (!fromSample) {
|
|
|
|
|
return res.status(400).json({status: 'Invalid body format', details: 'from-id not found'});
|
|
|
|
@ -149,7 +136,8 @@ router.get('/samples', async (req, res, next) => {
|
|
|
|
|
collection = SampleModel;
|
|
|
|
|
queryPtr[0].$match.$and.push(statusQuery(filters, 'status'));
|
|
|
|
|
|
|
|
|
|
if (sampleKeys.indexOf(filters.sort[0]) >= 0) { // sorting for sample keys
|
|
|
|
|
// sorting for sample keys
|
|
|
|
|
if (SampleValidate.sampleKeys.indexOf(filters.sort[0]) >= 0 || /condition\./.test(filters.sort[0])) {
|
|
|
|
|
let sortStartValue = null;
|
|
|
|
|
if (filters['from-id']) { // from-id specified
|
|
|
|
|
const fromSample = await SampleModel.findById(filters['from-id']).lean().exec().catch(err => {
|
|
|
|
@ -168,13 +156,15 @@ router.get('/samples', async (req, res, next) => {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addFilterQueries(queryPtr, filters.filters.filter(e => sampleKeys.indexOf(e.field) >= 0)); // sample filters
|
|
|
|
|
addFilterQueries(queryPtr, filters.filters.filter(
|
|
|
|
|
e => (SampleValidate.sampleKeys.indexOf(e.field) >= 0) || /condition\./.test(e.field))
|
|
|
|
|
); // sample filters
|
|
|
|
|
|
|
|
|
|
let materialQuery = []; // put material query together separate first to reuse for first-id
|
|
|
|
|
let materialAdded = false;
|
|
|
|
|
if (sortFilterKeys.find(e => /material\./.test(e))) { // add material fields
|
|
|
|
|
materialAdded = true;
|
|
|
|
|
materialQuery.push( // add material properties // TODO: project out unnecessary fields
|
|
|
|
|
materialQuery.push( // add material properties
|
|
|
|
|
{$lookup: {from: 'materials', localField: 'material_id', foreignField: '_id', as: 'material'}},
|
|
|
|
|
{$addFields: {material: {$arrayElemAt: ['$material', 0]}}}
|
|
|
|
|
);
|
|
|
|
@ -198,16 +188,8 @@ router.get('/samples', async (req, res, next) => {
|
|
|
|
|
{$addFields: {'material.group': { $arrayElemAt: ['$material.group.name', 0]}}}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
// TODO: adapt code to new numbers format
|
|
|
|
|
// if (sortFilterKeys.find(e => e === 'material.number')) { // add material number if needed
|
|
|
|
|
// materialQuery.push(
|
|
|
|
|
// {$addFields: {'material.number': { $arrayElemAt: [
|
|
|
|
|
// '$material.numbers.number', {$indexOfArray: ['$material.numbers.color', '$color']}
|
|
|
|
|
// ]}}}
|
|
|
|
|
// );
|
|
|
|
|
// }
|
|
|
|
|
const specialMFilters = sortFilterKeys.filter(e => /material\./.test(e))
|
|
|
|
|
.filter(e => ['material.supplier', 'material.group', 'material.number'].indexOf(e) >= 0); // TODO
|
|
|
|
|
.filter(e => ['material.supplier', 'material.group', 'material.number'].indexOf(e) >= 0);
|
|
|
|
|
// base material filters
|
|
|
|
|
addFilterQueries(materialQuery, filters.filters.filter(e => specialMFilters.indexOf(e.field) >= 0));
|
|
|
|
|
queryPtr.push(...materialQuery);
|
|
|
|
@ -250,8 +232,17 @@ router.get('/samples', async (req, res, next) => {
|
|
|
|
|
]}}}],
|
|
|
|
|
as: 'measurements'
|
|
|
|
|
}});
|
|
|
|
|
measurementTemplates.forEach(template => {
|
|
|
|
|
addMeasurements(queryPtr, template);
|
|
|
|
|
const groupedMeasurementTemplates = measurementTemplates.reduce((s, e) => {
|
|
|
|
|
if (s.hasOwnProperty(e.name)) {
|
|
|
|
|
s[e.name].push(e);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
s[e.name] = [e];
|
|
|
|
|
}
|
|
|
|
|
return s;
|
|
|
|
|
}, {});
|
|
|
|
|
Object.values(groupedMeasurementTemplates).forEach(templates => {
|
|
|
|
|
addMeasurements(queryPtr, templates);
|
|
|
|
|
});
|
|
|
|
|
addFilterQueries(queryPtr, filters.filters
|
|
|
|
|
.filter(e => sortFilterKeys.filter(e => /measurements\./.test(e)).indexOf(e.field) >= 0)
|
|
|
|
@ -310,13 +301,6 @@ router.get('/samples', async (req, res, next) => {
|
|
|
|
|
{$addFields: {'material.group': { $arrayElemAt: ['$material.group.name', 0]}}}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
// if (fieldsToAdd.indexOf('material.number') >= 0) { // add material number if needed // TODO
|
|
|
|
|
// queryPtr.push(
|
|
|
|
|
// {$addFields: {'material.number': {
|
|
|
|
|
// $arrayElemAt: ['$material.numbers.number', {$indexOfArray: ['$material.numbers.color', '$color']}]
|
|
|
|
|
// }}}
|
|
|
|
|
// );
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
let measurementFieldsFields: string[] = _.uniq(
|
|
|
|
|
fieldsToAdd.filter(e => /measurements\./.test(e)).map(e => e.split('.')[1])
|
|
|
|
@ -328,8 +312,8 @@ router.get('/samples', async (req, res, next) => {
|
|
|
|
|
if (measurementTemplates.length < measurementFieldsFields.length) {
|
|
|
|
|
return res.status(400).json({status: 'Invalid body format', details: 'Measurement key not found'});
|
|
|
|
|
}
|
|
|
|
|
// use different lookup methods with and without spectrum for the best performance
|
|
|
|
|
if (fieldsToAdd.find(e => e.indexOf(globals.spectrum.spectrum + '.') >= 0)) {
|
|
|
|
|
// use different lookup methods with and without dpt for the best performance
|
|
|
|
|
if (fieldsToAdd.find(e => e.indexOf(globals.spectrum.spectrum + '.' + globals.spectrum.dpt) >= 0)) { // with dpt
|
|
|
|
|
queryPtr.push(
|
|
|
|
|
{$lookup: {from: 'measurements', localField: '_id', foreignField: 'sample_id', as: 'measurements'}}
|
|
|
|
|
);
|
|
|
|
@ -344,21 +328,23 @@ router.get('/samples', async (req, res, next) => {
|
|
|
|
|
as: 'measurements'
|
|
|
|
|
}});
|
|
|
|
|
}
|
|
|
|
|
measurementTemplates.forEach(template => {
|
|
|
|
|
addMeasurements(queryPtr, template);
|
|
|
|
|
if (measurementFieldsFields.find(e => e === globals.spectrum.spectrum)) {
|
|
|
|
|
queryPtr.push({$unwind: '$' + globals.spectrum.spectrum});
|
|
|
|
|
const groupedMeasurementTemplates = measurementTemplates.reduce((s, e) => {
|
|
|
|
|
if (s.hasOwnProperty(e.name)) {
|
|
|
|
|
s[e.name].push(e);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
s[e.name] = [e];
|
|
|
|
|
}
|
|
|
|
|
return s;
|
|
|
|
|
}, {});
|
|
|
|
|
Object.values(groupedMeasurementTemplates).forEach(templates => {
|
|
|
|
|
addMeasurements(queryPtr, templates);
|
|
|
|
|
});
|
|
|
|
|
queryPtr.push({$project: {measurements: 0}});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const projection = filters.fields.map(e => e.replace('measurements.', ''))
|
|
|
|
|
.reduce((s, e) => {s[e] = true; return s; }, {});
|
|
|
|
|
if (filters.fields.indexOf('added') >= 0) { // add added date // TODO: upgrade MongoDB version or find alternative
|
|
|
|
|
// projection.added = {$toDate: '$_id'};
|
|
|
|
|
// projection.added = { $convert: { input: '$_id', to: "date" } }
|
|
|
|
|
}
|
|
|
|
|
if (filters.fields.indexOf('_id') < 0 && filters.fields.indexOf('added') < 0) { // disable _id explicitly
|
|
|
|
|
projection._id = false;
|
|
|
|
|
}
|
|
|
|
@ -781,12 +767,13 @@ function customFieldsChange (fields, amount, req) { // update custom_fields and
|
|
|
|
|
|
|
|
|
|
function sortQuery(filters, sortKeys, sortStartValue) { // sortKeys = ['primary key', 'secondary key']
|
|
|
|
|
if (filters['from-id']) { // from-id specified
|
|
|
|
|
const ssv = sortStartValue !== undefined; // if value is not given, match for existence
|
|
|
|
|
if ((filters['to-page'] === 0 && filters.sort[1] === 1) || (filters.sort[1] * filters['to-page'] > 0)) { // asc
|
|
|
|
|
return [
|
|
|
|
|
{$match: {$or: [
|
|
|
|
|
{[sortKeys[0]]: {$gt: sortStartValue}},
|
|
|
|
|
{[sortKeys[0]]: ssv ? {$gt: sortStartValue} : {$exists: true}},
|
|
|
|
|
{$and: [
|
|
|
|
|
{[sortKeys[0]]: sortStartValue},
|
|
|
|
|
{[sortKeys[0]]: ssv ? sortStartValue : {$exists: false}},
|
|
|
|
|
{[sortKeys[1]]: {$gte: new mongoose.Types.ObjectId(filters['from-id'])}}
|
|
|
|
|
]}
|
|
|
|
|
]}},
|
|
|
|
@ -795,9 +782,9 @@ function sortQuery(filters, sortKeys, sortStartValue) { // sortKeys = ['primary
|
|
|
|
|
} else {
|
|
|
|
|
return [
|
|
|
|
|
{$match: {$or: [
|
|
|
|
|
{[sortKeys[0]]: {$lt: sortStartValue}},
|
|
|
|
|
{[sortKeys[0]]: ssv ? {$lt: sortStartValue} : {$exists: false}},
|
|
|
|
|
{$and: [
|
|
|
|
|
{[sortKeys[0]]: sortStartValue},
|
|
|
|
|
{[sortKeys[0]]: ssv ? sortStartValue : {$exists: true}},
|
|
|
|
|
{[sortKeys[1]]: {$lte: new mongoose.Types.ObjectId(filters['from-id'])}}
|
|
|
|
|
]}
|
|
|
|
|
]}},
|
|
|
|
@ -834,20 +821,25 @@ function filterQueries (filters) {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// add measurements as property [template.name], if one result, array is reduced to direct values
|
|
|
|
|
function addMeasurements(queryPtr, template) {
|
|
|
|
|
// add measurements as property [template.name], if one result, array is reduced to direct values. All given templates
|
|
|
|
|
// must have the same name
|
|
|
|
|
function addMeasurements(queryPtr, templates) {
|
|
|
|
|
queryPtr.push(
|
|
|
|
|
{$addFields: {[template.name]: {$let: {vars: {
|
|
|
|
|
{$addFields: {[templates[0].name]: {$let: {vars: {
|
|
|
|
|
arr: {$filter: {
|
|
|
|
|
input: '$measurements', cond: {$eq: ['$$this.measurement_template', mongoose.Types.ObjectId(template._id)]}
|
|
|
|
|
input: '$measurements', cond: {$in: [
|
|
|
|
|
'$$this.measurement_template',
|
|
|
|
|
templates.map(e => mongoose.Types.ObjectId(e._id))
|
|
|
|
|
]}
|
|
|
|
|
}}},
|
|
|
|
|
in: {$cond: [{$lte: [{$size: '$$arr'}, 1]}, {$arrayElemAt: ['$$arr', 0]}, '$$arr']}
|
|
|
|
|
}}}},
|
|
|
|
|
{$addFields: {[template.name]: {$cond: [
|
|
|
|
|
'$' + template.name + '.values',
|
|
|
|
|
'$' + template.name + '.values',
|
|
|
|
|
template.parameters.reduce((s, e) => {s[e.name] = null; return s;}, {})
|
|
|
|
|
]}}}
|
|
|
|
|
{$addFields: {[templates[0].name]: {$cond: [
|
|
|
|
|
'$' + templates[0].name + '.values',
|
|
|
|
|
'$' + templates[0].name + '.values',
|
|
|
|
|
templates[0].parameters.reduce((s, e) => {s[e.name] = null; return s;}, {})
|
|
|
|
|
]}}},
|
|
|
|
|
{$unwind: '$' + templates[0].name}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|