-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
284 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
const async = require('async'); | ||
const builder = require('xmlbuilder'); | ||
const moment = require('moment-timezone'); | ||
const parser = require('xml2js'); | ||
const request = require('request'); | ||
|
||
const geography = require('../util/geography'); | ||
|
||
const CITY_BLACKLIST = /DISTRIBUTION CENTER|NETWORK DISTRIBUTION CENTER/ig; | ||
|
||
// These tracking status codes indicate the shipment was delivered: https://about.usps.com/publications/pub97/pub97_appi.htm | ||
const DELIVERED_TRACKING_STATUS_CODES = ['01']; | ||
|
||
// These tracking status codes indicate the shipment was shipped (shows movement beyond a shipping label being created): https://about.usps.com/publications/pub97/pub97_appi.htm | ||
const SHIPPED_TRACKING_STATUS_CODES = ['80', '81', '82', 'OF']; | ||
|
||
// The events from these tracking status codes are filtered because they do not provide any useful information: https://about.usps.com/publications/pub97/pub97_appi.htm | ||
const TRACKING_STATUS_CODES_BLACKLIST = ['NT']; | ||
|
||
function USPS(options) { | ||
this.isTrackingNumberValid = function(trackingNumber) { | ||
// remove whitespace | ||
trackingNumber = trackingNumber.replace(/\s/g, ''); | ||
|
||
if (/^(94001)\d{17}$/.test(trackingNumber)){ | ||
return true; | ||
} | ||
if (/^(92055)\d{17}$/.test(trackingNumber)){ | ||
return true; | ||
} | ||
if (/^(94073)\d{17}$/.test(trackingNumber)){ | ||
return true; | ||
} | ||
if (/^(93033)\d{17}$/.test(trackingNumber)){ | ||
return true; | ||
} | ||
if (/^(92701)\d{17}$/.test(trackingNumber)){ | ||
return true; | ||
} | ||
if (/^(92088)\d{17}$/.test(trackingNumber)){ | ||
return true; | ||
} | ||
if (/^(92021)\d{17}$/.test(trackingNumber)){ | ||
return true; | ||
} | ||
|
||
return false; | ||
}; | ||
this.track = function(trackingNumber, callback) { | ||
const baseUrl = options.baseUrl || 'http://production.shippingapis.com/ShippingAPI.dll?API=TrackV2&XML='; | ||
|
||
const obj = { | ||
TrackFieldRequest: { | ||
'@USERID': options.USPS_USERID, | ||
Revision: '1', | ||
ClientIp: options.ClientIp || '127.0.0.1', | ||
SourceId: options.SourceId || '@mediocre/bloodhound (+https://github.com/mediocre/bloodhound)', | ||
TrackID: { | ||
'@ID': trackingNumber | ||
} | ||
} | ||
} | ||
|
||
var xml = builder.create(obj, { headless: true }).end({ pretty: false }); | ||
const url = baseUrl + encodeURIComponent(xml); | ||
|
||
request(url, function (err, res) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
|
||
parser.parseString(res.body, function (err, data) { | ||
// 1. Invalid XML in parser | ||
// 2. Invalid credentials | ||
// 3. Invalid tracking number | ||
if (err) { | ||
return callback(err); | ||
} else if (data.Error) { | ||
return callback(new Error(data.Error.Description[0])); | ||
} else if (data.TrackResponse.TrackInfo[0].Error) { | ||
return callback(new Error(data.TrackResponse.TrackInfo[0].Error[0].Description[0])); | ||
} | ||
|
||
const results = { | ||
events: [] | ||
}; | ||
|
||
var scanDetailsList = []; | ||
|
||
// TrackSummary[0] exists for every item (with valid tracking number) | ||
const summary = data.TrackResponse.TrackInfo[0].TrackSummary[0]; | ||
|
||
scanDetailsList.push(summary); | ||
var trackDetailList = data.TrackResponse.TrackInfo[0].TrackDetail; | ||
|
||
// If we have tracking details, push them into statuses | ||
// Tracking details only exist if the item has more than one status update | ||
if (trackDetailList) { | ||
trackDetailList.forEach((trackDetail) => { | ||
if(TRACKING_STATUS_CODES_BLACKLIST.includes(trackDetail.EventCode[0])) { | ||
return; | ||
} | ||
|
||
scanDetailsList.push(trackDetail); | ||
}); | ||
} | ||
|
||
// Set address and location of each scan detail | ||
scanDetailsList.forEach(scanDetail => { | ||
scanDetail.address = { | ||
city: scanDetail.EventCity[0].replace(CITY_BLACKLIST, '').trim(), | ||
country: scanDetail.EventCountry[0], | ||
state: scanDetail.EventState[0], | ||
zip: scanDetail.EventZIPCode[0] | ||
}; | ||
|
||
scanDetail.location = geography.addressToString(scanDetail.address); | ||
}); | ||
|
||
// Get unqiue array of locations | ||
const locations = Array.from(new Set(scanDetailsList.map(scanDetail => scanDetail.location))); | ||
|
||
// Lookup each location | ||
async.mapLimit(locations, 10, function (location, callback) { | ||
geography.parseLocation(location, function (err, address) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
|
||
address.location = location; | ||
|
||
callback(null, address); | ||
}); | ||
}, function (err, addresses) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
|
||
scanDetailsList.forEach(scanDetail => { | ||
const address = addresses.find(a => a.location === scanDetail.location); | ||
let timezone = 'America/New_York'; | ||
|
||
if (address && address.timezone) { | ||
timezone = address.timezone; | ||
} | ||
|
||
const event = { | ||
address: scanDetail.address, | ||
date: moment.tz(`${scanDetail.EventDate[0]} ${scanDetail.EventTime[0]}`, 'MMMM D, YYYY h:mm a', timezone).toDate(), | ||
description: scanDetail.Event[0] | ||
}; | ||
|
||
if (DELIVERED_TRACKING_STATUS_CODES.includes(scanDetail.EventCode[0])) { | ||
results.deliveredAt = event.date; | ||
} | ||
|
||
if (SHIPPED_TRACKING_STATUS_CODES.includes(scanDetail.EventCode[0])) { | ||
results.shippedAt = event.date; | ||
} | ||
|
||
// Use the city and state from the parsed address (for scenarios where the city includes the state like "New York, NY") | ||
if (address) { | ||
if (address.city) { | ||
event.address.city = address.city; | ||
} | ||
|
||
if (address.state) { | ||
event.address.state = address.state; | ||
} | ||
} | ||
|
||
results.events.push(event); | ||
}); | ||
|
||
// Change description of most recent event to be StatusSummary (more descriptive) | ||
results.events[0].details = data.TrackResponse.TrackInfo[0].StatusSummary[0]; | ||
|
||
callback(null, results); | ||
}); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = USPS; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
const assert = require('assert'); | ||
const Bloodhound = require('../../index'); | ||
const USPS = require('../../carriers/usps.js') | ||
|
||
describe('usps.isTrackingNumberValid', function() { | ||
const usps = new USPS(); | ||
|
||
const validTrackingNumbers = [ | ||
'9400111899223818218407', | ||
'9400 1118 9922 3818 2184 07', | ||
'9400 1118 9956 1482 6100 74', | ||
'9400 1118 9922 3837 8204 90', | ||
'9400 1118 9922 3837 8611 41', | ||
'9400 1000 0000 0000 0000 00', | ||
'9205 5000 0000 0000 0000 00', | ||
'9407 3000 0000 0000 0000 00', | ||
'9303 3000 0000 0000 0000 00', | ||
'9208 8000 0000 0000 0000 00', | ||
'9270 1000 0000 0000 0000 00', | ||
'9202 1000 0000 0000 0000 00', | ||
'94001 11111111111111110', | ||
'92055 11111111111111111', | ||
'94073 11111111111111111', | ||
'93033 11111111111111111', | ||
'92701 11111111111111111', | ||
'92088 11111111111111111', | ||
'92021 11111111111111111' | ||
]; | ||
|
||
it('should detect valid USPS tracking numbers', function() { | ||
validTrackingNumbers.forEach(trackingNumber => { | ||
if (!usps.isTrackingNumberValid(trackingNumber)) { | ||
assert.fail(`${trackingNumber} is not recognized as a valid USPS tracking number`); | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
describe('USPS', function () { | ||
this.timeout(10000); | ||
|
||
const bloodhound = new Bloodhound({ | ||
usps: { | ||
USPS_USERID: process.env.USPS_USERID | ||
} | ||
}); | ||
|
||
describe('Invalid USPS Access', function() { | ||
const bloodhound1 = new Bloodhound({ | ||
usps: { | ||
USPS_USERID: process.env.USPS_USERID, | ||
baseUrl: 'https://google.com' | ||
} | ||
}); | ||
it('should return an error for invalid URL', function (done) { | ||
bloodhound1.track('9400111899223837861141', 'usps', function (err) { | ||
assert(err); | ||
done(); | ||
}); | ||
}) | ||
}); | ||
|
||
describe('Invalid USPS Credentials', function () { | ||
it('should return an error for invalid USERID', function (done) { | ||
const bloodhound1 = new Bloodhound({ | ||
usps: { | ||
USPS_USERID: 'invalid' | ||
} | ||
}); | ||
bloodhound1.track('9400111899223837861141', 'usps', function (err) { | ||
assert(err); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('USPS Tracking', function () { | ||
it('should return an error for an invalid tracking number', function (done) { | ||
bloodhound.track('An Invalid Tracking Number', 'usps', function (err) { | ||
assert(err); | ||
done(); | ||
}); | ||
}); | ||
it('should return tracking information with no errors', function (done) { | ||
bloodhound.track('9400111899223837861141', 'usps', function (err) { | ||
assert.ifError(err); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |