2018-06-23 17:10:20 -04:00
|
|
|
require('dotenv').config();
|
|
|
|
var express = require('express');
|
|
|
|
var handlebars = require('express-handlebars');
|
|
|
|
var bodyParser = require('body-parser');
|
|
|
|
var cookieParser = require('cookie-parser');
|
|
|
|
var mult = require('multer');
|
|
|
|
var upload = mult({ encoding: "utf8" });
|
|
|
|
var csvtojson = require('csvtojson');
|
|
|
|
var pg = require('pg');
|
|
|
|
|
|
|
|
var server = express();
|
|
|
|
server.engine('handlebars', handlebars());
|
|
|
|
server.set('view engine', 'handlebars');
|
2018-06-24 14:12:34 -04:00
|
|
|
|
|
|
|
server.use(function(inReq, inRes, inNext)
|
|
|
|
{
|
|
|
|
inRes.header("Access-Control-Allow-Origin", "*");
|
|
|
|
inRes.header("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS");
|
|
|
|
inRes.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
|
|
|
|
inNext();
|
2018-06-23 17:31:25 -04:00
|
|
|
});
|
2018-06-23 17:10:20 -04:00
|
|
|
|
2018-06-24 14:12:34 -04:00
|
|
|
|
2018-06-23 17:10:20 -04: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,
|
2018-09-03 16:58:31 -04:00
|
|
|
ssl: false,
|
2018-06-26 00:33:07 -04:00
|
|
|
application_name: "tps_etl_api"
|
2018-06-23 17:31:25 -04:00
|
|
|
});
|
2018-06-26 00:33:07 -04:00
|
|
|
Postgres.FirstRow = function(inSQL,args, inResponse)
|
2018-06-24 14:12:34 -04:00
|
|
|
{
|
2018-06-26 00:33:07 -04:00
|
|
|
Postgres.query(inSQL,args, (err, res) => {
|
2018-06-24 14:12:34 -04:00
|
|
|
if (err === null)
|
|
|
|
{
|
|
|
|
inResponse.json(res.rows[0]);
|
2018-06-23 17:10:20 -04:00
|
|
|
return;
|
|
|
|
}
|
2018-09-27 02:39:14 -04:00
|
|
|
inResponse.json(err.stack);
|
2018-06-23 17:10:20 -04:00
|
|
|
});
|
2018-06-24 14:12:34 -04:00
|
|
|
};
|
|
|
|
Postgres.connect();
|
2018-06-23 17:10:20 -04:00
|
|
|
|
2018-06-28 00:40:28 -04:00
|
|
|
//----------------------------------------------------------source definitions-------------------------------------------------------------------------------------------------------------------------
|
2018-06-23 17:10:20 -04:00
|
|
|
|
2018-06-26 00:33:07 -04:00
|
|
|
//returns array of all sources
|
2018-06-24 14:12:34 -04:00
|
|
|
server.get("/source", function (inReq, inRes)
|
|
|
|
{
|
|
|
|
var sql = "SELECT jsonb_agg(defn) source_list FROM tps.srce";
|
2018-06-26 00:33:07 -04:00
|
|
|
Postgres.FirstRow(sql,[], inRes);
|
2018-06-24 14:12:34 -04:00
|
|
|
});
|
2018-06-26 00:33:07 -04:00
|
|
|
//returns message about status and error description
|
2018-06-24 14:12:34 -04:00
|
|
|
server.post("/source", bodyParser.json(), function (inReq, inRes)// remove body parsing, just pass post body to the sql string build
|
|
|
|
{
|
2018-06-26 00:33:07 -04:00
|
|
|
var sql = "SELECT x.message FROM tps.srce_set($1::jsonb) as x(message)";
|
|
|
|
Postgres.FirstRow(sql,[JSON.stringify(inReq.body)], inRes);
|
2018-06-24 14:12:34 -04:00
|
|
|
});
|
2018-06-23 17:10:20 -04:00
|
|
|
|
2018-06-28 00:40:28 -04:00
|
|
|
//----------------------------------------------------------regex instrUctions-------------------------------------------------------------------------------------------------------------------------
|
2018-06-26 00:33:07 -04:00
|
|
|
//list all regex operations
|
|
|
|
server.get("/regex", function (inReq, inRes)
|
2018-06-24 14:12:34 -04:00
|
|
|
{
|
2018-07-04 16:32:59 -04:00
|
|
|
var sql = "SELECT jsonb_agg(regex) regex FROM tps.map_rm WHERE srce = $1::text";
|
|
|
|
Postgres.FirstRow(sql, [inReq.query.srce], inRes);
|
2018-06-24 14:12:34 -04:00
|
|
|
});
|
2018-06-23 17:10:20 -04:00
|
|
|
|
2018-06-26 00:33:07 -04:00
|
|
|
//set one or more map definitions
|
|
|
|
server.post("/regex", bodyParser.json(), function (inReq, inRes)
|
2018-06-24 14:12:34 -04:00
|
|
|
{
|
2018-06-26 00:33:07 -04:00
|
|
|
var sql = "SELECT x.message FROM tps.srce_map_def_set($1::jsonb) as x(message)";
|
|
|
|
Postgres.FirstRow(sql, [JSON.stringify(inReq.body)], inRes);
|
2018-06-24 14:12:34 -04:00
|
|
|
});
|
2018-06-23 17:10:20 -04:00
|
|
|
|
2018-06-28 00:40:28 -04:00
|
|
|
//------------------------------------------------------------mappings---------------------------------------------------------------------------------------------------------------------------------
|
2018-06-23 17:10:20 -04:00
|
|
|
|
2018-06-26 00:33:07 -04:00
|
|
|
//list unmapped items flagged to be mapped ?srce=
|
2018-09-04 15:06:27 -04:00
|
|
|
server.get("/unmapped_all", function (inReq, inRes)
|
2018-06-24 14:12:34 -04:00
|
|
|
{
|
2018-06-26 00:33:07 -04:00
|
|
|
var sql = "SELECT jsonb_agg(row_to_json(x)::jsonb) regex FROM tps.report_unmapped_recs($1::text) x";
|
|
|
|
Postgres.FirstRow(sql,[inReq.query.srce], inRes);
|
2018-06-24 14:12:34 -04:00
|
|
|
});
|
2018-06-23 17:10:20 -04:00
|
|
|
|
2018-07-04 16:32:59 -04:00
|
|
|
//list unmapped items flagged to be mapped ?srce=
|
2018-09-04 15:06:27 -04:00
|
|
|
server.get("/unmapped", function (inReq, inRes)
|
|
|
|
{
|
|
|
|
var sql = "SELECT jsonb_agg(row_to_json(x)::jsonb) regex FROM tps.report_unmapped($1::text) x";
|
2018-09-04 16:54:03 -04:00
|
|
|
Postgres.FirstRow(sql,[inReq.query.srce], inRes);
|
|
|
|
});
|
2018-09-04 15:08:14 -04:00
|
|
|
|
2018-07-04 16:32:59 -04:00
|
|
|
server.get("/mapping", function (inReq, inRes)
|
|
|
|
{
|
|
|
|
var sql = "SELECT jsonb_agg(row_to_json(x)::jsonb) regex FROM tps.map_rv x WHERE srce = $1::text";
|
2018-09-04 15:08:14 -04:00
|
|
|
|
2018-07-04 16:32:59 -04:00
|
|
|
Postgres.FirstRow(sql,[inReq.query.srce], inRes);
|
|
|
|
});
|
|
|
|
|
2018-06-24 14:12:34 -04:00
|
|
|
//add entries to lookup table
|
2018-06-26 00:33:07 -04:00
|
|
|
server.post("/mapping", bodyParser.json(), function (inReq, inRes)
|
2018-06-24 14:12:34 -04:00
|
|
|
{
|
2018-06-26 00:33:07 -04:00
|
|
|
var sql = "SELECT x.message FROM tps.map_rv_set($1::jsonb) as x(message)";
|
|
|
|
Postgres.FirstRow(sql,[JSON.stringify( inReq.body)], inRes);
|
2018-06-24 14:12:34 -04:00
|
|
|
});
|
2018-06-23 17:10:20 -04:00
|
|
|
|
2018-09-04 15:06:27 -04:00
|
|
|
//---------------------------------------------------------list imports--------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
server.get("/import_log", function (inReq, inRes)
|
|
|
|
{
|
|
|
|
var sql = "SELECT jsonb_agg(row_to_json(l)::jsonb) regex FROM tps.trans_log l";
|
|
|
|
Postgres.FirstRow(sql,[], inRes);
|
|
|
|
});
|
|
|
|
|
2018-06-23 17:10:20 -04:00
|
|
|
|
2018-06-28 00:40:28 -04:00
|
|
|
//-------------------------------------------------------------import data-----------------------------------------------------------------------------------------------------------------------------
|
2018-06-23 17:10:20 -04:00
|
|
|
|
|
|
|
server.use("/import", upload.single('upload'), function (inReq, inRes) {
|
|
|
|
|
|
|
|
console.log("should have gotten file as post body here");
|
|
|
|
var csv = inReq.file.buffer.toString('utf8')
|
|
|
|
//{headers: "true", delimiter: ",", output: "jsonObj", flatKeys: "true"}
|
2018-06-23 17:31:25 -04:00
|
|
|
csvtojson({ flatKeys: "true" }).fromString(csv).then(
|
2018-06-23 17:10:20 -04:00
|
|
|
(x) => {
|
2018-06-26 00:33:07 -04:00
|
|
|
var sql = "SELECT x.message FROM tps.srce_import($1, $2::jsonb) as x(message)"
|
|
|
|
console.log(sql);
|
|
|
|
Postgres.FirstRow(sql, [inReq.query.srce, JSON.stringify(x)], inRes);
|
2018-06-23 17:10:20 -04:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2018-09-03 16:58:31 -04:00
|
|
|
//----------------------------------------------------------list import logs---------------------------------------------------------------------------------------------------------------------------
|
2018-07-04 16:05:16 -04:00
|
|
|
|
|
|
|
server.get("/import_log", function (inReq, inRes)
|
|
|
|
{
|
|
|
|
var sql = "SELECT jsonb_agg(info) info FROM tps.trans_log WHERE info @> $1::jsonb";
|
|
|
|
Postgres.FirstRow(sql, [inReq.query], inRes);
|
|
|
|
});
|
|
|
|
|
2018-06-28 00:40:28 -04:00
|
|
|
//-------------------------------------------------------------suggest source def----------------------------------------------------------------------------------------------------------------------
|
2018-06-23 17:10:20 -04:00
|
|
|
|
|
|
|
server.use("/csv_suggest", upload.single('upload'), function (inReq, inRes) {
|
|
|
|
|
|
|
|
console.log("should have gotten file as post body here");
|
|
|
|
var csv = inReq.file.buffer.toString('utf8')
|
|
|
|
//{headers: "true", delimiter: ",", output: "jsonObj", flatKeys: "true"}
|
2018-06-23 17:31:25 -04:00
|
|
|
csvtojson({ flatKeys: "true" }).fromString(csv).then(
|
2018-06-23 17:10:20 -04:00
|
|
|
(x) => {
|
2018-06-26 01:12:16 -04:00
|
|
|
var sug = {
|
|
|
|
schemas: {
|
|
|
|
default: []
|
|
|
|
},
|
|
|
|
loading_function: "csv",
|
|
|
|
source:"client_file",
|
|
|
|
name: "",
|
|
|
|
constraint: []
|
|
|
|
};
|
2018-06-23 17:10:20 -04:00
|
|
|
for (var key in x[0]) {
|
2018-06-26 01:12:16 -04:00
|
|
|
var col = {};
|
2018-06-26 00:33:07 -04:00
|
|
|
//test if number
|
2018-06-23 17:10:20 -04:00
|
|
|
if (!isNaN(parseFloat(x[0][key])) && isFinite(x[0][key])) {
|
2018-06-26 00:33:07 -04:00
|
|
|
//if is a number but leading character is -0- then it's text
|
2018-06-23 17:10:20 -04:00
|
|
|
if (x[0][key].charAt(0) == "0"){
|
2018-06-26 01:12:16 -04:00
|
|
|
col["type"] = "text";
|
2018-06-23 17:10:20 -04:00
|
|
|
}
|
2018-06-26 00:33:07 -04:00
|
|
|
//if number and leadign character is not 0 then numeric
|
2018-06-23 17:10:20 -04:00
|
|
|
else {
|
2018-06-26 01:12:16 -04:00
|
|
|
col["type"] = "numeric";
|
2018-06-23 17:10:20 -04:00
|
|
|
}
|
|
|
|
}
|
2018-06-26 00:33:07 -04:00
|
|
|
//if can cast to a date within a hundred years its probably a date
|
2018-06-23 17:10:20 -04:00
|
|
|
else if (Date.parse(x[0][key]) > Date.parse('1950-01-01') && Date.parse(x[0][key]) < Date.parse('2050-01-01')) {
|
2018-06-26 01:12:16 -04:00
|
|
|
col["type"] = "date";
|
2018-06-23 17:10:20 -04:00
|
|
|
}
|
2018-06-26 00:33:07 -04:00
|
|
|
//otherwise its text
|
2018-06-23 17:10:20 -04:00
|
|
|
else {
|
2018-06-26 01:12:16 -04:00
|
|
|
col["type"] = "text";
|
2018-06-23 17:10:20 -04:00
|
|
|
}
|
2018-06-26 01:12:16 -04:00
|
|
|
col["path"] = "{" + key + "}";
|
|
|
|
col["column_name"] = key;
|
|
|
|
sug.schemas.default.push(col);
|
2018-06-23 17:10:20 -04:00
|
|
|
}
|
|
|
|
console.log(sug);
|
|
|
|
inRes.json(sug);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2018-09-03 16:58:31 -04:00
|
|
|
//add ledger array and create offset account for every line
|
|
|
|
server.get("/doc_add_gl_offset_multi", bodyParser.json(), function (inReq, inRes)
|
|
|
|
{
|
|
|
|
var l = 0;
|
|
|
|
console.log(inReq.body);
|
|
|
|
x = inReq.body;
|
2018-09-27 02:39:14 -04:00
|
|
|
x.gl = {};
|
|
|
|
x.gl.lines = [];
|
|
|
|
x.gl.jpath = [];
|
2018-09-03 16:58:31 -04:00
|
|
|
for (var i in x.item){
|
|
|
|
var line = x.item[i];
|
2018-09-27 02:39:14 -04:00
|
|
|
x.gl.lines.push(line);
|
|
|
|
var ref = [];
|
|
|
|
ref.push("{item,"+i+"}");
|
|
|
|
ref.push("{header}");
|
|
|
|
x.gl.jpath.push(ref);
|
2018-09-03 16:58:31 -04:00
|
|
|
//copy the existing line to the GL array
|
|
|
|
var ofs = JSON.parse(JSON.stringify(line));
|
2018-09-27 02:39:14 -04:00
|
|
|
ofs.account = x.header.account;
|
2018-09-03 16:58:31 -04:00
|
|
|
ofs.amount = -ofs.amount;
|
|
|
|
//add another line the GL array using the offset account
|
2018-09-27 02:39:14 -04:00
|
|
|
x.gl.lines.push(ofs);
|
|
|
|
x.gl.jpath.push(ref);
|
2018-09-03 16:58:31 -04:00
|
|
|
}
|
|
|
|
inRes.json(x);
|
|
|
|
});
|
|
|
|
|
2018-09-27 02:39:14 -04:00
|
|
|
//add ledger array and create offset account for every line
|
|
|
|
server.get("/add_gl_multi_and_post", bodyParser.json(), function (inReq, inRes)
|
|
|
|
{
|
|
|
|
var l = 0;
|
|
|
|
console.log(inReq.body);
|
|
|
|
x = inReq.body;
|
|
|
|
x.gl = {};
|
|
|
|
x.gl.lines = [];
|
|
|
|
x.gl.jpath = [];
|
|
|
|
for (var i in x.item){
|
2018-09-28 14:16:40 -04:00
|
|
|
//copy the current item to the gl array
|
2018-09-27 02:39:14 -04:00
|
|
|
var line = x.item[i];
|
|
|
|
x.gl.lines.push(line);
|
2018-09-28 14:16:40 -04:00
|
|
|
//build references to 'item' array
|
2018-09-27 02:39:14 -04:00
|
|
|
var ref = [];
|
|
|
|
ref.push("{item,"+i+"}");
|
|
|
|
ref.push("{header}");
|
|
|
|
x.gl.jpath.push(ref);
|
2018-09-28 14:16:40 -04:00
|
|
|
//copy the current item to the gl array again, but swap account with supplied 'account' in header
|
2018-09-27 02:39:14 -04:00
|
|
|
var ofs = JSON.parse(JSON.stringify(line));
|
|
|
|
ofs.account = x.header.account;
|
|
|
|
ofs.amount = -ofs.amount;
|
|
|
|
x.gl.lines.push(ofs);
|
2018-09-28 14:16:40 -04:00
|
|
|
//add the same reference again for the offset account
|
2018-09-27 02:39:14 -04:00
|
|
|
x.gl.jpath.push(ref);
|
|
|
|
}
|
|
|
|
var sql = "INSERT INTO evt.bpr (bpr) SELECT $1";
|
|
|
|
console.log(JSON.stringify(x));
|
|
|
|
Postgres.FirstRow(sql,[JSON.stringify(x)], inRes);
|
|
|
|
});
|
|
|
|
|
2018-09-03 16:58:31 -04:00
|
|
|
//add ledger array and create offset account for total of all lines
|
|
|
|
server.get("/doc_add_gl_offset_single", bodyParser.json(), function (inReq, inRes)
|
|
|
|
{
|
|
|
|
var l = 0;
|
|
|
|
var tot = 0.00;
|
2018-09-03 17:16:06 -04:00
|
|
|
var bomb = false;
|
2018-09-03 16:58:31 -04:00
|
|
|
console.log(inReq.body);
|
|
|
|
x = inReq.body;
|
2018-09-03 17:16:06 -04:00
|
|
|
//add GL array
|
2018-09-03 16:58:31 -04:00
|
|
|
x.GL = [];
|
|
|
|
for (var i in x.item){
|
|
|
|
var line = x.item[i];
|
2018-09-03 17:16:06 -04:00
|
|
|
if ((line.account != null) && (line.amount != null)) {
|
|
|
|
x.GL.push(line);
|
|
|
|
tot = tot + (line.amount || 0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
bomb = true;
|
|
|
|
}
|
|
|
|
//add the whole line as-is
|
|
|
|
}
|
2018-09-10 23:56:29 -04:00
|
|
|
if (bomb == false) {
|
2018-09-03 17:16:06 -04:00
|
|
|
var ofs = JSON.parse(JSON.stringify(x.header));
|
2018-09-27 02:39:14 -04:00
|
|
|
ofs.account = ofs.account;
|
|
|
|
delete ofs.account;
|
2018-09-03 17:16:06 -04:00
|
|
|
ofs.amount = -tot;
|
|
|
|
x.GL.push(ofs);
|
2018-09-03 16:58:31 -04:00
|
|
|
}
|
|
|
|
inRes.json(x);
|
|
|
|
});
|
|
|
|
|
2018-06-23 17:10:20 -04:00
|
|
|
|
2018-06-24 14:12:34 -04:00
|
|
|
server.get("/", function (inReq, inRes)
|
|
|
|
{
|
|
|
|
inRes.render("definition", { title: "definition", layout: "main" });
|
|
|
|
});
|
2018-06-23 17:10:20 -04:00
|
|
|
|
2018-06-24 14:12:34 -04:00
|
|
|
module.exports = server;
|