|
|
|
@ -28,6 +28,7 @@ const router = express.Router();
|
|
|
|
|
|
|
|
|
|
// TODO: think about filter keys with measurement template versions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
router.get('/samples', async (req, res, next) => {
|
|
|
|
|
if (!req.auth(res, ['read', 'write', 'maintain', 'dev', 'admin'], 'all')) return;
|
|
|
|
|
|
|
|
|
@ -35,7 +36,8 @@ router.get('/samples', async (req, res, next) => {
|
|
|
|
|
if (error) return res400(error, res);
|
|
|
|
|
|
|
|
|
|
// TODO: find a better place for these
|
|
|
|
|
const sampleKeys = ['_id', 'color', 'number', 'type', 'batch', 'added', 'condition', 'material_id', 'note_id', 'user_id'];
|
|
|
|
|
const sampleKeys = ['_id', 'color', 'number', 'type', 'batch', 'added', 'condition', 'material_id', 'note_id',
|
|
|
|
|
'user_id'];
|
|
|
|
|
|
|
|
|
|
// evaluate sort parameter from 'color-asc' to ['color', 1]
|
|
|
|
|
filters.sort = filters.sort.split('-');
|
|
|
|
@ -74,7 +76,8 @@ router.get('/samples', async (req, res, next) => {
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
// start and end of day
|
|
|
|
|
const date = [new Date(addedFilter.values[0]).setHours(0,0,0,0), new Date(addedFilter.values[0]).setHours(23,59,59,999)];
|
|
|
|
|
const date = [new Date(addedFilter.values[0]).setHours(0,0,0,0),
|
|
|
|
|
new Date(addedFilter.values[0]).setHours(23,59,59,999)];
|
|
|
|
|
if (addedFilter.mode === 'lt') { // lt start
|
|
|
|
|
filters.filters.push({mode: 'lt', field: '_id', values: [dateToOId(date[0])]});
|
|
|
|
|
}
|
|
|
|
@ -88,7 +91,8 @@ router.get('/samples', async (req, res, next) => {
|
|
|
|
|
filters.filters.push({mode: 'gte', field: '_id', values: [dateToOId(date[0])]});
|
|
|
|
|
}
|
|
|
|
|
if (addedFilter.mode === 'ne') {
|
|
|
|
|
filters.filters.push({mode: 'or', field: '_id', values: [{ _id: { '$lt': dateToOId(date[0])}}, { _id: { '$gt': dateToOId(date[1])}}]});
|
|
|
|
|
filters.filters.push({mode: 'or', field: '_id',
|
|
|
|
|
values: [{ _id: { '$lt': dateToOId(date[0])}}, { _id: { '$gt': dateToOId(date[1])}}]});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -103,27 +107,31 @@ router.get('/samples', async (req, res, next) => {
|
|
|
|
|
if (filters.sort[0].indexOf('measurements.') >= 0) { // sorting with measurements as starting collection
|
|
|
|
|
collection = MeasurementModel;
|
|
|
|
|
const [,measurementName, measurementParam] = filters.sort[0].split('.');
|
|
|
|
|
const measurementTemplates = await MeasurementTemplateModel.find({name: measurementName}).lean().exec().catch(err => {next(err);});
|
|
|
|
|
const measurementTemplates = await MeasurementTemplateModel.find({name: measurementName})
|
|
|
|
|
.lean().exec().catch(err => {next(err);});
|
|
|
|
|
if (measurementTemplates instanceof Error) return;
|
|
|
|
|
if (!measurementTemplates) {
|
|
|
|
|
return res.status(400).json({status: 'Invalid body format', details: filters.sort[0] + ' not found'});
|
|
|
|
|
}
|
|
|
|
|
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?
|
|
|
|
|
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?
|
|
|
|
|
if (fromSample instanceof Error) return;
|
|
|
|
|
if (!fromSample) {
|
|
|
|
|
return res.status(400).json({status: 'Invalid body format', details: 'from-id not found'});
|
|
|
|
|
}
|
|
|
|
|
sortStartValue = fromSample.values[measurementParam];
|
|
|
|
|
}
|
|
|
|
|
queryPtr[0].$match.$and.push({measurement_template: {$in: measurementTemplates.map(e => e._id)}}); // find measurements to sort
|
|
|
|
|
// find measurements to sort
|
|
|
|
|
queryPtr[0].$match.$and.push({measurement_template: {$in: measurementTemplates.map(e => e._id)}});
|
|
|
|
|
if (filters.filters.find(e => e.field === filters.sort[0])) { // sorted measurement should also be filtered
|
|
|
|
|
queryPtr[0].$match.$and.push(...filterQueries(filters.filters.filter(e => e.field === filters.sort[0]).map(e => {e.field = 'values.' + e.field.split('.')[2]; return e; })));
|
|
|
|
|
queryPtr[0].$match.$and.push(...filterQueries(filters.filters.filter(e => e.field === filters.sort[0])
|
|
|
|
|
.map(e => {e.field = 'values.' + e.field.split('.')[2]; return e; })));
|
|
|
|
|
}
|
|
|
|
|
queryPtr.push(
|
|
|
|
|
...sortQuery(filters, ['values.' + measurementParam, 'sample_id'], sortStartValue), // sort measurements
|
|
|
|
|
{$replaceRoot: {newRoot: {measurement: '$$ROOT'}}}, // fetch samples and restructure them to fit sample structure
|
|
|
|
|
{$replaceRoot: {newRoot: {measurement: '$$ROOT'}}}, // fetch samples and restructure them to fit sample structure
|
|
|
|
|
{$lookup: {from: 'samples', localField: 'measurement.sample_id', foreignField: '_id', as: 'sample'}},
|
|
|
|
|
{$match: statusQuery(filters, 'sample.status')}, // filter out wrong status once samples were added
|
|
|
|
|
{$addFields: {['sample.' + measurementName]: '$measurement.values'}}, // more restructuring
|
|
|
|
@ -159,43 +167,52 @@ router.get('/samples', async (req, res, next) => {
|
|
|
|
|
let materialAdded = false;
|
|
|
|
|
if (sortFilterKeys.find(e => /material\./.test(e))) { // add material fields
|
|
|
|
|
materialAdded = true;
|
|
|
|
|
materialQuery.push( // add material properties
|
|
|
|
|
{$lookup: {from: 'materials', localField: 'material_id', foreignField: '_id', as: 'material'}}, // TODO: project out unnecessary fields
|
|
|
|
|
materialQuery.push( // add material properties // TODO: project out unnecessary fields
|
|
|
|
|
{$lookup: {from: 'materials', localField: 'material_id', foreignField: '_id', as: 'material'}},
|
|
|
|
|
{$addFields: {material: {$arrayElemAt: ['$material', 0]}}}
|
|
|
|
|
);
|
|
|
|
|
const baseMFilters = sortFilterKeys.filter(e => /material\./.test(e)).filter(e => ['material.supplier', 'material.group', 'material.number'].indexOf(e) < 0);
|
|
|
|
|
addFilterQueries(materialQuery, filters.filters.filter(e => baseMFilters.indexOf(e.field) >= 0)); // base material filters
|
|
|
|
|
const baseMFilters = sortFilterKeys.filter(e => /material\./.test(e))
|
|
|
|
|
.filter(e => ['material.supplier', 'material.group', 'material.number'].indexOf(e) < 0);
|
|
|
|
|
// base material filters
|
|
|
|
|
addFilterQueries(materialQuery, filters.filters.filter(e => baseMFilters.indexOf(e.field) >= 0));
|
|
|
|
|
if (sortFilterKeys.find(e => e === 'material.supplier')) { // add supplier if needed
|
|
|
|
|
materialQuery.push(
|
|
|
|
|
{$lookup: { from: 'material_suppliers', localField: 'material.supplier_id', foreignField: '_id', as: 'material.supplier'}},
|
|
|
|
|
{$lookup: {
|
|
|
|
|
from: 'material_suppliers', localField: 'material.supplier_id', foreignField: '_id', as: 'material.supplier'}
|
|
|
|
|
},
|
|
|
|
|
{$addFields: {'material.supplier': {$arrayElemAt: ['$material.supplier.name', 0]}}}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if (sortFilterKeys.find(e => e === 'material.group')) { // add group if needed
|
|
|
|
|
materialQuery.push(
|
|
|
|
|
{$lookup: { from: 'material_groups', localField: 'material.group_id', foreignField: '_id', as: 'material.group' }},
|
|
|
|
|
{$lookup: {
|
|
|
|
|
from: 'material_groups', localField: 'material.group_id', foreignField: '_id', as: 'material.group' }
|
|
|
|
|
},
|
|
|
|
|
{$addFields: {'material.group': { $arrayElemAt: ['$material.group.name', 0]}}}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
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']}]}}}
|
|
|
|
|
{$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);
|
|
|
|
|
addFilterQueries(materialQuery, filters.filters.filter(e => specialMFilters.indexOf(e.field) >= 0)); // base material filters
|
|
|
|
|
const specialMFilters = sortFilterKeys.filter(e => /material\./.test(e))
|
|
|
|
|
.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);
|
|
|
|
|
if (/material\./.test(filters.sort[0])) { // sort by material key
|
|
|
|
|
let sortStartValue = null;
|
|
|
|
|
if (filters['from-id']) { // from-id specified
|
|
|
|
|
const fromSample = await SampleModel.aggregate([{$match: {_id: mongoose.Types.ObjectId(filters['from-id'])}}, ...materialQuery]).exec().catch(err => {next(err);});
|
|
|
|
|
const fromSample = await SampleModel.aggregate(
|
|
|
|
|
[{$match: {_id: mongoose.Types.ObjectId(filters['from-id'])}}, ...materialQuery]
|
|
|
|
|
).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'});
|
|
|
|
|
}
|
|
|
|
|
console.log(fromSample);
|
|
|
|
|
console.log(filters.sort[0]);
|
|
|
|
|
console.log(fromSample[filters.sort[0]]);
|
|
|
|
|
const filterKey = filters.sort[0].split('.');
|
|
|
|
|
if (filterKey.length === 2) {
|
|
|
|
|
sortStartValue = fromSample[0][filterKey[0]][filterKey[1]];
|
|
|
|
@ -208,23 +225,25 @@ router.get('/samples', async (req, res, next) => {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const measurementFilterFields = _.uniq(sortFilterKeys.filter(e => /measurements\./.test(e)).map(e => e.split('.')[1])); // filter measurement names and remove duplicates from parameters
|
|
|
|
|
const measurementFilterFields = _.uniq(sortFilterKeys.filter(e => /measurements\./.test(e))
|
|
|
|
|
.map(e => e.split('.')[1])); // filter measurement names and remove duplicates from parameters
|
|
|
|
|
if (sortFilterKeys.find(e => /measurements\./.test(e))) { // add measurement fields
|
|
|
|
|
const measurementTemplates = await MeasurementTemplateModel.find({name: {$in: measurementFilterFields}}).lean().exec().catch(err => {next(err);});
|
|
|
|
|
const measurementTemplates = await MeasurementTemplateModel.find({name: {$in: measurementFilterFields}})
|
|
|
|
|
.lean().exec().catch(err => {next(err);});
|
|
|
|
|
if (measurementTemplates instanceof Error) return;
|
|
|
|
|
if (measurementTemplates.length < measurementFilterFields.length) {
|
|
|
|
|
return res.status(400).json({status: 'Invalid body format', details: 'Measurement key not found'});
|
|
|
|
|
}
|
|
|
|
|
queryPtr.push({$lookup: {
|
|
|
|
|
from: 'measurements', let: {sId: '$_id'},
|
|
|
|
|
pipeline: [{$match: {$expr: {$and: [{$eq: ['$sample_id', '$$sId']}, {$in: ['$measurement_template', measurementTemplates.map(e => mongoose.Types.ObjectId(e._id))]}]}}}],
|
|
|
|
|
pipeline: [{$match: {$expr: {$and: [
|
|
|
|
|
{$eq: ['$sample_id', '$$sId']},
|
|
|
|
|
{$in: ['$measurement_template', measurementTemplates.map(e => mongoose.Types.ObjectId(e._id))]}
|
|
|
|
|
]}}}],
|
|
|
|
|
as: 'measurements'
|
|
|
|
|
}});
|
|
|
|
|
measurementTemplates.forEach(template => {
|
|
|
|
|
queryPtr.push({$addFields: {[template.name]: {$let: { // add measurements as property [template.name], if one result, array is reduced to direct values
|
|
|
|
|
vars: {arr: {$filter: {input: '$measurements', cond: {$eq: ['$$this.measurement_template', mongoose.Types.ObjectId(template._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;}, {})]}}});
|
|
|
|
|
addMeasurements(queryPtr, template);
|
|
|
|
|
});
|
|
|
|
|
addFilterQueries(queryPtr, filters.filters
|
|
|
|
|
.filter(e => sortFilterKeys.filter(e => /measurements\./.test(e)).indexOf(e.field) >= 0)
|
|
|
|
@ -232,14 +251,18 @@ router.get('/samples', async (req, res, next) => {
|
|
|
|
|
); // measurement filters
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!filters.fields.find(e => /spectrum\./.test(e)) && !filters['from-id']) { // count total number of items before $skip and $limit, only works when from-id is not specified and spectra are not included
|
|
|
|
|
// count total number of items before $skip and $limit, only works when from-id is not specified and spectra are not
|
|
|
|
|
// included
|
|
|
|
|
if (!filters.fields.find(e => /spectrum\./.test(e)) && !filters['from-id']) {
|
|
|
|
|
queryPtr.push({$facet: {count: [{$count: 'count'}], samples: []}});
|
|
|
|
|
queryPtr = queryPtr[queryPtr.length - 1].$facet.samples; // add rest of aggregation pipeline into $facet
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// paging
|
|
|
|
|
if (filters['to-page']) {
|
|
|
|
|
queryPtr.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
|
|
|
|
|
// number to skip, if going back pages, one page has to be skipped less but on sample more
|
|
|
|
|
queryPtr.push({$skip: Math.abs(filters['to-page'] + Number(filters['to-page'] < 0)) * filters['page-size'] +
|
|
|
|
|
Number(filters['to-page'] < 0)})
|
|
|
|
|
}
|
|
|
|
|
if (filters['page-size']) {
|
|
|
|
|
queryPtr.push({$limit: filters['page-size']});
|
|
|
|
@ -265,51 +288,65 @@ router.get('/samples', async (req, res, next) => {
|
|
|
|
|
}
|
|
|
|
|
if (fieldsToAdd.indexOf('material.supplier') >= 0) { // add supplier if needed
|
|
|
|
|
queryPtr.push(
|
|
|
|
|
{$lookup: { from: 'material_suppliers', localField: 'material.supplier_id', foreignField: '_id', as: 'material.supplier'}},
|
|
|
|
|
{$lookup: {
|
|
|
|
|
from: 'material_suppliers', localField: 'material.supplier_id', foreignField: '_id', as: 'material.supplier'
|
|
|
|
|
}},
|
|
|
|
|
{$addFields: {'material.supplier': {$arrayElemAt: ['$material.supplier.name', 0]}}}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if (fieldsToAdd.indexOf('material.group') >= 0) { // add group if needed
|
|
|
|
|
queryPtr.push(
|
|
|
|
|
{$lookup: { from: 'material_groups', localField: 'material.group_id', foreignField: '_id', as: 'material.group' }},
|
|
|
|
|
{$lookup: {
|
|
|
|
|
from: 'material_groups', localField: 'material.group_id', foreignField: '_id', as: 'material.group'
|
|
|
|
|
}},
|
|
|
|
|
{$addFields: {'material.group': { $arrayElemAt: ['$material.group.name', 0]}}}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if (fieldsToAdd.indexOf('material.number') >= 0) { // add material number if needed
|
|
|
|
|
queryPtr.push(
|
|
|
|
|
{$addFields: {'material.number': { $arrayElemAt: ['$material.numbers.number', {$indexOfArray: ['$material.numbers.color', '$color']}]}}}
|
|
|
|
|
{$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])); // filter measurement names and remove duplicates from parameters
|
|
|
|
|
let measurementFieldsFields: string[] = _.uniq(
|
|
|
|
|
fieldsToAdd.filter(e => /measurements\./.test(e)).map(e => e.split('.')[1])
|
|
|
|
|
); // filter measurement names and remove duplicates from parameters
|
|
|
|
|
if (fieldsToAdd.find(e => /measurements\./.test(e))) { // add measurement fields
|
|
|
|
|
const measurementTemplates = await MeasurementTemplateModel.find({name: {$in: measurementFieldsFields}}).lean().exec().catch(err => {next(err);});
|
|
|
|
|
const measurementTemplates = await MeasurementTemplateModel.find({name: {$in: measurementFieldsFields}})
|
|
|
|
|
.lean().exec().catch(err => {next(err);});
|
|
|
|
|
if (measurementTemplates instanceof Error) return;
|
|
|
|
|
if (measurementTemplates.length < measurementFieldsFields.length) {
|
|
|
|
|
return res.status(400).json({status: 'Invalid body format', details: 'Measurement key not found'});
|
|
|
|
|
}
|
|
|
|
|
if (fieldsToAdd.find(e => /spectrum\./.test(e))) { // use different lookup methods with and without spectrum for the best performance
|
|
|
|
|
queryPtr.push({$lookup: {from: 'measurements', localField: '_id', foreignField: 'sample_id', as: 'measurements'}});
|
|
|
|
|
// use different lookup methods with and without spectrum for the best performance
|
|
|
|
|
if (fieldsToAdd.find(e => /spectrum\./.test(e))) {
|
|
|
|
|
queryPtr.push(
|
|
|
|
|
{$lookup: {from: 'measurements', localField: '_id', foreignField: 'sample_id', as: 'measurements'}}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
queryPtr.push({$lookup: {
|
|
|
|
|
from: 'measurements', let: {sId: '$_id'},
|
|
|
|
|
pipeline: [{$match: {$expr: {$and: [{$eq: ['$sample_id', '$$sId']}, {$in: ['$measurement_template', measurementTemplates.map(e => mongoose.Types.ObjectId(e._id))]}]}}}],
|
|
|
|
|
pipeline: [{$match: {$expr: {$and: [
|
|
|
|
|
{$eq: ['$sample_id', '$$sId']},
|
|
|
|
|
{$in: ['$measurement_template', measurementTemplates.map(e => mongoose.Types.ObjectId(e._id))]}
|
|
|
|
|
]}}}],
|
|
|
|
|
as: 'measurements'
|
|
|
|
|
}});
|
|
|
|
|
}
|
|
|
|
|
measurementTemplates.forEach(template => { // TODO: hard coded dpt for special treatment, change later
|
|
|
|
|
queryPtr.push({$addFields: {[template.name]: {$let: { // add measurements as property [template.name], if one result, array is reduced to direct values
|
|
|
|
|
vars: {arr: {$filter: {input: '$measurements', cond: {$eq: ['$$this.measurement_template', mongoose.Types.ObjectId(template._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;}, {})]}}});
|
|
|
|
|
addMeasurements(queryPtr, template);
|
|
|
|
|
if (measurementFieldsFields.find(e => e === 'spectrum')) {
|
|
|
|
|
queryPtr.push({$unwind: '$spectrum'});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
// if (measurementFieldsFields.find(e => e === 'spectrum')) { // TODO: remove hardcoded as well
|
|
|
|
|
// queryPtr.push(
|
|
|
|
|
// {$addFields: {spectrum: {$filter: {input: '$measurements', cond: {$eq: ['$$this.measurement_template', measurementTemplates.filter(e => e.name === 'spectrum')[0]._id]}}}}},
|
|
|
|
|
// {$addFields: {spectrum: {$filter: {input: '$measurements', cond: {
|
|
|
|
|
// $eq: ['$$this.measurement_template', measurementTemplates.filter(e => e.name === 'spectrum')[0]._id]
|
|
|
|
|
// }}}}},
|
|
|
|
|
// {$addFields: {spectrum: '$spectrum.values'}},
|
|
|
|
|
// {$unwind: '$spectrum'}
|
|
|
|
|
// );
|
|
|
|
@ -318,10 +355,11 @@ router.get('/samples', async (req, res, next) => {
|
|
|
|
|
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
|
|
|
|
|
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" } } // TODO: upgrade MongoDB version or find alternative
|
|
|
|
|
// projection.added = { $convert: { input: '$_id', to: "date" } }
|
|
|
|
|
}
|
|
|
|
|
if (filters.fields.indexOf('_id') < 0 && filters.fields.indexOf('added') < 0) { // disable _id explicitly
|
|
|
|
|
projection._id = false;
|
|
|
|
@ -347,7 +385,10 @@ router.get('/samples', async (req, res, next) => {
|
|
|
|
|
if (filters['to-page'] < 0) {
|
|
|
|
|
data.reverse();
|
|
|
|
|
}
|
|
|
|
|
const measurementFields = _.uniq([filters.sort[0].split('.')[1], ...measurementFilterFields, ...measurementFieldsFields]);
|
|
|
|
|
const measurementFields = _.uniq(
|
|
|
|
|
[filters.sort[0].split('.')[1],
|
|
|
|
|
...measurementFilterFields, ...measurementFieldsFields]
|
|
|
|
|
);
|
|
|
|
|
if (filters.csv) { // output as csv
|
|
|
|
|
csv(_.compact(data.map(e => SampleValidate.output(e, 'refs', measurementFields))), (err, data) => {
|
|
|
|
|
if (err) return next(err);
|
|
|
|
@ -355,8 +396,8 @@ router.get('/samples', async (req, res, next) => {
|
|
|
|
|
res.send(data);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
res.json(_.compact(data.map(e => SampleValidate.output(e, 'refs', measurementFields)))); // validate all and filter null values from validation errors
|
|
|
|
|
else { // validate all and filter null values from validation errors
|
|
|
|
|
res.json(_.compact(data.map(e => SampleValidate.output(e, 'refs', measurementFields))));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
@ -389,7 +430,8 @@ router.get('/samples/:state(new|deleted)', (req, res, next) => {
|
|
|
|
|
|
|
|
|
|
SampleModel.find({status: globals.status[req.params.state]}).lean().exec((err, data) => {
|
|
|
|
|
if (err) return next(err);
|
|
|
|
|
res.json(_.compact(data.map(e => SampleValidate.output(e)))); // validate all and filter null values from validation errors
|
|
|
|
|
// validate all and filter null values from validation errors
|
|
|
|
|
res.json(_.compact(data.map(e => SampleValidate.output(e))));
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
@ -405,7 +447,8 @@ router.get('/samples/count', (req, res, next) => {
|
|
|
|
|
router.get('/sample/' + IdValidate.parameter(), (req, res, next) => {
|
|
|
|
|
if (!req.auth(res, ['read', 'write', 'maintain', 'dev', 'admin'], 'all')) return;
|
|
|
|
|
|
|
|
|
|
SampleModel.findById(req.params.id).populate('material_id').populate('user_id', 'name').populate('note_id').exec(async (err, sampleData: any) => {
|
|
|
|
|
SampleModel.findById(req.params.id).populate('material_id').populate('user_id', 'name').populate('note_id')
|
|
|
|
|
.exec(async (err, sampleData: any) => {
|
|
|
|
|
if (err) return next(err);
|
|
|
|
|
await sampleReturn(sampleData, req, res, next);
|
|
|
|
|
});
|
|
|
|
@ -434,8 +477,11 @@ router.put('/sample/' + IdValidate.parameter(), (req, res, next) => {
|
|
|
|
|
else if (sample.hasOwnProperty('color')) {
|
|
|
|
|
if (!await materialCheck(sample, res, next, sampleData.material_id)) return;
|
|
|
|
|
}
|
|
|
|
|
if (sample.hasOwnProperty('condition') && !(_.isEmpty(sample.condition) && _.isEmpty(sampleData.condition))) { // do not execute check if condition is and was empty
|
|
|
|
|
if (!await conditionCheck(sample.condition, 'change', res, next, sampleData.condition.condition_template.toString() !== sample.condition.condition_template)) return;
|
|
|
|
|
// do not execute check if condition is and was empty
|
|
|
|
|
if (sample.hasOwnProperty('condition') && !(_.isEmpty(sample.condition) && _.isEmpty(sampleData.condition))) {
|
|
|
|
|
if (!await conditionCheck(sample.condition, 'change', res, next,
|
|
|
|
|
!(sampleData.condition.condition_template &&
|
|
|
|
|
sampleData.condition.condition_template.toString() === sample.condition.condition_template))) return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sample.hasOwnProperty('notes')) {
|
|
|
|
@ -443,7 +489,8 @@ router.put('/sample/' + IdValidate.parameter(), (req, res, next) => {
|
|
|
|
|
if (sampleData.note_id !== null) { // old notes data exists
|
|
|
|
|
const data = await NoteModel.findById(sampleData.note_id).lean().exec().catch(err => {next(err);}) as any;
|
|
|
|
|
if (data instanceof Error) return;
|
|
|
|
|
newNotes = !_.isEqual(_.pick(IdValidate.stringify(data), _.keys(sample.notes)), sample.notes); // check if notes were changed
|
|
|
|
|
// check if notes were changed
|
|
|
|
|
newNotes = !_.isEqual(_.pick(IdValidate.stringify(data), _.keys(sample.notes)), sample.notes);
|
|
|
|
|
if (newNotes) {
|
|
|
|
|
if (data.hasOwnProperty('custom_fields')) { // update note_fields
|
|
|
|
|
customFieldsChange(Object.keys(data.custom_fields), -1, req);
|
|
|
|
@ -456,7 +503,8 @@ router.put('/sample/' + IdValidate.parameter(), (req, res, next) => {
|
|
|
|
|
|
|
|
|
|
if (_.keys(sample.notes).length > 0 && newNotes) { // save new notes
|
|
|
|
|
if (!await sampleRefCheck(sample, res, next)) return;
|
|
|
|
|
if (sample.notes.hasOwnProperty('custom_fields') && Object.keys(sample.notes.custom_fields).length > 0) { // new custom_fields
|
|
|
|
|
// new custom_fields
|
|
|
|
|
if (sample.notes.hasOwnProperty('custom_fields') && Object.keys(sample.notes.custom_fields).length > 0) {
|
|
|
|
|
customFieldsChange(Object.keys(sample.notes.custom_fields), 1, req);
|
|
|
|
|
}
|
|
|
|
|
let data = await new NoteModel(sample.notes).save().catch(err => { return next(err)}); // save new notes
|
|
|
|
@ -491,11 +539,13 @@ router.delete('/sample/' + IdValidate.parameter(), (req, res, next) => {
|
|
|
|
|
// only maintain and admin are allowed to edit other user's data
|
|
|
|
|
if (sampleData.user_id.toString() !== req.authDetails.id && !req.auth(res, ['maintain', 'admin'], 'basic')) return;
|
|
|
|
|
|
|
|
|
|
await SampleModel.findByIdAndUpdate(req.params.id, {status:globals.status.deleted}).log(req).lean().exec(err => { // set sample status
|
|
|
|
|
// set sample status
|
|
|
|
|
await SampleModel.findByIdAndUpdate(req.params.id, {status:globals.status.deleted}).log(req).lean().exec(err => {
|
|
|
|
|
if (err) return next(err);
|
|
|
|
|
|
|
|
|
|
// set status of associated measurements also to deleted
|
|
|
|
|
MeasurementModel.updateMany({sample_id: mongoose.Types.ObjectId(req.params.id)}, {status: -1}).log(req).lean().exec(err => {
|
|
|
|
|
MeasurementModel.updateMany({sample_id: mongoose.Types.ObjectId(req.params.id)}, {status: -1})
|
|
|
|
|
.log(req).lean().exec(err => {
|
|
|
|
|
if (err) return next(err);
|
|
|
|
|
|
|
|
|
|
if (sampleData.note_id !== null) { // handle notes
|
|
|
|
@ -518,7 +568,8 @@ router.delete('/sample/' + IdValidate.parameter(), (req, res, next) => {
|
|
|
|
|
router.get('/sample/number/:number', (req, res, next) => {
|
|
|
|
|
if (!req.auth(res, ['read', 'write', 'maintain', 'dev', 'admin'], 'all')) return;
|
|
|
|
|
|
|
|
|
|
SampleModel.findOne({number: req.params.number}).populate('material_id').populate('user_id', 'name').populate('note_id').exec(async (err, sampleData: any) => {
|
|
|
|
|
SampleModel.findOne({number: req.params.number}).populate('material_id').populate('user_id', 'name')
|
|
|
|
|
.populate('note_id').exec(async (err, sampleData: any) => {
|
|
|
|
|
if (err) return next(err);
|
|
|
|
|
await sampleReturn(sampleData, req, res, next);
|
|
|
|
|
});
|
|
|
|
@ -572,13 +623,15 @@ router.post('/sample/new', async (req, res, next) => {
|
|
|
|
|
req.body.condition = {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const {error, value: sample} = SampleValidate.input(req.body, 'new' + (req.authDetails.level === 'admin' ? '-admin' : ''));
|
|
|
|
|
const {error, value: sample} =
|
|
|
|
|
SampleValidate.input(req.body, 'new' + (req.authDetails.level === 'admin' ? '-admin' : ''));
|
|
|
|
|
if (error) return res400(error, res);
|
|
|
|
|
|
|
|
|
|
if (!await materialCheck(sample, res, next)) return;
|
|
|
|
|
if (!await sampleRefCheck(sample, res, next)) return;
|
|
|
|
|
|
|
|
|
|
if (sample.notes.hasOwnProperty('custom_fields') && Object.keys(sample.notes.custom_fields).length > 0) { // new custom_fields
|
|
|
|
|
// new custom_fields
|
|
|
|
|
if (sample.notes.hasOwnProperty('custom_fields') && Object.keys(sample.notes.custom_fields).length > 0) {
|
|
|
|
|
customFieldsChange(Object.keys(sample.notes.custom_fields), 1, req);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -615,21 +668,27 @@ router.get('/sample/notes/fields', (req, res, next) => {
|
|
|
|
|
|
|
|
|
|
NoteFieldModel.find({}).lean().exec((err, data) => {
|
|
|
|
|
if (err) return next(err);
|
|
|
|
|
res.json(_.compact(data.map(e => NoteFieldValidate.output(e)))); // validate all and filter null values from validation errors
|
|
|
|
|
// validate all and filter null values from validation errors
|
|
|
|
|
res.json(_.compact(data.map(e => NoteFieldValidate.output(e))));
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = router;
|
|
|
|
|
|
|
|
|
|
// store the highest generated number for each location to avoid duplicate numbers
|
|
|
|
|
const numberBuffer: {[location: string]: number} = {};
|
|
|
|
|
|
|
|
|
|
async function numberGenerate (sample, req, res, next) { // generate number in format Location32, returns false on error
|
|
|
|
|
// generate number in format Location32, returns false on error
|
|
|
|
|
async function numberGenerate (sample, req, res, next) {
|
|
|
|
|
const sampleData = await SampleModel
|
|
|
|
|
.aggregate([
|
|
|
|
|
{$match: {number: new RegExp('^' + req.authDetails.location + '[0-9]+$', 'm')}},
|
|
|
|
|
// {$addFields: {number2: {$toDecimal: {$arrayElemAt: [{$split: [{$arrayElemAt: [{$split: ['$number', 'Rng']}, 1]}, '_']}, 0]}}}}, // not working with MongoDb 3.6
|
|
|
|
|
// {$addFields: {number2: {$toDecimal: {$arrayElemAt: [{$split: [{$arrayElemAt:
|
|
|
|
|
// [{$split: ['$number', 'Rng']}, 1]}, '_']}, 0]}}}}, // not working with MongoDb 3.6
|
|
|
|
|
{$addFields: {sortNumber: {$let: {
|
|
|
|
|
vars: {tmp: {$concat: ['000000000000000000000000000000', {$arrayElemAt: [{$split: [{$arrayElemAt: [{$split: ['$number', 'Rng']}, 1]}, '_']}, 0]}]}},
|
|
|
|
|
vars: {tmp: {$concat: ['000000000000000000000000000000',
|
|
|
|
|
{$arrayElemAt: [{$split: [{$arrayElemAt: [{$split: ['$number', 'Rng']}, 1]}, '_']}, 0]}]}},
|
|
|
|
|
in: {$substrCP: ['$$tmp', {$subtract: [{$strLenCP: '$$tmp'}, 30]}, {$strLenCP: '$$tmp'}]}
|
|
|
|
|
}}}},
|
|
|
|
|
{$sort: {sortNumber: -1}},
|
|
|
|
@ -638,11 +697,18 @@ async function numberGenerate (sample, req, res, next) { // generate number in
|
|
|
|
|
.exec()
|
|
|
|
|
.catch(err => next(err));
|
|
|
|
|
if (sampleData instanceof Error) return false;
|
|
|
|
|
return req.authDetails.location + (sampleData[0] ? Number(sampleData[0].number.replace(/[^0-9]+/g, '')) + 1 : 1);
|
|
|
|
|
let number = (sampleData[0] ? Number(sampleData[0].number.replace(/[^0-9]+/g, '')) : 0);
|
|
|
|
|
if (numberBuffer[req.authDetails.location] && numberBuffer[req.authDetails.location] >= number) {
|
|
|
|
|
number = numberBuffer[req.authDetails.location];
|
|
|
|
|
}
|
|
|
|
|
number ++;
|
|
|
|
|
numberBuffer[req.authDetails.location] = number;
|
|
|
|
|
return req.authDetails.location + number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function numberCheck(sample, res, next) {
|
|
|
|
|
const sampleData = await SampleModel.findOne({number: sample.number}).lean().exec().catch(err => {next(err); return false;});
|
|
|
|
|
const sampleData = await SampleModel.findOne({number: sample.number})
|
|
|
|
|
.lean().exec().catch(err => {next(err); return false;});
|
|
|
|
|
if (sampleData) { // found entry with sample number
|
|
|
|
|
res.status(400).json({status: 'Sample number already taken'});
|
|
|
|
|
return false
|
|
|
|
@ -650,7 +716,8 @@ async function numberCheck(sample, res, next) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function materialCheck (sample, res, next, id = sample.material_id) { // validate material_id and color, returns false if invalid
|
|
|
|
|
// validate material_id and color, returns false if invalid
|
|
|
|
|
async function materialCheck (sample, res, next, id = sample.material_id) {
|
|
|
|
|
const materialData = await MaterialModel.findById(id).lean().exec().catch(err => next(err)) as any;
|
|
|
|
|
if (materialData instanceof Error) return false;
|
|
|
|
|
if (!materialData) { // could not find material_id
|
|
|
|
@ -660,12 +727,14 @@ async function materialCheck (sample, res, next, id = sample.material_id) { //
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function conditionCheck (condition, param, res, next, checkVersion = true) { // validate treatment template, returns false if invalid, otherwise template data
|
|
|
|
|
// validate treatment template, returns false if invalid, otherwise template data
|
|
|
|
|
async function conditionCheck (condition, param, res, next, checkVersion = true) {
|
|
|
|
|
if (!condition.condition_template || !IdValidate.valid(condition.condition_template)) { // template id not found
|
|
|
|
|
res.status(400).json({status: 'Condition template not available'});
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
const conditionData = await ConditionTemplateModel.findById(condition.condition_template).lean().exec().catch(err => next(err)) as any;
|
|
|
|
|
const conditionData = await ConditionTemplateModel.findById(condition.condition_template)
|
|
|
|
|
.lean().exec().catch(err => next(err)) as any;
|
|
|
|
|
if (conditionData instanceof Error) return false;
|
|
|
|
|
if (!conditionData) { // template not found
|
|
|
|
|
res.status(400).json({status: 'Condition template not available'});
|
|
|
|
@ -674,7 +743,8 @@ async function conditionCheck (condition, param, res, next, checkVersion = true)
|
|
|
|
|
|
|
|
|
|
if (checkVersion) {
|
|
|
|
|
// get all template versions and check if given is latest
|
|
|
|
|
const conditionVersions = await ConditionTemplateModel.find({first_id: conditionData.first_id}).sort({version: -1}).lean().exec().catch(err => next(err)) as any;
|
|
|
|
|
const conditionVersions = await ConditionTemplateModel.find({first_id: conditionData.first_id})
|
|
|
|
|
.sort({version: -1}).lean().exec().catch(err => next(err)) as any;
|
|
|
|
|
if (conditionVersions instanceof Error) return false;
|
|
|
|
|
if (condition.condition_template !== conditionVersions[0]._id.toString()) { // template not latest
|
|
|
|
|
res.status(400).json({status: 'Old template version not allowed'});
|
|
|
|
@ -683,14 +753,16 @@ async function conditionCheck (condition, param, res, next, checkVersion = true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// validate parameters
|
|
|
|
|
const {error, value: ignore} = ParametersValidate.input(_.omit(condition, 'condition_template'), conditionData.parameters, param);
|
|
|
|
|
const {error, value: ignore} =
|
|
|
|
|
ParametersValidate.input(_.omit(condition, 'condition_template'), conditionData.parameters, param);
|
|
|
|
|
if (error) {res400(error, res); return false;}
|
|
|
|
|
return conditionData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function sampleRefCheck (sample, res, next) { // validate sample_references, resolves false for invalid reference
|
|
|
|
|
return new Promise(resolve => {
|
|
|
|
|
if (sample.notes.hasOwnProperty('sample_references') && sample.notes.sample_references.length > 0) { // there are sample_references
|
|
|
|
|
// there are sample_references
|
|
|
|
|
if (sample.notes.hasOwnProperty('sample_references') && sample.notes.sample_references.length > 0) {
|
|
|
|
|
let referencesCount = sample.notes.sample_references.length; // count to keep track of running async operations
|
|
|
|
|
|
|
|
|
|
sample.notes.sample_references.forEach(reference => {
|
|
|
|
@ -715,7 +787,8 @@ function sampleRefCheck (sample, res, next) { // validate sample_references, re
|
|
|
|
|
|
|
|
|
|
function customFieldsChange (fields, amount, req) { // update custom_fields and respective quantities
|
|
|
|
|
fields.forEach(field => {
|
|
|
|
|
NoteFieldModel.findOneAndUpdate({name: field}, {$inc: {qty: amount}} as any, {new: true}).log(req).lean().exec((err, data: any) => { // check if field exists
|
|
|
|
|
NoteFieldModel.findOneAndUpdate({name: field}, {$inc: {qty: amount}} as any, {new: true})
|
|
|
|
|
.log(req).lean().exec((err, data: any) => { // check if field exists
|
|
|
|
|
if (err) return console.error(err);
|
|
|
|
|
if (!data) { // new field
|
|
|
|
|
new NoteFieldModel({name: field, qty: 1}).save((err, data) => {
|
|
|
|
@ -735,11 +808,27 @@ 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
|
|
|
|
|
if ((filters['to-page'] === 0 && filters.sort[1] === 1) || (filters.sort[1] * filters['to-page'] > 0)) { // asc
|
|
|
|
|
return [{$match: {$or: [{[sortKeys[0]]: {$gt: sortStartValue}}, {$and: [{[sortKeys[0]]: sortStartValue}, {[sortKeys[1]]: {$gte: new mongoose.Types.ObjectId(filters['from-id'])}}]}]}},
|
|
|
|
|
{$sort: {[sortKeys[0]]: 1, _id: 1}}];
|
|
|
|
|
return [
|
|
|
|
|
{$match: {$or: [
|
|
|
|
|
{[sortKeys[0]]: {$gt: sortStartValue}},
|
|
|
|
|
{$and: [
|
|
|
|
|
{[sortKeys[0]]: sortStartValue},
|
|
|
|
|
{[sortKeys[1]]: {$gte: new mongoose.Types.ObjectId(filters['from-id'])}}
|
|
|
|
|
]}
|
|
|
|
|
]}},
|
|
|
|
|
{$sort: {[sortKeys[0]]: 1, _id: 1}}
|
|
|
|
|
];
|
|
|
|
|
} else {
|
|
|
|
|
return [{$match: {$or: [{[sortKeys[0]]: {$lt: sortStartValue}}, {$and: [{[sortKeys[0]]: sortStartValue}, {[sortKeys[1]]: {$lte: new mongoose.Types.ObjectId(filters['from-id'])}}]}]}},
|
|
|
|
|
{$sort: {[sortKeys[0]]: -1, _id: -1}}];
|
|
|
|
|
return [
|
|
|
|
|
{$match: {$or: [
|
|
|
|
|
{[sortKeys[0]]: {$lt: sortStartValue}},
|
|
|
|
|
{$and: [
|
|
|
|
|
{[sortKeys[0]]: sortStartValue},
|
|
|
|
|
{[sortKeys[1]]: {$lte: new mongoose.Types.ObjectId(filters['from-id'])}}
|
|
|
|
|
]}
|
|
|
|
|
]}},
|
|
|
|
|
{$sort: {[sortKeys[0]]: -1, _id: -1}}
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
} else { // sort from beginning
|
|
|
|
|
return [{$sort: {[sortKeys[0]]: filters.sort[1], [sortKeys[1]]: filters.sort[1]}}]; // set _id as secondary sort
|
|
|
|
@ -775,29 +864,49 @@ function filterQueries (filters) {
|
|
|
|
|
return {[e.field]: {['$in']: [new RegExp(e.values[0])]}};
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return {[e.field]: {['$' + e.mode]: (e.mode.indexOf('in') >= 0 ? e.values : e.values[0])}}; // add filter criteria as {field: {$mode: value}}, only use first value when mode is not in/nin
|
|
|
|
|
// add filter criteria as {field: {$mode: value}}, only use first value when mode is not in/nin
|
|
|
|
|
return {[e.field]: {['$' + e.mode]: (e.mode.indexOf('in') >= 0 ? e.values : e.values[0])}};
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// add measurements as property [template.name], if one result, array is reduced to direct values
|
|
|
|
|
function addMeasurements(queryPtr, template) {
|
|
|
|
|
queryPtr.push(
|
|
|
|
|
{$addFields: {[template.name]: {$let: {vars: {
|
|
|
|
|
arr: {$filter: {
|
|
|
|
|
input: '$measurements', cond: {$eq: ['$$this.measurement_template', mongoose.Types.ObjectId(template._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;}, {})
|
|
|
|
|
]}}}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function dateToOId (date) { // convert date to ObjectId
|
|
|
|
|
return mongoose.Types.ObjectId(Math.floor(date / 1000).toString(16) + '0000000000000000');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function sampleReturn (sampleData, req, res, next) {
|
|
|
|
|
if (sampleData) {
|
|
|
|
|
console.log(sampleData);
|
|
|
|
|
await sampleData.populate('material_id.group_id').populate('material_id.supplier_id').execPopulate().catch(err => next(err));
|
|
|
|
|
await sampleData.populate('material_id.group_id').populate('material_id.supplier_id')
|
|
|
|
|
.execPopulate().catch(err => next(err));
|
|
|
|
|
if (sampleData instanceof Error) return;
|
|
|
|
|
sampleData = sampleData.toObject();
|
|
|
|
|
|
|
|
|
|
if (sampleData.status === globals.status.deleted && !req.auth(res, ['maintain', 'admin'], 'all')) return; // deleted samples only available for maintain/admin
|
|
|
|
|
// deleted samples only available for maintain/admin
|
|
|
|
|
if (sampleData.status === globals.status.deleted && !req.auth(res, ['maintain', 'admin'], 'all')) return;
|
|
|
|
|
sampleData.material = sampleData.material_id; // map data to right keys
|
|
|
|
|
sampleData.material.group = sampleData.material.group_id.name;
|
|
|
|
|
sampleData.material.supplier = sampleData.material.supplier_id.name;
|
|
|
|
|
sampleData.user = sampleData.user_id.name;
|
|
|
|
|
sampleData.notes = sampleData.note_id ? sampleData.note_id : {};
|
|
|
|
|
MeasurementModel.find({sample_id: sampleData._id, status: {$ne: globals.status.deleted}}).lean().exec((err, data) => {
|
|
|
|
|
MeasurementModel.find({sample_id: sampleData._id, status: {$ne: globals.status.deleted}})
|
|
|
|
|
.lean().exec((err, data) => {
|
|
|
|
|
sampleData.measurements = data;
|
|
|
|
|
res.json(SampleValidate.output(sampleData, 'details'));
|
|
|
|
|
});
|
|
|
|
|