fixed validation to return measurements in /sample/{id}
This commit is contained in:
		| @@ -55,8 +55,8 @@ | |||||||
|   parameters: |   parameters: | ||||||
|     - $ref: 'api.yaml#/components/parameters/Id' |     - $ref: 'api.yaml#/components/parameters/Id' | ||||||
|   get: |   get: | ||||||
|     summary: TODO sample details |     summary: sample details | ||||||
|     description: 'Auth: all, levels: read, write, maintain, dev, admin' |     description: 'Auth: all, levels: read, write, maintain, dev, admin<br>Returns validated as well as new measurements' | ||||||
|     x-doc: deleted samples are available only for maintain/admin |     x-doc: deleted samples are available only for maintain/admin | ||||||
|     tags: |     tags: | ||||||
|       - /sample |       - /sample | ||||||
| @@ -225,12 +225,14 @@ | |||||||
|         content: |         content: | ||||||
|           application/json: |           application/json: | ||||||
|             schema: |             schema: | ||||||
|               properties: |               type: array | ||||||
|                 name: |               items: | ||||||
|                   type: string |                 properties: | ||||||
|                 qty: |                   name: | ||||||
|                   type: number |                     type: string | ||||||
|                   example: 20 |                   qty: | ||||||
|  |                     type: number | ||||||
|  |                     example: 20 | ||||||
|       401: |       401: | ||||||
|         $ref: 'api.yaml#/components/responses/401' |         $ref: 'api.yaml#/components/responses/401' | ||||||
|       500: |       500: | ||||||
|   | |||||||
| @@ -69,6 +69,8 @@ Sample: | |||||||
|               relation: |               relation: | ||||||
|                 type: string |                 type: string | ||||||
|                 example: part to this sample |                 example: part to this sample | ||||||
|  |         custom_fields: | ||||||
|  |           type: object | ||||||
|  |  | ||||||
| SampleDetail: | SampleDetail: | ||||||
|   allOf: |   allOf: | ||||||
|   | |||||||
| @@ -215,7 +215,7 @@ describe('/sample', () => { | |||||||
|         url: '/sample/400000000000000000000003', |         url: '/sample/400000000000000000000003', | ||||||
|         auth: {basic: 'janedoe'}, |         auth: {basic: 'janedoe'}, | ||||||
|         httpStatus: 200, |         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 => { |     it('works with an API key', done => { | ||||||
| @@ -224,7 +224,7 @@ describe('/sample', () => { | |||||||
|         url: '/sample/400000000000000000000003', |         url: '/sample/400000000000000000000003', | ||||||
|         auth: {key: 'janedoe'}, |         auth: {key: 'janedoe'}, | ||||||
|         httpStatus: 200, |         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 => { |     it('returns a deleted sample for a maintain/admin user', done => { | ||||||
| @@ -233,7 +233,7 @@ describe('/sample', () => { | |||||||
|         url: '/sample/400000000000000000000005', |         url: '/sample/400000000000000000000005', | ||||||
|         auth: {basic: 'admin'}, |         auth: {basic: 'admin'}, | ||||||
|         httpStatus: 200, |         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 => { |     it('returns 403 for a write user when requesting a deleted sample', done => { | ||||||
|   | |||||||
| @@ -71,7 +71,7 @@ router.get('/sample/' + IdValidate.parameter(), (req, res, next) => { | |||||||
|       sampleData.material.supplier = sampleData.material.supplier_id.name; |       sampleData.material.supplier = sampleData.material.supplier_id.name; | ||||||
|       sampleData.user = sampleData.user_id.name; |       sampleData.user = sampleData.user_id.name; | ||||||
|       sampleData.notes = sampleData.note_id ? sampleData.note_id : {}; |       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; |         sampleData.measurements = data; | ||||||
|         res.json(SampleValidate.output(sampleData, 'details')); |         res.json(SampleValidate.output(sampleData, 'details')); | ||||||
|       }); |       }); | ||||||
|   | |||||||
| @@ -44,4 +44,13 @@ export default class MeasurementValidate { | |||||||
|     }).validate(data, {stripUnknown: true}); |     }).validate(data, {stripUnknown: true}); | ||||||
|     return error !== undefined? null : value; |     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() | ||||||
|  |     }); | ||||||
|  |   } | ||||||
| } | } | ||||||
| @@ -3,6 +3,7 @@ import Joi from '@hapi/joi'; | |||||||
| import IdValidate from './id'; | import IdValidate from './id'; | ||||||
| import UserValidate from './user'; | import UserValidate from './user'; | ||||||
| import MaterialValidate from './material'; | import MaterialValidate from './material'; | ||||||
|  | import MeasurementValidate from './measurement'; | ||||||
|  |  | ||||||
| export default class SampleValidate { | export default class SampleValidate { | ||||||
|   private static sample = { |   private static sample = { | ||||||
| @@ -108,6 +109,7 @@ export default class SampleValidate { | |||||||
|         batch: this.sample.batch, |         batch: this.sample.batch, | ||||||
|         condition: this.sample.condition, |         condition: this.sample.condition, | ||||||
|         material: MaterialValidate.outputV(), |         material: MaterialValidate.outputV(), | ||||||
|  |         measurements: Joi.array().items(MeasurementValidate.outputV()), | ||||||
|         notes: this.sample.notes, |         notes: this.sample.notes, | ||||||
|         user: UserValidate.username() |         user: UserValidate.username() | ||||||
|       } |       } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 VLE2FE
					VLE2FE