Archived
2

delete measurements with sample

This commit is contained in:
VLE2FE 2020-05-28 13:05:00 +02:00
parent 54168e4500
commit d58026426c
3 changed files with 40 additions and 15 deletions

View File

@ -69,7 +69,7 @@
put:
summary: change sample
description: 'Auth: basic, levels: write, maintain, dev, admin <br>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:

View File

@ -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',

View File

@ -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,6 +141,11 @@ 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);
// 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);
@ -155,6 +160,7 @@ router.delete('/sample/' + IdValidate.parameter(), (req, res, next) => {
}
});
});
});
});
router.post('/sample/new', async (req, res, next) => {