From d58026426c8e3b5cdcce5006065791b8b4e119e5 Mon Sep 17 00:00:00 2001 From: VLE2FE Date: Thu, 28 May 2020 13:05:00 +0200 Subject: [PATCH] delete measurements with sample --- api/sample.yaml | 2 +- src/routes/sample.spec.ts | 23 +++++++++++++++++++++-- src/routes/sample.ts | 30 ++++++++++++++++++------------ 3 files changed, 40 insertions(+), 15 deletions(-) diff --git a/api/sample.yaml b/api/sample.yaml index cea8de7..9f52f77 100644 --- a/api/sample.yaml +++ b/api/sample.yaml @@ -69,7 +69,7 @@ put: summary: change sample description: 'Auth: basic, levels: write, maintain, dev, admin
Only maintain and admin are allowed to edit samples created by another user' - x-doc: status is reset to 0 on any changes, deleted samples cannot be changed # TODO + x-doc: status is reset to 0 on any changes, deleted samples cannot be changed tags: - /sample security: diff --git a/src/routes/sample.spec.ts b/src/routes/sample.spec.ts index f0bbe88..1bbfd5d 100644 --- a/src/routes/sample.spec.ts +++ b/src/routes/sample.spec.ts @@ -2,13 +2,15 @@ import should from 'should/as-function'; import SampleModel from '../models/sample'; import NoteModel from '../models/note'; import NoteFieldModel from '../models/note_field'; +import MeasurementModel from '../models/measurement'; import TestHelper from "../test/helper"; import globals from '../globals'; +import mongoose from 'mongoose'; // TODO: generate output for ML in format DPT -> data, implement filtering, field selection +// TODO: generate csv // TODO: filter by not completely filled/no measurements // TODO: write script for data import -// TODO: delete everything (measurements, condition) with sample // TODO: allow adding sample numbers for existing samples // TODO: Do not allow validation or measurement entry without condition @@ -752,6 +754,24 @@ describe('/sample', () => { }); }); }); + it('deletes associated measurements', done => { + TestHelper.request(server, done, { + method: 'delete', + url: '/sample/400000000000000000000001', + auth: {basic: 'janedoe'}, + httpStatus: 200 + }).end((err, res) => { + if (err) return done(err); + should(res.body).be.eql({status: 'OK'}); + MeasurementModel.find({sample_id: mongoose.Types.ObjectId('400000000000000000000001')}).lean().exec((err, data: any) => { + if (err) return done(err); + should(data).matchEach(sample => { + should(sample).have.property('status', -1); + }); + done(); + }); + }); + }); it('rejects deleting samples of other users for write users', done => { TestHelper.request(server, done, { method: 'delete', @@ -768,7 +788,6 @@ describe('/sample', () => { httpStatus: 404 }); }); - it('rejects deleting a sample referenced by conditions'); // TODO after decision it('rejects requests from a read user', done => { TestHelper.request(server, done, { method: 'delete', diff --git a/src/routes/sample.ts b/src/routes/sample.ts index 3976231..9166bbb 100644 --- a/src/routes/sample.ts +++ b/src/routes/sample.ts @@ -10,7 +10,7 @@ import MaterialModel from '../models/material'; import NoteModel from '../models/note'; import NoteFieldModel from '../models/note_field'; import IdValidate from './validate/id'; -import mongoose from "mongoose"; +import mongoose from 'mongoose'; import ConditionTemplateModel from '../models/condition_template'; import ParametersValidate from './validate/parameters'; import globals from '../globals'; @@ -141,18 +141,24 @@ router.delete('/sample/' + IdValidate.parameter(), (req, res, next) => { await SampleModel.findByIdAndUpdate(req.params.id, {status:globals.status.deleted}).lean().exec(err => { // set sample status if (err) return next(err); - if (sampleData.note_id !== null) { // handle notes - NoteModel.findById(sampleData.note_id).lean().exec((err, data: any) => { // find notes to update note_fields - if (err) return next(err); - if (data.hasOwnProperty('custom_fields')) { // update note_fields - customFieldsChange(Object.keys(data.custom_fields), -1); - } + + // set status of associated measurements also to deleted + MeasurementModel.update({sample_id: mongoose.Types.ObjectId(req.params.id)}, {status: -1}).lean().exec(err => { + if (err) return next(err); + + if (sampleData.note_id !== null) { // handle notes + NoteModel.findById(sampleData.note_id).lean().exec((err, data: any) => { // find notes to update note_fields + if (err) return next(err); + if (data.hasOwnProperty('custom_fields')) { // update note_fields + customFieldsChange(Object.keys(data.custom_fields), -1); + } + res.json({status: 'OK'}); + }); + } + else { res.json({status: 'OK'}); - }); - } - else { - res.json({status: 'OK'}); - } + } + }); }); }); });