Skip to content

Commit

Permalink
Merge pull request #16 from mediocre/pitney-bowes-marketing-mail-flats
Browse files Browse the repository at this point in the history
Pitney bowes marketing mail flats
  • Loading branch information
freshlogic committed Aug 21, 2019
2 parents 683e1e2 + a8dcd43 commit 9bf4ed6
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 231 deletions.
23 changes: 21 additions & 2 deletions carriers/pitneyBowes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,23 @@ const CITY_BLACKLIST = /DISTRIBUTION CENTER|INTERNATIONAL DISTRIBUTION CENTER|NE
const DELIVERED_TRACKING_STATUS_CODES = ['01', 'DEL'];

// 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', 'IPS', 'OF', 'OFD', 'PC', 'SS'];
const SHIPPED_TRACKING_STATUS_CODES = ['02', '07', '10', '14', '30', '336', '401', '403', '81', '82', 'AD', 'IPS', 'OF', 'OFD', 'PC', 'SS'];

const geography = require('../util/geography');

function PitneyBowes(options) {
const pitneyBowesClient = new PitneyBowesClient(options);

this.track = function(trackingNumber, callback) {
// Pitney Bowes Marketing Mail Flats (length 31): 0004290252994200071698133931119
const isImb = trackingNumber.length === 31;

async.retry(function(callback) {
if (isImb) {
return pitneyBowesClient.tracking({ carrier: 'IMB', trackingNumber: trackingNumber.substring(0, 20) }, callback);
}

// Newgistics Ground (length 34): 4201913892748927005269000023298282
pitneyBowesClient.tracking({ carrier: 'FDR', trackingNumber }, callback);
}, function(err, data) {
if (err) {
Expand All @@ -29,6 +37,10 @@ function PitneyBowes(options) {
events: []
};

if (isImb) {
results.carrier = 'Pitney Bowes';
}

if (!data | !data.scanDetailsList) {
return callback(null, results);
}
Expand Down Expand Up @@ -73,7 +85,10 @@ function PitneyBowes(options) {
return callback(err);
}

data.scanDetailsList.reverse().forEach(scanDetail => {
// Sort list by date/time
data.scanDetailsList.sort((a, b) => `${a.eventDate} ${a.eventTime}` - `${b.eventDate} ${b.eventTime}`);

data.scanDetailsList.forEach(scanDetail => {
const address = addresses.find(a => a && a.location === scanDetail.location);
let timezone = 'America/New_York';

Expand Down Expand Up @@ -112,6 +127,10 @@ function PitneyBowes(options) {
// Add url to carrier tracking page
results.url = `https://tools.usps.com/go/TrackConfirmAction?qtc_tLabels1=${encodeURIComponent(trackingNumber)}`;

if (isImb) {
results.url = `https://checkout.org/tracking/${trackingNumber}`;
}

if (!results.shippedAt && results.deliveredAt) {
results.shippedAt = results.deliveredAt;
}
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ function Bloodhound(options) {
fedEx.track(trackingNumber, callback);
} else if (carrier === 'newgistics') {
pitneyBowes.track(trackingNumber, callback);
} else if (carrier === 'pitney bowes') {
pitneyBowes.track(trackingNumber, callback);
} else if (carrier === 'ups'){
ups.track(trackingNumber, callback);
} else if (carrier === 'usps') {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@
"type": "git",
"url": "https://github.com/mediocre/bloodhound.git"
},
"version": "1.0.1"
"version": "1.1.0"
}
232 changes: 5 additions & 227 deletions test/carriers/pitneyBowes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ describe('Newgistics', function() {

it('should return an error for invalid base URL', function(done) {
const bloodhound = new Bloodhound({
pettyCache: {},
pitneyBowes: {
baseUrl: 'https://httpbin.org/status/500#'
}
Expand All @@ -18,242 +17,21 @@ describe('Newgistics', function() {
done();
});
});
});

it('should return a shippedAt field when results have no shipping status', function(done) {
const bloodhound = new Bloodhound({
pitneyBowes: {
api_key: process.env.PITNEY_BOWES_API_KEY,
api_secret: process.env.PITNEY_BOWES_API_SECRET
}
});

bloodhound.track('4207866492612927005269000029964826', 'newgistics', function(err, actual) {
assert.ifError(err);

const expected = {
carrier: 'Newgistics',
events: [
{
address: {
city: 'LA',
country: null,
state: 'CA',
zip: null
},
date: new Date('2019-07-19T22:57:00.000Z'),
description: 'Delivered'
},
{
address: {
city: 'Carrollton',
country: null,
state: 'TX',
zip: null
},
date: new Date('2019-07-10T16:08:00.000Z'),
description: 'Electronic Shipping Info Received'
}
],
deliveredAt: new Date('2019-07-19T22:57:00.000Z'),
url: 'https://tools.usps.com/go/TrackConfirmAction?qtc_tLabels1=4207866492612927005269000029964826',
shippedAt: new Date('2019-07-19T22:57:00.000Z')
}

assert.deepStrictEqual(actual, expected);
done();
});
});
describe('Pitney Bowes', function() {
this.timeout(20000);

it('4204540992748927005269000020006828', function(done) {
it('should return an error', function(done) {
const bloodhound = new Bloodhound({
pitneyBowes: {
api_key: process.env.PITNEY_BOWES_API_KEY,
api_secret: process.env.PITNEY_BOWES_API_SECRET
}
});

bloodhound.track('4204540992748927005269000020006828', 'newgistics', function(err, actual) {
bloodhound.track('0004290252994200071698133931119', 'Pitney Bowes', function(err) {
assert.ifError(err);

const expected = {
carrier: 'Newgistics',
events: [
{
address: {
city: 'Dayton',
country: null,
state: 'OH',
zip: null
},
date: new Date('2019-06-22T13:59:00.000Z'),
description: 'Return to Sender because the addressee moved and left no forwarding address'
},
{
address: {
city: 'Dayton',
country: null,
state: 'OH',
zip: null
},
date: new Date('2019-06-21T19:47:00.000Z'),
description: 'Return to Sender due to No such Number'
},
{
address: {
city: 'Dayton',
country: null,
state: 'OH',
zip: null
},
date: new Date('2019-06-21T13:33:00.000Z'),
description: 'Return to Sender because the addressee moved and left no forwarding address'
},
{
address: {
city: 'Dayton',
country: null,
state: 'OH',
zip: null
},
date: new Date('2019-06-21T11:14:00.000Z'),
description: 'Out for Delivery'
},
{
address: {
city: 'Dayton',
country: null,
state: 'OH',
zip: null
},
date: new Date('2019-06-21T11:04:00.000Z'),
description: 'Sorting Complete'
},
{
address: {
city: 'Dayton',
country: null,
state: 'OH',
zip: null
},
date: new Date('2019-06-20T14:26:00.000Z'),
description: 'Arrival at Unit'
},
{
address: {
city: 'Dayton',
country: null,
state: 'OH',
zip: null
},
date: new Date('2019-06-20T05:53:00.000Z'),
description: 'Pre-Shipment Info Sent to USPS, USPS Awaiting Item'
},
{
address: {
city: 'Hebron',
country: null,
state: 'KY',
zip: null
},
date: new Date('2019-06-20T05:44:00.000Z'),
description: 'In transit'
},
{
address: {
city: 'Hebron',
country: null,
state: 'KY',
zip: null
},
date: new Date('2019-06-20T05:27:00.000Z'),
description: 'In transit'
},
{
address: {
city: 'Hebron',
country: null,
state: 'KY',
zip: null
},
date: new Date('2019-06-19T18:12:00.000Z'),
description: 'In transit'
},
{
address: {
city: 'Hebron',
country: null,
state: 'KY',
zip: null
},
date: new Date('2019-06-19T11:47:00.000Z'),
description: 'Estimated Delivery'
},
{
address: {
city: 'Hebron',
country: null,
state: 'KY',
zip: null
},
date: new Date('2019-06-19T09:05:00.000Z'),
description: 'Delivered to Partner Facility'
},
{
address: {
city: 'Grapevine',
country: null,
state: 'TX',
zip: null
},
date: new Date('2019-06-18T23:24:00.000Z'),
description: 'In transit'
},
{
address: {
city: 'Grapevine',
country: null,
state: 'TX',
zip: null
},
date: new Date('2019-06-18T17:38:00.000Z'),
description: 'Enroute Processed'
},
{
address: {
city: 'Grapevine',
country: null,
state: 'TX',
zip: null
},
date: new Date('2019-06-18T17:23:00.000Z'),
description: 'Arrived at Pick-up Location'
},
{
address: {
city: 'Grapevine',
country: null,
state: 'TX',
zip: null
},
date: new Date('2019-06-18T14:44:00.000Z'),
description: 'Arrived at Shipping Facility'
},
{
address: {
city: 'Carrollton',
country: null,
state: 'TX',
zip: null
},
date: new Date('2019-06-17T12:07:00.000Z'),
description: 'Electronic Shipping Info Received'
}
],
shippedAt: new Date('2019-06-21T11:14:00.000Z'),
url: 'https://tools.usps.com/go/TrackConfirmAction?qtc_tLabels1=4204540992748927005269000020006828'
};

assert.deepStrictEqual(actual, expected);
done();
});
});
Expand Down
1 change: 0 additions & 1 deletion test/util/geography.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ describe('petty-cache', function() {

const expected = {
city: 'Carrollton',
location: 'Carrollton, TX',
state: 'TX',
timezone: 'America/Chicago'
};
Expand Down

0 comments on commit 9bf4ed6

Please sign in to comment.