diff --git a/src/routes/template.spec.ts b/src/routes/template.spec.ts index c42430b..d9673b7 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: convert name to id, criteria for new name, criteria for new version, criteria for prefix + describe('/template', () => { let server; @@ -305,6 +305,7 @@ describe('/template', () => { should(res.body.parameters[0]).have.property('range'); should(res.body.parameters[0].range).have.property('values'); should(res.body.parameters[0].range.values[0]).be.eql('copper'); + done(); }); }); it('stores the template', done => { @@ -366,17 +367,27 @@ describe('/template', () => { url: '/template/treatment/new', auth: {basic: 'admin'}, httpStatus: 400, - req: {name: 'heat aging', parameters: [{range: {min: 1}}]}, + req: {name: 'heat aging', number_prefix: 'C', parameters: [{range: {min: 1}}]}, res: {status: 'Invalid body format', details: '"parameters[0].name" is required'} }); }); + it('rejects a missing number prefix', done => { + TestHelper.request(server, done, { + method: 'post', + url: '/template/treatment/new', + auth: {basic: 'admin'}, + httpStatus: 400, + req: {name: 'heat aging', parameters: [{name: 'time', range: {min: 1}}]}, + res: {status: 'Invalid body format', details: '"number_prefix" is required'} + }); + }); it('rejects a missing parameter range', done => { TestHelper.request(server, done, { method: 'post', url: '/template/treatment/new', auth: {basic: 'admin'}, httpStatus: 400, - req: {name: 'heat aging', parameters: [{name: 'time'}]}, + req: {name: 'heat aging', number_prefix: 'C', parameters: [{name: 'time'}]}, res: {status: 'Invalid body format', details: '"parameters[0].range" is required'} }); }); @@ -386,7 +397,7 @@ describe('/template', () => { url: '/template/treatment/new', auth: {basic: 'admin'}, httpStatus: 400, - req: {name: 'heat aging', parameters: [{name: 'time', range: {xx: 1}}]}, + req: {name: 'heat aging', number_prefix: 'C', parameters: [{name: 'time', range: {xx: 1}}]}, res: {status: 'Invalid body format', details: '"parameters[0].range.xx" is not allowed'} }); }); @@ -396,8 +407,8 @@ describe('/template', () => { url: '/template/treatment/new', auth: {basic: 'admin'}, httpStatus: 400, - req: {name: 'heat aging', parameters: [{name: 'time', range: {}}], xx: 33}, - res: {} + req: {name: 'heat aging', number_prefix: 'C', parameters: [{name: 'time', range: {}}], xx: 33}, + res: {status: 'Invalid body format', details: '"xx" is not allowed'} }); }); it('rejects already existing number prefixes', done => { @@ -720,6 +731,7 @@ describe('/template', () => { should(res.body.parameters[0]).have.property('name', 'vz'); should(res.body.parameters[0]).have.property('range'); should(res.body.parameters[0].range).have.property('min', 1); + done(); }); }); it('stores the template', done => { @@ -736,7 +748,7 @@ describe('/template', () => { should(data).have.lengthOf(1); should(data[0]).have.only.keys('_id', 'name', 'version', 'parameters', '__v'); should(data[0]).have.property('name', 'vz'); - should(data[0]).have.property('vaersion', 1); + should(data[0]).have.property('version', 1); should(data[0]).have.property('parameters').have.lengthOf(1); should(data[0].parameters[0]).have.property('name', 'vz'); should(data[0].parameters[0]).have.property('range'); @@ -802,7 +814,7 @@ describe('/template', () => { auth: {basic: 'admin'}, httpStatus: 400, req: {name: 'IR spectrum', parameters: [{name: 'data point table', range: {}}], xx: 35}, - res: {} + res: {status: 'Invalid body format', details: '"xx" is not allowed'} }); }); it('rejects an API key', done => { diff --git a/src/routes/template.ts b/src/routes/template.ts index dac7a01..3997944 100644 --- a/src/routes/template.ts +++ b/src/routes/template.ts @@ -63,20 +63,22 @@ router.put('/template/:collection(measurement|treatment)/' + IdValidate.paramete } }); -// router.delete('/template/:collection(measurement|treatment)/:name', (req, res, next) => { -// if (!req.auth(res, ['maintain', 'admin'], 'basic')) return; -// -// (req.params.collection === 'treatment' ? TemplateTreatmentModel : TemplateMeasurementModel) -// .findOneAndDelete({name: req.params.name}).lean().exec((err, data) => { -// if (err) return next(err); -// if (data) { -// res.json({status: 'OK'}) -// } -// else { -// res.status(404).json({status: 'Not found'}); -// } -// }); -// }); +router.post('/template/:collection(measurement|treatment)/new', async (req, res, next) => { + if (!req.auth(res, ['maintain', 'admin'], 'basic')) return; + + const {error, value: template} = TemplateValidate.input(req.body, 'new', req.params.collection); + if (error) return res400(error, res); + + if (_.has(template, 'number_prefix')) { // got number_prefix + if (!await numberPrefixCheck(template, req, res, next)) return; + } + + template.version = 1; + await new (model(req))(template).save((err, data) => { + if (err) next (err); + res.json(TemplateValidate.output(data.toObject(), req.params.collection)); + }); +}); module.exports = router;