From 4ce65ad7cc8dbf1f0ea87ae305332b216832ad46 Mon Sep 17 00:00:00 2001 From: VLE2FE Date: Fri, 7 Aug 2020 09:37:02 +0200 Subject: [PATCH] added deleted status for /samples --- api/sample.yaml | 8 ++-- src/db.ts | 6 +-- src/helpers/mail.ts | 80 +++++++++++++++++---------------- src/routes/sample.spec.ts | 84 +++++++++++++++++++++-------------- src/routes/sample.ts | 15 +------ src/routes/validate/sample.ts | 9 ++-- src/routes/validate/user.ts | 1 - 7 files changed, 107 insertions(+), 96 deletions(-) diff --git a/api/sample.yaml b/api/sample.yaml index 52b5705..193e9de 100644 --- a/api/sample.yaml +++ b/api/sample.yaml @@ -8,11 +8,13 @@ - /sample parameters: - name: status - description: 'values: validated|new|all, defaults to validated' + description: 'values: validated|new, for dev/admin also deleted, defaults to validated' in: query schema: - type: string - example: all + type: array + items: + type: string + example: ['validated'] - name: from-id description: first id of the requested page, if not given the results are displayed from start in: query diff --git a/src/db.ts b/src/db.ts index 6873ade..933cdc1 100644 --- a/src/db.ts +++ b/src/db.ts @@ -51,7 +51,7 @@ export default class db { connectTimeoutMS: 10000 }, err => { if (err) done(err); - }).then(() => {}); + }); mongoose.connection.on('error', console.error.bind(console, 'connection error:')); mongoose.connection.on('connected', () => { // evaluation connection behaviour on prod if (process.env.NODE_ENV !== 'test') { // Do not interfere with testing @@ -69,7 +69,7 @@ export default class db { mongoose.connection.close(() => { console.info('Mongoose default connection disconnected through app termination'); process.exit(0); - }).then(() => {}); + }); } }); mongoose.connection.once('open', () => { @@ -85,7 +85,7 @@ export default class db { console.info(process.env.NODE_ENV === 'test' ? '' : `Disconnected from database`); this.state.db = 0; done(); - }).then(() => {}); + }); } static getState () { diff --git a/src/helpers/mail.ts b/src/helpers/mail.ts index 7f7e64d..a76b287 100644 --- a/src/helpers/mail.ts +++ b/src/helpers/mail.ts @@ -10,50 +10,52 @@ export default class Mail{ static mailPass: string; static init() { - this.mailPass = Array(64).fill(0).map(() => Math.floor(Math.random() * 10)).join(''); - this.uri = JSON.parse(process.env.VCAP_SERVICES).Mail[0].credentials.uri; - this.auth.username = JSON.parse(process.env.VCAP_SERVICES).Mail[0].credentials.username; - this.auth.password = JSON.parse(process.env.VCAP_SERVICES).Mail[0].credentials.password; - axios({ // get registered mail addresses - method: 'get', - url: this.uri + '/management/userDomainMapping', - auth: this.auth - }).then(res => { - return new Promise(async (resolve, reject) => { - try { - if (res.data.addresses.indexOf(this.address) < 0) { // mail address not registered - if (res.data.addresses.length) { // delete wrong registered mail address - await axios({ - method: 'delete', - url: this.uri + '/management/mailAddresses/' + res.data.addresses[0], + if (process.env.NODE_ENV === 'production') { // only send mails in production + this.mailPass = Array(64).fill(0).map(() => Math.floor(Math.random() * 10)).join(''); + this.uri = JSON.parse(process.env.VCAP_SERVICES).Mail[0].credentials.uri; + this.auth.username = JSON.parse(process.env.VCAP_SERVICES).Mail[0].credentials.username; + this.auth.password = JSON.parse(process.env.VCAP_SERVICES).Mail[0].credentials.password; + axios({ // get registered mail addresses + method: 'get', + url: this.uri + '/management/userDomainMapping', + auth: this.auth + }).then(res => { + return new Promise(async (resolve, reject) => { + try { + if (res.data.addresses.indexOf(this.address) < 0) { // mail address not registered + if (res.data.addresses.length) { // delete wrong registered mail address + await axios({ + method: 'delete', + url: this.uri + '/management/mailAddresses/' + res.data.addresses[0], + auth: this.auth + }); + } + await axios({ // register right mail address + method: 'post', + url: this.uri + '/management/mailAddresses/' + this.address, auth: this.auth }); } - await axios({ // register right mail address - method: 'post', - url: this.uri + '/management/mailAddresses/' + this.address, - auth: this.auth - }); + resolve(); } - resolve(); - } - catch (e) { - reject(e); - } + catch (e) { + reject(e); + } + }); + }).then(() => { + return axios({ // set new mail password + method: 'put', + url: this.uri + '/management/mailAddresses/' + this.address + '/password/' + this.mailPass, + auth: this.auth + }); + }).then(() => { // init done successfully + console.info('Mail service established successfully'); + this.send('lukas.veit@bosch.com', 'Mail Service started', new Date().toString()); + }).catch(err => { // anywhere an error occurred + console.error(`Mail init error: ${err.request.method} ${err.request.path}: ${err.response.status}`, + err.response.data); }); - }).then(() => { - return axios({ // set new mail password - method: 'put', - url: this.uri + '/management/mailAddresses/' + this.address + '/password/' + this.mailPass, - auth: this.auth - }); - }).then(() => { // init done successfully - console.info('Mail service established successfully'); - this.send('lukas.veit@bosch.com', 'Mail Service started', new Date().toString()); - }).catch(err => { // anywhere an error occurred - console.error(`Mail init error: ${err.request.method} ${err.request.path}: ${err.response.status}`, - err.response.data); - }); + } } static send (mailAddress, subject, content, f: (x?) => void = () => {}) { // callback, executed empty or with error diff --git a/src/routes/sample.spec.ts b/src/routes/sample.spec.ts index c32146a..bd798a4 100644 --- a/src/routes/sample.spec.ts +++ b/src/routes/sample.spec.ts @@ -48,6 +48,22 @@ describe('/sample', () => { done(); }); }); + it('returns deleted samples for admin', done => { + TestHelper.request(server, done, { + method: 'get', + url: '/samples?status[]=deleted&fields[]=number&fields=status', + auth: {basic: 'admin'}, + 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 ==='deleted').length); + should(res.body).matchEach(sample => { + should(sample).have.property('status', 'deleted').be.type('string'); + }); + done(); + }); + }); it('works with an API key', done => { TestHelper.request(server, done, { method: 'get', @@ -78,7 +94,7 @@ describe('/sample', () => { it('allows filtering by state', done => { TestHelper.request(server, done, { method: 'get', - url: '/samples?status=new', + url: '/samples?status[]=new', auth: {basic: 'janedoe'}, httpStatus: 200 }).end((err, res) => { @@ -104,7 +120,7 @@ describe('/sample', () => { it('uses the given page size', done => { TestHelper.request(server, done, { method: 'get', - url: '/samples?status=all&page-size=3', + url: '/samples?status[]=new&status[]=validated&page-size=3', auth: {basic: 'janedoe'}, httpStatus: 200 }).end((err, res) => { @@ -116,7 +132,7 @@ describe('/sample', () => { it('returns results starting from first-id', done => { TestHelper.request(server, done, { method: 'get', - url: '/samples?status=all&from-id=400000000000000000000002', + url: '/samples?status[]=new&status[]=validated&from-id=400000000000000000000002', auth: {basic: 'janedoe'}, httpStatus: 200 }).end((err, res) => { @@ -129,7 +145,7 @@ describe('/sample', () => { it('returns the right page number', done => { TestHelper.request(server, done, { method: 'get', - url: '/samples?status=all&to-page=2&page-size=2', + url: '/samples?status[]=new&status[]=validated&to-page=2&page-size=2', auth: {basic: 'janedoe'}, httpStatus: 200 }).end((err, res) => { @@ -141,7 +157,7 @@ describe('/sample', () => { it('works with negative page numbers', done => { TestHelper.request(server, done, { method: 'get', - url: '/samples?status=all&to-page=-1&page-size=2&from-id=400000000000000000000004', + url: '/samples?status[]=new&status[]=validated&to-page=-1&page-size=2&from-id=400000000000000000000004', auth: {basic: 'janedoe'}, httpStatus: 200 }).end((err, res) => { @@ -154,7 +170,7 @@ describe('/sample', () => { it('returns an empty array for a page number out of range', done => { TestHelper.request(server, done, { method: 'get', - url: '/samples?status=all&to-page=100&page-size=2', + url: '/samples?status[]=new&status[]=validated&to-page=100&page-size=2', auth: {basic: 'janedoe'}, httpStatus: 200 }).end((err, res) => { @@ -167,7 +183,7 @@ describe('/sample', () => { it('returns an empty array for a page number out of negative range', done => { TestHelper.request(server, done, { method: 'get', - url: '/samples?status=all&to-page=-100&page-size=3&from-id=400000000000000000000004', + url: '/samples?status[]=new&status[]=validated&to-page=-100&page-size=3&from-id=400000000000000000000004', auth: {basic: 'janedoe'}, httpStatus: 200 }).end((err, res) => { @@ -180,7 +196,7 @@ describe('/sample', () => { it('sorts the samples ascending', done => { TestHelper.request(server, done, { method: 'get', - url: '/samples?status=all&sort=color-asc', + url: '/samples?status[]=new&status[]=validated&sort=color-asc', auth: {basic: 'janedoe'}, httpStatus: 200 }).end((err, res) => { @@ -193,7 +209,7 @@ describe('/sample', () => { it('sorts the samples descending', done => { TestHelper.request(server, done, { method: 'get', - url: '/samples?status=all&sort=number-desc', + url: '/samples?status[]=new&status[]=validated&sort=number-desc', auth: {basic: 'janedoe'}, httpStatus: 200 }).end((err, res) => { @@ -207,7 +223,7 @@ describe('/sample', () => { it('sorts the samples correctly in combination with paging', done => { TestHelper.request(server, done, { method: 'get', - url: '/samples?status=all&sort=color-asc&page-size=2&from-id=400000000000000000000006', + url: '/samples?status[]=new&status[]=validated&sort=color-asc&page-size=2&from-id=400000000000000000000006', auth: {basic: 'janedoe'}, httpStatus: 200 }).end((err, res) => { @@ -220,7 +236,7 @@ describe('/sample', () => { it('sorts the samples correctly in combination with going pages backward', done => { TestHelper.request(server, done, { method: 'get', - url: '/samples?status=all&sort=color-desc&page-size=2&from-id=400000000000000000000004&to-page=-1', + url: '/samples?status[]=new&status[]=validated&sort=color-desc&page-size=2&from-id=400000000000000000000004&to-page=-1', auth: {basic: 'janedoe'}, httpStatus: 200 }).end((err, res) => { @@ -233,7 +249,7 @@ describe('/sample', () => { it('sorts the samples correctly for material keys', done => { TestHelper.request(server, done, { method: 'get', - url: '/samples?status=all&sort=material.name-desc', + url: '/samples?status[]=new&status[]=validated&sort=material.name-desc', auth: {basic: 'janedoe'}, httpStatus: 200 }).end((err, res) => { @@ -247,7 +263,7 @@ describe('/sample', () => { it('adds the status if specified', done => { TestHelper.request(server, done, { method: 'get', - url: '/samples?status=all&fields[]=number&fields[]=status', + url: '/samples?status[]=new&status[]=validated&fields[]=number&fields[]=status', auth: {basic: 'janedoe'}, httpStatus: 200 }).end((err, res) => { @@ -260,7 +276,7 @@ describe('/sample', () => { it('adds the specified measurements', done => { TestHelper.request(server, done, { method: 'get', - url: '/samples?status=all&fields[]=number&fields[]=measurements.kf', + url: '/samples?status[]=new&status[]=validated&fields[]=number&fields[]=measurements.kf', auth: {basic: 'janedoe'}, httpStatus: 200 }).end((err, res) => { @@ -273,7 +289,7 @@ describe('/sample', () => { it('multiplies the sample information for each spectrum', done => { TestHelper.request(server, done, { method: 'get', - url: '/samples?status=all&fields[]=number&fields[]=measurements.spectrum.dpt', + url: '/samples?status[]=new&status[]=validated&fields[]=number&fields[]=measurements.spectrum.dpt', auth: {basic: 'admin'}, httpStatus: 200 }).end((err, res) => { @@ -287,7 +303,7 @@ describe('/sample', () => { it('filters a sample property', done => { // TODO: implement filters TestHelper.request(server, done, { method: 'get', - url: '/samples?status=all&fields[]=number&fields[]=type&filters[]=%7B%22mode%22%3A%22eq%22%2C%22field%22%3A%22type%22%2C%22values%22%3A%5B%22part%22%5D%7D', + url: '/samples?status[]=new&status[]=validated&fields[]=number&fields[]=type&filters[]=%7B%22mode%22%3A%22eq%22%2C%22field%22%3A%22type%22%2C%22values%22%3A%5B%22part%22%5D%7D', auth: {basic: 'janedoe'}, httpStatus: 200 }).end((err, res) => { @@ -303,7 +319,7 @@ describe('/sample', () => { it('filters a material property', done => { TestHelper.request(server, done, { method: 'get', - url: '/samples?status=all&fields[]=number&fields[]=material.name&filters[]=%7B%22mode%22%3A%22in%22%2C%22field%22%3A%22material.name%22%2C%22values%22%3A%5B%22Schulamid%2066%20GF%2025%20H%22%2C%22Stanyl%20TW%20200%20F8%22%5D%7D', + url: '/samples?status[]=new&status[]=validated&fields[]=number&fields[]=material.name&filters[]=%7B%22mode%22%3A%22in%22%2C%22field%22%3A%22material.name%22%2C%22values%22%3A%5B%22Schulamid%2066%20GF%2025%20H%22%2C%22Stanyl%20TW%20200%20F8%22%5D%7D', auth: {basic: 'janedoe'}, httpStatus: 200 }).end((err, res) => { @@ -319,7 +335,7 @@ describe('/sample', () => { it('filters by measurement value', done => { TestHelper.request(server, done, { method: 'get', - url: '/samples?status=all&fields[]=number&fields[]=material.name&fields[]=measurements.kf.weight%20%25&filters[]=%7B%22mode%22%3A%22gt%22%2C%22field%22%3A%22measurements.kf.weight%20%25%22%2C%22values%22%3A%5B0.5%5D%7D', + url: '/samples?status[]=new&status[]=validated&fields[]=number&fields[]=material.name&fields[]=measurements.kf.weight%20%25&filters[]=%7B%22mode%22%3A%22gt%22%2C%22field%22%3A%22measurements.kf.weight%20%25%22%2C%22values%22%3A%5B0.5%5D%7D', auth: {basic: 'janedoe'}, httpStatus: 200 }).end((err, res) => { @@ -335,7 +351,7 @@ describe('/sample', () => { it('filters by measurement value not in the fields', done => { TestHelper.request(server, done, { method: 'get', - url: '/samples?status=all&fields[]=number&fields[]=material.name&filters[]=%7B%22mode%22%3A%22gt%22%2C%22field%22%3A%22measurements.kf.weight%20%25%22%2C%22values%22%3A%5B0.5%5D%7D', + url: '/samples?status[]=new&status[]=validated&fields[]=number&fields[]=material.name&filters[]=%7B%22mode%22%3A%22gt%22%2C%22field%22%3A%22measurements.kf.weight%20%25%22%2C%22values%22%3A%5B0.5%5D%7D', auth: {basic: 'janedoe'}, httpStatus: 200 }).end((err, res) => { @@ -349,7 +365,7 @@ describe('/sample', () => { it('filters by a measurement properties property', done => { TestHelper.request(server, done, { method: 'get', - url: '/samples?status=all&fields[]=number&fields[]=material.name&fields[]=material.properties.glass_fiber&filters[]=%7B%22mode%22%3A%22eq%22%2C%22field%22%3A%22material.properties.glass_fiber%22%2C%22values%22%3A%5B%2225%22%5D%7D', + url: '/samples?status[]=new&status[]=validated&fields[]=number&fields[]=material.name&fields[]=material.properties.glass_fiber&filters[]=%7B%22mode%22%3A%22eq%22%2C%22field%22%3A%22material.properties.glass_fiber%22%2C%22values%22%3A%5B%2225%22%5D%7D', auth: {basic: 'janedoe'}, httpStatus: 200 }).end((err, res) => { @@ -364,7 +380,7 @@ describe('/sample', () => { it('filters and sorts by a measurement properties property', done => { TestHelper.request(server, done, { method: 'get', - url: '/samples?status=all&sort=material.properties.glass_fiber-desc&fields[]=number&fields[]=material.name&fields[]=material.properties.glass_fiber&filters[]=%7B%22mode%22%3A%22eq%22%2C%22field%22%3A%22material.properties.glass_fiber%22%2C%22values%22%3A%5B%2225%22%5D%7D', + url: '/samples?status[]=new&status[]=validated&sort=material.properties.glass_fiber-desc&fields[]=number&fields[]=material.name&fields[]=material.properties.glass_fiber&filters[]=%7B%22mode%22%3A%22eq%22%2C%22field%22%3A%22material.properties.glass_fiber%22%2C%22values%22%3A%5B%2225%22%5D%7D', auth: {basic: 'janedoe'}, httpStatus: 200 }).end((err, res) => { @@ -381,7 +397,7 @@ describe('/sample', () => { it('filters multiple properties', done => { TestHelper.request(server, done, { method: 'get', - url: '/samples?status=all&fields[]=number&fields[]=batch&filters[]=%7B%22mode%22%3A%22lte%22%2C%22field%22%3A%22number%22%2C%22values%22%3A%5B%22Rng33%22%5D%7D&filters[]=%7B%22mode%22%3A%22nin%22%2C%22field%22%3A%22batch%22%2C%22values%22%3A%5B%221704-005%22%5D%7D', + url: '/samples?status[]=new&status[]=validated&fields[]=number&fields[]=batch&filters[]=%7B%22mode%22%3A%22lte%22%2C%22field%22%3A%22number%22%2C%22values%22%3A%5B%22Rng33%22%5D%7D&filters[]=%7B%22mode%22%3A%22nin%22%2C%22field%22%3A%22batch%22%2C%22values%22%3A%5B%221704-005%22%5D%7D', auth: {basic: 'janedoe'}, httpStatus: 200 }).end((err, res) => { @@ -394,7 +410,7 @@ describe('/sample', () => { it('rejects returning spectral data for a write user', done => { TestHelper.request(server, done, { method: 'get', - url: '/samples?status=all&fields[]=number&fields[]=measurements.spectrum.dpt', + url: '/samples?status[]=new&status[]=validated&fields[]=number&fields[]=measurements.spectrum.dpt', auth: {basic: 'janedoe'}, httpStatus: 403 }); @@ -402,7 +418,7 @@ describe('/sample', () => { it('rejects an invalid JSON string as a filters parameter', done => { TestHelper.request(server, done, { method: 'get', - url: '/samples?status=all&fields[]=number&fields[]=material.glass_fiber&fields[]=batch&filters[]=xx', + url: '/samples?status[]=new&status[]=validated&fields[]=number&fields[]=material.glass_fiber&fields[]=batch&filters[]=xx', auth: {basic: 'janedoe'}, httpStatus: 400, res: {status: 'Invalid body format', details: 'Invalid JSON string for filter parameter'} @@ -411,7 +427,7 @@ describe('/sample', () => { it('rejects an invalid filter mode', done => { TestHelper.request(server, done, { method: 'get', - url: '/samples?status=all&fields[]=number&fields[]=batch&filters[]=%7B%22mode%22%3A%22xx%22%2C%22field%22%3A%22batch%22%2C%22values%22%3A%5B%221704-005%22%5D%7D', + url: '/samples?status[]=new&status[]=validated&fields[]=number&fields[]=batch&filters[]=%7B%22mode%22%3A%22xx%22%2C%22field%22%3A%22batch%22%2C%22values%22%3A%5B%221704-005%22%5D%7D', auth: {basic: 'janedoe'}, httpStatus: 400, res: {status: 'Invalid body format', details: '"filters[0].mode" must be one of [eq, ne, lt, lte, gt, gte, in, nin, stringin]'} @@ -420,7 +436,7 @@ describe('/sample', () => { it('rejects an filter field not existing', done => { TestHelper.request(server, done, { method: 'get', - url: '/samples?status=all&fields[]=number&fields[]=material.glass_fiber&fields[]=batch&filters[]=%7B%22mode%22%3A%22eq%22%2C%22field%22%3A%22xx%22%2C%22values%22%3A%5B%221704-005%22%5D%7D', + url: '/samples?status[]=new&status[]=validated&fields[]=number&fields[]=material.glass_fiber&fields[]=batch&filters[]=%7B%22mode%22%3A%22eq%22%2C%22field%22%3A%22xx%22%2C%22values%22%3A%5B%221704-005%22%5D%7D', auth: {basic: 'janedoe'}, httpStatus: 400, res: {status: 'Invalid body format', details: 'Invalid JSON string for filter parameter'} @@ -429,7 +445,7 @@ describe('/sample', () => { it('rejects unknown measurement names', done => { TestHelper.request(server, done, { method: 'get', - url: '/samples?status=all&fields[]=number&fields[]=measurements.xx', + url: '/samples?status[]=new&status[]=validated&fields[]=number&fields[]=measurements.xx', auth: {basic: 'janedoe'}, httpStatus: 400, res: {status: 'Invalid body format', details: 'Measurement key not found'} @@ -438,7 +454,7 @@ describe('/sample', () => { it('returns a correct csv file if specified', done => { TestHelper.request(server, done, { method: 'get', - url: '/samples?status=all&page-size=2&csv=true', + url: '/samples?status[]=new&status[]=validated&page-size=2&csv=true', contentType: /text\/csv/, auth: {basic: 'janedoe'}, httpStatus: 200 @@ -453,7 +469,7 @@ describe('/sample', () => { it('returns only the fields specified', done => { TestHelper.request(server, done, { method: 'get', - url: '/samples?status=all&page-size=1&fields[]=number&fields[]=condition&fields[]=color&fields[]=material.name&fields[]=material.supplier', + url: '/samples?status[]=new&status[]=validated&page-size=1&fields[]=number&fields[]=condition&fields[]=color&fields[]=material.name&fields[]=material.supplier', auth: {basic: 'janedoe'}, httpStatus: 200, res: [{number: '1', condition: {material: 'copper', weeks: 3, condition_template: '200000000000000000000001'}, color: 'black', material: {name: 'Schulamid 66 GF 25 H', supplier: 'Schulmann'}}] @@ -462,7 +478,7 @@ describe('/sample', () => { it('returns specified material properties fields', done => { TestHelper.request(server, done, { method: 'get', - url: '/samples?status=all&fields[]=number&fields[]=material.properties.glass_fiber&fields[]=material.name', + url: '/samples?status[]=new&status[]=validated&fields[]=number&fields[]=material.properties.glass_fiber&fields[]=material.name', auth: {basic: 'janedoe'}, httpStatus: 200 }).end((err, res) => { @@ -490,7 +506,7 @@ describe('/sample', () => { it('rejects an invalid fields parameter', done => { TestHelper.request(server, done, { method: 'get', - url: '/samples?status=all&page-size=1&fields=number', + url: '/samples?status[]=new&status[]=validated&page-size=1&fields=number', auth: {basic: 'janedoe'}, httpStatus: 400, res: {status: 'Invalid body format', details: '"fields" must be an array'} @@ -499,7 +515,7 @@ describe('/sample', () => { it('rejects an unknown field name', done => { TestHelper.request(server, done, { method: 'get', - url: '/samples?status=all&page-size=1&fields[]=xx', + url: '/samples?status[]=new&status[]=validated&page-size=1&fields[]=xx', auth: {basic: 'janedoe'}, httpStatus: 400, res: {status: 'Invalid body format', details: 'Invalid field name'} @@ -535,10 +551,10 @@ describe('/sample', () => { it('rejects an invalid state name', done => { TestHelper.request(server, done, { method: 'get', - url: '/samples?status=xxx', + url: '/samples?status[]=xxx', auth: {basic: 'janedoe'}, httpStatus: 400, - res: {status: 'Invalid body format', details: '"status" must be one of [validated, new, all]'} + res: {status: 'Invalid body format', details: '"status[0]" must be one of [validated, new]'} }); }); it('rejects unauthorized requests', done => { diff --git a/src/routes/sample.ts b/src/routes/sample.ts index 3bbfe9c..922b576 100644 --- a/src/routes/sample.ts +++ b/src/routes/sample.ts @@ -31,7 +31,7 @@ const router = express.Router(); 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); + const {error, value: filters} = SampleValidate.query(req.query, ['dev', 'admin'].indexOf(req.authDetails.level) >= 0); if (error) return res400(error, res); // spectral data not allowed for read/write users @@ -460,7 +460,6 @@ router.put('/sample/' + IdValidate.parameter(), (req, res, next) => { if (!req.auth(res, ['write', 'dev', 'admin'], 'basic')) return; const {error, value: sample} = SampleValidate.input(req.body, 'change'); - console.log(error); if (error) return res400(error, res); SampleModel.findById(req.params.id).lean().exec(async (err, sampleData: any) => { // check if id exists @@ -824,17 +823,7 @@ function sortQuery(filters, sortKeys, sortStartValue) { // sortKeys = ['primary } function statusQuery(filters, field) { - if (filters.hasOwnProperty('status')) { - if(filters.status === 'all') { - return {$or: [{[field]: 'validated'}, {[field]: 'new'}]}; - } - else { - return {[field]: filters.status}; - } - } - else { // default - return {[field]: 'validated'}; - } + return {$or: filters.status.map(e => ({[field]: e}))}; } function addFilterQueries (queryPtr, filters) { // returns array of match queries from given filters diff --git a/src/routes/validate/sample.ts b/src/routes/validate/sample.ts index e831fc1..96cbc9c 100644 --- a/src/routes/validate/sample.ts +++ b/src/routes/validate/sample.ts @@ -168,11 +168,10 @@ export default class SampleValidate { joiObject[param] = Joi.any(); }); const {value, error} = Joi.object(joiObject).validate(data, {stripUnknown: true}); - console.log(error); return error !== undefined? null : value; } - static query (data) { + static query (data, dev = false) { if (data.filters && data.filters.length) { const filterValidation = Joi.array().items(Joi.string()).validate(data.filters); if (filterValidation.error) return filterValidation; @@ -216,8 +215,12 @@ export default class SampleValidate { return {error: {details: [{message: 'Invalid JSON string for filter parameter'}]}, value: null} } } + const acceptedStatuses = ['validated', 'new']; + if (dev) { // dev and admin can also access deleted samples + acceptedStatuses.push('deleted') + } return Joi.object({ - status: Joi.string().valid('validated', 'new', 'all'), + status: Joi.array().items(Joi.string().valid(...acceptedStatuses)).default(['validated']), 'from-id': IdValidate.get(), 'to-page': Joi.number().integer(), 'page-size': Joi.number().integer().min(1), diff --git a/src/routes/validate/user.ts b/src/routes/validate/user.ts index b7de592..f049fd4 100644 --- a/src/routes/validate/user.ts +++ b/src/routes/validate/user.ts @@ -82,7 +82,6 @@ export default class UserValidate { // validate input for user location: this.user.location, devices: this.user.devices }).validate(data, {stripUnknown: true}); - console.log(data); return error !== undefined? null : value; }