@heuristical/trackit
Version:
This module allows you to connect to many shipping carriers like UPS and FedEx and download tracking data for your packages in a common schema
145 lines • 7.7 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.A1Client = void 0;
/* eslint-disable
camelcase,
constructor-super,
no-constant-condition,
no-eval,
no-this-before-super,
no-unused-vars,
standard/no-callback-literal,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS201: Simplify complex destructure assignments
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const xml2js_1 = require("xml2js");
const moment_timezone_1 = __importDefault(require("moment-timezone"));
const shipper_1 = require("./shipper");
class A1Client extends shipper_1.ShipperClient {
constructor(options) {
super();
this.STATUS_MAP = new Map([
['101', shipper_1.STATUS_TYPES.EN_ROUTE],
['102', shipper_1.STATUS_TYPES.EN_ROUTE],
['302', shipper_1.STATUS_TYPES.OUT_FOR_DELIVERY],
['304', shipper_1.STATUS_TYPES.DELAYED],
['301', shipper_1.STATUS_TYPES.DELIVERED]
]);
this.options = options;
this.parser = new xml2js_1.Parser();
}
validateResponse(response, cb) {
function handleResponse(xmlErr, trackResult) {
var _a, _b, _c, _d, _e, _f, _g;
if ((xmlErr != null) || (trackResult == null)) {
return cb(xmlErr);
}
const trackingInfo = (_b = (_a = trackResult === null || trackResult === void 0 ? void 0 : trackResult.AmazonTrackingResponse) === null || _a === void 0 ? void 0 : _a.PackageTrackingInfo) === null || _b === void 0 ? void 0 : _b[0];
if ((trackingInfo === null || trackingInfo === void 0 ? void 0 : trackingInfo.TrackingNumber) == null) {
const errorInfo = (_d = (_c = trackResult === null || trackResult === void 0 ? void 0 : trackResult.AmazonTrackingResponse) === null || _c === void 0 ? void 0 : _c.TrackingErrorInfo) === null || _d === void 0 ? void 0 : _d[0];
const error = (_g = (_f = (_e = errorInfo === null || errorInfo === void 0 ? void 0 : errorInfo.TrackingErrorDetail) === null || _e === void 0 ? void 0 : _e[0]) === null || _f === void 0 ? void 0 : _f.ErrorDetailCodeDesc) === null || _g === void 0 ? void 0 : _g[0];
if (error != null) {
return cb(error);
}
cb('unknown error');
}
return cb(null, trackingInfo);
}
this.parser.reset();
return this.parser.parseString(response, handleResponse);
}
presentAddress(address) {
var _a, _b, _c, _d;
if (address == null) {
return;
}
const city = (_a = address === null || address === void 0 ? void 0 : address.City) === null || _a === void 0 ? void 0 : _a[0];
const stateCode = (_b = address === null || address === void 0 ? void 0 : address.StateProvince) === null || _b === void 0 ? void 0 : _b[0];
const countryCode = (_c = address === null || address === void 0 ? void 0 : address.CountryCode) === null || _c === void 0 ? void 0 : _c[0];
const postalCode = (_d = address === null || address === void 0 ? void 0 : address.PostalCode) === null || _d === void 0 ? void 0 : _d[0];
return this.presentLocation({ city, stateCode, countryCode, postalCode });
}
getStatus(shipment) {
var _a, _b, _c, _d, _e;
const lastActivity = (_c = (_b = (_a = shipment === null || shipment === void 0 ? void 0 : shipment.TrackingEventHistory) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.TrackingEventDetail) === null || _c === void 0 ? void 0 : _c[0];
const statusCode = (_d = lastActivity === null || lastActivity === void 0 ? void 0 : lastActivity.EventCode) === null || _d === void 0 ? void 0 : _d[0];
if (statusCode == null) {
return;
}
const code = (_e = statusCode.match(/EVENT_(.*)$/)) === null || _e === void 0 ? void 0 : _e[1];
if (isNaN(code)) {
return;
}
if (this.STATUS_MAP.has(code.toString())) {
return this.STATUS_MAP.get(code.toString());
}
else {
if (code < 300) {
return shipper_1.STATUS_TYPES.EN_ROUTE;
}
else {
return shipper_1.STATUS_TYPES.UNKNOWN;
}
}
}
getActivitiesAndStatus(shipment) {
var _a, _b, _c, _d, _e;
const activities = [];
const status = this.getStatus(shipment);
let rawActivities = (_b = (_a = shipment === null || shipment === void 0 ? void 0 : shipment.TrackingEventHistory) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.TrackingEventDetail;
rawActivities = rawActivities !== null && rawActivities !== void 0 ? rawActivities : [];
for (const rawActivity of rawActivities) {
let datetime, timestamp;
const location = this.presentAddress((_c = rawActivity === null || rawActivity === void 0 ? void 0 : rawActivity.EventLocation) === null || _c === void 0 ? void 0 : _c[0]);
const rawTimestamp = (_d = rawActivity === null || rawActivity === void 0 ? void 0 : rawActivity.EventDateTime) === null || _d === void 0 ? void 0 : _d[0];
if (rawTimestamp != null) {
const eventTime = moment_timezone_1.default(rawTimestamp);
timestamp = eventTime.toDate();
datetime = rawTimestamp.slice(0, 19);
}
const details = (_e = rawActivity === null || rawActivity === void 0 ? void 0 : rawActivity.EventCodeDesc) === null || _e === void 0 ? void 0 : _e[0];
if ((details != null) && (timestamp != null)) {
const activity = { timestamp, datetime, location, details };
activities.push(activity);
}
}
return { activities, status };
}
getEta(shipment) {
var _a, _b, _c, _d;
const activities = ((_b = (_a = shipment === null || shipment === void 0 ? void 0 : shipment.TrackingEventHistory) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.TrackingEventDetail) || [];
const firstActivity = activities[activities.length - 1];
if (((_c = firstActivity === null || firstActivity === void 0 ? void 0 : firstActivity.EstimatedDeliveryDate) === null || _c === void 0 ? void 0 : _c[0]) == null) {
return;
}
return moment_timezone_1.default(`${(_d = firstActivity === null || firstActivity === void 0 ? void 0 : firstActivity.EstimatedDeliveryDate) === null || _d === void 0 ? void 0 : _d[0]}T00:00:00Z`).toDate();
}
getService(shipment) {
return null;
}
getWeight(shipment) {
return null;
}
getDestination(shipment) {
var _a;
return this.presentAddress((_a = shipment === null || shipment === void 0 ? void 0 : shipment.PackageDestinationLocation) === null || _a === void 0 ? void 0 : _a[0]);
}
requestOptions({ trackingNumber }) {
return {
method: 'GET',
uri: `http://www.aoneonline.com/pages/customers/trackingrequest.php?tracking_number=${trackingNumber}`
};
}
}
exports.A1Client = A1Client;
//# sourceMappingURL=a1.js.map