From 5744162220b9eb1a421c71515c92012d19e00828 Mon Sep 17 00:00:00 2001 From: VLE2FE Date: Thu, 3 Sep 2020 16:26:16 +0200 Subject: [PATCH] code improvements --- README.md | 2 +- src/db.ts | 2 -- src/helpers/csv.ts | 2 +- src/helpers/flatten.ts | 6 +++--- src/helpers/mail.ts | 12 ++++++------ src/index.ts | 3 +++ src/routes/material.ts | 16 +--------------- src/routes/sample.ts | 8 -------- src/routes/template.ts | 2 -- src/routes/validate/sample.ts | 2 -- src/routes/validate/user.ts | 2 +- 11 files changed, 16 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index 437116a..acb2920 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ Testing is done with mocha and can be executed using `npm test`. ## General structure -[index.ts](./src/index.ts) is exectued when starting the server. It includes all setup tasks, registers middleware, +[index.ts](./src/index.ts) is executed when starting the server. It includes all setup tasks, registers middleware, routes and error handlers. Setting the `NODE_ENV` environment variable allows starting the server either in `production`, `development` or `test` mode. diff --git a/src/db.ts b/src/db.ts index f41cee3..b82151d 100644 --- a/src/db.ts +++ b/src/db.ts @@ -149,7 +149,6 @@ export default class db { // changelog entry, expects (req, this (from query helper)) or (req, collection, conditions, data) static log(req, thisOrCollection, conditions = null, data = null) { if (! (conditions || data)) { // (req, this) - console.log(11); data = thisOrCollection._update ? _.cloneDeep(thisOrCollection._update) : {}; // replace undefined with {} // replace keys with a leading $ Object.keys(data).forEach(key => { @@ -158,7 +157,6 @@ export default class db { delete data[key]; } }); - console.log(thisOrCollection._conditions); new ChangelogModel(this.logEscape(_.cloneDeep({ action: req.method + ' ' + req.url, collection_name: thisOrCollection._collection.collectionName, diff --git a/src/helpers/csv.ts b/src/helpers/csv.ts index d7774d7..5e8ae03 100644 --- a/src/helpers/csv.ts +++ b/src/helpers/csv.ts @@ -1,7 +1,7 @@ import {parseAsync} from 'json2csv'; import flatten from './flatten'; -export default function csv(input: any[], f: (err, data) => void) { +export default function csv(input: any[], f: (err, data) => void) { // parse JSON to CSV parseAsync(input.map(e => flatten(e)), {includeEmptyRows: true}) .then(csv => f(null, csv)) .catch(err => f(err, null)); diff --git a/src/helpers/flatten.ts b/src/helpers/flatten.ts index ed54da9..d28bd64 100644 --- a/src/helpers/flatten.ts +++ b/src/helpers/flatten.ts @@ -3,10 +3,10 @@ import globals from '../globals'; export default function flatten (data, keepArray = false) { // flatten object: {a: {b: true}} -> {a.b: true} const result = {}; function recurse (cur, prop) { - if (Object(cur) !== cur || Object.keys(cur).length === 0) { + if (Object(cur) !== cur || Object.keys(cur).length === 0) { // simple value result[prop] = cur; } - else if (prop === `${globals.spectrum.spectrum}.${globals.spectrum.dpt}`) { + else if (prop === `${globals.spectrum.spectrum}.${globals.spectrum.dpt}`) { // convert spectrum for ML result[prop + '.labels'] = cur.map(e => parseFloat(e[0])); result[prop + '.values'] = cur.map(e => parseFloat(e[1])); } @@ -27,7 +27,7 @@ export default function flatten (data, keepArray = false) { // flatten object: } } } - else { + else { // object let isEmpty = true; for (let p in cur) { isEmpty = false; diff --git a/src/helpers/mail.ts b/src/helpers/mail.ts index a76b287..2c0ab57 100644 --- a/src/helpers/mail.ts +++ b/src/helpers/mail.ts @@ -4,10 +4,10 @@ import axios from 'axios'; export default class Mail{ - static readonly address = 'definma@bosch-iot.com'; - static uri: string; - static auth = {username: '', password: ''}; - static mailPass: string; + static readonly address = 'definma@bosch-iot.com'; // email address + static uri: string; // mail API URI + static auth = {username: '', password: ''}; // mail API credentials + static mailPass: string; // mail API generates password static init() { if (process.env.NODE_ENV === 'production') { // only send mails in production @@ -51,14 +51,14 @@ export default class Mail{ }).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 + }).catch(err => { // somewhere 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 + static send (mailAddress, subject, content, f: (x?) => void = () => {}) { // callback executed empty or with error if (process.env.NODE_ENV === 'production') { // only send mails in production axios({ method: 'post', diff --git a/src/index.ts b/src/index.ts index a2165e9..f64e847 100644 --- a/src/index.ts +++ b/src/index.ts @@ -100,6 +100,9 @@ app.use(require('./helpers/authorize')); // handle authentication // redirect /api routes for Angular proxy in development if (process.env.NODE_ENV !== 'production') { app.use('/api/:url([^]+)', (req, res) => { + if (/help\//.test(req.params.url)) { // encode URI again for help route + req.params.url = 'help/' + encodeURIComponent(req.params.url.replace('help/', '')); + } req.url = '/' + req.params.url; app.handle(req, res); }); diff --git a/src/routes/material.ts b/src/routes/material.ts index a75f73d..3996e30 100644 --- a/src/routes/material.ts +++ b/src/routes/material.ts @@ -25,21 +25,7 @@ router.get('/materials', (req, res, next) => { MaterialValidate.query(req.query, ['dev', 'admin'].indexOf(req.authDetails.level) >= 0); if (error) return res400(error, res); - let conditions; - - if (filters.hasOwnProperty('status')) { - if(filters.status === 'all') { - conditions = {$or: [{status: globals.status.val}, {status: globals.status.new}]} - } - else { - conditions = {status: filters.status}; - } - } - else { // default - conditions = {status: globals.status.val}; - } - - MaterialModel.find(conditions).sort({name: 1}).populate('group_id').populate('supplier_id') + MaterialModel.find(filters).sort({name: 1}).populate('group_id').populate('supplier_id') .lean().exec((err, data) => { if (err) return next(err); diff --git a/src/routes/sample.ts b/src/routes/sample.ts index d5bc207..71e84e9 100644 --- a/src/routes/sample.ts +++ b/src/routes/sample.ts @@ -22,16 +22,12 @@ import globals from '../globals'; const router = express.Router(); -// TODO: do not use streaming for spectrum filenames - 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, ['dev', 'admin'].indexOf(req.authDetails.level) >= 0); - console.log(error); if (error) return res400(error, res); - console.log(filters.filters); // spectral data and csv not allowed for read/write users if ((filters.fields.find(e => e.indexOf('.' + globals.spectrum.dpt) >= 0) || filters.output !== 'json') && @@ -388,7 +384,6 @@ router.get('/samples', async (req, res, next) => { projection._id = false; } queryPtr.push({$project: projection}); - console.log(JSON.stringify(query)); // use streaming when including spectrum files if (!fieldsToAdd.find(e => e.indexOf(globals.spectrum.spectrum + '.' + globals.spectrum.dpt) >= 0)) { collection.aggregate(query).allowDiskUse(true).exec((err, data) => { @@ -687,8 +682,6 @@ async function numberGenerate (sample, req, res, next) { const sampleData = await SampleModel .aggregate([ {$match: {number: new RegExp('^' + req.authDetails.location + '[0-9]+$', 'm')}}, - // {$addFields: {number2: {$toDecimal: {$arrayElemAt: [{$split: [{$arrayElemAt: - // [{$split: ['$number', 'Rng']}, 1]}, '_']}, 0]}}}}, // not working with MongoDb 3.6 {$addFields: {sortNumber: {$let: { vars: {tmp: {$concat: ['000000000000000000000000000000', {$arrayElemAt: [{$split: @@ -701,7 +694,6 @@ async function numberGenerate (sample, req, res, next) { .exec() .catch(err => next(err)); if (sampleData instanceof Error) return false; - console.log(sampleData); let number = (sampleData[0] ? Number(sampleData[0].number.replace(/[^0-9]+/g, '')) : 0); if (numberBuffer[req.authDetails.location] && numberBuffer[req.authDetails.location] >= number) { number = numberBuffer[req.authDetails.location]; diff --git a/src/routes/template.ts b/src/routes/template.ts index d569bd2..8eeffdd 100644 --- a/src/routes/template.ts +++ b/src/routes/template.ts @@ -64,8 +64,6 @@ router.put('/template/:collection(measurement|condition|material)/' + IdValidate } if (!_.isEqual(_.pick(templateData, _.keys(template)), template)) { // data was changed - console.log(template); - console.log(templateData); if (!template.parameters || _.isEqual(templateData.parameters, template.parameters)) { // only name was changed model(req).findByIdAndUpdate(req.params.id, {name: template.name}, {new: true}) .log(req).lean().exec((err, data) => { diff --git a/src/routes/validate/sample.ts b/src/routes/validate/sample.ts index 0d50576..efaedf1 100644 --- a/src/routes/validate/sample.ts +++ b/src/routes/validate/sample.ts @@ -199,7 +199,6 @@ export default class SampleValidate { data.filters[i] = decodeURIComponent(data.filters[i]); } catch (ignore) {} - console.log(data.filters[i]); data.filters[i] = JSON.parse(data.filters[i]); data.filters[i].values = data.filters[i].values.map(e => { // validate filter values if (e === null) { // null values are always allowed @@ -241,7 +240,6 @@ export default class SampleValidate { validator = Joi.object(this.sample); } const {value, error} = validator.validate({[field]: e}); - console.log(error); if (error) throw error; // reject invalid values return value[field]; }); diff --git a/src/routes/validate/user.ts b/src/routes/validate/user.ts index 4ac78d4..c9288d7 100644 --- a/src/routes/validate/user.ts +++ b/src/routes/validate/user.ts @@ -75,7 +75,7 @@ export default class UserValidate { // validate input for user }).validate(data); } else { - return{error: 'No parameter specified!', value: {}}; + return {error: 'No parameter specified!', value: {}}; } }