From ac72d8a9752dfa7c00bf08d1153925742408a948 Mon Sep 17 00:00:00 2001 From: VLE2FE Date: Thu, 18 Jun 2020 08:57:50 +0200 Subject: [PATCH] fixed validation to return measurements in /sample/{id} --- api/sample.yaml | 18 ++++++++++-------- api/schemas.yaml | 2 ++ src/routes/sample.spec.ts | 6 +++--- src/routes/sample.ts | 2 +- src/routes/validate/measurement.ts | 9 +++++++++ src/routes/validate/sample.ts | 2 ++ 6 files changed, 27 insertions(+), 12 deletions(-) diff --git a/api/sample.yaml b/api/sample.yaml index 1810a65..9331806 100644 --- a/api/sample.yaml +++ b/api/sample.yaml @@ -55,8 +55,8 @@ parameters: - $ref: 'api.yaml#/components/parameters/Id' get: - summary: TODO sample details - description: 'Auth: all, levels: read, write, maintain, dev, admin' + summary: sample details + description: 'Auth: all, levels: read, write, maintain, dev, admin
Returns validated as well as new measurements' x-doc: deleted samples are available only for maintain/admin tags: - /sample @@ -225,12 +225,14 @@ content: application/json: schema: - properties: - name: - type: string - qty: - type: number - example: 20 + type: array + items: + properties: + name: + type: string + qty: + type: number + example: 20 401: $ref: 'api.yaml#/components/responses/401' 500: diff --git a/api/schemas.yaml b/api/schemas.yaml index 21ceddf..9704e08 100644 --- a/api/schemas.yaml +++ b/api/schemas.yaml @@ -69,6 +69,8 @@ Sample: relation: type: string example: part to this sample + custom_fields: + type: object SampleDetail: allOf: diff --git a/src/routes/sample.spec.ts b/src/routes/sample.spec.ts index ee4b07e..9a483a4 100644 --- a/src/routes/sample.spec.ts +++ b/src/routes/sample.spec.ts @@ -215,7 +215,7 @@ describe('/sample', () => { url: '/sample/400000000000000000000003', auth: {basic: 'janedoe'}, httpStatus: 200, - res: {_id: '400000000000000000000003', number: '33', type: 'part', color: 'black', batch: '1704-005', condition: {material: 'copper', weeks: 3, condition_template: '200000000000000000000001'}, material: {_id: '100000000000000000000005', name: 'Amodel A 1133 HS', supplier: 'Solvay', group: 'PPA', mineral: 0, glass_fiber: 33, carbon_fiber: 0, numbers: [{color: 'black', number: '5514262406'}]}, notes: {comment: '', sample_references: [{sample_id: '400000000000000000000004', relation: 'granulate to sample'}], custom_fields: {'not allowed for new applications': true}}, user: 'admin'} + res: {_id: '400000000000000000000003', number: '33', type: 'part', color: 'black', batch: '1704-005', condition: {material: 'copper', weeks: 3, condition_template: '200000000000000000000001'}, material: {_id: '100000000000000000000005', name: 'Amodel A 1133 HS', supplier: 'Solvay', group: 'PPA', mineral: 0, glass_fiber: 33, carbon_fiber: 0, numbers: [{color: 'black', number: '5514262406'}]}, notes: {comment: '', sample_references: [{sample_id: '400000000000000000000004', relation: 'granulate to sample'}], custom_fields: {'not allowed for new applications': true}}, measurements: [{_id: '800000000000000000000003', sample_id: '400000000000000000000003', values: {val1: 1}, measurement_template: '300000000000000000000003'}], user: 'admin'} }); }); it('works with an API key', done => { @@ -224,7 +224,7 @@ describe('/sample', () => { url: '/sample/400000000000000000000003', auth: {key: 'janedoe'}, httpStatus: 200, - res: {_id: '400000000000000000000003', number: '33', type: 'part', color: 'black', batch: '1704-005', condition: {material: 'copper', weeks: 3, condition_template: '200000000000000000000001'}, material: {_id: '100000000000000000000005', name: 'Amodel A 1133 HS', supplier: 'Solvay', group: 'PPA', mineral: 0, glass_fiber: 33, carbon_fiber: 0, numbers: [{color: 'black', number: '5514262406'}]}, notes: {comment: '', sample_references: [{sample_id: '400000000000000000000004', relation: 'granulate to sample'}], custom_fields: {'not allowed for new applications': true}}, user: 'admin'} + res: {_id: '400000000000000000000003', number: '33', type: 'part', color: 'black', batch: '1704-005', condition: {material: 'copper', weeks: 3, condition_template: '200000000000000000000001'}, material: {_id: '100000000000000000000005', name: 'Amodel A 1133 HS', supplier: 'Solvay', group: 'PPA', mineral: 0, glass_fiber: 33, carbon_fiber: 0, numbers: [{color: 'black', number: '5514262406'}]}, notes: {comment: '', sample_references: [{sample_id: '400000000000000000000004', relation: 'granulate to sample'}], custom_fields: {'not allowed for new applications': true}}, measurements: [{_id: '800000000000000000000003', sample_id: '400000000000000000000003', values: {val1: 1}, measurement_template: '300000000000000000000003'}], user: 'admin'} }); }); it('returns a deleted sample for a maintain/admin user', done => { @@ -233,7 +233,7 @@ describe('/sample', () => { url: '/sample/400000000000000000000005', auth: {basic: 'admin'}, httpStatus: 200, - res: {_id: '400000000000000000000005', number: 'Rng33', type: 'granulate', color: 'black', batch: '1653000308', condition: {condition_template: '200000000000000000000003'}, material: {_id: '100000000000000000000005', name: 'Amodel A 1133 HS', supplier: 'Solvay', group: 'PPA', mineral: 0, glass_fiber: 33, carbon_fiber: 0, numbers: [{color: 'black', number: '5514262406'}]}, notes: {}, user: 'admin'} + res: {_id: '400000000000000000000005', number: 'Rng33', type: 'granulate', color: 'black', batch: '1653000308', condition: {condition_template: '200000000000000000000003'}, material: {_id: '100000000000000000000005', name: 'Amodel A 1133 HS', supplier: 'Solvay', group: 'PPA', mineral: 0, glass_fiber: 33, carbon_fiber: 0, numbers: [{color: 'black', number: '5514262406'}]}, notes: {}, measurements: [], user: 'admin'} }); }); it('returns 403 for a write user when requesting a deleted sample', done => { diff --git a/src/routes/sample.ts b/src/routes/sample.ts index 487711b..d28e725 100644 --- a/src/routes/sample.ts +++ b/src/routes/sample.ts @@ -71,7 +71,7 @@ router.get('/sample/' + IdValidate.parameter(), (req, res, next) => { sampleData.material.supplier = sampleData.material.supplier_id.name; sampleData.user = sampleData.user_id.name; sampleData.notes = sampleData.note_id ? sampleData.note_id : {}; - MeasurementModel.find({sample_id: mongoose.Types.ObjectId(req.params.id)}).lean().exec((err, data) => { + MeasurementModel.find({sample_id: mongoose.Types.ObjectId(req.params.id), status: {$ne: globals.status.deleted}}).lean().exec((err, data) => { sampleData.measurements = data; res.json(SampleValidate.output(sampleData, 'details')); }); diff --git a/src/routes/validate/measurement.ts b/src/routes/validate/measurement.ts index 74c2409..0af8fbd 100644 --- a/src/routes/validate/measurement.ts +++ b/src/routes/validate/measurement.ts @@ -44,4 +44,13 @@ export default class MeasurementValidate { }).validate(data, {stripUnknown: true}); return error !== undefined? null : value; } + + static outputV() { // return output validator + return Joi.object({ + _id: IdValidate.get(), + sample_id: IdValidate.get(), + values: this.measurement.values, + measurement_template: IdValidate.get() + }); + } } \ No newline at end of file diff --git a/src/routes/validate/sample.ts b/src/routes/validate/sample.ts index 616e060..1da56da 100644 --- a/src/routes/validate/sample.ts +++ b/src/routes/validate/sample.ts @@ -3,6 +3,7 @@ import Joi from '@hapi/joi'; import IdValidate from './id'; import UserValidate from './user'; import MaterialValidate from './material'; +import MeasurementValidate from './measurement'; export default class SampleValidate { private static sample = { @@ -108,6 +109,7 @@ export default class SampleValidate { batch: this.sample.batch, condition: this.sample.condition, material: MaterialValidate.outputV(), + measurements: Joi.array().items(MeasurementValidate.outputV()), notes: this.sample.notes, user: UserValidate.username() }