Skip to content

Commit

Permalink
Add a package map
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthaeusHarris committed Mar 21, 2015
1 parent e4bb198 commit 3e0a49e
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 11 deletions.
3 changes: 3 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
var packageApi = require('./lib/package');
var credentials = require('./credentials');
var fs = require('fs');
var mongoose = require('mongoose');

var uri = 'mongodb://localhost/packagetracker';
mongoose.connect(uri);

module.exports = {
packageApi: packageApi,
Expand Down
107 changes: 98 additions & 9 deletions lib/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,50 @@ var fedexApi = require('shipping-fedex');
var upsApi = require('shipping-ups');
var uspsApi = require('shipping-usps');

var credentials = require('../credentials').shippingProviders;
var credentials = require('../credentials');//.shippingProviders;

var fedex = new fedexApi(credentials.fedex);
var ups = new upsApi(credentials.ups);
var usps = new uspsApi(credentials.usps);
var fedex = new fedexApi(credentials.shippingProviders.fedex);
var ups = new upsApi(credentials.shippingProviders.ups);
var usps = new uspsApi(credentials.shippingProviders.usps);

var Package = require('../models/package');
var Address = require('../models/address');

var geocoder = require('node-geocoder')('openstreetmap', 'https');

module.exports = {
getPackageLatLong: function(package, callback) {
var city = this.getCurrentLocation(package);
console.log(city);
if (city) {
Address.findOne({address: city}, function(err, data) {
console.log(err, data);
if (err) {
callback(err, null);
} else {
if (data) {
callback(null, data);
} else {
console.log("Address cache not found. Looking up");
geocoder.geocode(city, function(err, data) {
if (err) {
callback(err, null);
} else {
console.log(data);
var address = new Address({
address: city,
geoInfo: data
});
address.save(callback);
}
});
}
}
});
} else {
callback('invalid city', city);
}
},
buildFedexTrackingLink: function(trackingNumber) {
return "https://www.fedex.com/apps/fedextrack/?tracknumbers="
+ trackingNumber + "&language=en&cntry_code=us";
Expand Down Expand Up @@ -105,9 +140,60 @@ module.exports = {
}
},

getCurrentLocation: function(package) {
var event;
switch(package.carrier.toLowerCase()) {
case "usps":
try {
event = package.lastResponse.TrackResponse.TrackInfo;
return event.TrackDetail[0].match(/[A-Z][A-Z0-9, ]*$/)[0];
} catch(e) {
console.error(package.description);
console.error(e);
return null;
}

break;
case "ups":
try {
event = package.lastResponse.Shipment.Package.Activity[0];
} catch(e) {
try {
event = package.lastResponse.Shipment.Package.Activity;
} catch(e) {
console.error(package.description);
console.error(e);
return null;
}
}
try {
return event.ActivityLocation.Address.City + ", " + event.ActivityLocation.Address.StateProvinceCode;
} catch(e) {
console.error(package.description);
console.error(e);
return null;
}
break;
case "fedex":
try {
event = package.lastResponse.CompletedTrackDetails[0].TrackDetails[0].StatusDetail;
return event.Location.City + ", " + event.Location.StateOrProvinceCode;
} catch(e) {
console.error(package.description);
console.error(e);
return null;
}
break;
default:
return null;
break;
}
},

parseFedexTrackingInfo: function(trackingInfo) {
try {
return trackingInfo.CompletedTrackDetails[0].TrackDetails[0].StatusDetail.Description;
var status = trackingInfo.CompletedTrackDetails[0].TrackDetails[0].StatusDetail;
return status.Description + " at " + status.Location.City + ", " + status.Location.StateOrProvinceCode;
} catch(e) {
console.error("Could not parse FedEx package status from: " + JSON.stringify(trackingInfo));
console.error(e.message);
Expand Down Expand Up @@ -163,12 +249,15 @@ module.exports = {
package.lastResponse = data;
package.timestamps.lastUpdated = Date.now();
package.timestamps.nextUpdate = (new Date().setHours(new Date().getHours() + 3));
package.save(function(err, data) {
console.log("Package " + package._id + " saved.");
callback(err, data);
this.getPackageLatLong(package, function(err, data) {
package.currentLocation = data;
package.save(function(err, data) {
console.log("Package " + package._id + " saved.");
callback(err, data);
});
});
}
});
}.bind(this));
},

updateAllPackages: function(callback) {
Expand Down
15 changes: 15 additions & 0 deletions models/address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Created by matt on 3/20/15.
*/

var mongoose = require('mongoose');

var addressSchema = mongoose.Schema({
address: String,
geoInfo: Object
});

addressSchema.index({"address": 1});

var Address = mongoose.model('Address', addressSchema);
module.exports = Address;
3 changes: 2 additions & 1 deletion models/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ var packageSchema = mongoose.Schema({
update: Boolean,
hidden: Boolean
},
lastResponse: Object
lastResponse: Object,
currentLocation: Object
});

packageSchema.index({"carrier": 1});
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"longjohn": "^0.2.4",
"mongoose": "^3.8.23",
"morgan": "^1.5.1",
"node-geocoder": "^2.19.0",
"passport": "^0.2.1",
"passport-google-oauth2": "^0.1.6",
"serve-favicon": "^2.2.0",
Expand Down
6 changes: 6 additions & 0 deletions public/css/packagetracker.css
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,10 @@ body {

.revision .subject {
display: block;
}

#map-canvas {
display: block;
width: 100%;
height: 500px;
}
24 changes: 23 additions & 1 deletion routes/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var auth = require('../lib/auth');
var Package = require('../models/package');
var flash = require('../lib/flash');
var packageUtils = require('../lib/package');
var credentials = require('../credentials');

router.get('/packages', auth.ensureAuthenticated, function(req, res) {
var query = {
Expand All @@ -30,7 +31,7 @@ router.get('/packages', auth.ensureAuthenticated, function(req, res) {
data[package].latestEvent = packageUtils.parseTrackingInfo(data[package], data[package].lastResponse);
data[package].deliveryEstimate = packageUtils.getDeliveryEstimate(data[package]);
}
res.render('packages', {packages: data, csrf: 'blarg'});
res.render('packages', {packages: data});
}
}
});
Expand Down Expand Up @@ -139,4 +140,25 @@ router.get('/package/:id', auth.ensureAuthenticated, function(req, res) {
});
});

router.get('/map', auth.ensureAuthenticated, function(req, res) {
var query = {
userId: req.user.authId,
'flags.hidden': false
};

Package.find(query, function(err, data) {
if (err) {
res.render('500', {error: err});
} else {
for(var package in data) {
data[package].location = {
latitude: data[package].currentLocation.geoInfo[0].latitude,
longitude: data[package].currentLocation.geoInfo[0].longitude
};
}
res.render('map', {packages: data, mapsApiKey: credentials.apiKeys.googleMaps});
}
});
});

module.exports = router;
2 changes: 2 additions & 0 deletions views/layouts/main.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="http://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
{{{ _sections.header }}}
</head>
<body>
<!--<header><img src="/img/logo.png" alt="Package Tracker" /></header>-->
Expand All @@ -31,6 +32,7 @@
<li><a href="/user">User</a></li>
<!--<li><a href="/about">About</a></li>-->
<li><a href="/packages">Packages</a></li>
<li><a href="/map">Map</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
Expand Down
40 changes: 40 additions & 0 deletions views/map.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<div id="map-canvas"></div>

{{#section 'header'}}
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key={{ mapsApiKey }}">
</script>
{{/section}}

{{#section 'jquery'}}
<script type="text/javascript">
var packages = {};
{{#each packages}}
packages["{{_id}}"] = {
location: {
latitude: {{location.latitude}},
longitude: {{location.longitude}}
},
description: "{{description}}"
};
{{/each}}
function initialize() {
var mapOptions = {
center: { lat: 39.156, lng: 263.808 },
zoom: 4
};
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
globalMap = map;
for (var packageId in packages) {
var pos = new google.maps.LatLng(packages[packageId].location.latitude, packages[packageId].location.longitude);
var marker = new google.maps.Marker({
position: pos,
map: map,
title: packages[packageId].description
});
}
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
{{/section}}

0 comments on commit 3e0a49e

Please sign in to comment.