@things-factory/worksheet-base 4.3.539 → 4.3.541
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist-server/controllers/outbound/index.js +7 -1
- package/dist-server/controllers/outbound/index.js.map +1 -1
- package/dist-server/controllers/outbound/picking-worksheet-controller.js +512 -138
- package/dist-server/controllers/outbound/picking-worksheet-controller.js.map +1 -1
- package/dist-server/graphql/resolvers/worksheet/picking/activate-picking.js.map +1 -1
- package/dist-server/utils/lmd-util.js +213 -124
- package/dist-server/utils/lmd-util.js.map +1 -1
- package/package.json +9 -9
- package/server/controllers/outbound/index.ts +9 -1
- package/server/controllers/outbound/picking-worksheet-controller.ts +593 -131
- package/server/graphql/resolvers/worksheet/picking/activate-picking.ts +1 -1
- package/server/utils/lmd-util.ts +271 -140
package/server/utils/lmd-util.ts
CHANGED
|
@@ -20,13 +20,18 @@ import {
|
|
|
20
20
|
refreshAccessTokenNinjavan,
|
|
21
21
|
getAccessTokenCityLink
|
|
22
22
|
} from '@things-factory/integration-lmd'
|
|
23
|
-
import { createReleaseGoodLog, OrderPackage, ReleaseGood } from '@things-factory/sales-base'
|
|
23
|
+
import { createReleaseGoodLog, OrderPackage, ReleaseGood, OrderConsignment } from '@things-factory/sales-base'
|
|
24
24
|
import { MarketplaceOrder, MarketplaceStore } from '@things-factory/marketplace-base'
|
|
25
25
|
import { Lambda } from './lambda/lambda-const'
|
|
26
26
|
|
|
27
27
|
import { EcommerceController } from '../index'
|
|
28
|
+
import { config } from '@things-factory/env'
|
|
28
29
|
|
|
29
30
|
const debug = require('debug')('things-factory:worksheet-base:lmd-util')
|
|
31
|
+
const ninjavanConfig = config.get('lmdIntegrationNinjavan', {})
|
|
32
|
+
const citylinkConfig = config.get('lmdIntegrationCitylink', {})
|
|
33
|
+
const emsConfig = config.get('lmdIntegrationEms', {})
|
|
34
|
+
const lmdConfig = config.get('lmdIntegrationConfig', {})
|
|
30
35
|
import i18next from 'i18next'
|
|
31
36
|
|
|
32
37
|
// import { SellercraftController } from '../controllers'
|
|
@@ -40,6 +45,8 @@ const LMD_BOARDS = {
|
|
|
40
45
|
export async function createLmdParcel(releaseGoods, tx, domain, user, marketplaceOrder) {
|
|
41
46
|
try {
|
|
42
47
|
let parcelsRequest = []
|
|
48
|
+
let parcelsRequestV2 = []
|
|
49
|
+
|
|
43
50
|
for (let index = 0; index < releaseGoods.length; index++) {
|
|
44
51
|
let releaseGood = releaseGoods[index]
|
|
45
52
|
try {
|
|
@@ -54,27 +61,18 @@ export async function createLmdParcel(releaseGoods, tx, domain, user, marketplac
|
|
|
54
61
|
|
|
55
62
|
try {
|
|
56
63
|
let lmd: LastMileDelivery = releaseGood.lastMileDelivery
|
|
57
|
-
let
|
|
64
|
+
let version = lmd?.version ? lmd.version : 'v1'
|
|
58
65
|
let lmdBizplace: LastMileDeliveryBizplace = await tx.getRepository(LastMileDeliveryBizplace).findOne({
|
|
59
66
|
where: { lastMileDelivery: lmd.id, bizplace: releaseGood.bizplace },
|
|
60
67
|
relations: ['lastMileDelivery', 'bizplace']
|
|
61
68
|
})
|
|
62
69
|
|
|
70
|
+
let bizplace: Bizplace = await assignBizplaceAddress(lmdBizplace, releaseGood, tx)
|
|
71
|
+
let [latitude, longitude] = bizplace?.latlng?.split(',')
|
|
72
|
+
|
|
63
73
|
let validLmd: Boolean = await validateLmd(tx, releaseGood, lmd.id)
|
|
64
74
|
if (!validLmd) throw new Error(`Last mile delivery account is not valid. Please contact support team.`)
|
|
65
75
|
|
|
66
|
-
if (lmdBizplace?.customAddress == true) {
|
|
67
|
-
bizplace = await tx.getRepository(Bizplace).findOne({
|
|
68
|
-
where: { id: lmdBizplace.bizplace.id },
|
|
69
|
-
relations: ['domain']
|
|
70
|
-
})
|
|
71
|
-
} else {
|
|
72
|
-
bizplace = await tx.getRepository(Bizplace).findOne({
|
|
73
|
-
where: { domain: releaseGood.domain },
|
|
74
|
-
relations: ['domain']
|
|
75
|
-
})
|
|
76
|
-
}
|
|
77
|
-
|
|
78
76
|
const senderGeoArea: GeoArea = await tx
|
|
79
77
|
.getRepository(GeoArea)
|
|
80
78
|
.findOne({ where: { postalCode: bizplace.postalCode } })
|
|
@@ -99,37 +97,13 @@ export async function createLmdParcel(releaseGoods, tx, domain, user, marketplac
|
|
|
99
97
|
return releaseGood
|
|
100
98
|
}
|
|
101
99
|
|
|
102
|
-
const currentDate = new Date()
|
|
103
|
-
const currentTimestamp = Math.floor(Date.now() / 1000)
|
|
104
|
-
|
|
105
100
|
switch (lmd?.platform) {
|
|
106
101
|
case 'CITYLINK':
|
|
107
|
-
const expiredDateCitylink = new Date(lmd?.expiresIn)
|
|
108
|
-
// Calculate the renewal date as 2 days before the expiry date
|
|
109
|
-
const renewalDateCitylink = new Date(expiredDateCitylink.getTime() - 2 * 24 * 60 * 60 * 1000)
|
|
110
|
-
|
|
111
|
-
if (currentDate >= renewalDateCitylink) {
|
|
112
|
-
const lastMileDeliveryCitylink = await getAccessTokenCityLink(lmd?.id, { state: { tx } })
|
|
113
|
-
lmd.accessToken = lastMileDeliveryCitylink.accessToken
|
|
114
|
-
}
|
|
115
102
|
releaseGood = fnSplitAddress(releaseGood)
|
|
116
|
-
break
|
|
117
|
-
|
|
118
103
|
case 'NINJA_VAN':
|
|
119
104
|
case 'EMS':
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
if (currentTimestamp >= renewalTimestamp) {
|
|
123
|
-
let lastMileDelivery
|
|
124
|
-
if (lmd?.platform === 'NINJA_VAN') {
|
|
125
|
-
lastMileDelivery = await refreshAccessTokenNinjavan(lmd?.id, { state: { tx } })
|
|
126
|
-
} else if (lmd?.platform === 'EMS') {
|
|
127
|
-
lastMileDelivery = await getAccessTokenEfmx(lmd?.id, lmd?.clientId, lmd?.secretKey, { state: { tx } })
|
|
128
|
-
}
|
|
129
|
-
lmd.accessToken = lastMileDelivery.accessToken
|
|
130
|
-
}
|
|
105
|
+
lmd = await refreshLmdAccessToken(lmd, tx)
|
|
131
106
|
break
|
|
132
|
-
|
|
133
107
|
case 'DHL_EXPRESS':
|
|
134
108
|
releaseGood = fnSplitAddress(releaseGood)
|
|
135
109
|
break
|
|
@@ -138,8 +112,6 @@ export async function createLmdParcel(releaseGoods, tx, domain, user, marketplac
|
|
|
138
112
|
break
|
|
139
113
|
}
|
|
140
114
|
|
|
141
|
-
let [latitude, longitude] = bizplace?.latlng?.split(',')
|
|
142
|
-
|
|
143
115
|
let data = {
|
|
144
116
|
courier: lmd.platform,
|
|
145
117
|
clientId: lmd?.clientId,
|
|
@@ -154,6 +126,7 @@ export async function createLmdParcel(releaseGoods, tx, domain, user, marketplac
|
|
|
154
126
|
remark: releaseGood?.remark,
|
|
155
127
|
prefix: lmd?.prefix,
|
|
156
128
|
scope: lmd?.scope,
|
|
129
|
+
mps: lmd.mps,
|
|
157
130
|
|
|
158
131
|
//pass releaseGoodId for rg logs
|
|
159
132
|
releaseGoodId: releaseGood?.id,
|
|
@@ -163,6 +136,7 @@ export async function createLmdParcel(releaseGoods, tx, domain, user, marketplac
|
|
|
163
136
|
custRef: releaseGood?.refNo2,
|
|
164
137
|
cod: releaseGood?.codOption,
|
|
165
138
|
paidAmount: releaseGood?.paidAmount?.toString(),
|
|
139
|
+
source: releaseGood?.source,
|
|
166
140
|
|
|
167
141
|
recipient: releaseGood.attentionTo,
|
|
168
142
|
phone: releaseGood?.phone1 ? releaseGood.phone1 : releaseGood?.phone2 ? releaseGood.phone2 : '',
|
|
@@ -197,46 +171,34 @@ export async function createLmdParcel(releaseGoods, tx, domain, user, marketplac
|
|
|
197
171
|
handoverMethod: 1
|
|
198
172
|
}
|
|
199
173
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
if (lmd.platform == 'SAPX' || lmd.platform == 'VIETTEL_POST') {
|
|
224
|
-
if (lengthUnit == 'mm' || lengthUnit == 'm') {
|
|
225
|
-
;(items.width = (items.width / 10)?.toString()),
|
|
226
|
-
(items.length = (items.length / 10)?.toString()),
|
|
227
|
-
(items.height = (items.height / 10)?.toString())
|
|
228
|
-
} else {
|
|
229
|
-
;(items.width = items.width?.toString()),
|
|
230
|
-
(items.length = items.length?.toString()),
|
|
231
|
-
(items.height = items.height?.toString())
|
|
232
|
-
}
|
|
174
|
+
if (version == 'v2') {
|
|
175
|
+
let unprocessedConsignments: OrderConsignment = releaseGood?.orderConsignments
|
|
176
|
+
.map(oc => ({
|
|
177
|
+
...oc,
|
|
178
|
+
orderPackages: oc.orderPackages.filter(op => !op.parcelId)
|
|
179
|
+
}))
|
|
180
|
+
.filter(oc => oc.orderPackages.length > 0)
|
|
181
|
+
let consignmentData = await processOrderConsignments(unprocessedConsignments, data, lmd.platform)
|
|
182
|
+
parcelsRequestV2.push(...consignmentData)
|
|
183
|
+
} else {
|
|
184
|
+
// for version1 lmd endpoint
|
|
185
|
+
let unprocessedOrderPackages: OrderPackage[] = releaseGood.orderPackages.filter(op => !op.parcelId)
|
|
186
|
+
|
|
187
|
+
let packagesData = await Promise.all(
|
|
188
|
+
unprocessedOrderPackages.map(async op => {
|
|
189
|
+
const items = await Promise.all(
|
|
190
|
+
op.orderPackageItems.map(opi => processOrderPackageItems(opi, lmd.platform))
|
|
191
|
+
)
|
|
192
|
+
return {
|
|
193
|
+
...data,
|
|
194
|
+
refNo: op.name,
|
|
195
|
+
items: items
|
|
233
196
|
}
|
|
234
|
-
return items
|
|
235
197
|
})
|
|
236
|
-
|
|
237
|
-
})
|
|
198
|
+
)
|
|
238
199
|
|
|
239
|
-
|
|
200
|
+
parcelsRequest.push(...packagesData)
|
|
201
|
+
}
|
|
240
202
|
} catch (e) {
|
|
241
203
|
createReleaseGoodLog('create-lmd-parcel', e.message, releaseGood.id, {
|
|
242
204
|
state: { domain: null }
|
|
@@ -245,14 +207,23 @@ export async function createLmdParcel(releaseGoods, tx, domain, user, marketplac
|
|
|
245
207
|
}
|
|
246
208
|
}
|
|
247
209
|
|
|
210
|
+
if (parcelsRequestV2 && parcelsRequestV2.length > 0) {
|
|
211
|
+
await lmdIntegrationV2(parcelsRequestV2, releaseGoods, tx, domain, user, marketplaceOrder)
|
|
212
|
+
}
|
|
213
|
+
|
|
248
214
|
if (parcelsRequest && parcelsRequest.length > 0) {
|
|
249
215
|
let orderPackages: OrderPackage[]
|
|
216
|
+
let res
|
|
250
217
|
|
|
251
218
|
try {
|
|
252
|
-
|
|
253
|
-
{
|
|
219
|
+
res = await LastMileAPI.createParcel(
|
|
220
|
+
{
|
|
221
|
+
...releaseGoods[0].lastMileDelivery,
|
|
222
|
+
platform: lmdConfig.version.v1
|
|
223
|
+
},
|
|
254
224
|
parcelsRequest
|
|
255
225
|
)
|
|
226
|
+
|
|
256
227
|
for (let parcel of res) {
|
|
257
228
|
let courier = parcel.transporter
|
|
258
229
|
let transporter = courier.replace(/_/g, ' ')
|
|
@@ -304,19 +275,98 @@ export async function createLmdParcel(releaseGoods, tx, domain, user, marketplac
|
|
|
304
275
|
}
|
|
305
276
|
}
|
|
306
277
|
|
|
278
|
+
async function lmdIntegrationV2(parcelsRequestV2, releaseGoods, tx, domain, user, marketplaceOrder) {
|
|
279
|
+
let orderPackages: OrderPackage[]
|
|
280
|
+
let responses
|
|
281
|
+
let packages
|
|
282
|
+
let orderPackageIds = []
|
|
283
|
+
|
|
284
|
+
try {
|
|
285
|
+
responses = await LastMileAPI.createParcelV2(
|
|
286
|
+
{
|
|
287
|
+
...releaseGoods[0].lastMileDelivery,
|
|
288
|
+
platform: lmdConfig.version.v2
|
|
289
|
+
},
|
|
290
|
+
parcelsRequestV2
|
|
291
|
+
)
|
|
292
|
+
|
|
293
|
+
// diff way return data
|
|
294
|
+
for (let response of responses) {
|
|
295
|
+
let courier = response.transporter
|
|
296
|
+
let transporter = courier.replace(/_/g, ' ')
|
|
297
|
+
packages = response.packages
|
|
298
|
+
|
|
299
|
+
//update consignmentNumber @orderConsignmentNo in orderConsignment
|
|
300
|
+
await tx
|
|
301
|
+
.getRepository(OrderConsignment)
|
|
302
|
+
.update({ name: response.refNo }, { consignmentNumber: response.shipmentID })
|
|
303
|
+
|
|
304
|
+
for (let op of packages) {
|
|
305
|
+
await tx
|
|
306
|
+
.getRepository(OrderPackage)
|
|
307
|
+
.update({ name: op.refNo }, { parcelId: op.parcelId, transporter: transporter })
|
|
308
|
+
|
|
309
|
+
orderPackageIds.push(op.refNo)
|
|
310
|
+
|
|
311
|
+
createReleaseGoodLog(
|
|
312
|
+
'lmd-create-parcel',
|
|
313
|
+
JSON.stringify({ parcelId: op.parcelId, parcelMessage: response.message }),
|
|
314
|
+
response?.releaseGoodId,
|
|
315
|
+
{
|
|
316
|
+
state: { domain: null }
|
|
317
|
+
}
|
|
318
|
+
)
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
const qb: SelectQueryBuilder<OrderPackage> = tx
|
|
323
|
+
.getRepository(OrderPackage)
|
|
324
|
+
.createQueryBuilder('op')
|
|
325
|
+
.innerJoinAndSelect('op.releaseGood', 'rg')
|
|
326
|
+
.innerJoinAndSelect('rg.domain', 'd')
|
|
327
|
+
.innerJoinAndSelect('rg.bizplace', 'b')
|
|
328
|
+
.innerJoinAndSelect('b.company', 'c')
|
|
329
|
+
.innerJoinAndSelect('c.domain', 'companyDomain')
|
|
330
|
+
.innerJoinAndSelect('b.domain', 'd2')
|
|
331
|
+
.leftJoinAndSelect('rg.lastMileDelivery', 'lmd')
|
|
332
|
+
.where('op.name IN (:...orderPackageIds)', {
|
|
333
|
+
orderPackageIds: orderPackageIds
|
|
334
|
+
})
|
|
335
|
+
|
|
336
|
+
orderPackages = await qb.getMany()
|
|
337
|
+
|
|
338
|
+
await processAwbAndTrackingNo(orderPackages, tx, domain, user, marketplaceOrder)
|
|
339
|
+
} catch (error) {
|
|
340
|
+
parcelsRequestV2.map(rg => {
|
|
341
|
+
createReleaseGoodLog('error-lmd-create-parcel', error.message, rg.releaseGoodId, {
|
|
342
|
+
state: { domain: null }
|
|
343
|
+
})
|
|
344
|
+
})
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
307
348
|
export async function processAwbAndTrackingNo(orderPackages, tx, domain, user, marketplaceOrder) {
|
|
308
349
|
const ecommerceCtrl: EcommerceController = new EcommerceController(tx, domain, user)
|
|
309
350
|
let releaseOrders: ReleaseGood = []
|
|
310
351
|
|
|
311
352
|
for (let orderPackage of orderPackages) {
|
|
312
|
-
// await getConnection().transaction(async tx => {
|
|
313
353
|
try {
|
|
354
|
+
let awbRes
|
|
314
355
|
let releaseGood = orderPackage.releaseGood
|
|
315
|
-
let
|
|
316
|
-
|
|
317
|
-
|
|
356
|
+
let lmdVersion = releaseGood.lastMileDelivery.version
|
|
357
|
+
|
|
358
|
+
const platform = lmdConfig.version[lmdVersion === 'v2' ? 'v2' : 'v1']
|
|
359
|
+
awbRes = await LastMileAPI[lmdVersion === 'v2' ? 'getAwbV2' : 'getAwb'](
|
|
360
|
+
{
|
|
361
|
+
...orderPackage.releaseGood.lastMileDelivery,
|
|
362
|
+
platform
|
|
363
|
+
},
|
|
364
|
+
{
|
|
365
|
+
parcelId: orderPackage.parcelId,
|
|
366
|
+
staging: orderPackage.releaseGood.lastMileDelivery?.staging
|
|
367
|
+
}
|
|
318
368
|
)
|
|
319
|
-
|
|
369
|
+
|
|
320
370
|
let awbCustomize: Boolean = orderPackage.releaseGood.lastMileDelivery?.awbCustomize
|
|
321
371
|
|
|
322
372
|
if (awbRes) {
|
|
@@ -454,44 +504,37 @@ export async function processAwbAndTrackingNo(orderPackages, tx, domain, user, m
|
|
|
454
504
|
} catch (e) {
|
|
455
505
|
let releaseGoodId = orderPackage.releaseGood.id
|
|
456
506
|
logger.error(e)
|
|
457
|
-
createReleaseGoodLog('[processAwbAndTrackingNo]error-process-awb-and-tracking-no', e, releaseGoodId, {
|
|
507
|
+
createReleaseGoodLog('[processAwbAndTrackingNo]error-process-awb-and-tracking-no', e.message, releaseGoodId, {
|
|
458
508
|
state: { domain: null }
|
|
459
509
|
})
|
|
460
510
|
}
|
|
461
511
|
}
|
|
462
|
-
// })
|
|
463
512
|
}
|
|
464
513
|
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
// }
|
|
490
|
-
// }
|
|
491
|
-
// } catch (e) {
|
|
492
|
-
// logger.error(`[get-lmd-awb-sof-trigger]: ${e}`)
|
|
493
|
-
// }
|
|
494
|
-
//
|
|
514
|
+
function dateTime() {
|
|
515
|
+
const currentDate = new Date()
|
|
516
|
+
const timezoneGMT = 8 * 60 // 8 hours ahead in minutes
|
|
517
|
+
currentDate.setMinutes(currentDate.getMinutes() + timezoneGMT)
|
|
518
|
+
|
|
519
|
+
const yy = currentDate.getFullYear()
|
|
520
|
+
const mm = String(currentDate.getMonth() + 1).padStart(2, '0')
|
|
521
|
+
const dd = String(currentDate.getDate()).padStart(2, '0')
|
|
522
|
+
const hrs = String(currentDate.getHours()).padStart(2, '0')
|
|
523
|
+
const mins = String(currentDate.getMinutes()).padStart(2, '0')
|
|
524
|
+
const secs = String(currentDate.getSeconds()).padStart(2, '0')
|
|
525
|
+
const dateTime = `${yy}-${mm}-${dd}T${hrs}:${mins}:${secs}GMT+08:00`
|
|
526
|
+
|
|
527
|
+
return dateTime
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
function formatDate(isoDateString) {
|
|
531
|
+
const date = new Date(isoDateString)
|
|
532
|
+
const day = date.getDate()
|
|
533
|
+
const monthNames = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC']
|
|
534
|
+
const month = monthNames[date.getMonth()]
|
|
535
|
+
const year = date.getFullYear()
|
|
536
|
+
return `${day} ${month} ${year}`
|
|
537
|
+
}
|
|
495
538
|
|
|
496
539
|
async function processCallback(packageInfo) {
|
|
497
540
|
try {
|
|
@@ -521,22 +564,6 @@ async function processCallback(packageInfo) {
|
|
|
521
564
|
}
|
|
522
565
|
}
|
|
523
566
|
|
|
524
|
-
function dateTime() {
|
|
525
|
-
const currentDate = new Date()
|
|
526
|
-
const timezoneGMT = 8 * 60 // 8 hours ahead in minutes
|
|
527
|
-
currentDate.setMinutes(currentDate.getMinutes() + timezoneGMT)
|
|
528
|
-
|
|
529
|
-
const yy = currentDate.getFullYear()
|
|
530
|
-
const mm = String(currentDate.getMonth() + 1).padStart(2, '0')
|
|
531
|
-
const dd = String(currentDate.getDate()).padStart(2, '0')
|
|
532
|
-
const hrs = String(currentDate.getHours()).padStart(2, '0')
|
|
533
|
-
const mins = String(currentDate.getMinutes()).padStart(2, '0')
|
|
534
|
-
const secs = String(currentDate.getSeconds()).padStart(2, '0')
|
|
535
|
-
const dateTime = `${yy}-${mm}-${dd}T${hrs}:${mins}:${secs}GMT+08:00`
|
|
536
|
-
|
|
537
|
-
return dateTime
|
|
538
|
-
}
|
|
539
|
-
|
|
540
567
|
async function constructAwb(orderPackage, awbRes) {
|
|
541
568
|
const releaseOrder = orderPackage.releaseGood
|
|
542
569
|
let {
|
|
@@ -677,13 +704,117 @@ async function uploadInvoiceToS3(result, orderPackage) {
|
|
|
677
704
|
return invoice
|
|
678
705
|
}
|
|
679
706
|
|
|
680
|
-
function
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
707
|
+
export async function assignBizplaceAddress(lmdBizplace, releaseGood, tx) {
|
|
708
|
+
let bizplace: Bizplace
|
|
709
|
+
|
|
710
|
+
if (lmdBizplace?.customAddress == true) {
|
|
711
|
+
bizplace = await tx.getRepository(Bizplace).findOne({
|
|
712
|
+
where: { id: lmdBizplace.bizplace.id },
|
|
713
|
+
relations: ['domain']
|
|
714
|
+
})
|
|
715
|
+
} else {
|
|
716
|
+
bizplace = await tx.getRepository(Bizplace).findOne({
|
|
717
|
+
where: { domain: releaseGood.domain },
|
|
718
|
+
relations: ['domain']
|
|
719
|
+
})
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
return bizplace
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
export async function refreshLmdAccessToken(lmd, tx) {
|
|
726
|
+
const platform = lmd.platform
|
|
727
|
+
const currentDate = new Date()
|
|
728
|
+
const currentTimestamp = Math.floor(Date.now() / 1000)
|
|
729
|
+
|
|
730
|
+
switch (platform) {
|
|
731
|
+
case 'CITYLINK':
|
|
732
|
+
const expiredDateCitylink = new Date(lmd?.expiresIn)
|
|
733
|
+
// Calculate the renewal date as 2 days before the expiry date
|
|
734
|
+
const renewalDateCitylink = new Date(expiredDateCitylink.getTime() - 2 * 24 * 60 * 60 * 1000)
|
|
735
|
+
|
|
736
|
+
if (currentDate >= renewalDateCitylink) {
|
|
737
|
+
const lastMileDeliveryCitylink = await getAccessTokenCityLink(lmd?.id, { state: { tx } })
|
|
738
|
+
lmd.accessToken = lastMileDeliveryCitylink.accessToken
|
|
739
|
+
}
|
|
740
|
+
break
|
|
741
|
+
|
|
742
|
+
case 'NINJA_VAN':
|
|
743
|
+
case 'EMS':
|
|
744
|
+
const renewalTimestamp = lmd?.expiresIn - 12 * 60 * 60 // Renew 12 hr before expiry
|
|
745
|
+
|
|
746
|
+
if (currentTimestamp >= renewalTimestamp) {
|
|
747
|
+
let lastMileDelivery
|
|
748
|
+
if (platform === 'NINJA_VAN') {
|
|
749
|
+
lastMileDelivery = await refreshAccessTokenNinjavan(lmd?.id, { state: { tx } })
|
|
750
|
+
} else if (platform === 'EMS') {
|
|
751
|
+
lastMileDelivery = await getAccessTokenEfmx(lmd?.id, lmd?.clientId, lmd?.secretKey, { state: { tx } })
|
|
752
|
+
}
|
|
753
|
+
lmd.accessToken = lastMileDelivery.accessToken
|
|
754
|
+
}
|
|
755
|
+
break
|
|
756
|
+
|
|
757
|
+
default:
|
|
758
|
+
break
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
return lmd
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
export async function processOrderConsignments(orderConsignments, data, platform) {
|
|
765
|
+
return Promise.all(
|
|
766
|
+
orderConsignments.map(async oc => {
|
|
767
|
+
const packages = await Promise.all(oc.orderPackages.map(op => processOrderPackage(op, platform)))
|
|
768
|
+
return {
|
|
769
|
+
...data,
|
|
770
|
+
refNo: oc.name,
|
|
771
|
+
packages
|
|
772
|
+
}
|
|
773
|
+
})
|
|
774
|
+
)
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
export async function processOrderPackage(orderPackage, platform) {
|
|
778
|
+
const items = await Promise.all(orderPackage.orderPackageItems.map(opi => processOrderPackageItems(opi, platform)))
|
|
779
|
+
return {
|
|
780
|
+
refNo: orderPackage.name,
|
|
781
|
+
items
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
export async function processOrderPackageItems(opi, platform) {
|
|
786
|
+
try {
|
|
787
|
+
let lengthUnit = opi?.orderProduct?.productDetail?.lengthUnit?.toString()?.toLowerCase() || ''
|
|
788
|
+
|
|
789
|
+
let items = {
|
|
790
|
+
quantity: opi?.releaseQty?.toString() || ' ',
|
|
791
|
+
item: opi?.orderProduct?.product.name.slice(0, 49).trim() || ' ',
|
|
792
|
+
sku: opi?.orderProduct?.product.sku || ' ',
|
|
793
|
+
price:
|
|
794
|
+
opi?.orderProduct?.paidAmount?.toString() ||
|
|
795
|
+
(opi?.releaseQty * opi?.orderProduct?.productDetail?.costPrice || 0)?.toString() ||
|
|
796
|
+
' ',
|
|
797
|
+
weight: opi?.orderProduct?.productDetail?.grossWeight?.toString() || ' ',
|
|
798
|
+
width: opi?.orderProduct?.productDetail?.width || ' ',
|
|
799
|
+
length: opi?.orderProduct?.productDetail?.depth || ' ',
|
|
800
|
+
height: opi?.orderProduct?.productDetail?.height || ' '
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
if (platform == 'SAPX' || platform == 'VIETTEL_POST') {
|
|
804
|
+
if (lengthUnit == 'mm' || lengthUnit == 'm') {
|
|
805
|
+
;(items.width = (items.width / 10)?.toString()),
|
|
806
|
+
(items.length = (items.length / 10)?.toString()),
|
|
807
|
+
(items.height = (items.height / 10)?.toString())
|
|
808
|
+
} else {
|
|
809
|
+
;(items.width = items.width?.toString()),
|
|
810
|
+
(items.length = items.length?.toString()),
|
|
811
|
+
(items.height = items.height?.toString())
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
return items
|
|
815
|
+
} catch (e) {
|
|
816
|
+
logger.error(`[process-order-package-items]: ${e}`)
|
|
817
|
+
}
|
|
687
818
|
}
|
|
688
819
|
|
|
689
820
|
export async function validateLmd(tx, releaseGood, lmdId) {
|