From dd8ba2736e55f5f791b48140970f484e47fe5fbb Mon Sep 17 00:00:00 2001 From: Trowbridge Date: Tue, 12 Mar 2019 15:03:21 -0400 Subject: [PATCH] modify the get_pool route --- index.js | 106 ++++++++--------------------------------- route_sql/get_pool.sql | 80 +++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 87 deletions(-) create mode 100644 route_sql/get_pool.sql diff --git a/index.js b/index.js index 0abfc51..f3e4a03 100644 --- a/index.js +++ b/index.js @@ -51,94 +51,26 @@ server.get('/test_sql', function(req, res){ server.get('/get_pool', bodyParser.json(), function (req, res) { + var sql = ""; var args = [req.body.quota_rep]; - //------------------------------------------set base SQL------------------------------------ - var w = - ` - WITH rows AS ( - 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 - ,iter - ) - SELECT - json_agg(row_to_json(rows)) x - FROM - rows`; - //-----------------replace default quota_rep---------------------------------------------------- - console.log(w); - Postgres.FirstRow(w, args, res) - //res.json("hi") + var path = './route_sql/get_pool.sql'; + var callback = function(arg){ + sql = arg; + console.log(req.body.quota_rep); + sql = sql.replace("rep_replace",req.body.quota_rep); + 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); + } + }); + }) server.get('/scenario_package', bodyParser.json(), function (req, res) { diff --git a/route_sql/get_pool.sql b/route_sql/get_pool.sql new file mode 100644 index 0000000..3422c57 --- /dev/null +++ b/route_sql/get_pool.sql @@ -0,0 +1,80 @@ +WITH rows AS ( +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 = 'rep_replace' +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 \ No newline at end of file