diff --git a/carriers/pitneyBowes.js b/carriers/pitneyBowes.js index 899d19b..8dd355a 100644 --- a/carriers/pitneyBowes.js +++ b/carriers/pitneyBowes.js @@ -2,13 +2,21 @@ const async = require('async'); const moment = require('moment-timezone'); const PitneyBowesClient = require('pitney-bowes'); +// These tracking status codes indicate the shipment was delivered +const DELIVERED_TRACKING_STATUS_CODES = ['01']; + +// These tracking status codes indicate the shipment was shipped (shows movement beyond a shipping label being created) +const SHIPPED_TRACKING_STATUS_CODES = ['02', '07', '10', '14', '30', '81', '82', 'AD', 'OF', 'PC']; + const geography = require('../util/geography'); function PitneyBowes(options) { const pitneyBowesClient = new PitneyBowesClient(options); this.track = function(trackingNumber, callback) { - pitneyBowesClient.tracking({ trackingNumber }, function(err, data) { + async.retry(function(callback) { + pitneyBowesClient.tracking({ trackingNumber }, callback); + }, function(err, data) { if (err) { return callback(err); } @@ -62,6 +70,14 @@ function PitneyBowes(options) { description: scanDetail.scanDescription }; + if (DELIVERED_TRACKING_STATUS_CODES.includes(scanDetail.scanType)) { + results.deliveredAt = new Date(event.date); + } + + if (SHIPPED_TRACKING_STATUS_CODES.includes(scanDetail.scanType)) { + results.shippedAt = new Date(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) { diff --git a/carriers/usps.js b/carriers/usps.js index 44d7f62..8c547a5 100644 --- a/carriers/usps.js +++ b/carriers/usps.js @@ -12,7 +12,7 @@ const CITY_BLACKLIST = /DISTRIBUTION CENTER|NETWORK DISTRIBUTION CENTER/ig; 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 = ['02', '07', '10', '14', 'OF', 'PC']; +const SHIPPED_TRACKING_STATUS_CODES = ['02', '07', '10', '14', '30', '81', '82', 'AD', 'OF', 'PC']; // 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']; diff --git a/test/carriers/pitneyBowes.js b/test/carriers/pitneyBowes.js index 01c824d..384d3a2 100644 --- a/test/carriers/pitneyBowes.js +++ b/test/carriers/pitneyBowes.js @@ -5,6 +5,19 @@ const Bloodhound = require('../../index.js'); describe('Newgistics', function() { this.timeout(10000); + it('should return an error for invalid base URL', function(done) { + const bloodhound = new Bloodhound({ + pitneyBowes: { + baseUrl: 'https://httpbin.org/status/500#' + } + }); + + bloodhound.track('9400111899223837861141', 'newgistics', function(err) { + assert(err); + done(); + }); + }); + it('4206336792748927005269000010615207', function(done) { const bloodhound = new Bloodhound({ pitneyBowes: { @@ -118,7 +131,9 @@ describe('Newgistics', function() { date: new Date('2019-06-25T13:09:00.000Z'), description: 'Picked Up by Shipping Partner, USPS Awaiting Item' } - ] + ], + deliveredAt: new Date('2019-06-30T18:03:00.000Z'), + shippedAt: new Date('2019-06-28T21:43:00.000Z') }; assert.deepStrictEqual(actual, expected); diff --git a/test/carriers/usps.js b/test/carriers/usps.js index a584aef..d9ad99f 100644 --- a/test/carriers/usps.js +++ b/test/carriers/usps.js @@ -232,7 +232,7 @@ describe('USPS', function () { } ], deliveredAt: new Date('2019-07-01T19:25:00.000Z'), - shippedAt: new Date('2019-06-30T22:02:00.000Z') + shippedAt: new Date('2019-06-29T02:26:00.000Z') } assert.deepStrictEqual(actual, expected); diff --git a/test/index.js b/test/index.js index c4ff80f..02969f7 100644 --- a/test/index.js +++ b/test/index.js @@ -3,6 +3,8 @@ const assert = require('assert'); const Bloodhound = require('../index.js'); describe('Error handling', function() { + this.timeout(10000); + const bloodhound = new Bloodhound({ fedEx: { account_number: process.env.FEDEX_ACCOUNT_NUMBER,