Archived
2

added status filter

This commit is contained in:
VLE2FE 2020-06-15 12:49:32 +02:00
parent 5c04263475
commit c95af7bc0b
6 changed files with 69 additions and 2 deletions

View File

@ -5,6 +5,13 @@
x-doc: returns only samples with status 10 x-doc: returns only samples with status 10
tags: tags:
- /sample - /sample
parameters:
- name: status
description: 'values: validated|new|all, defaults to validated'
in: query
schema:
type: string
example: all
responses: responses:
200: 200:
description: samples overview description: samples overview
@ -14,6 +21,8 @@
type: array type: array
items: items:
$ref: 'api.yaml#/components/schemas/SampleRefs' $ref: 'api.yaml#/components/schemas/SampleRefs'
400:
$ref: 'api.yaml#/components/responses/400'
401: 401:
$ref: 'api.yaml#/components/responses/401' $ref: 'api.yaml#/components/responses/401'
500: 500:

View File

@ -89,6 +89,7 @@ function key (req, next): any { // checks API key and returns changed user obje
if (err) return next(err); if (err) return next(err);
if (data.length === 1) { // one user found if (data.length === 1) { // one user found
resolve({level: data[0].level, name: data[0].name, id: data[0]._id.toString(), location: data[0].location}); resolve({level: data[0].level, name: data[0].name, id: data[0]._id.toString(), location: data[0].location});
delete req.query.key; // delete query parameter to avoid interference with later validation
} }
else { else {
resolve(null); resolve(null);

View File

@ -48,7 +48,7 @@ app.use(require('./helpers/authorize')); // handle authentication
// redirect /api routes for Angular proxy in development // redirect /api routes for Angular proxy in development
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
app.use('/api/:url', (req, res) => { app.use('/api/:url([^]+)', (req, res) => {
req.url = '/' + req.params.url; req.url = '/' + req.params.url;
app.handle(req, res); app.handle(req, res);
}); });

View File

@ -71,6 +71,40 @@ describe('/sample', () => {
done(); done();
}); });
}); });
it('allows filtering by state', done => {
TestHelper.request(server, done, {
method: 'get',
url: '/samples?status=new',
auth: {basic: 'janedoe'},
httpStatus: 200
}).end((err, res) => {
if (err) return done(err);
const json = require('../test/db.json');
should(res.body).have.lengthOf(json.collections.samples.filter(e => e.status ===globals.status.new).length);
should(res.body).matchEach(sample => {
should(sample).have.only.keys('_id', 'number', 'type', 'color', 'batch', 'condition', 'material_id', 'note_id', 'user_id');
should(sample).have.property('_id').be.type('string');
should(sample).have.property('number').be.type('string');
should(sample).have.property('type').be.type('string');
should(sample).have.property('color').be.type('string');
should(sample).have.property('batch').be.type('string');
should(sample).have.property('condition').be.type('object');
should(sample).have.property('material_id').be.type('string');
should(sample).have.property('note_id');
should(sample).have.property('user_id').be.type('string');
});
done();
});
});
it('rejects an invalid state name', done => {
TestHelper.request(server, done, {
method: 'get',
url: '/samples?status=xxx',
auth: {basic: 'janedoe'},
httpStatus: 400,
res: {status: 'Invalid body format', details: '"status" must be one of [validated, new, all]'}
});
});
it('rejects unauthorized requests', done => { it('rejects unauthorized requests', done => {
TestHelper.request(server, done, { TestHelper.request(server, done, {
method: 'get', method: 'get',

View File

@ -22,7 +22,24 @@ const router = express.Router();
router.get('/samples', (req, res, next) => { router.get('/samples', (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.find({status: globals.status.validated}).lean().exec((err, data) => { const {error, value: filters} = SampleValidate.query(req.query);
if (error) return res400(error, res);
let conditions;
if (filters.hasOwnProperty('status')) {
if(filters.status === 'all') {
conditions = {$or: [{status: globals.status.validated}, {status: globals.status.new}]}
}
else {
conditions = {status: globals.status[filters.status]};
}
}
else { // default
conditions = {status: globals.status.validated};
}
SampleModel.find(conditions).lean().exec((err, data) => {
if (err) return next(err); if (err) return next(err);
res.json(_.compact(data.map(e => SampleValidate.output(e)))); // validate all and filter null values from validation errors res.json(_.compact(data.map(e => SampleValidate.output(e)))); // validate all and filter null values from validation errors
}) })

View File

@ -118,4 +118,10 @@ export default class SampleValidate {
const {value, error} = Joi.object(joiObject).validate(data, {stripUnknown: true}); const {value, error} = Joi.object(joiObject).validate(data, {stripUnknown: true});
return error !== undefined? null : value; return error !== undefined? null : value;
} }
static query (data) {
return Joi.object({
status: Joi.string().valid('validated', 'new', 'all')
}).validate(data);
}
} }