var db require lib db var Shipper db models Shipper var Agent db model

 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
var db = require('./../lib/db')
var Shipper = db.models.Shipper
var Agent = db.models.Agent
var Point = db.models.Point
var _ = require('underscore');
var fs = require('fs');
var async = require('async');
var agent_names_path = __dirname + '../data_parsers/data/agentsnames.json';
var shipper_names_path = __dirname + '/snippetsnames.json';
var pipe_names_path = __dirname + '/data/pipenames.json';
var agent_clean_names, shipper_clean_names, pipe_clean_names;
var iter = 0;
fs.readFile(shipper_names_path, function(err, data) {
if (err) console.log ('readFile shipper_names_path', err);
var fil = JSON.parse(data)
shipper_clean_names = _.object(fil);
});
exports.boot = function(app) {
app.get('/search', function (req, res) {
var subQuery = {shipper_name: 1, shipper_name_clean: 1}
var arr = [];
Shipper.find(req.query, subQuery)
.exec(function(err, shippers) {
if (err) console.log('err', err);
else {
_.each(shippers, function(shipper, i){
if(!shipper.shipper_name_clean.length){
var name = shipper.shipper_name.replace(/(\\\")|(\")/g, '')
// var name = name + ' '
var name = name + ' '
// name = name.replace(//g,'')
console.log('name is', name, "///", shipper_clean_names[name.toUpperCase()])
if(shipper_clean_names[name.toUpperCase()] || shipper_clean_names[name]){
shipper.shipper_name_clean = shipper_clean_names[name.toUpperCase()] ? shipper_clean_names[name.toUpperCase()] : shipper_clean_names[name];
shipper.save(function(err, newShipper){
console.log('SAVED!' + iter++)
// cb(null);
});
}else{
arr.push(shipper.shipper_name)
}
}
});
res.send({"shippers.len": shippers.length ,status: "success", shippers: shippers.length, "arr.len":arr.length, arr: _.uniq(arr)});
}
})
})
}