added GET /sample/number/{number} route
This commit is contained in:
@ -242,6 +242,13 @@ router.get('/samples', async (req, res, next) => {
|
||||
&& e !== filters.sort[0] // field was not in sort
|
||||
);
|
||||
|
||||
if (fieldsToAdd.find(e => e === 'notes')) { // add notes
|
||||
queryPtr.push(
|
||||
{$lookup: {from: 'notes', localField: 'note_id', foreignField: '_id', as: 'notes'}},
|
||||
{$addFields: {notes: { $arrayElemAt: ['$notes', 0]}}}
|
||||
);
|
||||
}
|
||||
|
||||
if (fieldsToAdd.find(e => /material\./.test(e)) && !materialAdded) { // add material, was not added already
|
||||
queryPtr.push(
|
||||
{$lookup: {from: 'materials', localField: 'material_id', foreignField: '_id', as: 'material'}},
|
||||
@ -387,26 +394,7 @@ router.get('/sample/' + IdValidate.parameter(), (req, res, next) => {
|
||||
|
||||
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);
|
||||
|
||||
if (sampleData) {
|
||||
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
|
||||
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: mongoose.Types.ObjectId(req.params.id), status: {$ne: globals.status.deleted}}).lean().exec((err, data) => {
|
||||
sampleData.measurements = data;
|
||||
res.json(SampleValidate.output(sampleData, 'details'));
|
||||
});
|
||||
}
|
||||
else {
|
||||
res.status(404).json({status: 'Not found'});
|
||||
}
|
||||
await sampleReturn(sampleData, req, res, next);
|
||||
});
|
||||
});
|
||||
|
||||
@ -514,6 +502,15 @@ 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) => {
|
||||
if (err) return next(err);
|
||||
await sampleReturn(sampleData, req, res, next);
|
||||
});
|
||||
});
|
||||
|
||||
router.put('/sample/restore/' + IdValidate.parameter(), (req, res, next) => {
|
||||
if (!req.auth(res, ['maintain', 'admin'], 'basic')) return;
|
||||
|
||||
@ -769,4 +766,27 @@ function filterQueries (filters) {
|
||||
|
||||
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));
|
||||
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
|
||||
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) => {
|
||||
sampleData.measurements = data;
|
||||
res.json(SampleValidate.output(sampleData, 'details'));
|
||||
});
|
||||
}
|
||||
else {
|
||||
res.status(404).json({status: 'Not found'});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user