From abf0a99d8a872931bc968d8995e5e7714ac9eddc Mon Sep 17 00:00:00 2001 From: VLE2FE Date: Fri, 15 May 2020 11:16:17 +0200 Subject: [PATCH] number generation for condition done --- api/condition.yaml | 5 +- api/material.yaml | 2 +- api/measurement.yaml | 2 +- api/sample.yaml | 2 +- api/schemas.yaml | 1 + src/routes/condition.spec.ts | 121 ++++++++++++++++--------------- src/routes/condition.ts | 34 +++++---- src/routes/sample.spec.ts | 1 + src/routes/template.spec.ts | 2 +- src/routes/template.ts | 2 +- src/routes/validate/condition.ts | 2 - src/test/db.json | 8 +- 12 files changed, 96 insertions(+), 86 deletions(-) diff --git a/api/condition.yaml b/api/condition.yaml index f924707..ec8b245 100644 --- a/api/condition.yaml +++ b/api/condition.yaml @@ -4,7 +4,7 @@ get: summary: condition by id description: 'Auth: all, levels: read, write, maintain, dev, admin' - x-doc: status handling (accessible (only for maintain/admin))? # TODO + x-doc: status handling (accessible (only for maintain/admin))? # TODO after decision tags: - /condition responses: @@ -38,9 +38,6 @@ allOf: - $ref: 'api.yaml#/components/schemas/_Id' properties: - number: - type: string - example: B1 parameters: type: object responses: diff --git a/api/material.yaml b/api/material.yaml index 6a86b38..9775b29 100644 --- a/api/material.yaml +++ b/api/material.yaml @@ -25,7 +25,7 @@ get: summary: get material details description: 'Auth: all, levels: read, write, maintain, dev, admin' - x-doc: status handling (accessible (only for maintain/admin))? # TODO + x-doc: status handling (accessible (only for maintain/admin))? # TODO after decision tags: - /material responses: diff --git a/api/measurement.yaml b/api/measurement.yaml index 2882883..298b04e 100644 --- a/api/measurement.yaml +++ b/api/measurement.yaml @@ -4,7 +4,7 @@ get: summary: measurement values by id description: 'Auth: all, levels: read, write, maintain, dev, admin' - x-doc: status handling (accessible (only for maintain/admin))? # TODO + x-doc: status handling (accessible (only for maintain/admin))? # TODO after decision tags: - /measurement responses: diff --git a/api/sample.yaml b/api/sample.yaml index 9539053..30500b4 100644 --- a/api/sample.yaml +++ b/api/sample.yaml @@ -24,7 +24,7 @@ get: summary: TODO sample details description: 'Auth: all, levels: read, write, maintain, dev, admin' - x-doc: status handling (accessible (only for maintain/admin))? # TODO + x-doc: status handling (accessible (only for maintain/admin))? # TODO after decision tags: - /sample responses: diff --git a/api/schemas.yaml b/api/schemas.yaml index 3f5098c..7141cbb 100644 --- a/api/schemas.yaml +++ b/api/schemas.yaml @@ -122,6 +122,7 @@ Condition: $ref: 'api.yaml#/components/schemas/Id' number: type: string + readOnly: true example: B1 parameters: type: object diff --git a/src/routes/condition.spec.ts b/src/routes/condition.spec.ts index 2967108..5881e4b 100644 --- a/src/routes/condition.spec.ts +++ b/src/routes/condition.spec.ts @@ -16,7 +16,7 @@ describe('/condition', () => { url: '/condition/700000000000000000000001', auth: {basic: 'janedoe'}, httpStatus: 200, - res: {_id: '700000000000000000000001', sample_id: '400000000000000000000001', number: 'B1', parameters: {material: 'copper', weeks: 3}, treatment_template: '200000000000000000000001'} + res: {_id: '700000000000000000000001', sample_id: '400000000000000000000001', number: 'A1', parameters: {material: 'copper', weeks: 3}, treatment_template: '200000000000000000000001'} }); }); it('returns the right condition for an API key', done => { @@ -25,7 +25,7 @@ describe('/condition', () => { url: '/condition/700000000000000000000001', auth: {key: 'janedoe'}, httpStatus: 200, - res: {_id: '700000000000000000000001', sample_id: '400000000000000000000001', number: 'B1', parameters: {material: 'copper', weeks: 3}, treatment_template: '200000000000000000000001'} + res: {_id: '700000000000000000000001', sample_id: '400000000000000000000001', number: 'A1', parameters: {material: 'copper', weeks: 3}, treatment_template: '200000000000000000000001'} }); }); it('rejects an invalid id', done => { @@ -61,7 +61,7 @@ describe('/condition', () => { auth: {basic: 'janedoe'}, httpStatus: 200, req: {}, - res: {_id: '700000000000000000000001', sample_id: '400000000000000000000001', number: 'B1', treatment_template: '200000000000000000000001', parameters: {material: 'copper', weeks: 3}} + res: {_id: '700000000000000000000001', sample_id: '400000000000000000000001', number: 'A1', treatment_template: '200000000000000000000001', parameters: {material: 'copper', weeks: 3}} }); }); it('keeps unchanged properties', done => { @@ -73,7 +73,7 @@ describe('/condition', () => { req: {parameters: {material: 'copper', weeks: 3}} }).end((err, res) => { if (err) return done(err); - should(res.body).be.eql({_id: '700000000000000000000001', sample_id: '400000000000000000000001', number: 'B1', treatment_template: '200000000000000000000001', parameters: {material: 'copper', weeks: 3}}); + should(res.body).be.eql({_id: '700000000000000000000001', sample_id: '400000000000000000000001', number: 'A1', treatment_template: '200000000000000000000001', parameters: {material: 'copper', weeks: 3}}); ConditionModel.findById('700000000000000000000001').lean().exec((err, data) => { if (err) return done(err); should(data).have.property('status', 10); @@ -90,7 +90,7 @@ describe('/condition', () => { req: {parameters: {material: 'copper'}} }).end((err, res) => { if (err) return done(err); - should(res.body).be.eql({_id: '700000000000000000000001', sample_id: '400000000000000000000001', number: 'B1', treatment_template: '200000000000000000000001', parameters: {material: 'copper', weeks: 3}}); + should(res.body).be.eql({_id: '700000000000000000000001', sample_id: '400000000000000000000001', number: 'A1', treatment_template: '200000000000000000000001', parameters: {material: 'copper', weeks: 3}}); ConditionModel.findById('700000000000000000000001').lean().exec((err, data) => { if (err) return done(err); should(data).have.property('status', 10); @@ -107,12 +107,12 @@ describe('/condition', () => { req: {parameters: {material: 'hot air', weeks: 10}} }).end((err, res) => { if (err) return done(err); - should(res.body).be.eql({_id: '700000000000000000000001', sample_id: '400000000000000000000001', number: 'B1', treatment_template: '200000000000000000000001', parameters: {material: 'hot air', weeks: 10}}); + should(res.body).be.eql({_id: '700000000000000000000001', sample_id: '400000000000000000000001', number: 'A1', treatment_template: '200000000000000000000001', parameters: {material: 'hot air', weeks: 10}}); ConditionModel.findById('700000000000000000000001').lean().exec((err, data: any) => { if (err) return done(err); should(data).have.only.keys('_id', 'sample_id', 'number', 'parameters', 'treatment_template', 'status', '__v'); should(data.sample_id.toString()).be.eql('400000000000000000000001'); - should(data).have.property('number', 'B1'); + should(data).have.property('number', 'A1'); should(data.treatment_template.toString()).be.eql('200000000000000000000001'); should(data).have.property('status', 0); should(data).have.property('parameters'); @@ -129,7 +129,17 @@ describe('/condition', () => { auth: {basic: 'janedoe'}, httpStatus: 200, req: {parameters: {weeks: 8}}, - res: {_id: '700000000000000000000001', sample_id: '400000000000000000000001', number: 'B1', treatment_template: '200000000000000000000001', parameters: {material: 'copper', weeks: 8}} + res: {_id: '700000000000000000000001', sample_id: '400000000000000000000001', number: 'A1', treatment_template: '200000000000000000000001', parameters: {material: 'copper', weeks: 8}} + }); + }); + it('rejects changing the condition number', done => { + TestHelper.request(server, done, { + method: 'put', + url: '/condition/700000000000000000000001', + auth: {basic: 'janedoe'}, + httpStatus: 400, + req: {number: 'C2'}, + res: {status: 'Invalid body format', details: '"number" is not allowed'} }); }); it('rejects not specified parameters', done => { @@ -198,7 +208,7 @@ describe('/condition', () => { auth: {basic: 'admin'}, httpStatus: 200, req: {parameters: {material: 'hot air', weeks: 10}}, - res: {_id: '700000000000000000000001', sample_id: '400000000000000000000001', number: 'B1', treatment_template: '200000000000000000000001', parameters: {material: 'hot air', weeks: 10}} + res: {_id: '700000000000000000000001', sample_id: '400000000000000000000001', number: 'A1', treatment_template: '200000000000000000000001', parameters: {material: 'hot air', weeks: 10}} }); }); it('rejects an API key', done => { @@ -227,34 +237,41 @@ describe('/condition', () => { req: {parameters: {material: 'hot air', weeks: 10}} }); }); - }); // TODO: how to deal with template changes? Template versioning? - // TODO: rewrite delete methods -> set status for every database collection + }); describe('DELETE /condition/{id}', () => { it('sets the status to deleted', done => { TestHelper.request(server, done, { method: 'delete', - url: '/condition/700000000000000000000002', + url: '/condition/700000000000000000000004', auth: {basic: 'janedoe'}, httpStatus: 200 }).end((err, res) => { if (err) return done(err); should(res.body).be.eql({status: 'OK'}); - ConditionModel.findById('700000000000000000000002').lean().exec((err, data: any) => { + ConditionModel.findById('700000000000000000000004').lean().exec((err, data: any) => { if (err) return done(err); should(data).have.only.keys('_id', 'sample_id', 'number', 'parameters', 'treatment_template', 'status', '__v'); - should(data.sample_id.toString()).be.eql('400000000000000000000002'); - should(data).have.property('number', 'B1'); + should(data.sample_id.toString()).be.eql('400000000000000000000001'); + should(data).have.property('number', 'A6'); should(data.treatment_template.toString()).be.eql('200000000000000000000001'); should(data).have.property('status', -1); should(data).have.property('parameters'); - should(data.parameters).have.property('material', 'copper'); - should(data.parameters).have.property('weeks', 3); + should(data.parameters).have.property('material', 'hot air'); + should(data.parameters).have.property('weeks', 5); done(); }); }); }); - it('rejects a deleting a condition referenced by measurements'); // TODO + it('rejects deleting a condition referenced by measurements'/*, done => { + TestHelper.request(server, done, { + method: 'delete', + url: '/condition/700000000000000000000002', + auth: {basic: 'janedoe'}, + httpStatus: 200, + res: {status: 'Condition still in use'} + }); + }*/); // TODO after decision it('rejects an invalid id', done => { TestHelper.request(server, done, { method: 'delete', @@ -266,7 +283,7 @@ describe('/condition', () => { it('rejects an API key', done => { TestHelper.request(server, done, { method: 'delete', - url: '/condition/700000000000000000000002', + url: '/condition/700000000000000000000004', auth: {key: 'janedoe'}, httpStatus: 401 }); @@ -274,7 +291,7 @@ describe('/condition', () => { it('rejects requests from a read user', done => { TestHelper.request(server, done, { method: 'delete', - url: '/condition/700000000000000000000002', + url: '/condition/700000000000000000000004', auth: {basic: 'user'}, httpStatus: 403 }); @@ -290,7 +307,7 @@ describe('/condition', () => { it('accepts an maintain/admin user deleting a condition belonging to a sample of another user', done => { TestHelper.request(server, done, { method: 'delete', - url: '/condition/700000000000000000000002', + url: '/condition/700000000000000000000004', auth: {basic: 'admin'}, httpStatus: 200 }).end((err, res) => { @@ -302,7 +319,7 @@ describe('/condition', () => { it('returns 404 for an unknown id', done => { TestHelper.request(server, done, { method: 'delete', - url: '/condition/00000000000w000000000002', + url: '/condition/000000000000000000000002', auth: {basic: 'janedoe'}, httpStatus: 404 }); @@ -310,7 +327,7 @@ describe('/condition', () => { it('rejects unauthorized requests', done => { TestHelper.request(server, done, { method: 'delete', - url: '/condition/700000000000000000000002', + url: '/condition/700000000000000000000004', httpStatus: 401 }); }); @@ -323,13 +340,13 @@ describe('/condition', () => { url: '/condition/new', auth: {basic: 'janedoe'}, httpStatus: 200, - req: {sample_id: '400000000000000000000002', number: 'B2', parameters: {material: 'hot air', weeks: 10}, treatment_template: '200000000000000000000001'} + req: {sample_id: '400000000000000000000002', parameters: {material: 'hot air', weeks: 10}, treatment_template: '200000000000000000000001'} }).end((err, res) => { if (err) return done(err); should(res.body).have.only.keys('_id', 'sample_id', 'number', 'parameters', 'treatment_template'); should(res.body).have.property('_id').be.type('string'); should(res.body).have.property('sample_id', '400000000000000000000002'); - should(res.body).have.property('number', 'B2'); + should(res.body).have.property('number', 'A7'); should(res.body).have.property('treatment_template', '200000000000000000000001'); should(res.body).have.property('parameters'); should(res.body.parameters).have.property('material', 'hot air'); @@ -343,14 +360,14 @@ describe('/condition', () => { url: '/condition/new', auth: {basic: 'janedoe'}, httpStatus: 200, - req: {sample_id: '400000000000000000000002', number: 'B2', parameters: {material: 'hot air', weeks: 10}, treatment_template: '200000000000000000000001'} + req: {sample_id: '400000000000000000000002', parameters: {material: 'hot air', weeks: 10}, treatment_template: '200000000000000000000001'} }).end((err, res) => { if (err) return done(err); ConditionModel.findById(res.body._id).lean().exec((err, data: any) => { if (err) return done(err); should(data).have.only.keys('_id', 'sample_id', 'number', 'parameters', 'treatment_template', 'status', '__v'); should(data.sample_id.toString()).be.eql('400000000000000000000002'); - should(data).have.property('number', 'B2'); + should(data).have.property('number', 'A7'); should(data.treatment_template.toString()).be.eql('200000000000000000000001'); should(data).have.property('status', 0); should(data).have.property('parameters'); @@ -366,7 +383,7 @@ describe('/condition', () => { url: '/condition/new', auth: {basic: 'janedoe'}, httpStatus: 400, - req: {sample_id: '4000000000h0000000000002', number: 'B2', parameters: {material: 'hot air', weeks: 10}, treatment_template: '200000000000000000000001'}, + req: {sample_id: '4000000000h0000000000002', parameters: {material: 'hot air', weeks: 10}, treatment_template: '200000000000000000000001'}, res: {status: 'Invalid body format', details: '"sample_id" with value "4000000000h0000000000002" fails to match the required pattern: /[0-9a-f]{24}/'} }); }); @@ -376,7 +393,7 @@ describe('/condition', () => { url: '/condition/new', auth: {basic: 'janedoe'}, httpStatus: 400, - req: {sample_id: '000000000000000000000002', number: 'B2', parameters: {material: 'hot air', weeks: 10}, treatment_template: '200000000000000000000001'}, + req: {sample_id: '000000000000000000000002', parameters: {material: 'hot air', weeks: 10}, treatment_template: '200000000000000000000001'}, res: {status: 'Sample id not available'} }); }); @@ -386,7 +403,7 @@ describe('/condition', () => { url: '/condition/new', auth: {basic: 'janedoe'}, httpStatus: 400, - req: {sample_id: '400000000000000000000002', number: 'B2', parameters: {material: 'hot air', weeks: 10}, treatment_template: '200000000000h00000000001'}, + req: {sample_id: '400000000000000000000002', parameters: {material: 'hot air', weeks: 10}, treatment_template: '200000000000h00000000001'}, res: {status: 'Invalid body format', details: '"treatment_template" with value "200000000000h00000000001" fails to match the required pattern: /[0-9a-f]{24}/'} }); }); @@ -396,18 +413,18 @@ describe('/condition', () => { url: '/condition/new', auth: {basic: 'janedoe'}, httpStatus: 400, - req: {sample_id: '400000000000000000000002', number: 'B2', parameters: {material: 'hot air', weeks: 10}, treatment_template: '000000000000000000000001'}, + req: {sample_id: '400000000000000000000002', parameters: {material: 'hot air', weeks: 10}, treatment_template: '000000000000000000000001'}, res: {status: 'Treatment template not available'} }); }); - it('rejects a condition number already in use for this sample', done => { + it('rejects setting a condition number', done => { TestHelper.request(server, done, { method: 'post', url: '/condition/new', auth: {basic: 'janedoe'}, httpStatus: 400, - req: {sample_id: '400000000000000000000001', number: 'B1', parameters: {material: 'hot air', weeks: 10}, treatment_template: '200000000000000000000001'}, - res: {status: 'Condition number already taken'} + req: {sample_id: '400000000000000000000001', number: 'A7', parameters: {material: 'hot air', weeks: 10}, treatment_template: '200000000000000000000001'}, + res: {status: 'Invalid body format', details: '"number" is not allowed'} }); }); it('rejects not specified parameters', done => { @@ -416,7 +433,7 @@ describe('/condition', () => { url: '/condition/new', auth: {basic: 'janedoe'}, httpStatus: 400, - req: {sample_id: '400000000000000000000002', number: 'B2', parameters: {material: 'hot air', weeks: 10, xx: 12}, treatment_template: '200000000000000000000001'}, + req: {sample_id: '400000000000000000000002', parameters: {material: 'hot air', weeks: 10, xx: 12}, treatment_template: '200000000000000000000001'}, res: {status: 'Invalid body format', details: '"xx" is not allowed'} }); }); @@ -426,7 +443,7 @@ describe('/condition', () => { url: '/condition/new', auth: {basic: 'janedoe'}, httpStatus: 400, - req: {sample_id: '400000000000000000000002', number: 'B2', parameters: {material: 'hot air'}, treatment_template: '200000000000000000000001'}, + req: {sample_id: '400000000000000000000002', parameters: {material: 'hot air'}, treatment_template: '200000000000000000000001'}, res: {status: 'Invalid body format', details: '"weeks" is required'} }); }); @@ -436,7 +453,7 @@ describe('/condition', () => { url: '/condition/new', auth: {basic: 'janedoe'}, httpStatus: 400, - req: {sample_id: '400000000000000000000002', number: 'B2', parameters: {material: 'xxx', weeks: 10}, treatment_template: '200000000000000000000001'}, + req: {sample_id: '400000000000000000000002', parameters: {material: 'xxx', weeks: 10}, treatment_template: '200000000000000000000001'}, res: {status: 'Invalid body format', details: '"material" must be one of [copper, hot air]'} }); }); @@ -446,7 +463,7 @@ describe('/condition', () => { url: '/condition/new', auth: {basic: 'janedoe'}, httpStatus: 400, - req: {sample_id: '400000000000000000000002', number: 'B2', parameters: {material: 'hot air', weeks: -10}, treatment_template: '200000000000000000000001'}, + req: {sample_id: '400000000000000000000002', parameters: {material: 'hot air', weeks: -10}, treatment_template: '200000000000000000000001'}, res: {status: 'Invalid body format', details: '"weeks" must be larger than or equal to 1'} }); }); @@ -456,7 +473,7 @@ describe('/condition', () => { url: '/condition/new', auth: {basic: 'janedoe'}, httpStatus: 400, - req: {sample_id: '400000000000000000000002', number: 'B2', parameters: {material: 'hot air', weeks: 11}, treatment_template: '200000000000000000000001'}, + req: {sample_id: '400000000000000000000002', parameters: {material: 'hot air', weeks: 11}, treatment_template: '200000000000000000000001'}, res: {status: 'Invalid body format', details: '"weeks" must be less than or equal to 10'} }); }); @@ -466,7 +483,7 @@ describe('/condition', () => { url: '/condition/new', auth: {basic: 'janedoe'}, httpStatus: 400, - req: {number: 'B2', parameters: {material: 'hot air', weeks: 10}, treatment_template: '200000000000000000000001'}, + req: {parameters: {material: 'hot air', weeks: 10}, treatment_template: '200000000000000000000001'}, res: {status: 'Invalid body format', details: '"sample_id" is required'} }); }); @@ -476,27 +493,17 @@ describe('/condition', () => { url: '/condition/new', auth: {basic: 'janedoe'}, httpStatus: 400, - req: {sample_id: '400000000000000000000002', number: 'B2', parameters: {material: 'hot air', weeks: 10}}, + req: {sample_id: '400000000000000000000002', parameters: {material: 'hot air', weeks: 10}}, res: {status: 'Invalid body format', details: '"treatment_template" is required'} }); }); - it('rejects a missing number', done => { - TestHelper.request(server, done, { - method: 'post', - url: '/condition/new', - auth: {basic: 'janedoe'}, - httpStatus: 400, - req: {sample_id: '400000000000000000000002', parameters: {material: 'hot air', weeks: 10}, treatment_template: '200000000000000000000001'}, - res: {status: 'Invalid body format', details: '"number" is required'} - }); - }); it('rejects adding a condition to the sample of an other user for a write user', done => { TestHelper.request(server, done, { method: 'post', url: '/condition/new', auth: {basic: 'janedoe'}, httpStatus: 403, - req: {sample_id: '400000000000000000000003', number: 'B2', parameters: {material: 'hot air', weeks: 10}, treatment_template: '200000000000000000000001'} + req: {sample_id: '400000000000000000000003', parameters: {material: 'hot air', weeks: 10}, treatment_template: '200000000000000000000001'} }); }); it('accepts adding a condition to the sample of an other user for a maintain/admin user', done => { @@ -505,13 +512,13 @@ describe('/condition', () => { url: '/condition/new', auth: {basic: 'admin'}, httpStatus: 200, - req: {sample_id: '400000000000000000000002', number: 'B2', parameters: {material: 'hot air', weeks: 10}, treatment_template: '200000000000000000000001'} + req: {sample_id: '400000000000000000000002', parameters: {material: 'hot air', weeks: 10}, treatment_template: '200000000000000000000001'} }).end((err, res) => { if (err) return done(err); should(res.body).have.only.keys('_id', 'sample_id', 'number', 'parameters', 'treatment_template'); should(res.body).have.property('_id').be.type('string'); should(res.body).have.property('sample_id', '400000000000000000000002'); - should(res.body).have.property('number', 'B2'); + should(res.body).have.property('number', 'A7'); should(res.body).have.property('treatment_template', '200000000000000000000001'); should(res.body).have.property('parameters'); should(res.body.parameters).have.property('material', 'hot air'); @@ -525,7 +532,7 @@ describe('/condition', () => { url: '/condition/new', auth: {key: 'janedoe'}, httpStatus: 401, - req: {sample_id: '400000000000000000000002', number: 'B2', parameters: {material: 'hot air', weeks: 10}, treatment_template: '200000000000000000000001'} + req: {sample_id: '400000000000000000000002', parameters: {material: 'hot air', weeks: 10}, treatment_template: '200000000000000000000001'} }); }); it('rejects requests from a read user', done => { @@ -534,7 +541,7 @@ describe('/condition', () => { url: '/condition/new', auth: {basic: 'user'}, httpStatus: 403, - req: {sample_id: '400000000000000000000002', number: 'B2', parameters: {material: 'hot air', weeks: 10}, treatment_template: '200000000000000000000001'} + req: {sample_id: '400000000000000000000002', parameters: {material: 'hot air', weeks: 10}, treatment_template: '200000000000000000000001'} }); }); it('rejects unauthorized requests', done => { @@ -542,7 +549,7 @@ describe('/condition', () => { method: 'post', url: '/condition/new', httpStatus: 401, - req: {sample_id: '400000000000000000000002', number: 'B2', parameters: {material: 'hot air', weeks: 10}, treatment_template: '200000000000000000000001'} + req: {sample_id: '400000000000000000000002', parameters: {material: 'hot air', weeks: 10}, treatment_template: '200000000000000000000001'} }); }); }); diff --git a/src/routes/condition.ts b/src/routes/condition.ts index 89ddce0..1e54a00 100644 --- a/src/routes/condition.ts +++ b/src/routes/condition.ts @@ -1,5 +1,4 @@ import express from 'express'; -import mongoose from 'mongoose'; import _ from 'lodash'; import ConditionValidate from './validate/condition'; @@ -79,9 +78,11 @@ router.post('/condition/new', async (req, res, next) => { if (error) return res400(error, res); if (!await sampleIdCheck(condition, req, res, next)) return; - if (!await numberCheck(condition, res, next)) return; - if (!await treatmentCheck(condition, 'new', res, next)) return; + const treatmentData = await treatmentCheck(condition, 'new', res, next) + if (!treatmentData) return; + condition.number = await numberGenerate(condition, treatmentData, next); + if (!condition.number) return; condition.status = 0; await new ConditionModel(condition).save((err, data) => { if (err) return next(err); @@ -104,24 +105,29 @@ async function sampleIdCheck (condition, req, res, next) { // validate sample_i return true; } -async function numberCheck (condition, res, next) { // validate number, returns false if invalid - const data = await ConditionModel.find({sample_id: new mongoose.Types.ObjectId(condition.sample_id), number: condition.number}).lean().exec().catch(err => {next(err); return false;}) as any; - if (data.length) { - res.status(400).json({status: 'Condition number already taken'}); - return false; - } - return true; +async function numberGenerate (condition, treatmentData, next) { // validate number, returns false if invalid + const conditionData = await ConditionModel + .find({number: new RegExp('^' + treatmentData.number_prefix + '[0-9]+$', 'm')}) + .sort({number: -1}) + .limit(1) + .lean() + .exec() + .catch(err => next(err)) as any; + if (conditionData instanceof Error) return false; + console.log(conditionData); + return treatmentData.number_prefix + (Number(conditionData[0].number.replace(/[^0-9]+/g, '')) + 1); } -async function treatmentCheck (condition, param, res, next) { - const treatmentData = await TreatmentTemplateModel.findById(condition.treatment_template).lean().exec().catch(err => {next(err); return false;}) as any; +async function treatmentCheck (condition, param, res, next) { // validate treatment template, returns false if invalid, otherwise template data + const treatmentData = await TreatmentTemplateModel.findById(condition.treatment_template).lean().exec().catch(err => next(err)) as any; + if (treatmentData instanceof Error) return false; if (!treatmentData) { // template not found res.status(400).json({status: 'Treatment template not available'}); - return false + return false; } // validate parameters const {error, value: ignore} = ParametersValidate.input(condition.parameters, treatmentData.parameters, param); if (error) {res400(error, res); return false;} - return true; + return treatmentData; } \ No newline at end of file diff --git a/src/routes/sample.spec.ts b/src/routes/sample.spec.ts index 42c8435..8efce68 100644 --- a/src/routes/sample.spec.ts +++ b/src/routes/sample.spec.ts @@ -490,6 +490,7 @@ describe('/sample', () => { httpStatus: 404 }); }); + it('rejects deleting a sample referenced by conditions'); // TODO after decision it('rejects requests from a read user', done => { TestHelper.request(server, done, { method: 'delete', diff --git a/src/routes/template.spec.ts b/src/routes/template.spec.ts index d9673b7..68b99e9 100644 --- a/src/routes/template.spec.ts +++ b/src/routes/template.spec.ts @@ -4,7 +4,7 @@ import TemplateTreatmentModel from '../models/treatment_template'; import TemplateMeasurementModel from '../models/measurement_template'; import TestHelper from "../test/helper"; - +// TODO: check number prefix to have no numbers describe('/template', () => { let server; diff --git a/src/routes/template.ts b/src/routes/template.ts index 3997944..28caf2b 100644 --- a/src/routes/template.ts +++ b/src/routes/template.ts @@ -7,7 +7,7 @@ import TemplateMeasurementModel from '../models/measurement_template'; import res400 from './validate/res400'; import IdValidate from './validate/id'; -// TODO: remove f() for await + const router = express.Router(); diff --git a/src/routes/validate/condition.ts b/src/routes/validate/condition.ts index f130076..491c318 100644 --- a/src/routes/validate/condition.ts +++ b/src/routes/validate/condition.ts @@ -22,14 +22,12 @@ export default class ConditionValidate { if (param === 'new') { return Joi.object({ sample_id: IdValidate.get().required(), - number: this.condition.number.required(), parameters: this.condition.parameters.required(), treatment_template: IdValidate.get().required() }).validate(data); } else if (param === 'change') { return Joi.object({ - number: this.condition.number, parameters: this.condition.parameters }).validate(data); } diff --git a/src/test/db.json b/src/test/db.json index 619fb75..62d18ea 100644 --- a/src/test/db.json +++ b/src/test/db.json @@ -221,7 +221,7 @@ { "_id": {"$oid":"700000000000000000000001"}, "sample_id": {"$oid":"400000000000000000000001"}, - "number": "B1", + "number": "A1", "parameters": { "material": "copper", "weeks": 3 @@ -233,7 +233,7 @@ { "_id": {"$oid":"700000000000000000000002"}, "sample_id": {"$oid":"400000000000000000000002"}, - "number": "B1", + "number": "A3", "parameters": { "material": "copper", "weeks": 3 @@ -245,7 +245,7 @@ { "_id": {"$oid":"700000000000000000000003"}, "sample_id": {"$oid":"400000000000000000000004"}, - "number": "B1", + "number": "A4", "parameters": { "material": "copper", "weeks": 3 @@ -257,7 +257,7 @@ { "_id": {"$oid":"700000000000000000000004"}, "sample_id": {"$oid":"400000000000000000000001"}, - "number": "B3", + "number": "A6", "parameters": { "material": "hot air", "weeks": 5