#!/usr/bin/env node require('dotenv').config(); const express = require('express'); //var https = require('https'); var bodyParser = require('body-parser'); const server = express(); const pg = require('pg'); var fs = require('fs'); var readline = require('readline'); //-----------------------------point to ssl info------------------------------------ //var options = { // key: fs.readFileSync(process.env.wd + 'key.pem'), // cert: fs.readFileSync(process.env.wd + 'cert.pem'), // passprase: [] //}; //-----------------------------create server process-------------------------------- //https.createServer( /*options,*/ server).listen(process.env.nodeport, () => { server.listen(process.env.nodeport, () => { console.log('started on ' + process.env.nodeport) }); //-----------------------------create permanent database connetion------------------ var Postgres = new pg.Client({ user: process.env.user, password: process.env.password, host: process.env.host, port: process.env.port, database: process.env.database, ssl: false, application_name: "ps_api" }); Postgres.connect(); const SQLTemplate = (inSql, inJSON) => { for (key in inJSON) { inSql = inSql.replace(new RegExp(key, 'g'), inJSON[key]); } return inSql; }; const build_where = (req, c, w) => { //loop through each top level item expected to be a simple key/value list reflecting the column and the target value // "part":"XFRM500", "customer":"Sanford and Son" --> SQL --> part = 'XFRM500' // AND customer = 'Sanford and Son' var d = 1; var args = []; for (var i in req.body.app_scenario) { //console.log(i); ///console.log(req.body[i]); //this step applies the AND seperator only if (c > 1) { w = w + ` AND `; } if (Array.isArray(req.body.app_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') w = w + i + " IN ("; for (var j in req.body.app_scenario[i]) { if (d > 1) { w = w + ","; } w = w + "'" + req.body.app_scenario[i][j] + "'"; d = d + 1; } w = w + ")"; } else { w = w + i + " = '" + req.body.app_scenario[i] + "'"; } args.push(req.body.app_scenario[i]); c = c + 1; }; return { c, w }; } //------------route to test if the process is running------------------------------- server.get('/', (req, res) => res.send('pivotscale api is running')) //------------build forecast baseline that is a mirror of the target period--------- server.get('/baseline', bodyParser.json(), function(req, res) { var sql = ""; var path = './routes/baseline/baseline.sql'; var args = []; fs.readFile(path, 'utf8', function(err, fileContents) { if (err) { console.log("fatal error pulling sql file"); return; } console.log(new Date().toISOString() + "-------------------------baseline build-----------------------------") sql = SQLTemplate(fileContents, { ...req.body, app_req: JSON.stringify(req.body) }); Postgres.query(sql, []).then((result, error) => { res.json(result[1].rows[0]); }); }); }); //------------scale a selected slice by the specified amounts----------------------- server.get('/scale', bodyParser.json(), function(req, res) { var sql = ""; var where = ""; //holds the where var check = 1; //flag if body is empty var path = './routes/scale/scale.sql'; fs.readFile(path, 'utf8', function(err, data) { if (err) { console.log("fatal error pulling sql file") return } ({ c: check, w: where } = build_where(req, check, where)); //if there was no body sent, return with nothing //if (c == 1) { // res.send("no body was sent"); // return; //} console.log(new Date().toISOString() + "-------------------------scale type adjustment----------------------") let sql = SQLTemplate(data, { app_req: JSON.stringify(req.body), app_forecast_name: req.body.app_forecast_name, app_where: where }); //execute the sql and send the result Postgres.query(sql, []).then((result, error) => { res.json(result.rows[0]); }); }); });