updated sample restore and validate
This commit is contained in:
parent
8d894fa27e
commit
3fbd6ccec3
@ -271,7 +271,7 @@ describe('/sample', () => {
|
||||
it('adds the specified measurements', done => {
|
||||
TestHelper.request(server, done, {
|
||||
method: 'get',
|
||||
url: '/samples?status[]=new&status[]=validated&fields[]=number&fields[]=measurements.kf',
|
||||
url: '/samples?status[]=new&status[]=validated&fields[]=number&fields[]=measurements.kf.weight%20%25&fields[]=measurements.kf.standard%20deviation',
|
||||
auth: {basic: 'janedoe'},
|
||||
httpStatus: 200
|
||||
}).end((err, res) => {
|
||||
@ -1525,22 +1525,23 @@ describe('/sample', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
it('creates a changelog', done => {
|
||||
it('restores associated measurements', done => {
|
||||
TestHelper.request(server, done, {
|
||||
method: 'put',
|
||||
url: '/sample/restore/400000000000000000000005',
|
||||
auth: {basic: 'admin'},
|
||||
httpStatus: 200,
|
||||
req: {},
|
||||
log: {
|
||||
collection: 'samples',
|
||||
dataAdd: {
|
||||
group_id: '900000000000000000000002',
|
||||
supplier_id: '110000000000000000000002',
|
||||
status: 'new'
|
||||
},
|
||||
dataIgn: ['group_id', 'supplier_id']
|
||||
}
|
||||
req: {}
|
||||
}).end((err, res) => {
|
||||
if (err) return done (err);
|
||||
should(res.body).be.eql({status: 'OK'});
|
||||
MeasurementModel.find({sample_id: mongoose.Types.ObjectId('400000000000000000000005')}).lean().exec((err, data: any) => {
|
||||
if (err) return done(err);
|
||||
should(data).matchEach(measurement => {
|
||||
should(measurement).have.property('status', 'new')
|
||||
});
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
it('rejects an API key', done => {
|
||||
@ -1598,22 +1599,23 @@ describe('/sample', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
it('creates a changelog', done => {
|
||||
it('validates associated measurements', done => {
|
||||
TestHelper.request(server, done, {
|
||||
method: 'put',
|
||||
url: '/sample/validate/400000000000000000000003',
|
||||
auth: {basic: 'admin'},
|
||||
httpStatus: 200,
|
||||
req: {},
|
||||
log: {
|
||||
collection: 'samples',
|
||||
dataAdd: {
|
||||
group_id: '900000000000000000000002',
|
||||
supplier_id: '110000000000000000000002',
|
||||
status: 'validated'
|
||||
},
|
||||
dataIgn: ['group_id', 'supplier_id']
|
||||
}
|
||||
req: {}
|
||||
}).end((err, res) => {
|
||||
if (err) return done (err);
|
||||
should(res.body).be.eql({status: 'OK'});
|
||||
MeasurementModel.find({sample_id: mongoose.Types.ObjectId('400000000000000000000003')}).lean().exec((err, data: any) => {
|
||||
if (err) return done(err);
|
||||
should(data).matchEach(measurement => {
|
||||
should(measurement).have.property('status', 'validated')
|
||||
});
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
it('allows validating a sample without condition', done => {
|
||||
|
@ -29,6 +29,7 @@ router.get('/samples', async (req, res, next) => {
|
||||
if (!req.auth(res, ['read', 'write', 'dev', 'admin'], 'all')) return;
|
||||
|
||||
const {error, value: filters} = SampleValidate.query(req.query, ['dev', 'admin'].indexOf(req.authDetails.level) >= 0);
|
||||
console.log(error);
|
||||
if (error) return res400(error, res);
|
||||
console.log(filters.filters);
|
||||
|
||||
@ -221,7 +222,6 @@ 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
|
||||
if (sortFilterKeys.find(e => /measurements\./.test(e))) { // add measurement fields
|
||||
console.log(measurementFilterFields);
|
||||
const measurementTemplates = await MeasurementTemplateModel.find({name: {$in: measurementFilterFields}})
|
||||
.lean().exec().catch(err => {next(err);});
|
||||
if (measurementTemplates instanceof Error) return;
|
||||
@ -233,12 +233,7 @@ router.get('/samples', async (req, res, next) => {
|
||||
pipeline: [{$match: {$expr: {$and: [
|
||||
{$eq: ['$sample_id', '$$sId']},
|
||||
{$in: ['$measurement_template', measurementTemplates.map(e => mongoose.Types.ObjectId(e._id))]}
|
||||
]}}},
|
||||
{$project: _.merge(
|
||||
filters.fields.filter(e => /measurements\./.test(e))
|
||||
.map(e => 'values.' + e.split('.')[2]).reduce((s, e) => {s[e] = true; return s; }, {}),
|
||||
{measurement_template: true, status: true, sample_id: true}
|
||||
)}
|
||||
]}}}
|
||||
],
|
||||
as: 'measurements'
|
||||
}});
|
||||
@ -367,6 +362,7 @@ router.get('/samples', async (req, res, next) => {
|
||||
projection._id = false;
|
||||
}
|
||||
queryPtr.push({$project: projection});
|
||||
console.log(JSON.stringify(query));
|
||||
// use streaming when including spectrum files
|
||||
if (!fieldsToAdd.find(e => e.indexOf(globals.spectrum.spectrum + '.' + globals.spectrum.dpt) >= 0)) {
|
||||
collection.aggregate(query).allowDiskUse(true).exec((err, data) => {
|
||||
@ -904,6 +900,11 @@ function setStatus (status, req, res, next) {
|
||||
if (!data) {
|
||||
return res.status(404).json({status: 'Not found'});
|
||||
}
|
||||
MeasurementModel.updateMany({sample_id: mongoose.Types.ObjectId(req.params.id)}, {status})
|
||||
.log(req).lean().exec(err => {
|
||||
if (err) return next(err);
|
||||
|
||||
res.json({status: 'OK'});
|
||||
});
|
||||
});
|
||||
}
|
@ -11,6 +11,7 @@ export default class MeasurementValidate {
|
||||
Joi.string().max(128),
|
||||
Joi.number(),
|
||||
Joi.boolean(),
|
||||
Joi.array().items(Joi.array().items(Joi.number())), // for spectra
|
||||
Joi.array()
|
||||
)
|
||||
.allow(null)
|
||||
|
@ -192,9 +192,11 @@ export default class SampleValidate {
|
||||
if (filterValidation.error) return filterValidation;
|
||||
try {
|
||||
for (let i in data.filters) {
|
||||
// data.filters[i] = JSON.parse(decodeURIComponent(data.filters[i]));
|
||||
data.filters[i] = JSON.parse(decodeURIComponent(data.filters[i]));
|
||||
console.log(data.filters[i]);
|
||||
try {
|
||||
data.filters[i] = decodeURIComponent(data.filters[i]);
|
||||
}
|
||||
catch (ignore) {}
|
||||
data.filters[i] = JSON.parse(data.filters[i]);
|
||||
data.filters[i].values = data.filters[i].values.map(e => { // validate filter values
|
||||
if (e === null) { // null values are always allowed
|
||||
return null;
|
||||
@ -225,6 +227,7 @@ export default class SampleValidate {
|
||||
validator = Joi.object(this.sample);
|
||||
}
|
||||
const {value, error} = validator.validate({[field]: e});
|
||||
console.log(error);
|
||||
if (error) throw error; // reject invalid values
|
||||
return value[field];
|
||||
});
|
||||
|
@ -477,6 +477,21 @@
|
||||
"status": "validated",
|
||||
"measurement_template": {"$oid":"300000000000000000000001"},
|
||||
"__v": 0
|
||||
},
|
||||
{
|
||||
"_id": {"$oid":"800000000000000000000008"},
|
||||
"sample_id": {"$oid":"400000000000000000000005"},
|
||||
"values": {
|
||||
"dpt": [
|
||||
[3996.12558,98.00555],
|
||||
[3995.08519,98.03253],
|
||||
[3993.04480,98.02657]
|
||||
],
|
||||
"device": "Alpha II"
|
||||
},
|
||||
"status": "deleted",
|
||||
"measurement_template": {"$oid":"300000000000000000000001"},
|
||||
"__v": 0
|
||||
}
|
||||
],
|
||||
"condition_templates": [
|
||||
|
Reference in New Issue
Block a user