More refactoring. Code is much DRY-er.
I removed a bunch of duplication by: 1. Moving `if (!where)` logic into the build_where function - sending the "No body was sent." message back in the HTTP response object. 2. Putting all file read operations into a dedicated function - process_route. This takes a callback function that handles the remaining logic of each route after successfully reading the SQL file.
This commit is contained in:
parent
d5738cc7af
commit
b171b4d7ac
491
index.js
491
index.js
@ -7,9 +7,7 @@ var bodyParser = require('body-parser');
|
|||||||
const server = express();
|
const server = express();
|
||||||
const pg = require('pg');
|
const pg = require('pg');
|
||||||
|
|
||||||
//---------read sql files into variables----------------
|
|
||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
//-------------------------------------------------------
|
|
||||||
|
|
||||||
var options = {
|
var options = {
|
||||||
key: fs.readFileSync(process.env.wd + 'key.pem'),
|
key: fs.readFileSync(process.env.wd + 'key.pem'),
|
||||||
@ -32,23 +30,73 @@ var Postgres = new pg.Client({
|
|||||||
});
|
});
|
||||||
Postgres.connect();
|
Postgres.connect();
|
||||||
|
|
||||||
Postgres.FirstRow = function(inSQL, inResponse) {
|
Postgres.FirstRow = function(sql, response) {
|
||||||
console.log(`${new Date().toISOString()}: Running query:\n${inSQL}`);
|
console.log(`${new Date().toISOString()}: Running query:\n${sql}`);
|
||||||
Postgres.query(inSQL, [], (err, res) => {
|
Postgres.query(sql, [], (err, res) => {
|
||||||
if (err === null) {
|
if (err === null) {
|
||||||
console.log(`${new Date().toISOString()}: Received reponse: ${JSON.stringify(res.rows[0]).slice(0,100)}…`);
|
console.log(`${new Date().toISOString()}: Reponse: ${JSON.stringify(res.rows[0]).slice(0,100)}${JSON.stringify(res.rows[0]).length<100?'':'… (truncated)'}`);
|
||||||
inResponse.json(res.rows[0]);
|
response.json(res.rows[0]);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.log(`${new Date().toISOString()}: ${JSON.stringify(err.stack)}`);
|
console.log(`${new Date().toISOString()}: ERROR|${JSON.stringify(err.stack)}`);
|
||||||
inResponse.json(err.stack);
|
response.json(err.stack);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
server.get('/', function(_, res) {
|
function process_route(route, description, path, res, callback) {
|
||||||
console.log(`${new Date().toISOString()}: GET / (Test if server is up.)… SQL: n/a ${"-._.".repeat(20)}`);
|
console.log(`${"▼".repeat(120)}`)
|
||||||
res.send('node.js express is up and running');
|
console.log(`${new Date().toISOString()}: ${route} (${description})`)
|
||||||
|
if (!path) {
|
||||||
|
callback(undefined);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`${new Date().toISOString()}: SQL file: ${path}`)
|
||||||
|
fs.readFile(path, 'utf8', function(err, data) {
|
||||||
|
if (!err) {
|
||||||
|
callback(data);
|
||||||
|
} else {
|
||||||
|
console.log(`${new Date().toISOString()}: ERROR|Could not read sql file: ${JSON.stringify(err)}`);
|
||||||
|
res.send('Failed to read needed SQL file.');
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function build_where(req, res) {
|
||||||
|
console.log(`${new Date().toISOString()}: Building WHERE from ${JSON.stringify(req.body)}`);
|
||||||
|
var where = '';
|
||||||
|
for (var column in req.body.scenario) {
|
||||||
|
if (where) {
|
||||||
|
where = `${where}\n AND `;
|
||||||
|
}
|
||||||
|
if (Array.isArray(req.body.scenario[column])) {
|
||||||
|
where = `${where} ${column} IN ('${req.body.scenario[column].join("','")}')`;
|
||||||
|
} else {
|
||||||
|
where = `${where} ${column} = '${req.body.scenario[column]}'`;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
//if the request has a stamp, then it's an adjustment
|
||||||
|
//prevent making adjustment to actual history by limiting to current stamp and beyond
|
||||||
|
if (req.body.stamp) {
|
||||||
|
where = `${where}\n AND order_date >= least('${req.body.stamp}'::date,'2021-06-01')`;
|
||||||
|
}
|
||||||
|
console.log(`${new Date().toISOString()}: Returning ${where}`);
|
||||||
|
|
||||||
|
if (where == '') {
|
||||||
|
console.log(`${new Date().toISOString()}: ERROR|Unable to create a WHERE clause.`);
|
||||||
|
|
||||||
|
res.send('No body was sent.');
|
||||||
|
}
|
||||||
|
return where;
|
||||||
|
}
|
||||||
|
|
||||||
|
server.get('/', function(_, res) {
|
||||||
|
process_route('GET /', 'Test if server is up.', undefined, res,
|
||||||
|
function(_) {
|
||||||
|
res.send('node.js express is up and running');
|
||||||
|
}
|
||||||
|
);
|
||||||
|
})
|
||||||
|
|
||||||
server.get('/login', (_, res) => res.sendFile(process.env.wd + 'msauth.html'))
|
server.get('/login', (_, res) => res.sendFile(process.env.wd + 'msauth.html'))
|
||||||
|
|
||||||
@ -58,156 +106,69 @@ server.get('/pgbadger', (_, res) => res.sendFile(process.env.wd + 'logs.html'))
|
|||||||
|
|
||||||
server.get('/totals', (_, res) => res.sendFile(process.env.wd + 'totals.log'))
|
server.get('/totals', (_, res) => res.sendFile(process.env.wd + 'totals.log'))
|
||||||
|
|
||||||
server.get('/test_sql', function(_, res) {
|
|
||||||
var path = './route_meta/scenario_package.sql'
|
|
||||||
var callback = function(arg) {
|
|
||||||
res.send(arg)
|
|
||||||
};
|
|
||||||
|
|
||||||
fs.readFile(path, 'utf8', function(err, data) {
|
|
||||||
if (!err) {
|
|
||||||
callback(data);
|
|
||||||
} else {
|
|
||||||
res.send('Failed to read needed SQL file.');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
server.get('/get_pool', bodyParser.json(), function(req, res) {
|
server.get('/get_pool', bodyParser.json(), function(req, res) {
|
||||||
|
process_route('GET /get_pool', 'Get all data for one DSM.', './route_sql/get_pool.sql', res,
|
||||||
var path = './route_sql/get_pool.sql';
|
function(arg) {
|
||||||
console.log(`${new Date().toISOString()}: GET /get_pool (Get all data for one DSM.)… SQL: ${path} ${"-._.".repeat(20)}`);
|
|
||||||
|
|
||||||
var callback = function(arg) {
|
|
||||||
|
|
||||||
if (req.body.quota_rep) {
|
if (req.body.quota_rep) {
|
||||||
// ensure backward compatibility
|
// ensure backward compatibility
|
||||||
console.log(`${new Date().toISOString()}:Converting old format… ${JSON.stringify(req.body)}`);
|
console.log(`${new Date().toISOString()}:Converting old format… ${JSON.stringify(req.body)}`);
|
||||||
req.body = {'scenario':{'quota_rep_descr':req.body.quota_rep}};
|
req.body = {'scenario':{'quota_rep_descr':req.body.quota_rep}};
|
||||||
}
|
}
|
||||||
|
|
||||||
var where = build_where(req);
|
var where = build_where(req, res);
|
||||||
if (where === '') {
|
if (!where) return;
|
||||||
res.send("no body was sent");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var sql = arg.replace(new RegExp("where_clause", 'g'), where);
|
var sql = arg.replace(new RegExp("where_clause", 'g'), where);
|
||||||
Postgres.FirstRow(sql, res)
|
Postgres.FirstRow(sql, res)
|
||||||
};
|
|
||||||
|
|
||||||
fs.readFile(path, 'utf8', function(err, data) {
|
|
||||||
if (!err) {
|
|
||||||
callback(data);
|
|
||||||
} else {
|
|
||||||
console.log(`${new Date().toISOString()}: error reading sql file: ${JSON.stringify(err)}`);
|
|
||||||
res.send('Failed to read needed SQL file.');
|
|
||||||
}
|
}
|
||||||
});
|
);
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
server.get('/scenario_package', bodyParser.json(), function(req, res) {
|
server.get('/scenario_package', bodyParser.json(), function(req, res) {
|
||||||
|
process_route('GET /scenario_package', 'Get all data for a given scenario.', './route_sql/scenario_package.sql', res,
|
||||||
var path = './route_sql/scenario_package.sql';
|
function(arg) {
|
||||||
console.log(`${new Date().toISOString()}: GET /scenario_package (Get all data for a given scenario)… SQL: ${path} ${"-._.".repeat(20)}`)
|
var where = build_where(req, res);
|
||||||
|
if (!where) return;
|
||||||
fs.readFile(path, 'utf8', function(err, data) {
|
|
||||||
if (!err) {
|
|
||||||
callback(data);
|
|
||||||
} else {
|
|
||||||
console.log(`${new Date().toISOString()}: error reading sql file: ${JSON.stringify(err)}`);
|
|
||||||
res.send('Failed to read needed SQL file.');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
var callback = function(arg) {
|
|
||||||
var where = build_where(req);
|
|
||||||
if (where === '') {
|
|
||||||
res.send("no body was sent");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var sql = arg.replace(new RegExp("where_clause", 'g'), where)
|
var sql = arg.replace(new RegExp("where_clause", 'g'), where)
|
||||||
Postgres.FirstRow(sql, res)
|
Postgres.FirstRow(sql, res)
|
||||||
};
|
}
|
||||||
|
);
|
||||||
})
|
})
|
||||||
|
|
||||||
server.get('/swap_fit', bodyParser.json(), function(req, res) {
|
server.get('/swap_fit', bodyParser.json(), function(req, res) {
|
||||||
|
process_route('GET /swap_fit', 'Obsolete.', './route_sql/swap_fit.sql', res,
|
||||||
|
function(arg) {
|
||||||
|
var where = build_where(req, res);
|
||||||
|
if (!where) return;
|
||||||
|
|
||||||
var path = './route_sql/swap_fit.sql';
|
|
||||||
console.log(`${new Date().toISOString()}: GET /swap_fit (Obsolete.)… SQL: ${path} ${"-._.".repeat(20)}`)
|
|
||||||
|
|
||||||
fs.readFile(path, 'utf8', function(err, data) {
|
|
||||||
if (!err) {
|
|
||||||
callback(data);
|
|
||||||
} else {
|
|
||||||
console.log(`${new Date().toISOString()}: error reading sql file: ${JSON.stringify(err)}`);
|
|
||||||
res.send('Failed to read needed SQL file.');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
var callback = function(arg) {
|
|
||||||
var where = build_where(req);
|
|
||||||
if (where === '') {
|
|
||||||
res.send("no body was sent");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var sql = arg.replace(new RegExp("where_clause", 'g'), where);
|
var sql = arg.replace(new RegExp("where_clause", 'g'), where);
|
||||||
sql = sql.replace(new RegExp("replace_new_mold", 'g'), req.body.new_mold);
|
sql = sql.replace(new RegExp("replace_new_mold", 'g'), req.body.new_mold);
|
||||||
Postgres.FirstRow(sql, res)
|
Postgres.FirstRow(sql, res)
|
||||||
};
|
});
|
||||||
})
|
})
|
||||||
|
|
||||||
server.post('/swap', bodyParser.json(), function(req, res) {
|
server.post('/swap', bodyParser.json(), function(req, res) {
|
||||||
|
process_route('POST /swap', 'Obsolete.', './route_sql/swap_post.sql', res,
|
||||||
|
function(arg) {
|
||||||
|
var where = build_where(req, res);
|
||||||
|
if (!where) return;
|
||||||
|
|
||||||
var path = './route_sql/swap_post.sql';
|
|
||||||
console.log(`${new Date().toISOString()}: POST /swap (Obsolete.)… SQL: ${path} ${"-._.".repeat(20)}`)
|
|
||||||
|
|
||||||
fs.readFile(path, 'utf8', function(err, data) {
|
|
||||||
if (!err) {
|
|
||||||
callback(data);
|
|
||||||
} else {
|
|
||||||
console.log(`${new Date().toISOString()}: error reading sql file: ${JSON.stringify(err)}`);
|
|
||||||
res.send('Failed to read needed SQL file.');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
var callback = function(arg) {
|
|
||||||
var where = build_where(req);
|
|
||||||
if (where === '') {
|
|
||||||
res.send("no body was sent");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var sql = arg.replace(new RegExp("where_clause", 'g'), where);
|
var sql = arg.replace(new RegExp("where_clause", 'g'), where);
|
||||||
sql = sql.replace(new RegExp("swap_doc", 'g'), JSON.stringify(req.body.swap));
|
sql = sql.replace(new RegExp("swap_doc", 'g'), JSON.stringify(req.body.swap));
|
||||||
sql = sql.replace(new RegExp("replace_version", 'g'), req.body.scenario.version);
|
sql = sql.replace(new RegExp("replace_version", 'g'), req.body.scenario.version);
|
||||||
sql = sql.replace(new RegExp("replace_source", 'g'), req.body.source);
|
sql = sql.replace(new RegExp("replace_source", 'g'), req.body.source);
|
||||||
sql = sql.replace(new RegExp("replace_iterdef", 'g'), JSON.stringify(req.body));
|
sql = sql.replace(new RegExp("replace_iterdef", 'g'), JSON.stringify(req.body));
|
||||||
Postgres.FirstRow(sql, res)
|
Postgres.FirstRow(sql, res)
|
||||||
};
|
}
|
||||||
|
);
|
||||||
})
|
})
|
||||||
|
|
||||||
server.post('/cust_swap', bodyParser.json(), function(req, res) {
|
server.post('/cust_swap', bodyParser.json(), function(req, res) {
|
||||||
|
process_route('POST /cust_swap', 'Obsolete.', './route_sql/swap_cust.sql', res,
|
||||||
var path = './route_sql/swap_cust.sql';
|
function(arg) {
|
||||||
console.log(`${new Date().toISOString()}: POST /cust_swap (Obsolete.)… SQL: ${path} ${"-._.".repeat(20)}`)
|
var where = build_where(req, res);
|
||||||
|
if (!where) return;
|
||||||
fs.readFile(path, 'utf8', function(err, data) {
|
|
||||||
if (!err) {
|
|
||||||
callback(data);
|
|
||||||
} else {
|
|
||||||
console.log(`${new Date().toISOString()}: error reading sql file: ${JSON.stringify(err)}`);
|
|
||||||
res.send('Failed to read needed SQL file.');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
var callback = function(arg) {
|
|
||||||
var where = build_where(req);
|
|
||||||
if (where === '') {
|
|
||||||
res.send("no body was sent");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var sql = arg.replace(new RegExp("where_clause", 'g'), where);
|
var sql = arg.replace(new RegExp("where_clause", 'g'), where);
|
||||||
sql = sql.replace(new RegExp("swap_doc", 'g'), JSON.stringify(req.body.swap));
|
sql = sql.replace(new RegExp("swap_doc", 'g'), JSON.stringify(req.body.swap));
|
||||||
@ -215,77 +176,37 @@ server.post('/cust_swap', bodyParser.json(), function(req, res) {
|
|||||||
sql = sql.replace(new RegExp("replace_source", 'g'), req.body.source);
|
sql = sql.replace(new RegExp("replace_source", 'g'), req.body.source);
|
||||||
sql = sql.replace(new RegExp("replace_iterdef", 'g'), JSON.stringify(req.body));
|
sql = sql.replace(new RegExp("replace_iterdef", 'g'), JSON.stringify(req.body));
|
||||||
Postgres.FirstRow(sql, res)
|
Postgres.FirstRow(sql, res)
|
||||||
};
|
}
|
||||||
|
);
|
||||||
})
|
})
|
||||||
|
|
||||||
server.get('/list_changes', bodyParser.json(), function(req, res) {
|
server.get('/list_changes', bodyParser.json(), function(req, res) {
|
||||||
|
process_route('GET /list_changes', 'Get a list of adjustments made to DSM\'s pool.', './route_sql/list_changes.sql', res,
|
||||||
var path = './route_sql/list_changes.sql';
|
function(arg) {
|
||||||
console.log(`${new Date().toISOString()}: GET /list_changes (Get a list of adjustments made to DSM's pool.)… SQL: ${path} ${"-._.".repeat(20)}`)
|
var where = build_where(req, res);
|
||||||
|
if (!where) return;
|
||||||
fs.readFile(path, 'utf8', function(err, data) {
|
|
||||||
if (!err) {
|
|
||||||
callback(data);
|
|
||||||
} else {
|
|
||||||
console.log(`${new Date().toISOString()}: error reading sql file: ${JSON.stringify(err)}`);
|
|
||||||
res.send('Failed to read needed SQL file.');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
var callback = function(arg) {
|
|
||||||
var where = build_where(req);
|
|
||||||
if (where === '') {
|
|
||||||
res.send("no body was sent");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var sql = arg.replace(new RegExp("where_clause", 'g'), where);
|
var sql = arg.replace(new RegExp("where_clause", 'g'), where);
|
||||||
Postgres.FirstRow(sql, res)
|
Postgres.FirstRow(sql, res)
|
||||||
};
|
}
|
||||||
|
);
|
||||||
})
|
})
|
||||||
|
|
||||||
server.get('/undo_change', bodyParser.json(), function(req, res) {
|
server.get('/undo_change', bodyParser.json(), function(req, res) {
|
||||||
|
process_route('GET /undo_change', 'Remove an adjustment from the DSM\'s pool.', './route_sql/undo.sql', res,
|
||||||
var path = './route_sql/undo.sql';
|
function(arg) {
|
||||||
console.log(`${new Date().toISOString()}: GET /undo_change (Remove an adjustment from the DSM's pool.)… SQL: ${path} ${"-._.".repeat(20)}`)
|
|
||||||
|
|
||||||
fs.readFile(path, 'utf8', function(err, data) {
|
|
||||||
if (!err) {
|
|
||||||
callback(data);
|
|
||||||
} else {
|
|
||||||
console.log(`${new Date().toISOString()}: error reading sql file: ${JSON.stringify(err)}`);
|
|
||||||
res.send('Failed to read needed SQL file.');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
var callback = function(arg) {
|
|
||||||
var sql = arg.replace(new RegExp("replace_id", 'g'), JSON.stringify(req.body.logid))
|
var sql = arg.replace(new RegExp("replace_id", 'g'), JSON.stringify(req.body.logid))
|
||||||
Postgres.FirstRow(sql, res)
|
Postgres.FirstRow(sql, res)
|
||||||
};
|
}
|
||||||
|
);
|
||||||
})
|
})
|
||||||
|
|
||||||
//deprecating this route, just use _vp for volume and prive
|
//deprecating this route, just use _vp for volume and prive
|
||||||
/*
|
/*
|
||||||
server.post('/addmonth_v', bodyParser.json(), function(req, res) {
|
server.post('/addmonth_v', bodyParser.json(), function(req, res) {
|
||||||
|
process_route('POST /add_month_v', 'Obsolete.', './route_sql/addmonth_vd.sql', res,
|
||||||
var path = './route_sql/addmonth_vd.sql';
|
function(arg) {
|
||||||
console.log(`${new Date().toISOString()}: POST /add_month_v (Obsolete.)… SQL: ${path} ${"-._.".repeat(20)}`);
|
var where = build_where(req, res);
|
||||||
|
if (!where) return;
|
||||||
fs.readFile(path, 'utf8', function(err, data) {
|
|
||||||
if (!err) {
|
|
||||||
callback(data);
|
|
||||||
} else {
|
|
||||||
console.log(`${new Date().toISOString()}: error reading sql file: ${JSON.stringify(err)}`);
|
|
||||||
res.send('Failed to read needed SQL file.');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
var callback = function(arg) {
|
|
||||||
var where = build_where(req);
|
|
||||||
if (where === '') {
|
|
||||||
res.send("no body was sent");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
req.body.stamp = new Date().toISOString()
|
req.body.stamp = new Date().toISOString()
|
||||||
var sql = arg.replace(new RegExp("scenario = target_scenario", 'g'), where);
|
var sql = arg.replace(new RegExp("scenario = target_scenario", 'g'), where);
|
||||||
@ -296,20 +217,15 @@ server.post('/addmonth_v', bodyParser.json(), function(req, res) {
|
|||||||
sql = sql.replace(new RegExp("replace_iterdef", 'g'), JSON.stringify(req.body));
|
sql = sql.replace(new RegExp("replace_iterdef", 'g'), JSON.stringify(req.body));
|
||||||
Postgres.FirstRow(sql, res)
|
Postgres.FirstRow(sql, res)
|
||||||
}
|
}
|
||||||
|
);
|
||||||
})
|
})
|
||||||
*/
|
*/
|
||||||
|
|
||||||
server.post('/addmonth_vp', bodyParser.json(), function(req, res) {
|
server.post('/addmonth_vp', bodyParser.json(), function(req, res) {
|
||||||
|
process_route('POST /add_month_vp', 'Add volume and pricing for a new month in the forecast.', './route_sql/addmonth_vupd.sql', res,
|
||||||
var path = './route_sql/addmonth_vupd.sql';
|
function(arg) {
|
||||||
console.log(`${new Date().toISOString()}: POST /add_month_vp (Add volume and pricing for a new month in the forecast.)… SQL: ${path} ${"-._.".repeat(20)}`);
|
var where = build_where(req, res);
|
||||||
|
if (!where) return;
|
||||||
var callback = function(arg) {
|
|
||||||
var where = build_where(req);
|
|
||||||
if (where === '') {
|
|
||||||
res.send("no body was sent");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
req.body.stamp = new Date().toISOString()
|
req.body.stamp = new Date().toISOString()
|
||||||
var sql = arg.replace(new RegExp("where_clause", 'g'), where);
|
var sql = arg.replace(new RegExp("where_clause", 'g'), where);
|
||||||
@ -321,28 +237,14 @@ server.post('/addmonth_vp', bodyParser.json(), function(req, res) {
|
|||||||
sql = sql.replace(new RegExp("replace_iterdef", 'g'), JSON.stringify(req.body));
|
sql = sql.replace(new RegExp("replace_iterdef", 'g'), JSON.stringify(req.body));
|
||||||
Postgres.FirstRow(sql, res)
|
Postgres.FirstRow(sql, res)
|
||||||
}
|
}
|
||||||
|
);
|
||||||
fs.readFile(path, 'utf8', function(err, data) {
|
|
||||||
if (!err) {
|
|
||||||
callback(data);
|
|
||||||
} else {
|
|
||||||
console.log(`${new Date().toISOString()}: error reading sql file: ${JSON.stringify(err)}`);
|
|
||||||
res.send('Failed to read needed SQL file.');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
})
|
})
|
||||||
|
|
||||||
server.post('/scale_v', bodyParser.json(), function(req, res) {
|
server.post('/scale_v', bodyParser.json(), function(req, res) {
|
||||||
|
process_route('POST /scale_v', 'Scale the volume for the given scenario.', './route_sql/scale_vd.sql', res,
|
||||||
var path = './route_sql/scale_vd.sql';
|
function(arg) {
|
||||||
console.log(`${new Date().toISOString()}: POST /scale_v (Scale the volume for the given scenario.)… SQL: ${path} ${"-._.".repeat(20)}`);
|
var where = build_where(req, res);
|
||||||
|
if (!where) return;
|
||||||
var callback = function(arg) {
|
|
||||||
var where = build_where(req);
|
|
||||||
if (where === '') {
|
|
||||||
res.send("no body was sent");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
req.body.stamp = new Date().toISOString()
|
req.body.stamp = new Date().toISOString()
|
||||||
var sql = arg.replace(new RegExp("where_clause", 'g'), where);
|
var sql = arg.replace(new RegExp("where_clause", 'g'), where);
|
||||||
@ -352,28 +254,14 @@ server.post('/scale_v', bodyParser.json(), function(req, res) {
|
|||||||
sql = sql.replace(new RegExp("replace_iterdef", 'g'), JSON.stringify(req.body));
|
sql = sql.replace(new RegExp("replace_iterdef", 'g'), JSON.stringify(req.body));
|
||||||
Postgres.FirstRow(sql, res)
|
Postgres.FirstRow(sql, res)
|
||||||
}
|
}
|
||||||
|
);
|
||||||
fs.readFile(path, 'utf8', function(err, data) {
|
|
||||||
if (!err) {
|
|
||||||
callback(data);
|
|
||||||
} else {
|
|
||||||
console.log(`${new Date().toISOString()}: error reading sql file: ${JSON.stringify(err)}`);
|
|
||||||
res.send('Failed to read needed SQL file.');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
})
|
})
|
||||||
|
|
||||||
server.post('/scale_p', bodyParser.json(), function(req, res) {
|
server.post('/scale_p', bodyParser.json(), function(req, res) {
|
||||||
|
process_route('POST /scale_p', 'Scale price for the given scenario.', './route_sql/scale_pd.sql', res,
|
||||||
var path = './route_sql/scale_pd.sql';
|
function(arg) {
|
||||||
console.log(`${new Date().toISOString()}: POST /scale_p (Scale price for the given scenario.)… SQL: ${path} ${"-._.".repeat(20)}`);
|
var where = build_where(req, res);
|
||||||
|
if (!where) return;
|
||||||
var callback = function(arg) {
|
|
||||||
var where = build_where(req);
|
|
||||||
if (where === '') {
|
|
||||||
res.send("no body was sent");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
req.body.stamp = new Date().toISOString()
|
req.body.stamp = new Date().toISOString()
|
||||||
var sql = arg.replace(new RegExp("where_clause", 'g'), where);
|
var sql = arg.replace(new RegExp("where_clause", 'g'), where);
|
||||||
@ -383,28 +271,14 @@ server.post('/scale_p', bodyParser.json(), function(req, res) {
|
|||||||
sql = sql.replace(new RegExp("replace_iterdef", 'g'), JSON.stringify(req.body));
|
sql = sql.replace(new RegExp("replace_iterdef", 'g'), JSON.stringify(req.body));
|
||||||
Postgres.FirstRow(sql, res)
|
Postgres.FirstRow(sql, res)
|
||||||
}
|
}
|
||||||
|
);
|
||||||
fs.readFile(path, 'utf8', function(err, data) {
|
|
||||||
if (!err) {
|
|
||||||
callback(data);
|
|
||||||
} else {
|
|
||||||
console.log(`${new Date().toISOString()}: error reading sql file: ${JSON.stringify(err)}`);
|
|
||||||
res.send('Failed to read needed SQL file.');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
})
|
})
|
||||||
|
|
||||||
server.post('/scale_vp', bodyParser.json(), function(req, res) {
|
server.post('/scale_vp', bodyParser.json(), function(req, res) {
|
||||||
|
process_route('POST /scale_vp', 'Scale volume and price for the given scenario.', './route_sql/scale_vupd.sql', res,
|
||||||
var path = './route_sql/scale_vupd.sql';
|
function(arg) {
|
||||||
console.log(`${new Date().toISOString()}: POST /scale_vp (Scale volume and price for the given scenario.)… SQL: ${path} ${"-._.".repeat(20)}`);
|
var where = build_where(req, res);
|
||||||
|
if (!where) return;
|
||||||
var callback = function(arg) {
|
|
||||||
var where = build_where(req);
|
|
||||||
if (where === '') {
|
|
||||||
res.send("no body was sent");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
req.body.stamp = new Date().toISOString()
|
req.body.stamp = new Date().toISOString()
|
||||||
var sql = arg.replace(new RegExp("where_clause", 'g'), where);
|
var sql = arg.replace(new RegExp("where_clause", 'g'), where);
|
||||||
@ -415,28 +289,14 @@ server.post('/scale_vp', bodyParser.json(), function(req, res) {
|
|||||||
sql = sql.replace(new RegExp("replace_iterdef", 'g'), JSON.stringify(req.body));
|
sql = sql.replace(new RegExp("replace_iterdef", 'g'), JSON.stringify(req.body));
|
||||||
Postgres.FirstRow(sql, res)
|
Postgres.FirstRow(sql, res)
|
||||||
}
|
}
|
||||||
|
);
|
||||||
fs.readFile(path, 'utf8', function(err, data) {
|
|
||||||
if (!err) {
|
|
||||||
callback(data);
|
|
||||||
} else {
|
|
||||||
console.log(`${new Date().toISOString()}: error reading sql file: ${JSON.stringify(err)}`);
|
|
||||||
res.send('Failed to read needed SQL file.');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
})
|
})
|
||||||
|
|
||||||
server.post('/scale_vp_sales', bodyParser.json(), function(req, res) {
|
server.post('/scale_vp_sales', bodyParser.json(), function(req, res) {
|
||||||
|
process_route('POST /scale_vp_sales', 'Obsolete.', './route_sql/scale_vupd.sql', res,
|
||||||
var path = './route_sql/scale_vupd.sql';
|
function(arg) {
|
||||||
console.log(`${new Date().toISOString()}: POST /scale_vp_sales (Obsolete.)… SQL: ${path} ${"-._.".repeat(20)}`);
|
var where = build_where(req, res);
|
||||||
|
if (!where) return;
|
||||||
var callback = function(arg) {
|
|
||||||
var where = build_where(req);
|
|
||||||
if (where === '') {
|
|
||||||
res.send("no body was sent");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
req.body.stamp = new Date().toISOString()
|
req.body.stamp = new Date().toISOString()
|
||||||
var sql = arg.replace(new RegExp("where_clause", 'g'), where);
|
var sql = arg.replace(new RegExp("where_clause", 'g'), where);
|
||||||
@ -447,28 +307,14 @@ server.post('/scale_vp_sales', bodyParser.json(), function(req, res) {
|
|||||||
sql = sql.replace(new RegExp("replace_iterdef", 'g'), JSON.stringify(req.body));
|
sql = sql.replace(new RegExp("replace_iterdef", 'g'), JSON.stringify(req.body));
|
||||||
Postgres.FirstRow(sql, res)
|
Postgres.FirstRow(sql, res)
|
||||||
}
|
}
|
||||||
|
);
|
||||||
fs.readFile(path, 'utf8', function(err, data) {
|
|
||||||
if (!err) {
|
|
||||||
callback(data);
|
|
||||||
} else {
|
|
||||||
console.log(`${new Date().toISOString()}: error reading sql file: ${JSON.stringify(err)}`);
|
|
||||||
res.send('Failed to read needed SQL file.');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
})
|
})
|
||||||
|
|
||||||
server.post('/new_part', bodyParser.json(), function(req, res) {
|
server.post('/new_part', bodyParser.json(), function(req, res) {
|
||||||
|
process_route('POST /new_part', 'Obsolete.', './route_sql/new_part.sql', res,
|
||||||
var path = './route_sql/new_part.sql';
|
function(arg) {
|
||||||
console.log(`${new Date().toISOString()}: POST /new_part (Obsolete.)… SQL: ${path} ${"-._.".repeat(20)}`);
|
var where = build_where(req, res);
|
||||||
|
if (!where) return;
|
||||||
var callback = function(arg) {
|
|
||||||
var where = build_where(req);
|
|
||||||
if (where === '') {
|
|
||||||
res.send("no body was sent");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
req.body.stamp = new Date().toISOString()
|
req.body.stamp = new Date().toISOString()
|
||||||
var sql = arg.replace(new RegExp("where_clause", 'g'), where);
|
var sql = arg.replace(new RegExp("where_clause", 'g'), where);
|
||||||
@ -480,30 +326,16 @@ server.post('/new_part', bodyParser.json(), function(req, res) {
|
|||||||
sql = sql.replace(new RegExp("replace_iterdef", 'g'), JSON.stringify(req.body));
|
sql = sql.replace(new RegExp("replace_iterdef", 'g'), JSON.stringify(req.body));
|
||||||
Postgres.FirstRow(sql, res)
|
Postgres.FirstRow(sql, res)
|
||||||
}
|
}
|
||||||
|
);
|
||||||
fs.readFile(path, 'utf8', function(err, data) {
|
|
||||||
if (!err) {
|
|
||||||
callback(data);
|
|
||||||
} else {
|
|
||||||
console.log(`${new Date().toISOString()}: error reading sql file: ${JSON.stringify(err)}`);
|
|
||||||
res.send('Failed to read needed SQL file.');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
})
|
})
|
||||||
|
|
||||||
server.post('/new_basket', bodyParser.json(), function(req, res) {
|
server.post('/new_basket', bodyParser.json(), function(req, res) {
|
||||||
|
process_route('POST /new_basket', 'Add new part and/or customer.', './route_sql/new_basket.sql', res,
|
||||||
var path = './route_sql/new_basket.sql';
|
function(arg) {
|
||||||
console.log(`${new Date().toISOString()}: POST /new_basket (Add new part and/or customer.)… SQL: ${path} ${"-._.".repeat(20)}`);
|
|
||||||
|
|
||||||
var callback = function(arg) {
|
|
||||||
req.body.scenario.iter.push("adj volume"); //intercept the request body and force in a "adj volume" at position 1, only a "copy" iteration is being used
|
req.body.scenario.iter.push("adj volume"); //intercept the request body and force in a "adj volume" at position 1, only a "copy" iteration is being used
|
||||||
|
|
||||||
var where = build_where(req);
|
var where = build_where(req, res);
|
||||||
if (where === '') {
|
if (!where) return;
|
||||||
res.send("no body was sent");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
req.body.stamp = new Date().toISOString()
|
req.body.stamp = new Date().toISOString()
|
||||||
var sql = arg.replace(new RegExp("where_clause", 'g'), where);
|
var sql = arg.replace(new RegExp("where_clause", 'g'), where);
|
||||||
@ -515,52 +347,5 @@ server.post('/new_basket', bodyParser.json(), function(req, res) {
|
|||||||
sql = sql.replace(new RegExp("replace_iterdef", 'g'), JSON.stringify(req.body));
|
sql = sql.replace(new RegExp("replace_iterdef", 'g'), JSON.stringify(req.body));
|
||||||
Postgres.FirstRow(sql, res)
|
Postgres.FirstRow(sql, res)
|
||||||
}
|
}
|
||||||
|
);
|
||||||
fs.readFile(path, 'utf8', function(err, data) {
|
|
||||||
if (!err) {
|
|
||||||
callback(data);
|
|
||||||
} else {
|
|
||||||
console.log(`${new Date().toISOString()}: error reading sql file: ${JSON.stringify(err)}`);
|
|
||||||
res.send('Failed to read needed SQL file.');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
})
|
})
|
||||||
|
|
||||||
function build_where(req) {
|
|
||||||
console.log(`${new Date().toISOString()}: Building WHERE from ${JSON.stringify(req.body)}`);
|
|
||||||
var where = '';
|
|
||||||
var clauseCount = 1;
|
|
||||||
for (var i in req.body.scenario) {
|
|
||||||
if (clauseCount > 1) {
|
|
||||||
where = where +
|
|
||||||
`
|
|
||||||
AND `;
|
|
||||||
}
|
|
||||||
if (Array.isArray(req.body.scenario[i])) {
|
|
||||||
//if the scenario key has a value that is an array of items, push it into an `IN` statement
|
|
||||||
//iter = [stage1, stage2] --> SQL --> iter IN ('stag1', stage2')
|
|
||||||
var listCount = 1;
|
|
||||||
where = where + i + " IN (";
|
|
||||||
for (var j in req.body.scenario[i]) {
|
|
||||||
if (listCount > 1) {
|
|
||||||
where = where + ",";
|
|
||||||
}
|
|
||||||
where = where + "'" + req.body.scenario[i][j] + "'";
|
|
||||||
listCount = listCount + 1;
|
|
||||||
}
|
|
||||||
where = where + ")";
|
|
||||||
} else {
|
|
||||||
where = where + i + " = '" + req.body.scenario[i] + "'";
|
|
||||||
}
|
|
||||||
clauseCount = clauseCount + 1;
|
|
||||||
};
|
|
||||||
//if the request has a stamp, then it's an adjustment
|
|
||||||
//prevent making adjustment to actual history by limiting to current stamp and beyond
|
|
||||||
if (req.body.stamp) {
|
|
||||||
where = where +
|
|
||||||
`
|
|
||||||
AND order_date >= least('` + req.body.stamp + "'::date,'2021-06-01')";
|
|
||||||
}
|
|
||||||
console.log(`${new Date().toISOString()}: Returning ${where}`);
|
|
||||||
return where;
|
|
||||||
}
|
|
||||||
|
|||||||
@ -14,7 +14,7 @@ from
|
|||||||
inner join rlarp.osm_pool on
|
inner join rlarp.osm_pool on
|
||||||
id = logid
|
id = logid
|
||||||
WHERE
|
WHERE
|
||||||
quota_rep_descr = 'replace_user'
|
where_clause
|
||||||
AND tag NOT IN ('Initial Build','last price')
|
AND tag NOT IN ('Initial Build','last price')
|
||||||
group BY
|
group BY
|
||||||
ol.doc->>'user'
|
ol.doc->>'user'
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user