made GET /sample/{id} work with new material model
This commit is contained in:
parent
48b1a9da6e
commit
e342224b7f
@ -39,12 +39,18 @@ router.get('/samples/:state(new|deleted)', (req, res, next) => {
|
|||||||
router.get('/sample/' + IdValidate.parameter(), (req, res, next) => {
|
router.get('/sample/' + IdValidate.parameter(), (req, res, next) => {
|
||||||
if (!req.auth(res, ['read', 'write', 'maintain', 'dev', 'admin'], 'all')) return;
|
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').lean().exec((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);
|
if (err) return next(err);
|
||||||
|
|
||||||
if (sampleData) {
|
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
|
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 = 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.user = sampleData.user_id.name;
|
||||||
sampleData.notes = sampleData.note_id ? sampleData.note_id : {};
|
sampleData.notes = sampleData.note_id ? sampleData.note_id : {};
|
||||||
MeasurementModel.find({sample_id: mongoose.Types.ObjectId(req.params.id)}).lean().exec((err, data) => {
|
MeasurementModel.find({sample_id: mongoose.Types.ObjectId(req.params.id)}).lean().exec((err, data) => {
|
||||||
@ -143,7 +149,7 @@ router.delete('/sample/' + IdValidate.parameter(), (req, res, next) => {
|
|||||||
if (err) return next(err);
|
if (err) return next(err);
|
||||||
|
|
||||||
// set status of associated measurements also to deleted
|
// set status of associated measurements also to deleted
|
||||||
MeasurementModel.update({sample_id: mongoose.Types.ObjectId(req.params.id)}, {status: -1}).lean().exec(err => {
|
MeasurementModel.updateMany({sample_id: mongoose.Types.ObjectId(req.params.id)}, {status: -1}).lean().exec(err => {
|
||||||
if (err) return next(err);
|
if (err) return next(err);
|
||||||
|
|
||||||
if (sampleData.note_id !== null) { // handle notes
|
if (sampleData.note_id !== null) { // handle notes
|
||||||
|
Reference in New Issue
Block a user