From bb83caa46f66eccbe9bef0a930b3fdc5ad8923dd Mon Sep 17 00:00:00 2001 From: Trowbridge Date: Thu, 21 Mar 2019 15:29:17 -0400 Subject: [PATCH] parameterize sql and add route for new part --- index.js | 45 ++++++++++++++++++++++++++++++++++++++++-- route_sql/new_part.sql | 20 ++++++------------- 2 files changed, 49 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index cd609eb..1360aa1 100644 --- a/index.js +++ b/index.js @@ -330,6 +330,48 @@ server.post('/scale_vp', bodyParser.json(), function (req, res) { }); }) +server.post('/new_part', bodyParser.json(), function (req, res) { + + var sql = ""; + var w = ""; + var c = 1; + var d = 1; + var args = []; + var path = './route_sql/new_part.sql'; + + var callback = function(arg){ + sql = arg; + + ({ c, w, d } = build_where(req, c, w, d, args)); + + if (c == 1) { + res.send("no body was sent"); + return; + } + console.log("--------------------scale volume & price:-------------------"); + console.log(req.body); + //console.log(args); + sql = sql.replace(new RegExp("where_clause",'g'),w); + sql = sql.replace(new RegExp("target_vol",'g'),req.body.qty); + sql = sql.replace(new RegExp("target_prc",'g'),req.body.amount); + sql = sql.replace(new RegExp("replace_request",'g'),JSON.stringify(req.body)); + 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_iterdef",'g'),JSON.stringify(req.body)); + console.log(sql); + Postgres.FirstRow(sql,[],res) + } + + fs.readFile(path, 'utf8', function(err, data){ + if (!err){ + callback(data); + } else { + console.log("fatal error pulling sql file") + callback(err); + } + }); +}) + function build_where(req, c, w, d, args) { for (var i in req.body.scenario) { //console.log(i); @@ -360,5 +402,4 @@ function build_where(req, c, w, d, args) { } ; return { c, w, d }; -} - +} \ No newline at end of file diff --git a/route_sql/new_part.sql b/route_sql/new_part.sql index a659b00..847f63a 100644 --- a/route_sql/new_part.sql +++ b/route_sql/new_part.sql @@ -2,7 +2,7 @@ WITH /* the volume must be expressed in terms of units, since that is what it will be scaling */ -target AS (select '{"scenario":{"quota_rep_descr":"10032 - BRYAN HILL","part_group":"XRD16002 - 16 CM SP COEX POT 4 SLOT WHBK","version":"b20","iter":["copy","adj volume","adj price"]},"stamp":"2019-03-21 11:31:57","user":"Trowbridge, Paul","source":"adj","type":"new_part","months":{"Oct":{"amount":25080,"qty":264000},"Nov":{"amount":69540,"qty":732000},"Dec":{"amount":950,"qty":10000},"Jan":{"amount":21850,"qty":230000},"Feb":{"amount":19380,"qty":204000},"Mar":{"amount":20790.621864,"qty":218848.6512},"Apr":{"amount":53297.1118899,"qty":561022.23042}},"newpart":"XNT0T1G0X19B118PYAUE - 01.00 PW SS T1G TALL CX WH/BK"}'::json def) +target AS (select $$replace_request$$::json def) --select * from newm ,GLD AS ( SELECT @@ -179,11 +179,7 @@ target AS (select '{"scenario":{"quota_rep_descr":"10032 - BRYAN HILL","part_gro mseq.m = order_month WHERE -----------------scenario---------------------------- - --where_clause - quota_rep_descr = '10032 - BRYAN HILL' - AND part_group = 'XRD16002 - 16 CM SP COEX POT 4 SLOT WHBK' - AND version = 'b20' - AND iter in ('copy','short ship','bad_ship') + where_clause -----------------additional params------------------- AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments AND adj_orderdate <= adj_shipdate @@ -399,11 +395,7 @@ SELECT rlarp.osm_fcpool WHERE -----------------scenario---------------------------- - --where_clause - quota_rep_descr = '10032 - BRYAN HILL' - AND part_group = 'XRD16002 - 16 CM SP COEX POT 4 SLOT WHBK' - AND version = 'b20' - AND iter in ('copy','short ship','bad_ship') + where_clause -----------------additional params------------------- AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments AND adj_orderdate <= adj_shipdate @@ -519,10 +511,10 @@ SELECT ,make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag adj_requestdate ,make_date(mxm.yr + 2020,mxm.cal,mxm.odom) + rlag + slag adj_shipdate ,'replace_version' "version" - ,'replace_source'||' volume' iter + ,'replace_source'||' new part' iter -----------------------ui columns-------------------------------- ,'replace_iterdet' iterdet - ,$${"replace_iterdef":""}$$::json iterdef + ,$$replace_iterdef$$::json iterdef ,od.ssyr order_season ,to_char(make_date(mxm.yr + 2020,mxm.cal,mxm.odom),'Mon') order_month ,sd.ssyr ship_season @@ -808,4 +800,4 @@ GROUP BY ,iter ) --SELECT * FROM insagg -SELECT json_agg(row_to_json(insagg)) x from insagg +SELECT json_agg(row_to_json(insagg)) x from insagg \ No newline at end of file