Skip to content

Commit

Permalink
Added shippedAt and deliveredAt fields to results
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronmwhitehead committed Jul 1, 2019
1 parent 91ffe04 commit 043e46d
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions carriers/usps.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ const geography = require('../util/geography');

const CITY_BLACKLIST = /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'];

function USPS(options) {
this.isTrackingNumberValid = function(trackingNumber) {
if ([/^(91|92|93|94|95|96)\d{20}$/, /^(91|92|93|94|95|96)\d{18}$/, /^940\d{19}$/, /^940\d{17}$/, /^\d{22}$/, /^\d{20}$/].some(regex => regex.test(trackingNumber))){
Expand All @@ -17,7 +23,7 @@ function USPS(options) {
return false;
};
this.track = function(trackingNumber, callback) {
const host = 'http://production.shippingapis.com/ShippingAPI.dll?API=TrackV2&XML=';
const baseUrl = options.baseUrl || 'http://production.shippingapis.com/ShippingAPI.dll?API=TrackV2&XML=';

const obj = {
TrackFieldRequest: {
Expand All @@ -32,7 +38,7 @@ function USPS(options) {
}

var xml = builder.create(obj, { headless: true }).end({ pretty: false });
const url = host + encodeURIComponent(xml);
const url = baseUrl + encodeURIComponent(xml);

request(url, function (err, res) {
if (err) {
Expand Down Expand Up @@ -116,6 +122,14 @@ function USPS(options) {
description: scanDetail.Event[0]
};

if (DELIVERED_TRACKING_STATUS_CODES.includes(scanDetail.EventCode[0])) {
results.deliveredAt = new Date(event.date);
}

if (SHIPPED_TRACKING_STATUS_CODES.includes(scanDetail.EventCode[0])) {
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) {
Expand Down

0 comments on commit 043e46d

Please sign in to comment.