Skip to content

Commit

Permalink
fixed using api in sicepat
Browse files Browse the repository at this point in the history
  • Loading branch information
egg- committed Aug 19, 2024
1 parent 94d535c commit f57283b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 85 deletions.
128 changes: 45 additions & 83 deletions lib/courier/sicepat.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,130 +6,87 @@ 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
}

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') {
checkpoint.status = tracker.STATUS.INFO_RECEIVED
} 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,
Expand All @@ -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))
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "delivery-tracker",
"version": "2.8.1",
"version": "2.8.3",
"author": {
"name": "egg",
"email": "i@egg.pe.kr"
Expand Down
2 changes: 1 addition & 1 deletion test/sicepat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit f57283b

Please sign in to comment.