diff --git a/lib/courier/sicepat.js b/lib/courier/sicepat.js index 44164e7..00ca110 100644 --- a/lib/courier/sicepat.js +++ b/lib/courier/sicepat.js @@ -6,87 +6,16 @@ var moment = require('moment') var tracker = require('../') var loadHandler = function (opts) { - if (opts && opts.apikey) { - return { - trackingInfo: function (number) { - return { - method: 'GET', - url: 'http://api.sicepat.com/customer/waybill?waybill=' + number, - json: true - } - }, - parser: { - trace: function (body, number) { - var courier = { - code: tracker.COURIER.SICEPAT.CODE, - name: tracker.COURIER.SICEPAT.NAME - } - - var result = { - courier: courier, - number: number, - status: tracker.STATUS.PENDING - } - - var checkpoints = [] - var history = body.sicepat.result.track_history || [] - for (var i = history.length - 1; i >= 0; i--) { - var item = history[i] - var checkpoint = { - courier: courier, - location: '', - message: '[' + item.status + '] ' + (item.receiver_name || item.city || ''), - status: tracker.STATUS.IN_TRANSIT, - time: moment(item.date_time + '+0700').utc().format('YYYY-MM-DDTHH:mmZ') - } - - if (item.status === 'PICKREQ') { - checkpoint.status = tracker.STATUS.INFO_RECEIVED - } else if (item.status === 'DELIVERED') { - checkpoint.status = tracker.STATUS.DELIVERED - } - - checkpoints.push(checkpoint) - } - - result.checkpoints = checkpoints - result.status = tracker.normalizeStatus(result.checkpoints) - - return result - } - }, - load: function (tracking, cb) { - tracking.headers = { - 'api-key': opts.apikey - } - request(tracking, function (err, res, body) { - if (err) { - return cb(err) - } - - if (!body.sicepat) { - return cb(tracker.error(tracker.ERROR.UNKNOWN)) - } - if (body.sicepat.status.code !== 200) { - return cb(tracker.error(body.sicepat.status.description)) - } - - cb(null, res, body) - }) - } - } - } - return { trackingInfo: function (number) { return { method: 'GET', - url: 'https://content-main-api-production.sicepat.com/public/check-awb/' + number, + url: 'http://api.sicepat.com/customer/waybill?waybill=' + number, json: true } }, parser: { - trace: function (data, number) { + trace: function (body, number) { var courier = { code: tracker.COURIER.SICEPAT.CODE, name: tracker.COURIER.SICEPAT.NAME @@ -94,19 +23,26 @@ var loadHandler = function (opts) { var result = { courier: courier, - number: data.waybill_number, + number: number, status: tracker.STATUS.PENDING } var checkpoints = [] - for (var i = 0; i < data.track_history.length; i++) { - var item = data.track_history[i] + var history = body.sicepat.result.track_history || [] + for (var i = history.length - 1; i >= 0; i--) { + var item = history[i] var checkpoint = { courier: courier, location: '', - message: item.city || item.receiver_name, + message: + '[' + + item.status + + '] ' + + (item.receiver_name || item.city || ''), status: tracker.STATUS.IN_TRANSIT, - time: moment(item.date_time + '+0700', 'YYYY-MM-DD HH:mmZ').utc().format('YYYY-MM-DDTHH:mm:ssZ') + time: moment(item.date_time + '+0700') + .utc() + .format('YYYY-MM-DDTHH:mmZ') } if (item.status === 'PICKREQ') { @@ -114,22 +50,43 @@ var loadHandler = function (opts) { } else if (item.status === 'DELIVERED') { checkpoint.status = tracker.STATUS.DELIVERED } + checkpoints.push(checkpoint) } - result.checkpoints = checkpoints.reverse() + result.checkpoints = checkpoints result.status = tracker.normalizeStatus(result.checkpoints) return result } }, load: function (tracking, cb) { - request(tracking, cb) + tracking.headers = { + 'api-key': opts.apikey + } + request(tracking, function (err, res, body) { + if (err) { + return cb(err) + } + + if (!body.sicepat) { + return cb(tracker.error(tracker.ERROR.UNKNOWN)) + } + if (body.sicepat.status.code !== 200) { + return cb(tracker.error(body.sicepat.status.description)) + } + + cb(null, res, body) + }) } } } module.exports = function (opts) { + if (!opts || !opts.apikey) { + throw new Error('API Key is required') + } + var handler = loadHandler(opts) return { trackingInfo: handler.trackingInfo, @@ -140,14 +97,19 @@ module.exports = function (opts) { } try { - if (!body.sicepat) { // internal server error + if (!body.sicepat) { + // internal server error return cb(tracker.error(body.message)) } + if (body.sicepat.status.code !== 200) { return cb(tracker.error(body.sicepat.status.description)) } - var result = handler.parser.trace(body.sicepat.result, number) - cb(result ? null : tracker.error(tracker.ERROR.INVALID_NUMBER), result) + var result = handler.parser.trace(body, number) + cb( + result ? null : tracker.error(tracker.ERROR.INVALID_NUMBER), + result + ) } catch (e) { cb(tracker.error(e.message)) } diff --git a/package.json b/package.json index 8d439d6..5245274 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "delivery-tracker", - "version": "2.8.1", + "version": "2.8.3", "author": { "name": "egg", "email": "i@egg.pe.kr" diff --git a/test/sicepat.js b/test/sicepat.js index e49f432..889426d 100644 --- a/test/sicepat.js +++ b/test/sicepat.js @@ -7,7 +7,7 @@ var assert = require('assert') var prepare = require('./fixtures/prepare') var tracker = require('../') -var courier = tracker.courier(tracker.COURIER.SICEPAT.CODE) +var courier = tracker.courier(tracker.COURIER.SICEPAT.CODE, { apikey: 'test' }) describe(tracker.COURIER.SICEPAT.NAME, function () { var deliveredNumber = '123456789012'