2019-03-25 14:32:51 -04:00
#!/usr/bin/env node
2019-01-31 16:32:34 -05:00
require ( 'dotenv' ) . config ( ) ;
2019-01-31 14:19:42 -05:00
const express = require ( 'express' ) ;
2019-03-18 17:27:01 -04:00
var https = require ( 'https' ) ;
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-03-18 17:27:01 -04:00
var options = {
2019-03-27 12:50:09 -04:00
key : fs . readFileSync ( process . env . wd + 'key.pem' ) ,
cert : fs . readFileSync ( process . env . wd + 'cert.pem' ) ,
2019-03-18 17:27:01 -04:00
passprase : [ ]
} ;
2020-02-14 02:24:52 -05:00
https . createServer ( options , server ) . listen ( process . env . nodeport , ( ) => {
console . log ( 'started on ' + process . env . nodeport )
2019-03-25 14:32:51 -04:00
} ) ;
2019-03-18 17:27:01 -04:00
//server.listen(3000, () => console.log('started'))
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 ( ) ;
2020-02-14 02:24:52 -05:00
Postgres . FirstRow = function ( inSQL , args , inResponse ) {
2019-03-01 15:31:31 -05:00
Postgres . query ( inSQL , args , ( err , res ) => {
if ( err === null ) {
2019-01-31 16:32:34 -05:00
inResponse . json ( res . rows [ 0 ] ) ;
return ;
}
2019-03-12 15:31:24 -04:00
console . log ( err . stack ) ;
2019-01-31 16:32:34 -05:00
inResponse . json ( err . stack ) ;
2020-02-14 02:24:52 -05:00
} ) ;
2019-01-31 16:32:34 -05:00
} ;
2019-03-14 10:47:29 -04:00
server . get ( '/' , ( req , res ) => res . send ( 'node.js express is up and running' ) )
2019-03-05 23:57:31 -05:00
2019-03-27 12:50:09 -04:00
server . get ( '/login' , ( req , res ) => res . sendFile ( process . env . wd + 'msauth.html' ) )
2019-03-05 23:57:31 -05:00
2019-04-03 09:56:01 -04:00
server . get ( '/logs' , ( req , res ) => res . sendFile ( process . env . wd + 'changes.log' ) )
2019-03-14 00:49:33 -04:00
2019-04-03 09:56:01 -04:00
server . get ( '/pgbadger' , ( req , res ) => res . sendFile ( process . env . wd + 'logs.html' ) )
server . get ( '/totals' , ( req , res ) => res . sendFile ( process . env . wd + 'totals.log' ) )
2019-03-14 00:49:33 -04:00
2020-02-14 02:24:52 -05:00
server . get ( '/test_sql' , function ( req , res ) {
2019-03-06 05:54:30 -05:00
var path = './route_meta/scenario_package.sql'
2020-02-14 02:24:52 -05:00
var callback = function ( arg ) {
2019-03-05 23:57:31 -05:00
res . send ( arg )
} ;
2020-02-14 02:24:52 -05:00
fs . readFile ( path , 'utf8' , function ( err , data ) {
if ( ! err ) {
2019-03-05 23:57:31 -05:00
callback ( data ) ;
} else {
callback ( err ) ;
}
} ) ;
} ) ;
2020-02-14 02:24:52 -05:00
server . get ( '/get_pool' , bodyParser . json ( ) , function ( req , res ) {
2019-03-05 23:58:32 -05:00
2019-03-12 15:03:21 -04:00
var sql = "" ;
2019-03-05 23:58:32 -05:00
var args = [ req . body . quota _rep ] ;
2019-03-12 15:03:21 -04:00
var path = './route_sql/get_pool.sql' ;
2020-02-14 02:24:52 -05:00
var callback = function ( arg ) {
2019-03-12 15:03:21 -04:00
sql = arg ;
2019-03-25 02:44:40 -04:00
console . log ( new Date ( ) . toISOString ( ) + "-------------------------get pool:----------------------------" ) ;
2019-03-12 15:03:21 -04:00
console . log ( req . body . quota _rep ) ;
2020-02-14 02:24:52 -05:00
sql = sql . replace ( "rep_replace" , req . body . quota _rep ) ;
2020-02-14 14:41:29 -05:00
console . log ( sql ) ;
2020-02-14 02:24:52 -05:00
Postgres . FirstRow ( sql , [ ] , res )
2019-03-12 15:03:21 -04:00
} ;
2020-02-14 02:24:52 -05:00
fs . readFile ( path , 'utf8' , function ( err , data ) {
if ( ! err ) {
2019-03-12 15:03:21 -04:00
callback ( data ) ;
} else {
console . log ( "fatal error pulling sql file" )
callback ( err ) ;
}
} ) ;
2019-03-05 23:58:32 -05:00
} )
2020-02-14 02:24:52 -05:00
server . get ( '/scenario_package' , bodyParser . json ( ) , function ( req , res ) {
2019-03-01 15:37:00 -05:00
2019-03-12 15:31:24 -04:00
var sql = "" ;
2019-03-01 15:37:00 -05:00
var w = "" ;
var c = 1 ;
2019-03-13 13:41:54 -04:00
var d = 1 ;
2019-03-01 15:37:00 -05:00
var args = [ ] ;
2019-03-12 15:31:24 -04:00
var path = './route_sql/scenario_package.sql' ;
2019-03-01 15:37:00 -05:00
2020-02-14 02:24:52 -05:00
fs . readFile ( path , 'utf8' , function ( err , data ) {
if ( ! err ) {
2019-03-13 13:41:54 -04:00
callback ( data ) ;
} else {
console . log ( "fatal error pulling sql file" )
callback ( err ) ;
}
} ) ;
2020-02-14 02:24:52 -05:00
var callback = function ( arg ) {
2019-03-12 15:31:24 -04:00
sql = arg ;
2020-02-14 02:24:52 -05:00
2019-03-12 15:31:24 -04:00
//parse request body into a where clause
2019-03-13 13:41:54 -04:00
( { c , w , d } = build _where ( req , c , w , d , args ) ) ;
2019-03-12 15:31:24 -04:00
//if there was no body sent, return with nothing
if ( c == 1 ) {
res . send ( "no body was sent" ) ;
return ;
2019-03-01 15:37:00 -05:00
}
2019-03-25 02:44:40 -04:00
console . log ( new Date ( ) . toISOString ( ) + "-------------------------get scenario:------------------------------" )
2019-03-13 13:41:54 -04:00
console . log ( req . body ) ;
2019-03-12 15:31:24 -04:00
//parse the where clause into the main sql statement
2020-02-14 02:24:52 -05:00
sql = sql . replace ( new RegExp ( "where_clause" , 'g' ) , w )
//execute the sql and send the result
2020-02-14 17:00:06 -05:00
console . log ( sql ) ;
2020-02-14 02:24:52 -05:00
Postgres . FirstRow ( sql , [ ] , res )
2019-03-01 15:37:00 -05:00
} ;
} )
2020-03-13 10:48:26 -04:00
server . get ( '/swap_fit' , bodyParser . json ( ) , function ( req , res ) {
var sql = "" ;
var w = "" ;
var c = 1 ;
var d = 1 ;
var args = [ ] ;
var path = './route_sql/swap_fit.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 ( "replace_new_mold" , 'g' ) , req . body . new _mold ) ;
//execute the sql and send the result
console . log ( sql ) ;
Postgres . FirstRow ( sql , [ ] , 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 )
} ;
} )
2020-05-12 16:55:37 -04:00
server . post ( '/cust_swap' , bodyParser . json ( ) , function ( req , res ) {
var sql = "" ;
var w = "" ;
var c = 1 ;
var d = 1 ;
var args = [ ] ;
var path = './route_sql/swap_cust.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 ) ;
res . json ( null ) ;
//Postgres.FirstRow(sql, [], res)
} ;
} )
2020-02-14 02:24:52 -05:00
server . get ( '/list_changes' , bodyParser . json ( ) , function ( req , res ) {
2019-03-22 14:02:03 -04:00
var sql = "" ;
var w = "" ;
var c = 1 ;
var d = 1 ;
var args = [ ] ;
var path = './route_sql/list_changes.sql' ;
2020-02-14 02:24:52 -05:00
fs . readFile ( path , 'utf8' , function ( err , data ) {
if ( ! err ) {
2019-03-22 14:02:03 -04:00
callback ( data ) ;
} else {
console . log ( "fatal error pulling sql file" )
callback ( err ) ;
}
} ) ;
2020-02-14 02:24:52 -05:00
var callback = function ( arg ) {
2019-03-22 14:02:03 -04:00
sql = arg ;
2020-02-14 02:24:52 -05:00
2020-04-02 09:55:53 -04:00
//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 ;
}
2020-02-14 02:24:52 -05:00
console . log ( new Date ( ) . toISOString ( ) + "-------------------------list changes:------------------------------" )
2019-03-22 14:02:03 -04:00
console . log ( req . body ) ;
//parse the where clause into the main sql statement
2020-04-02 09:55:53 -04:00
sql = sql . replace ( new RegExp ( "where_clause" , 'g' ) , w )
2020-02-14 02:24:52 -05:00
//execute the sql and send the result
console . log ( sql ) ;
Postgres . FirstRow ( sql , [ ] , res )
2019-03-22 14:02:03 -04:00
} ;
2020-02-14 14:41:29 -05:00
} )
server . get ( '/undo_change' , bodyParser . json ( ) , function ( req , res ) {
var sql = "" ;
var w = "" ;
var c = 1 ;
var d = 1 ;
var args = [ ] ;
var path = './route_sql/undo.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 ;
console . log ( new Date ( ) . toISOString ( ) + "-------------------------undo change:------------------------------" )
console . log ( req . body ) ;
//parse the where clause into the main sql statement
sql = sql . replace ( new RegExp ( "replace_id" , 'g' ) , JSON . stringify ( req . body . logid ) )
//execute the sql and send the result
console . log ( sql ) ;
Postgres . FirstRow ( sql , [ ] , res )
} ;
2019-03-22 14:02:03 -04:00
} )
2020-02-16 22:34:06 -05:00
//deprecating this route, just use _vp for volume and prive
/ *
2020-02-14 02:24:52 -05:00
server . post ( '/addmonth_v' , bodyParser . json ( ) , function ( req , res ) {
2019-03-06 05:54:30 -05:00
2019-03-12 17:19:19 -04:00
var sql = "" ;
2019-03-06 05:54:30 -05:00
var w = "" ;
2019-03-13 13:07:07 -04:00
var c = 1 ; //counts iterations through each scaenario key
var d = 1 ; //counts cycles in scenario key values which are arrays
2019-03-06 05:54:30 -05:00
var args = [ ] ;
2019-03-12 17:19:19 -04:00
var path = './route_sql/addmonth_vd.sql' ;
2019-03-13 13:24:34 -04:00
2020-02-14 02:24:52 -05:00
fs . readFile ( path , 'utf8' , function ( err , data ) {
if ( ! err ) {
2019-03-13 13:24:34 -04:00
callback ( data ) ;
} else {
console . log ( "fatal error pulling sql file" )
callback ( err ) ;
}
} ) ;
2020-02-14 02:24:52 -05:00
var callback = function ( arg ) {
2019-03-13 13:18:41 -04:00
sql = arg ;
//buile where clause expression
( { c , w , d } = build _where ( req , c , w , d , args ) ) ;
2019-03-13 13:24:34 -04:00
2019-03-12 17:19:19 -04:00
if ( c == 1 ) {
res . send ( "no body was sent" ) ;
return ;
2019-03-06 09:53:33 -05:00
}
2020-02-14 02:24:52 -05:00
console . log ( new Date ( ) . toISOString ( ) + "-----------------------------add month volume:---------------------------------" ) ;
2019-03-22 14:39:16 -04:00
req . body . stamp = new Date ( ) . toISOString ( )
2019-03-12 17:19:19 -04:00
console . log ( req . body ) ;
//console.log(args);
2020-02-14 02:24:52 -05:00
sql = sql . replace ( new RegExp ( "scenario = target_scenario" , 'g' ) , w ) ;
sql = sql . replace ( new RegExp ( "target_increment" , 'g' ) , req . body . qty ) ;
sql = sql . replace ( new RegExp ( "target_month" , 'g' ) , req . body . month ) ;
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 ) ) ;
2019-03-13 13:07:07 -04:00
console . log ( sql )
2020-02-14 02:24:52 -05:00
Postgres . FirstRow ( sql , [ ] , res )
2019-03-06 09:53:33 -05:00
}
2019-03-06 10:10:02 -05:00
} )
2020-02-16 22:34:06 -05:00
* /
2019-03-06 10:10:02 -05:00
2020-02-14 02:24:52 -05:00
server . post ( '/addmonth_vp' , bodyParser . json ( ) , function ( req , res ) {
2019-03-12 17:40:20 -04:00
var sql = "" ;
var w = "" ;
var c = 1 ;
2019-03-13 13:28:40 -04:00
var d = 1 ;
2019-03-12 17:40:20 -04:00
var args = [ ] ;
var path = './route_sql/addmonth_vupd.sql' ;
2020-02-14 02:24:52 -05:00
var callback = function ( arg ) {
2019-03-13 13:53:19 -04:00
sql = arg ;
2019-03-12 17:40:20 -04:00
2019-03-13 13:28:40 -04:00
( { c , w , d } = build _where ( req , c , w , d , args ) ) ;
2019-03-12 17:40:20 -04:00
if ( c == 1 ) {
res . send ( "no body was sent" ) ;
return ;
}
2020-02-14 02:24:52 -05:00
console . log ( new Date ( ) . toISOString ( ) + "------------------add month volume and price:-------------------" ) ;
2019-03-22 14:39:16 -04:00
req . body . stamp = new Date ( ) . toISOString ( )
2019-03-12 17:40:20 -04:00
console . log ( req . body ) ;
//console.log(args);
2020-02-14 02:24:52 -05:00
sql = sql . replace ( new RegExp ( "where_clause" , 'g' ) , w ) ;
sql = sql . replace ( new RegExp ( "target_volume" , 'g' ) , req . body . qty ) ;
sql = sql . replace ( new RegExp ( "target_price" , 'g' ) , req . body . amount ) ;
sql = sql . replace ( new RegExp ( "target_month" , 'g' ) , req . body . month ) ;
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 ) ) ;
2019-03-20 00:37:36 -04:00
console . log ( sql ) ;
2020-02-14 02:24:52 -05:00
Postgres . FirstRow ( sql , [ ] , res )
2019-03-12 17:40:20 -04:00
}
2020-02-14 02:24:52 -05:00
fs . readFile ( path , 'utf8' , function ( err , data ) {
if ( ! err ) {
2019-03-12 17:40:20 -04:00
callback ( data ) ;
} else {
console . log ( "fatal error pulling sql file" )
callback ( err ) ;
}
} ) ;
} )
2020-02-14 02:24:52 -05:00
server . post ( '/scale_v' , bodyParser . json ( ) , function ( req , res ) {
2019-03-13 09:35:45 -04:00
var sql = "" ;
var w = "" ;
var c = 1 ;
2019-03-13 13:28:40 -04:00
var d = 1 ;
2019-03-13 09:35:45 -04:00
var args = [ ] ;
var path = './route_sql/scale_vd.sql' ;
2020-02-14 02:24:52 -05:00
var callback = function ( arg ) {
2019-03-13 13:53:19 -04:00
sql = arg ;
2019-03-13 09:35:45 -04:00
2019-03-13 13:28:40 -04:00
( { c , w , d } = build _where ( req , c , w , d , args ) ) ;
2019-03-13 09:35:45 -04:00
if ( c == 1 ) {
res . send ( "no body was sent" ) ;
return ;
}
2020-02-14 02:24:52 -05:00
console . log ( new Date ( ) . toISOString ( ) + "-----------------------scale volume:------------------------------" ) ;
2019-03-22 14:39:16 -04:00
req . body . stamp = new Date ( ) . toISOString ( )
2019-03-13 09:35:45 -04:00
console . log ( req . body ) ;
//console.log(args);
2020-02-14 02:24:52 -05:00
sql = sql . replace ( new RegExp ( "where_clause" , 'g' ) , w ) ;
sql = sql . replace ( new RegExp ( "incr_qty" , 'g' ) , req . body . qty ) ;
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 ) ) ;
2019-03-20 00:37:36 -04:00
console . log ( sql ) ;
2020-02-14 02:24:52 -05:00
Postgres . FirstRow ( sql , [ ] , res )
2019-03-13 09:35:45 -04:00
}
2020-02-14 02:24:52 -05:00
fs . readFile ( path , 'utf8' , function ( err , data ) {
if ( ! err ) {
2019-03-13 09:35:45 -04:00
callback ( data ) ;
} else {
console . log ( "fatal error pulling sql file" )
callback ( err ) ;
}
} ) ;
} )
2020-02-14 02:24:52 -05:00
server . post ( '/scale_p' , bodyParser . json ( ) , function ( req , res ) {
2019-03-13 09:35:45 -04:00
var sql = "" ;
var w = "" ;
var c = 1 ;
2019-03-13 13:28:40 -04:00
var d = 1 ;
2019-03-13 09:35:45 -04:00
var args = [ ] ;
var path = './route_sql/scale_pd.sql' ;
2020-02-14 02:24:52 -05:00
var callback = function ( arg ) {
2019-03-13 13:53:19 -04:00
sql = arg ;
2019-03-13 09:35:45 -04:00
2019-03-13 13:28:40 -04:00
( { c , w , d } = build _where ( req , c , w , d , args ) ) ;
2019-03-13 09:35:45 -04:00
if ( c == 1 ) {
res . send ( "no body was sent" ) ;
return ;
}
2020-02-14 02:24:52 -05:00
console . log ( new Date ( ) . toISOString ( ) + "--------------------scale price:-------------------" ) ;
2019-03-22 14:39:16 -04:00
req . body . stamp = new Date ( ) . toISOString ( )
2019-03-13 09:35:45 -04:00
console . log ( req . body ) ;
//console.log(args);
2020-02-14 02:24:52 -05:00
sql = sql . replace ( new RegExp ( "where_clause" , 'g' ) , w ) ;
sql = sql . replace ( new RegExp ( "target_increment" , 'g' ) , req . body . amount ) ;
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 ) ) ;
2019-03-20 00:37:36 -04:00
console . log ( sql ) ;
2020-02-14 02:24:52 -05:00
Postgres . FirstRow ( sql , [ ] , res )
2019-03-13 09:35:45 -04:00
}
2020-02-14 02:24:52 -05:00
fs . readFile ( path , 'utf8' , function ( err , data ) {
if ( ! err ) {
2019-03-13 09:35:45 -04:00
callback ( data ) ;
} else {
console . log ( "fatal error pulling sql file" )
callback ( err ) ;
}
} ) ;
} )
2020-02-14 02:24:52 -05:00
server . post ( '/scale_vp' , bodyParser . json ( ) , function ( req , res ) {
2019-03-13 09:44:41 -04:00
var sql = "" ;
var w = "" ;
var c = 1 ;
2019-03-13 13:28:40 -04:00
var d = 1 ;
2019-03-13 09:44:41 -04:00
var args = [ ] ;
var path = './route_sql/scale_vupd.sql' ;
2020-02-14 02:24:52 -05:00
var callback = function ( arg ) {
2019-03-13 13:53:19 -04:00
sql = arg ;
2019-03-13 09:44:41 -04:00
2019-03-13 13:28:40 -04:00
( { c , w , d } = build _where ( req , c , w , d , args ) ) ;
2019-03-13 09:44:41 -04:00
if ( c == 1 ) {
res . send ( "no body was sent" ) ;
return ;
}
2020-02-14 02:24:52 -05:00
console . log ( new Date ( ) . toISOString ( ) + "--------------------scale volume & price:-------------------" ) ;
2019-03-22 14:39:16 -04:00
req . body . stamp = new Date ( ) . toISOString ( )
2019-03-13 09:44:41 -04:00
console . log ( req . body ) ;
//console.log(args);
2020-02-14 02:24:52 -05:00
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_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 ) ) ;
2019-03-20 00:37:36 -04:00
console . log ( sql ) ;
2020-02-14 02:24:52 -05:00
Postgres . FirstRow ( sql , [ ] , res )
2019-03-13 09:44:41 -04:00
}
2020-02-14 02:24:52 -05:00
fs . readFile ( path , 'utf8' , function ( err , data ) {
if ( ! err ) {
2019-03-13 09:44:41 -04:00
callback ( data ) ;
} else {
console . log ( "fatal error pulling sql file" )
callback ( err ) ;
}
} ) ;
} )
2020-02-14 02:24:52 -05:00
server . post ( '/new_part' , bodyParser . json ( ) , function ( req , res ) {
2019-03-21 15:29:17 -04:00
var sql = "" ;
var w = "" ;
var c = 1 ;
var d = 1 ;
var args = [ ] ;
var path = './route_sql/new_part.sql' ;
2020-02-14 02:24:52 -05:00
var callback = function ( arg ) {
2019-03-21 15:29:17 -04:00
sql = arg ;
( { c , w , d } = build _where ( req , c , w , d , args ) ) ;
if ( c == 1 ) {
res . send ( "no body was sent" ) ;
return ;
}
2020-02-14 02:24:52 -05:00
console . log ( new Date ( ) . toISOString ( ) + "--------------------new part:-------------------" ) ;
2019-03-22 14:39:16 -04:00
req . body . stamp = new Date ( ) . toISOString ( )
2019-03-22 04:49:35 -04:00
console . log ( req . body ) ;
//console.log(args);
2020-02-14 02:24:52 -05:00
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 ) ) ;
2019-03-22 04:49:35 -04:00
console . log ( sql ) ;
2020-02-14 02:24:52 -05:00
Postgres . FirstRow ( sql , [ ] , res )
2019-03-22 04:49:35 -04:00
}
2020-02-14 02:24:52 -05:00
fs . readFile ( path , 'utf8' , function ( err , data ) {
if ( ! err ) {
2019-03-22 04:49:35 -04:00
callback ( data ) ;
} else {
console . log ( "fatal error pulling sql file" )
callback ( err ) ;
}
} ) ;
} )
2020-02-14 02:24:52 -05:00
server . post ( '/new_basket' , bodyParser . json ( ) , function ( req , res ) {
2019-03-22 04:49:35 -04:00
var sql = "" ;
var w = "" ;
var c = 1 ;
var d = 1 ;
var args = [ ] ;
var path = './route_sql/new_basket.sql' ;
2020-02-14 02:24:52 -05:00
var callback = function ( arg ) {
2019-03-22 04:49:35 -04:00
sql = arg ;
2020-03-19 12:59:17 -04:00
req . body . scenario . iter . push ( "adj volume" ) ; //intercept the request body and force in a "adj volume" at position 1, only a "copy" iteration is being used
2019-03-22 04:49:35 -04:00
( { c , w , d } = build _where ( req , c , w , d , args ) ) ;
if ( c == 1 ) {
res . send ( "no body was sent" ) ;
return ;
}
2020-02-14 02:24:52 -05:00
console . log ( new Date ( ) . toISOString ( ) + "--------------------new basket:-------------------" ) ;
2019-03-22 14:39:16 -04:00
req . body . stamp = new Date ( ) . toISOString ( )
2019-03-21 15:29:17 -04:00
console . log ( req . body ) ;
//console.log(args);
2020-02-14 02:24:52 -05:00
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 ) ) ;
2019-03-21 15:29:17 -04:00
console . log ( sql ) ;
2020-02-14 02:24:52 -05:00
Postgres . FirstRow ( sql , [ ] , res )
2019-03-21 15:29:17 -04:00
}
2020-02-14 02:24:52 -05:00
fs . readFile ( path , 'utf8' , function ( err , data ) {
if ( ! err ) {
2019-03-21 15:29:17 -04:00
callback ( data ) ;
} else {
console . log ( "fatal error pulling sql file" )
callback ( err ) ;
}
} ) ;
} )
2019-03-13 13:18:41 -04:00
function build _where ( req , c , w , d , args ) {
for ( var i in req . body . scenario ) {
//console.log(i);
///console.log(req.body[i]);
if ( c > 1 ) {
w = w +
`
AND ` ;
}
if ( Array . isArray ( req . body . scenario [ i ] ) ) {
//if the scenario key has a value that is an array of items, push it into an `IN` statement
//iter = [stage1, stage2] --> SQL --> iter IN ('stag1', stage2')
w = w + i + " IN (" ;
for ( var j in req . body . scenario [ i ] ) {
if ( d > 1 ) {
w = w + "," ;
}
w = w + "'" + req . body . scenario [ i ] [ j ] + "'" ;
d = d + 1 ;
}
w = w + ")" ;
2020-02-14 02:24:52 -05:00
} else {
2019-03-13 13:18:41 -04:00
w = w + i + " = '" + req . body . scenario [ i ] + "'" ;
}
args . push ( req . body . scenario [ i ] ) ;
c = c + 1 ;
2020-02-14 02:24:52 -05:00
} ;
2019-03-13 13:18:41 -04:00
return { c , w , d } ;
2020-02-14 02:24:52 -05:00
}