Guest User

Node data

a guest
Nov 19th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.10 KB | None | 0 0
  1. const app = require('express')();
  2. const bodyParser = require('body-parser');
  3. // Load the full build.
  4. const _ = require('lodash');
  5. const cron = require('node-cron');
  6. const fs = require('fs');
  7.  
  8.  
  9. const admin = require("firebase-admin");
  10.  
  11.  
  12. // parse application/x-www-form-urlencoded
  13. app.use(bodyParser.urlencoded({ extended: true }));
  14. // parse application/json
  15. app.use(bodyParser.json());
  16.  
  17. cron.schedule('0 */1 * * * *', function(){
  18. console.log('Running on '+new Date());
  19. DoStuff();
  20. syncCustList();
  21. });
  22.  
  23. //var mysql = require('mysql');
  24.  
  25. //var con = mysql.createConnection({
  26. // host: "db4free.net",
  27. // user: "testdb1234",
  28. // port: "3307",
  29. // database: "testdb1234",
  30. // password: "testdb1234"
  31. //});
  32.  
  33.  
  34.  
  35. // con.connect(function(err) {
  36.  
  37. // if (err) throw err;
  38. // console.log("Connected!");
  39. // var sql = `select * from table1`;
  40.  
  41. // con.query(sql, function (err, result) {
  42. // if (err) throw err;
  43. // for(var i = 0; i < result.length;i++){
  44. // var data = result[i];
  45. // var ref = admin.database().ref('/nodejs-data');
  46. // ref = ref.push();
  47. // ref.set(data);
  48. // ref = null; //dispose the ref for saving memory;
  49. // }
  50. // console.log(result);
  51.  
  52. // });
  53.  
  54. // console.log("Done");
  55. // });
  56. var Connection = require('tedious').Connection;
  57. var Request = require('tedious').Request;
  58. var TYPES = require('tedious').TYPES;
  59.  
  60.  
  61.  
  62. var config = {
  63. userName: 'devuser', // update me
  64. password: '123456', // update me
  65. server: 'localhost',
  66. options: {
  67. database: 'MedSQL',
  68. rowCollectionOnDone : true
  69. }
  70. }
  71.  
  72. function syncCustList() {
  73. var conn = new Connection(config);
  74. var data;
  75. conn.on('connect', function(err) {
  76. if (err) throw err;
  77. console.log("Connected!");
  78. var sql = "SELECT BB.Usr_Code, BB.Led_Name FROM dbo.tbl_TransactionBalance AS AA WITH (NOLOCK) LEFT JOIN dbo.tbl_LedgerSetup AS BB WITH (NOLOCK) On AA.Led_Id = BB.Led_Id LEFT JOIN dbo.tbl_GroupDetail AS CC WITH (NOLOCK) On BB.Grp_Id_City = CC.GrpId LEFT JOIN dbo.tbl_GroupDetail AS DD WITH (NOLOCK) On BB.Grp_Id_Area = DD.GrpId LEFT JOIN dbo.tbl_sw_LedgerHead AS EE WITH (NOLOCK) On AA.Type_Id = EE.Type_Id LEFT JOIN dbo.tbl_LedgerSetup AS FF WITH (NOLOCK) On BB.Led_Id_Salesperson = FF.Led_Id LEFT JOIN dbo.tbl_GroupDetail AS GG WITH (NOLOCK) On BB.GrpId_Grade = GG.GrpId LEFT JOIN dbo.tbl_sw_FixedLstData AS II WITH (NOLOCK) On BB.Fxd_TermId_S = II.SelfId LEFT JOIN dbo.tbl_GroupDetail AS JJ WITH (NOLOCK) On BB.GrpId_Catagory = JJ.GrpId LEFT JOIN dbo.tbl_GroupDetail AS KK WITH (NOLOCK) On BB.GrpId_Zone = KK.GrpId LEFT JOIN dbo.tbl_GdwnSetup AS LL WITH (NOLOCK) On BB.GrpId_Cost = LL.GdwnId LEFT JOIN dbo.tbl_GroupDetail AS MM WITH (NOLOCK) On BB.GrpId_SubLed = MM.GrpId LEFT JOIN dbo.tbl_GroupDetail AS NN WITH (NOLOCK) On BB.Grp_Id_Head = NN.GrpId LEFT JOIN dbo.tbl_LedgerSetup AS OO WITH (NOLOCK) On BB.LedId_Dman = OO.Led_Id LEFT JOIN dbo.tbl_sw_FixedLstData AS PP WITH (NOLOCK) On BB.SelfId_Stat = PP.SelfId LEFT JOIN dbo.tbl_GroupDetail AS QQ WITH (NOLOCK) On BB.Grp_Id_Transport = QQ.GrpId LEFT JOIN dbo.tbl_sw_FixedLstData AS RR WITH (NOLOCK) On BB.GrpId_State = RR.SelfId Where AA.CompanyId = 1 And AA.YearId = 13 And AA.Type_Id = 3 And BB.CompanyId In (0, 1) ORDER By ShowOrder, BB.Led_Name";
  79. var result='';
  80.  
  81. // Read all rows from tablecls
  82.  
  83. request = new Request(
  84. sql,
  85. function(err, rowCount, rows) {
  86.  
  87. if (err) {
  88. callback(err);
  89. } else {
  90. console.log(rowCount + ' row(s) returned');
  91. console.log(rows);
  92. // callback(null);
  93. }
  94. });
  95.  
  96. request.on('doneInProc', function(rowCount, more, rows) {
  97.  
  98. data = [];
  99. rows.forEach(function(row) {
  100. dataItem = {};
  101. for(var key in row){
  102. dataItem[row[key]['metadata']['colName']] = row[key]['value'];
  103. }
  104. // data.push(dataItem);
  105. fs.writeFile("custList.txt", dataObj, function(err){
  106. if (err) {
  107. return console.log(err);
  108. }
  109. console.log("Saved!");
  110. });
  111. }, this);
  112.  
  113. console.log(data.length);
  114.  
  115.  
  116.  
  117. var props = ['Usr_Code', 'Led_Name'];
  118.  
  119. var notNull = _.negate(_.isNull);
  120.  
  121. var groups = _.groupBy(data, function(dataItem){
  122. return _.find(_.pick(dataItem, props), notNull);
  123. });
  124.  
  125.  
  126. for(var key in groups){
  127. if(groups[key].length > 1){
  128. groups[key] = _.sortBy(groups[key], function(dataItem) {
  129. return new Date(dataItem.EntryDate);
  130. });
  131. var tempDI = groups[key].splice(0,1);
  132. groups[key] = [];
  133. groups[key].push(tempDI);
  134. tempDI = null;
  135.  
  136. }
  137. }
  138.  
  139.  
  140. // / console.log(groups["RHINO"]);
  141. // var refOld = admin.database().ref('/nodejs-data').child('Quant');
  142. // refOld.remove();
  143. // refOld = null;
  144. for(var prodname in groups ){
  145. var dataObj = groups[prodname][0][0] === undefined ? groups[prodname][0] : groups[prodname][0][0] ;
  146. // var ref = admin.database().ref('tempDB').child('/nodejs-data').child('Quant').child(prodname.replace(/\./g,"_"));
  147.  
  148. // ref.set(dataObj);
  149. // ref = null; //dispose the ref for saving memory; DONE
  150. }
  151.  
  152. });
  153.  
  154.  
  155. conn.execSql(request);
  156. console.log("Done");
  157. })
  158.  
  159.  
  160. }
  161.  
  162. function DoStuff(){
  163.  
  164. var conn = new Connection(config);
  165. var data;
  166. conn.on('connect', function(err) {
  167. if (err) throw err;
  168. console.log("Connected!");
  169. var sql = "SELECT distinct ISNULL(TABLE1.EntryDate,' ') as EntryDate, TABLE2.ItemUsrCode, TABLE2.ItemName,TABLE2.MRP, TABLE2.TSTK as TotalStock, ISNULL(TABLE1.SchCatagory,' ') as Scheme FROM ( select AA.UsrDate As EntryDate, AA.ItemUsrCode, AA.ItemName As ProductName,AA.MRP, FF.GrpName As SchCatagory FROM dbo.tbl_BatchSel AS AA WITH (NOLOCK, Index = ix_ItemUsrCode) LEFT JOIN dbo.tbl_ItemMaster AS BB WITH (NOLOCK) On AA.ItemDetailId = BB.ItemDetailId LEFT JOIN dbo.tbl_PI_Hdr AS CC WITH (NOLOCK) On AA.MyType = CC.MyType And AA.MyId = CC.PIH_Id LEFT JOIN dbo.tbl_GroupDetail AS DD WITH (NOLOCK) On BB.Pckg_GrpId = DD.GrpId LEFT JOIN dbo.tbl_LedgerSetup AS EE WITH (NOLOCK) On BB.Mfr_Led_Id = EE.Led_Id LEFT JOIN dbo.tbl_GroupDetail AS FF WITH (NOLOCK) On AA.GrpId_Sch = FF.GrpId WHERE (SchDescS <> '' OR GrpId_Sch > 0) And (SchUptoDate >= CONVERT(DATETIME,2017-10-31) OR SchUptoDate Is Null OR SchUptoQty >= (Sch-SchIssd)) And AA.CompanyId = 1 And AA.YearId <= 13 And AA.MyType Not in ('ICNTE') And BB.NIP = 0 ) TABLE1 RIGHT JOIN ( SELECT BS.MRP,BB.ItemUsrCode, BB.ItemName, AA.ClQty As TSTK FROM dbo.tbl_Inventory AS AA WITH (NOLOCK) LEFT JOIN dbo.tbl_ItemMaster AS BB WITH (NOLOCK) On AA.ItemDetailId = BB.ItemDetailId LEFT JOIN dbo.tbl_GroupDetail AS CC WITH (NOLOCK) On BB.Pckg_GrpId = CC.GrpId LEFT JOIN dbo.tbl_LedgerSetup AS DD WITH (NOLOCK) On BB.Mfr_Led_Id = DD.Led_Id LEFT JOIN dbo.tbl_LedgerSetup AS EE WITH (NOLOCK) On BB.Division_LedId = EE.Led_Id LEFT JOIN dbo.Med_FN_All_HSTK (1, 13) As GG On AA.ItemDetailId = GG.ItemDetailId LEFT JOIN dbo.Med_FN_All_PQTY (1, 13) As HH On AA.ItemDetailId = HH.ItemDetailId LEFT JOIN dbo.Med_FN_All_SQTY (1, 13) As II On AA.ItemDetailId = II.ItemDetailId left join DBO.tbl_BatchSel BS with(nolock) on BS.ItemUsrCode = AA.ItemUsrCode and BS.ItemDetailId = AA.ItemDetailId and BS.MyType <> 'ICNTE' LEFT JOIN dbo.tbl_GroupDetail AS JJ WITH (NOLOCK) On BB.ItemCatg_GrpId = JJ.GrpId Where AA.CompanyId = 1 And AA.YearId = 13 And (AA.OpQty + AA.Inward + AA.Outward + AA.ClQty) <> 0 And BB.NIP = 0 ) TABLE2 ON TABLE1.ItemUsrCode = TABLE2.ItemUsrCode";
  170. var result='';
  171.  
  172. // Read all rows from tablecls
  173.  
  174. request = new Request(
  175. sql,
  176. function(err, rowCount, rows) {
  177.  
  178. if (err) {
  179. callback(err);
  180. } else {
  181. console.log(rowCount + ' row(s) returned');
  182. console.log(rows);
  183. // callback(null);
  184. }
  185. });
  186.  
  187. request.on('doneInProc', function(rowCount, more, rows) {
  188.  
  189. data = [];
  190. rows.forEach(function(row) {
  191. dataItem = {};
  192. for(var key in row){
  193. dataItem[row[key]['metadata']['colName']] = row[key]['value'];
  194. }
  195. data.push(dataItem);
  196. }, this);
  197.  
  198. console.log(data.length);
  199.  
  200.  
  201.  
  202. var props = ['ItemUserCode', 'ItemName', 'MRP', 'TotalStock'];
  203.  
  204. var notNull = _.negate(_.isNull);
  205.  
  206. var groups = _.groupBy(data, function(dataItem){
  207. return _.find(_.pick(dataItem, props), notNull);
  208. });
  209.  
  210.  
  211. for(var key in groups){
  212. if(groups[key].length > 1){
  213. groups[key] = _.sortBy(groups[key], function(dataItem) {
  214. return new Date(dataItem.EntryDate);
  215. });
  216. var tempDI = groups[key].splice(0,1);
  217. groups[key] = [];
  218. groups[key].push(tempDI);
  219. tempDI = null;
  220.  
  221. }
  222. }
  223.  
  224. // / console.log(groups["RHINO"]);
  225. var refOld = admin.database().ref('/nodejs-data').child('Quant');
  226. refOld.remove();
  227. refOld = null;
  228. for(var prodname in groups ){
  229. var dataObj = groups[prodname][0][0] === undefined ? groups[prodname][0] : groups[prodname][0][0] ;
  230. var ref = admin.database().ref('tempDB').child('/nodejs-data').child('Quant').child(prodname.replace(/\./g,"_"));
  231.  
  232. ref.set(dataObj);
  233. ref = null; //dispose the ref for saving memory; DONE
  234. }
  235.  
  236. });
  237.  
  238.  
  239. conn.execSql(request);
  240. console.log("Done");
  241. })
  242.  
  243.  
  244. }
  245.  
  246. app.get('/Firebase', function(req,res){
  247.  
  248. var dataToBeSentAsRepsonse = "";
  249. var ref = admin.database().ref('nodejs-data');
  250. ref.once('value', function(snapshot){
  251. var dataArr = snapshot.val();
  252. for(var key in dataArr){
  253. dataToBeSentAsRepsonse += `<b>Key:</b> ${key} &nbsp; Value: &nbsp;column1: ${dataArr[key]['col1']} - &nbsp; column2: ${dataArr[key]['col2']} - &nbsp; column3: ${dataArr[key]['col3']} - &nbsp; column4: ${dataArr[key]['col4']} <br>`;
  254. }
  255. res.send(dataToBeSentAsRepsonse);
  256. console.log(dataToBeSentAsRepsonse);
  257. });
  258.  
  259. });
  260.  
  261.  
  262. app.listen(3000);
Add Comment
Please, Sign In to add comment