Skip to content
Snippets Groups Projects
Commit e7105c07 authored by Pietro Cavassin's avatar Pietro Cavassin
Browse files

Merge branch 'issue-843-new-middleware' into 'development'

Add functional version of the middleware that changes the default query based on request body

See merge request !343
parents 9f5af12d 5f0a221e
No related branches found
No related tags found
4 merge requests!377prd_version of simcaq,!368update filter in v2 school,!348Merge branch development into homologa,!343Add functional version of the middleware that changes the default query based on request body
class ReqBody {
constructor() {
}
add_metrics(req, column, metricsArray) {
for (let i in metricsArray) {
switch(metricsArray[i]["function"]) {
case "max":
req.sql.field("max(" + column + ")", metricsArray[i]["return_name"]);
req.hasMetrics = true;
break;
case "min":
req.sql.field("min(" + column + ")", metricsArray[i]["return_name"]);
req.hasMetrics = true;
break;
case "count":
req.sql.field("count(" + column + ")", metricsArray[i]["return_name"]);
req.hasMetrics = true;
break;
case "sum":
req.sql.field("sum(" + column + ")", metricsArray[i]["return_name"]);
req.hasMetrics = true;
break;
default:
break;
}
}
}
parse() {
return(req, res, next) => {
// Gets body of the HTTP requisition
let body = req.body;
// Chooses operation based on the mode field of the body
switch(body["mode"]) {
case "add_metrics":
// adds flag to check whether it runs the default query or not
req.hasMetrics = false;
// Gets all column names
let columns = Object.keys(body["add_metrics"]);
// Calls function to add metrics that were specified to req.sql
for (let i in columns) {
this.add_metrics(req, columns[i], body["add_metrics"][columns[i]]);
}
break;
default:
break;
}
next();
}
}
}
module.exports = ReqBody;
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment