forecast_api/index.js

180 lines
4.3 KiB
JavaScript
Raw Normal View History

2019-01-31 16:32:34 -05:00
require('dotenv').config();
2019-01-31 14:19:42 -05:00
const express = require('express');
var bodyParser = require('body-parser');
2019-01-31 14:19:42 -05:00
const server = express();
2019-01-31 16:32:34 -05:00
const pg = require('pg');
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: "osm_api"
});
Postgres.connect();
Postgres.FirstRow = function(inSQL,args, inResponse)
{
Postgres.query(inSQL,args, (err, res) => {
if (err === null)
{
inResponse.json(res.rows[0]);
return;
}
inResponse.json(err.stack);
});
};
server.get('/list_sources', function (req,res) {
var sql = "select jsonb_agg(defn) from tps.srce";
console.log(req.query);
Postgres.FirstRow(sql,[],res);
})
server.get('/monthly_orders', bodyParser.json(), function (req,res) {
var w = "";
var c = 1;
var args = [];
for (var i in req.body) {
//console.log(i);
///console.log(req.body[i]);
if (c > 1) {
w = w +
`
AND `
}
w = w + i + " = '" + req.body[i] + "'";
args.push(req.body[i]);
c = c + 1;
};
if (c == 1) {
res.send("no body was sent");
return;
}
//console.log(w);
//console.log(args);
w =
`
WITH
ini AS (
SELECT
oseas
,extract('month' from odate) monthn
,sum(qty) qty
,sum(sales) sales
,sum(stdcost) stdcost
FROM
rlarp.osm_ppfa_varto_mv
WHERE
` + w +
`
2019-02-15 00:22:13 -05:00
AND VERSION = 'Actual'
GROUP BY
oseas
,extract('month' from odate)
)
SELECT
jsonb_agg(row_to_json(ini)::jsonb)
FROM
ini`
console.log(w);
Postgres.FirstRow(w,[],res)
//res.json("hi")
2019-02-11 14:38:31 -05:00
})
2019-02-27 14:20:17 -05:00
server.get('/get_pool', bodyParser.json(), function (req,res) {
var args = [req.body.quota_rep];
//------------------------------------------set base SQL------------------------------------
var w =
`
2019-02-27 14:23:31 -05:00
WITH rows AS (
2019-02-27 14:20:17 -05:00
SELECT
---------customer info-----------------
bill_cust_descr
,billto_group
,ship_cust_descr
,shipto_group
,quota_rep_descr
,director_descr
,segm
,mod_chan
,mod_chansub
---------product info------------------
,majg_descr
,ming_descr
,majs_descr
,mins_descr
,brand
,part_family
,part_group
,branding
,color
,part_descr
---------dates-------------------------
,order_season
,order_month
,ship_season
,ship_month
,request_season
,request_month
,promo
,version
,iter
--------values-------------------------
,sum(value_loc) value_loc
,sum(value_usd) value_usd
,sum(cost_loc) cost_loc
,sum(cost_usd) cost_usd
,sum(units) units
FROM
rlarp.osm_fcpool
WHERE
quota_rep_descr = $1
GROUP BY
---------customer info-----------------
bill_cust_descr
,billto_group
,ship_cust_descr
,shipto_group
,quota_rep_descr
,director_descr
,segm
,mod_chan
,mod_chansub
---------product info------------------
,majg_descr
,ming_descr
,majs_descr
,mins_descr
,brand
,part_family
,part_group
,branding
,color
,part_descr
---------dates-------------------------
,order_season
,order_month
,ship_season
,ship_month
,request_season
,request_month
,promo
,version
2019-02-27 14:23:31 -05:00
,iter
)
SELECT row_to_json(rows) x FROM rows`;
2019-02-27 14:20:17 -05:00
//-----------------replace default quota_rep----------------------------------------------------
console.log(w);
Postgres.FirstRow(w,args,res)
//res.json("hi")
})
server.get('/', (req,res) => res.send('node.js express is running: 🎉'))
2019-01-31 14:19:42 -05:00
server.listen(3000,() => console.log('started'))