From 77fe829ece861cfbaf9a491ab13b617306493e28 Mon Sep 17 00:00:00 2001 From: Trowbridge Date: Wed, 4 Mar 2020 17:53:50 -0500 Subject: [PATCH] posting a part swap --- index.js | 43 +++++++ route_sql/swap_post.sql | 247 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 290 insertions(+) create mode 100644 route_sql/swap_post.sql diff --git a/index.js b/index.js index f068a1f..d1278b3 100644 --- a/index.js +++ b/index.js @@ -174,6 +174,49 @@ server.get('/swap_fit', bodyParser.json(), function(req, res) { }; }) +server.post('/swap', bodyParser.json(), function(req, res) { + + var sql = ""; + var w = ""; + var c = 1; + var d = 1; + var args = []; + var path = './route_sql/swap_post.sql'; + + fs.readFile(path, 'utf8', function(err, data) { + if (!err) { + callback(data); + } else { + console.log("fatal error pulling sql file") + callback(err); + } + }); + + var callback = function(arg) { + sql = arg; + + //parse request body into a where clause + ({ c, w, d } = build_where(req, c, w, d, args)); + + //if there was no body sent, return with nothing + if (c == 1) { + res.send("no body was sent"); + return; + } + console.log(new Date().toISOString() + "-------------------------get swap fit:------------------------------") + console.log(req.body); + //parse the where clause into the main sql statement + sql = sql.replace(new RegExp("where_clause", 'g'), w); + sql = sql.replace(new RegExp("swap_doc", 'g'), JSON.stringify(req.body.swap)); + 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)); + //execute the sql and send the result + console.log(sql); + Postgres.FirstRow(sql, [], res) + }; +}) + server.get('/list_changes', bodyParser.json(), function(req, res) { var sql = ""; diff --git a/route_sql/swap_post.sql b/route_sql/swap_post.sql new file mode 100644 index 0000000..f126d61 --- /dev/null +++ b/route_sql/swap_post.sql @@ -0,0 +1,247 @@ +WITH +target AS (SELECT $$swap_doc$$::jsonb swap) +,pl AS ( + SELECT + x.* + FROM + TARGET + LEFT JOIN LATERAL jsonb_to_recordset(target.swap->'rows') AS x(original text, sales numeric, replace text, fit text)ON TRUE +) +--select * from pl +,seg AS ( + SELECT + x.glec + ,x.segm + FROM + ( + VALUES + ('1CU','Retail'), + ('1GR','Greenhouse'), + ('1NU','Nursery'), + ('1RE','Retail'), + ('2WI','Greenhouse'), + ('3BM','Other'), + ('3CO','Other'), + ('3PE','Other'), + ('3PP','Other'), + ('4CO','Other'), + ('4RA','Other'), + ('9MI','Other'), + ('9SA','Other'), + ('9TO','Other') + ) x(glec, segm) +) +,log AS ( + INSERT INTO rlarp.osm_log(doc) SELECT $$replace_iterdef$$::jsonb doc RETURNING * +) +,remove AS ( + SELECT + fspr + ,plnt ---master data + ,promo --history date mix + ,terms + ,bill_cust_descr --history cust mix + ,ship_cust_descr --history cust mix + ,dsm + ,quota_rep_descr --master data + ,director + ,billto_group --master data + ,shipto_group + ,chan --master data + ,chansub + ,chan_retail + ,part + ,part_descr + ,part_group + ,branding + ,majg_descr + ,ming_descr + ,majs_descr + ,mins_descr + ,segm + ,substance + ,fs_line --master data + ,r_currency --history cust mix + ,r_rate --master data + ,c_currency --master data + ,c_rate --master data + ,-units units + ,-value_loc value_loc + ,-value_usd value_usd + ,-cost_loc cost_loc + ,-cost_usd cost_usd + ,calc_status --0 + ,flag --0 + ,order_date --history date mix + ,order_month + ,order_season + ,request_date --history date mix + ,request_month + ,request_season + ,ship_date --history date mix + ,ship_month + ,ship_season + ,'replace_version' "version" + ,'replace_source'||' volume' iter + ,log.id + ,COALESCE(log.doc->>'tag','') "tag" + ,log.doc->>'message' "comment" + ,log.doc->>'type' module + FROM + rlarp.osm_pool o + CROSS JOIN log + INNER JOIN pl ON + pl.original = o.part + WHERE + -----------------scenario---------------------------- + where_clause +) +,repl AS ( + SELECT + fspr + ,plnt ---master data + ,promo --history date mix + ,terms + ,bill_cust_descr --history cust mix + ,ship_cust_descr --history cust mix + ,dsm + ,quota_rep_descr --master data + ,director + ,billto_group --master data + ,shipto_group + ,chan --master data + ,chansub + ,chan_retail + ,pl.replace part + ,m.item || ' - ' || m.descr partd + ,substring(pl.replace,1,8) part_group + ,m.branding branding + ,m.majgd majg_descr + ,m.mingd ming_descr + ,m.majsd majs_descr + ,m.minsd mins_descr + ,seg.segm + ,substance + ,fs_line --master data + ,r_currency --history cust mix + ,r_rate --master data + ,c_currency --master data + ,c_rate --master data + ,-units units + ,-value_loc value_loc + ,-value_usd value_usd + ,-cost_loc cost_loc + ,-cost_usd cost_usd + ,calc_status --0 + ,flag --0 + ,order_date --history date mix + ,order_month + ,order_season + ,request_date --history date mix + ,request_month + ,request_season + ,ship_date --history date mix + ,ship_month + ,ship_season + ,o."version" + ,o.iter + ,o.id + ,o."tag" + ,o."comment" + ,o.module + FROM + remove o + INNER JOIN pl ON + pl.original = o.part + INNER JOIN rlarp.itemmv m ON + m.item = o.part + LEFT OUTER JOIN seg ON + seg.glec = m.glec +) +,ins AS ( + INSERT INTO rlarp.osm_pool SELECT * FROM remove UNION ALL SELECT * FROM repl RETURNING * +) +,insagg AS ( + SELECT + ---------customer info----------------- + bill_cust_descr + ,billto_group + ,ship_cust_descr + ,shipto_group + ,quota_rep_descr + ,director + ,segm + ,substance + ,chan + ,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 + ,logid + ,tag + ,comment + --------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 + ins + GROUP BY + ---------customer info----------------- + bill_cust_descr + ,billto_group + ,ship_cust_descr + ,shipto_group + ,quota_rep_descr + ,director + ,segm + ,substance + ,chan + ,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 + ,logid + ,tag + ,comment +) +SELECT json_agg(row_to_json(insagg)) x from insagg +