-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathindex.js
219 lines (207 loc) · 4.13 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
'use strict'
var moment = require('moment')
// cache
var COURIER_MODULES = {}
var COURIER = {
KOREAPOST: {
CODE: 'koreapost',
NAME: 'Korea Post'
},
ECARGO: {
CODE: 'ecargo',
NAME: 'Ecargo'
},
FEDEX: {
CODE: 'fedex',
NAME: 'FedEx'
},
AUSPOST: {
CODE: 'auspost',
NAME: 'Australia Post'
},
PANTOS: {
CODE: 'pantos',
NAME: 'Pantos'
},
RINCOS: {
CODE: 'rincos',
NAME: 'RINCOS'
},
ROYALMAIL: {
CODE: 'royalmail',
NAME: 'Royal Mail'
},
USPS: {
CODE: 'usps',
NAME: 'USPS'
},
CJKOREAEXPRESS: {
CODE: 'cjkoreaexpress',
NAME: 'CJ Korea Express'
},
DEPPON: {
CODE: 'deppon',
NAME: 'Deppon'
},
POSLAJU: {
CODE: 'poslaju',
NAME: 'POS Laju'
},
YELLOEXPRESS: {
CODE: 'yelloexpress',
NAME: 'Yello Express'
},
EFS: {
CODE: 'efs',
NAME: 'EFS'
},
AIRBRIDGE: {
CODE: 'airbridge',
NAME: 'AIR BRIDGE'
},
UPS: {
CODE: 'ups',
NAME: 'UPS'
},
TNT: {
CODE: 'tnt',
NAME: 'TNT'
},
CESCO: {
CODE: 'cesco',
NAME: 'CESCO'
},
XPOST: {
CODE: 'xpost',
NAME: 'XPOST'
},
KERRYTHAI: {
CODE: 'kerrythai',
NAME: 'KERRYTHAI'
},
SICEPAT: {
CODE: 'sicepat',
NAME: 'SICEPAT'
},
XIOEXPRESS: {
CODE: 'xioexpress',
NAME: 'XIOEXPRESS'
},
EPARCEL: {
CODE: 'eparcel',
NAME: 'eParcel'
},
LBC: {
CODE: 'lbc',
NAME: 'LBC'
},
JNT: {
CODE: 'jnt',
NAME: 'J&T'
},
DHL: {
CODE: 'dhl',
NAME: 'DHL'
},
CANADAPOST: {
CODE: 'canadapost',
NAME: 'Canada Post'
},
PAXEL: {
CODE: 'paxel',
NAME: 'Paxel'
}
}
var ERROR = {
UNKNOWN: -1,
NOT_SUPPORT_SHIPMENT: 20,
SEARCH_AGAIN: 21,
INVALID_NUMBER: 10,
INVALID_NUMBER_LENGTH: 11,
INVALID_NUMBER_HEADER: 12,
INVALID_NUMBER_COUNTRY: 13,
REQUIRED_APIKEY: 30,
SERVER_ERROR: 500
}
var ERROR_MESSAGE = {
30: 'required apikey.',
20: 'shipment does not support.',
21: 'working on it. Please search it again.',
10: 'invalid trace number.',
11: 'invalid trace number.',
12: 'invalid trace number.',
13: 'invalid trace number.'
}
var STATUS = {
INFO_RECEIVED: 'InfoReceived',
PENDING: 'Pending',
IN_TRANSIT: 'InTransit',
DELIVERED: 'Delivered',
RETURNED: 'Returned',
EXCEPTION: 'Exception',
FAIL_ATTEMPT: 'FailAttempt'
}
var getCourier = function (slug, opts) {
slug = slug || 'undefined'
var key = slug.toUpperCase()
if (!COURIER[key]) {
throw new Error('shipment does not support.')
}
if (!COURIER_MODULES[slug]) {
COURIER_MODULES[slug] = require('./courier/' + slug)
}
var courier = COURIER_MODULES[slug](opts)
courier.CODE = COURIER[key].CODE
courier.NAME = COURIER[key].NAME
return courier
}
module.exports = {
COURIER: COURIER,
STATUS: STATUS,
ERROR: ERROR,
error: function (code, message) {
var error = {
code: ERROR.UNKNOWN,
message: 'unknown error.'
}
if (ERROR_MESSAGE[code]) {
error.code = code
error.message = ERROR_MESSAGE[code]
} else if (message) {
error.message = message
error.code = code
} else {
error.message = code
}
return error
},
courier: function (slug, opts) {
return getCourier(slug, opts)
},
normalizeStatus: function (checkpoints) {
var status = STATUS.PENDING
var isDelivered = false
var latestCheckpoint = null
for (var i = 0; i < checkpoints.length; i++) {
isDelivered = isDelivered || checkpoints[i].status === STATUS.DELIVERED
if (
(checkpoints[i].status === STATUS.DELIVERED ||
latestCheckpoint === null) &&
checkpoints[i].time
) {
latestCheckpoint = checkpoints[i]
}
}
if (latestCheckpoint) {
// Last checkpoint information is not final information.
// The order may be mixed when the local delivery company progresses.
status = isDelivered ? STATUS.DELIVERED : latestCheckpoint.status
if ([STATUS.IN_TRANSIT, STATUS.FAIL_ATTEMPT].indexOf(status) !== -1) {
if (moment().unix() - moment(latestCheckpoint.time).unix() > 259200) {
status = STATUS.EXCEPTION
}
}
}
return status
}
}