aboutsummaryrefslogtreecommitdiffstats
path: root/admportal/server/router/routes/helpers.js
blob: 6ac8215faf93d9268f6e06d0af5406906dfa8637 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
var _ = require('lodash');
var csvtojson = require('csvtojson');
var async = require('async');
var uuid = require('node-uuid');   // generate a uuid with "uuid.v1()"
var path = require('path');
var fs = require("fs");
var moment = require('moment');

var helpers = module.exports;

function puts(obj) { console.log(obj); } 
function putd(obj) { console.log(obj); } 
helpers.puts = puts;
helpers.putd = putd;


helpers.readCsv = function(filedir, filename, callback) {


  var Converter=csvtojson.Converter;
  var csvFileName=path.join(filedir,filename);
  var fileStream=fs.createReadStream(csvFileName);
  fileStream.on('error', function(err){
	callback(err, null);
  });
  var param={noheader:true, checkType:false};
  var csvConverter=new Converter(param);
  csvConverter.on("end_parsed",function(jsonObj){
     var returnValue = jsonObj;
     callback(null, returnValue);
  });
  fileStream.on('error', function(err) {
    putd(err);
    callback(err,"");
  });
  fileStream.pipe(csvConverter);
}

helpers.getParam = function(csv, matchField, matchValue, returnField) {
  dataRow=_.find(csv, matchField, matchValue);
  dataValue=dataRow[returnField];
  return dataValue;
}

helpers.writeOutput = function(req, filename, jsonOutput, callback) {
  try {
  	fs.writeFileSync(filename, jsonOutput);
  }
  catch(err){
	callback(err);
  }
}

helpers.getFileName = function(req, defFilename) {
  
  var fileObj = null;
  for (var x=0; x < req.files.length; x++)
  {
	var fileObj = req.files[x];
	if ( fileObj.filename.indexOf(defFilename) != -1 ){
  		return fileObj.filename;
	}
  }
  return null;
}