2019-01-31 16:32:34 -05:00
|
|
|
require('dotenv').config();
|
2019-01-31 14:19:42 -05:00
|
|
|
const express = require('express');
|
2019-02-13 10:37:28 -05:00
|
|
|
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');
|
|
|
|
|
2019-03-01 15:31:31 -05:00
|
|
|
//---------read sql files into variables----------------
|
|
|
|
var fs = require('fs');
|
|
|
|
var readline = require('readline');
|
|
|
|
//-------------------------------------------------------
|
|
|
|
|
2019-01-31 16:32:34 -05:00
|
|
|
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();
|
|
|
|
|
2019-03-01 15:31:31 -05:00
|
|
|
Postgres.FirstRow = function (inSQL, args, inResponse) {
|
|
|
|
Postgres.query(inSQL, args, (err, res) => {
|
|
|
|
if (err === null) {
|
2019-01-31 16:32:34 -05:00
|
|
|
inResponse.json(res.rows[0]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
inResponse.json(err.stack);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-03-01 15:31:31 -05:00
|
|
|
server.get('/scenario_totals', bodyParser.json(), function (req, res) {
|
2019-02-13 10:37:28 -05:00
|
|
|
|
|
|
|
var w = "";
|
|
|
|
var c = 1;
|
|
|
|
var args = [];
|
2019-02-28 01:48:42 -05:00
|
|
|
|
|
|
|
for (var i in req.body) {
|
|
|
|
//console.log(i);
|
|
|
|
///console.log(req.body[i]);
|
|
|
|
if (c > 1) {
|
2019-03-01 15:31:31 -05:00
|
|
|
w = w +
|
|
|
|
`
|
2019-02-28 01:48:42 -05:00
|
|
|
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);
|
2019-03-01 15:31:31 -05:00
|
|
|
w =
|
|
|
|
`
|
2019-02-28 01:48:42 -05:00
|
|
|
WITH
|
|
|
|
ini AS (
|
|
|
|
SELECT
|
|
|
|
order_season
|
|
|
|
,version
|
|
|
|
,iter
|
|
|
|
,SUM(units) units
|
|
|
|
,SUM(value_usd) value_usd
|
|
|
|
FROM
|
|
|
|
rlarp.osm_fcpool
|
|
|
|
WHERE
|
|
|
|
` + w +
|
|
|
|
`
|
|
|
|
GROUP BY
|
|
|
|
order_season
|
|
|
|
,version
|
|
|
|
,iter
|
|
|
|
)
|
|
|
|
SELECT
|
|
|
|
jsonb_agg(row_to_json(ini)::jsonb) x
|
|
|
|
FROM
|
|
|
|
ini`
|
|
|
|
console.log(w);
|
2019-03-01 15:31:31 -05:00
|
|
|
Postgres.FirstRow(w, [], res)
|
2019-02-28 01:48:42 -05:00
|
|
|
//res.json("hi")
|
|
|
|
})
|
|
|
|
|
2019-03-01 15:37:00 -05:00
|
|
|
server.get('/scenario_package', 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 =
|
|
|
|
`
|
2019-03-02 00:30:29 -05:00
|
|
|
WITH
|
|
|
|
mseq AS (
|
|
|
|
SELECT * FROM
|
|
|
|
(
|
|
|
|
VALUES
|
|
|
|
('Jun',1)
|
|
|
|
,('Jul',2)
|
|
|
|
,('Aug',3)
|
|
|
|
,('Sep',4)
|
|
|
|
,('Oct',5)
|
|
|
|
,('Nov',6)
|
|
|
|
,('Dec',7)
|
|
|
|
,('Jan',8)
|
|
|
|
,('Feb',9)
|
|
|
|
,('Mar',10)
|
|
|
|
,('Apr',11)
|
|
|
|
,('May',12)
|
|
|
|
|
|
|
|
) x(m,s)
|
|
|
|
)
|
|
|
|
--select * from mseq
|
|
|
|
,base AS (
|
2019-03-01 15:37:00 -05:00
|
|
|
SELECT
|
|
|
|
order_season
|
|
|
|
,order_month
|
|
|
|
,version
|
|
|
|
,iter
|
|
|
|
,part_descr
|
|
|
|
,bill_cust_descr
|
|
|
|
,ship_cust_descr
|
|
|
|
,SUM(units) units
|
|
|
|
,SUM(value_usd) value_usd
|
|
|
|
FROM
|
|
|
|
rlarp.osm_fcpool
|
|
|
|
WHERE
|
|
|
|
`+ w +
|
|
|
|
`GROUP BY
|
2019-03-02 00:30:29 -05:00
|
|
|
order_season
|
|
|
|
,order_month
|
|
|
|
,version
|
|
|
|
,iter
|
|
|
|
,part_descr
|
|
|
|
,bill_cust_descr
|
|
|
|
,ship_cust_descr
|
|
|
|
)
|
|
|
|
,months AS (
|
|
|
|
SELECT
|
2019-03-01 15:37:00 -05:00
|
|
|
order_season
|
2019-03-02 00:30:29 -05:00
|
|
|
,version
|
|
|
|
,iter
|
2019-03-01 15:37:00 -05:00
|
|
|
,order_month
|
2019-03-02 00:30:29 -05:00
|
|
|
,mseq.s seq
|
|
|
|
,sum(units) units
|
|
|
|
,sum(value_usd) value_usd
|
|
|
|
FROM
|
|
|
|
base
|
|
|
|
INNER JOIN mseq ON
|
|
|
|
mseq.m = base.order_month
|
|
|
|
GROUP BY
|
|
|
|
order_season
|
2019-03-01 15:37:00 -05:00
|
|
|
,version
|
|
|
|
,iter
|
2019-03-02 00:30:29 -05:00
|
|
|
,order_month
|
|
|
|
,s
|
|
|
|
)
|
|
|
|
,mpvt AS (
|
|
|
|
SELECT
|
|
|
|
order_month
|
|
|
|
,seq
|
|
|
|
,SUM(units) FILTER (WHERE order_season = 2019) "2019 qty"
|
|
|
|
,SUM(units) FILTER (WHERE order_season = 2020 AND iter = 'copy') "2020 base qty"
|
|
|
|
,SUM(units) FILTER (WHERE order_season = 2020 AND iter = 'adjustment') "2020 adj qty"
|
|
|
|
,SUM(units) FILTER (WHERE order_season = 2020 AND iter IN ('copy','adjustment')) "2020 tot qty"
|
|
|
|
,SUM(value_usd) FILTER (WHERE order_season = 2019) "2019 value_usd"
|
|
|
|
,SUM(value_usd) FILTER (WHERE order_season = 2020 AND iter = 'copy') "2020 base value_usd"
|
|
|
|
,SUM(value_usd) FILTER (WHERE order_season = 2020 AND iter = 'adjustment') "2020 adj value_usd"
|
|
|
|
,SUM(value_usd) FILTER (WHERE order_season = 2020 AND iter IN ('copy','adjustment')) "2020 tot value_usd"
|
|
|
|
FROM
|
|
|
|
months
|
|
|
|
GROUP BY
|
|
|
|
order_month
|
|
|
|
,seq
|
|
|
|
ORDER BY
|
|
|
|
seq ASC
|
|
|
|
)
|
2019-03-02 00:52:47 -05:00
|
|
|
,mlist AS (
|
|
|
|
SELECT
|
|
|
|
mseq.m order_month
|
|
|
|
,"2019 qty"
|
|
|
|
,"2020 base qty"
|
|
|
|
,"2020 adj qty"
|
|
|
|
,"2020 tot qty"
|
|
|
|
,"2019 value_usd"
|
|
|
|
,"2020 base value_usd"
|
|
|
|
,"2020 adj value_usd"
|
|
|
|
,"2020 tot value_usd"
|
|
|
|
FROM
|
|
|
|
mseq
|
|
|
|
LEFT OUTER JOIN mpvt ON
|
|
|
|
mpvt.order_month = mseq.m
|
|
|
|
ORDER BY
|
|
|
|
mseq.s ASC
|
|
|
|
)
|
2019-03-02 00:30:29 -05:00
|
|
|
,totals AS (
|
|
|
|
SELECT
|
|
|
|
order_season
|
|
|
|
,version
|
|
|
|
,iter
|
|
|
|
,sum(units) units
|
|
|
|
,sum(value_usd) value_usd
|
|
|
|
FROM
|
|
|
|
months
|
|
|
|
GROUP BY
|
|
|
|
order_season
|
|
|
|
,version
|
|
|
|
,iter
|
|
|
|
)
|
|
|
|
SELECT
|
|
|
|
jsonb_build_object(
|
|
|
|
'months'
|
|
|
|
,(SELECT jsonb_agg(row_to_json(months)::jsonb) FROM months)
|
|
|
|
,'mpvt'
|
2019-03-02 00:54:17 -05:00
|
|
|
,(SELECT jsonb_agg(row_to_json(mlist)::jsonb) FROM mlist)
|
2019-03-02 00:30:29 -05:00
|
|
|
,'base'
|
|
|
|
,(SELECT jsonb_agg(row_to_json(base)::jsonb) FROM base)
|
|
|
|
,'totals'
|
|
|
|
,(SELECT jsonb_agg(row_to_json(totals)::jsonb) FROM totals)
|
|
|
|
) package`
|
2019-03-01 15:37:00 -05:00
|
|
|
console.log(w);
|
|
|
|
Postgres.FirstRow(w, [], res)
|
|
|
|
//res.json("hi")
|
|
|
|
})
|
|
|
|
|
2019-03-01 15:31:31 -05:00
|
|
|
server.get('/get_pool', bodyParser.json(), function (req, res) {
|
2019-02-27 14:20:17 -05:00
|
|
|
|
|
|
|
var args = [req.body.quota_rep];
|
|
|
|
//------------------------------------------set base SQL------------------------------------
|
2019-03-01 15:31:31 -05:00
|
|
|
var w =
|
|
|
|
`
|
2019-02-27 14:23:31 -05:00
|
|
|
WITH rows AS (
|
2019-02-27 14:26:28 -05:00
|
|
|
SELECT
|
2019-02-27 14:20:17 -05:00
|
|
|
---------customer info-----------------
|
2019-02-27 14:26:28 -05:00
|
|
|
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
|
2019-02-27 16:26:05 -05:00
|
|
|
quota_rep_descr = $1
|
2019-02-27 14:26:28 -05:00
|
|
|
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
|
|
|
|
,iter
|
|
|
|
)
|
|
|
|
SELECT
|
|
|
|
json_agg(row_to_json(rows)) x
|
|
|
|
FROM
|
|
|
|
rows`;
|
2019-02-27 14:20:17 -05:00
|
|
|
//-----------------replace default quota_rep----------------------------------------------------
|
|
|
|
console.log(w);
|
2019-03-01 15:31:31 -05:00
|
|
|
Postgres.FirstRow(w, args, res)
|
2019-02-27 14:20:17 -05:00
|
|
|
//res.json("hi")
|
|
|
|
})
|
|
|
|
|
2019-03-01 15:31:31 -05:00
|
|
|
server.get('/', (req, res) => res.send('node.js express is running: 🎉'))
|
|
|
|
|
|
|
|
|
|
|
|
server.get('/test_sql', function(req, res){
|
|
|
|
var path = './route_meta/scenario_totals.sql'
|
|
|
|
var callback = function(arg){
|
|
|
|
res.send(arg)
|
|
|
|
};
|
|
|
|
|
|
|
|
fs.readFile(path, 'utf8', function(err, data){
|
|
|
|
if (!err){
|
|
|
|
callback(data);
|
|
|
|
} else {
|
|
|
|
callback(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
2019-01-31 14:19:42 -05:00
|
|
|
|
2019-03-01 15:31:31 -05:00
|
|
|
server.listen(3000, () => console.log('started'))
|