@things-factory/worksheet-base 4.3.282 → 4.3.284

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.
@@ -41,14 +41,16 @@ export const completeBatchPickingResolver = {
41
41
  if (obsolete) {
42
42
  return { obsolete: true }
43
43
  }
44
- const bizplace: Bizplace = await getMyBizplace(domain, user)
45
- const worksheetController: WorksheetController = new WorksheetController(tx, domain, user)
46
- await worksheetController.notifyToCustomer(bizplace, {
47
- title: `Picking has been completed (${bizplace.name}, ${taskNo})`,
48
- body: `Items now are ready for sorting`,
49
- url: context.header.referer,
50
- data: { url: context.header.referer }
51
- })
44
+
45
+ // disabled notification to users due to performance issue by @ChrisLim-git
46
+ // const bizplace: Bizplace = await getMyBizplace(domain, user)
47
+ // const worksheetController: WorksheetController = new WorksheetController(tx, domain, user)
48
+ // await worksheetController.notifyToCustomer(bizplace, {
49
+ // title: `Picking has been completed (${bizplace.name}, ${taskNo})`,
50
+ // body: `Items now are ready for sorting`,
51
+ // url: context.header.referer,
52
+ // data: { url: context.header.referer }
53
+ // })
52
54
  }
53
55
  }
54
56
 
@@ -58,31 +60,36 @@ export async function completeBatchPicking(
58
60
  user: User,
59
61
  taskNo: string
60
62
  ): Promise<any> {
61
- const pickingWSCtrl: PickingWorksheetController = new PickingWorksheetController(tx, domain, user)
62
- const worksheet = await pickingWSCtrl.completeBatchPicking(taskNo)
63
-
64
- if (worksheet?.type) {
65
- const worksheetDetails: WorksheetDetail[] = worksheet.worksheetDetails
66
- const targetInventories: OrderInventory[] = worksheetDetails
67
- .filter(wsd => wsd.status != 'MISSING' && wsd.status != 'CANCELLED')
68
- .map((wsd: WorksheetDetail) => wsd.targetInventory)
69
-
70
- const releaseGoods: ReleaseGood[] = worksheetDetails.map(
71
- worksheetDetail => worksheetDetail.targetInventory && worksheetDetail.targetInventory.releaseGood
72
- )
73
-
74
- const uniqueReleaseGoods: ReleaseGood[] = []
75
- const map = new Map()
76
- for (const releaseGood of releaseGoods) {
77
- if (!map.has(releaseGood.id)) {
78
- map.set(releaseGood.id, true)
79
- uniqueReleaseGoods.push({ ...releaseGood })
63
+ try {
64
+ console.time(`complete-batch-picking[completeBatchPicking]: {domain:${domain.id}, taskNo:${taskNo}}`)
65
+
66
+ const pickingWSCtrl: PickingWorksheetController = new PickingWorksheetController(tx, domain, user)
67
+ const worksheet = await pickingWSCtrl.completeBatchPicking(taskNo)
68
+
69
+ if (worksheet?.type) {
70
+ const worksheetDetails: WorksheetDetail[] = worksheet.worksheetDetails
71
+ const targetInventories: OrderInventory[] = worksheetDetails
72
+ .filter(wsd => wsd.status != 'MISSING' && wsd.status != 'CANCELLED')
73
+ .map((wsd: WorksheetDetail) => wsd.targetInventory)
74
+
75
+ const releaseGoods: ReleaseGood[] = worksheetDetails.map(
76
+ worksheetDetail => worksheetDetail.targetInventory && worksheetDetail.targetInventory.releaseGood
77
+ )
78
+
79
+ let uniqueReleaseGoods: ReleaseGood[] = []
80
+ const map = new Map()
81
+
82
+ // remove any duplicated releaseGoods
83
+ for (const releaseGood of releaseGoods) {
84
+ if (!map.has(releaseGood.id)) {
85
+ map.set(releaseGood.id, true)
86
+ uniqueReleaseGoods.push({ ...releaseGood })
87
+ }
80
88
  }
81
- }
82
89
 
83
- for (let i = 0; i < uniqueReleaseGoods.length; i++) {
84
- let foundReleaseGood: ReleaseGood = await tx.getRepository(ReleaseGood).findOne({
85
- where: { id: uniqueReleaseGoods[i].id },
90
+ // fetch all release goods in 1 go
91
+ uniqueReleaseGoods = await tx.getRepository(ReleaseGood).find({
92
+ where: { id: In(uniqueReleaseGoods.map(rg => rg.id)) },
86
93
  relations: [
87
94
  'domain',
88
95
  'creator',
@@ -102,265 +109,265 @@ export async function completeBatchPicking(
102
109
  'bizplace.company.domain'
103
110
  ]
104
111
  })
105
- const orderSource: string = foundReleaseGood.source
106
- switch (orderSource) {
107
- case ApplicationType.SELLERCRAFT:
108
- const sellercraft: Sellercraft = await tx
109
- .getRepository(Sellercraft)
110
- .findOne({ domain: worksheet.bizplace.domain, status: SellercraftStatus.ACTIVE })
111
-
112
- if (sellercraft) {
113
- const initSCOrderShipment = async (sellercraft: Sellercraft, foundReleaseGood: ReleaseGood) => {
114
- await getManager().transaction(async txMgr => {
115
- const sellercraftCtrl: SellercraftController = new SellercraftController(txMgr, domain, user)
116
-
117
- if (!foundReleaseGood?.orderPackages?.length && !foundReleaseGood?.marketPackCallSuccess) {
118
- await sellercraftCtrl.packOrder(sellercraft, foundReleaseGood)
119
- }
120
112
 
121
- const rtsTriggerLevel: Setting = await tx.getRepository(Setting).findOne({
122
- where: { domain, category: 'id-rule', name: 'rts-trigger-level' }
113
+ // Integration triggers
114
+ for (let i = 0; i < uniqueReleaseGoods.length; i++) {
115
+ let foundReleaseGood: ReleaseGood = uniqueReleaseGoods[i]
116
+
117
+ const orderSource: string = foundReleaseGood.source
118
+ switch (orderSource) {
119
+ case ApplicationType.SELLERCRAFT:
120
+ const sellercraft: Sellercraft = await tx
121
+ .getRepository(Sellercraft)
122
+ .findOne({ domain: worksheet.bizplace.domain, status: SellercraftStatus.ACTIVE })
123
+
124
+ if (sellercraft) {
125
+ const initSCOrderShipment = async (sellercraft: Sellercraft, foundReleaseGood: ReleaseGood) => {
126
+ await getManager().transaction(async txMgr => {
127
+ const sellercraftCtrl: SellercraftController = new SellercraftController(txMgr, domain, user)
128
+
129
+ if (!foundReleaseGood?.orderPackages?.length && !foundReleaseGood?.marketPackCallSuccess) {
130
+ await sellercraftCtrl.packOrder(sellercraft, foundReleaseGood)
131
+ }
132
+
133
+ const rtsTriggerLevel: Setting = await tx.getRepository(Setting).findOne({
134
+ where: { domain, category: 'id-rule', name: 'rts-trigger-level' }
135
+ })
136
+
137
+ if (rtsTriggerLevel && parseInt(rtsTriggerLevel?.value || 0) == 1) {
138
+ await sellercraftCtrl.initiateOrderShipment(sellercraft, foundReleaseGood)
139
+ }
123
140
  })
141
+ }
124
142
 
125
- if (rtsTriggerLevel && parseInt(rtsTriggerLevel?.value || 0) == 1) {
126
- await sellercraftCtrl.initiateOrderShipment(sellercraft, foundReleaseGood)
127
- }
128
- })
143
+ // asynchronouly call to initiate sellercraft order shipment/ RTS
144
+ initSCOrderShipment(sellercraft, foundReleaseGood)
129
145
  }
146
+ break
130
147
 
131
- // asynchronouly call to initiate sellercraft order shipment/ RTS
132
- initSCOrderShipment(sellercraft, foundReleaseGood)
133
- }
134
- break
148
+ case ApplicationType.POWRUP:
149
+ const powrup: Powrup = await tx.getRepository(Powrup).findOne({ active: true, platform: 'POWRUP' })
135
150
 
136
- case ApplicationType.POWRUP:
137
- const powrup: Powrup = await tx.getRepository(Powrup).findOne({ active: true, platform: 'POWRUP' })
151
+ if (powrup) {
152
+ const initPowrupShipment = async (powrup: Powrup, foundReleaseGood: ReleaseGood) => {
153
+ await getManager().transaction(async txMgr => {
154
+ const powrupCtrl: PowrupController = new PowrupController()
138
155
 
139
- if (powrup) {
140
- const initPowrupShipment = async (powrup: Powrup, foundReleaseGood: ReleaseGood) => {
141
- await getManager().transaction(async txMgr => {
142
- const powrupCtrl: PowrupController = new PowrupController()
156
+ if (!foundReleaseGood?.orderPackages?.length && !foundReleaseGood?.marketPackCallSuccess) {
157
+ await powrupCtrl.packOrder(powrup, foundReleaseGood, domain, user, txMgr)
158
+ }
143
159
 
144
- if (!foundReleaseGood?.orderPackages?.length && !foundReleaseGood?.marketPackCallSuccess) {
145
- await powrupCtrl.packOrder(powrup, foundReleaseGood, domain, user, txMgr)
146
- }
160
+ const rtsTriggerLevel: Setting = await txMgr.getRepository(Setting).findOne({
161
+ where: { domain, category: 'id-rule', name: 'rts-trigger-level' }
162
+ })
147
163
 
148
- const rtsTriggerLevel: Setting = await txMgr.getRepository(Setting).findOne({
149
- where: { domain, category: 'id-rule', name: 'rts-trigger-level' }
164
+ if (rtsTriggerLevel && parseInt(rtsTriggerLevel?.value || 0) == 1) {
165
+ await powrupCtrl.initiateShipment(powrup, foundReleaseGood, domain, user, txMgr)
166
+ }
150
167
  })
168
+ }
151
169
 
152
- if (rtsTriggerLevel && parseInt(rtsTriggerLevel?.value || 0) == 1) {
153
- await powrupCtrl.initiateShipment(powrup, foundReleaseGood, domain, user, txMgr)
154
- }
155
- })
170
+ // asynchronouly call to initiate sellercraft order shipment/ RTS
171
+ initPowrupShipment(powrup, foundReleaseGood)
156
172
  }
173
+ break
174
+
175
+ case ApplicationType.MMS:
176
+ const companyDomain: Domain = foundReleaseGood.bizplace.company.domain
177
+ const marketplaceOrder: MarketplaceOrder = await tx.getRepository(MarketplaceOrder).findOne({
178
+ where: { orderNo: foundReleaseGood.refNo, domain: companyDomain },
179
+ relations: [
180
+ 'marketplaceOrderItems',
181
+ 'marketplaceOrderItems.marketplaceOrderShippingItems',
182
+ 'marketplaceOrderItems.marketplaceOrderShippingItems.marketplaceOrderShipping',
183
+ 'marketplaceStore',
184
+ 'marketplaceStore.marketplaceDistributors'
185
+ ]
186
+ })
187
+ const marketplaceStore: MarketplaceStore = marketplaceOrder.marketplaceStore
157
188
 
158
- // asynchronouly call to initiate sellercraft order shipment/ RTS
159
- initPowrupShipment(powrup, foundReleaseGood)
160
- }
161
- break
162
-
163
- case ApplicationType.MMS:
164
- const companyDomain: Domain = foundReleaseGood.bizplace.company.domain
165
- const marketplaceOrder: MarketplaceOrder = await tx.getRepository(MarketplaceOrder).findOne({
166
- where: { orderNo: foundReleaseGood.refNo, domain: companyDomain },
167
- relations: [
168
- 'marketplaceOrderItems',
169
- 'marketplaceOrderItems.marketplaceOrderShippingItems',
170
- 'marketplaceOrderItems.marketplaceOrderShippingItems.marketplaceOrderShipping',
171
- 'marketplaceStore',
172
- 'marketplaceStore.marketplaceDistributors'
173
- ]
174
- })
175
- const marketplaceStore: MarketplaceStore = marketplaceOrder.marketplaceStore
176
-
177
- const ecommerceCtrl: EcommerceController = new EcommerceController(tx, domain, user)
178
- if (marketplaceStore?.isAutoUpdateShipment) {
179
- await ecommerceCtrl.createOrderShip(foundReleaseGood, marketplaceStore, marketplaceOrder, companyDomain)
180
- }
181
- foundReleaseGood = await ecommerceCtrl.createOrderPackage(
182
- tx,
183
- marketplaceOrder,
184
- companyDomain,
185
- marketplaceStore,
186
- foundReleaseGood
187
- )
188
- break
189
-
190
- default:
191
- let orderPackage: OrderPackage = {
192
- name: OrderNoGenerator.orderPackage(),
193
- packageId: null,
194
- trackingNo: null,
195
- transporter: null,
196
- airwayBill: null,
197
- status: ORDER_STATUS.PROCESSING,
198
- releaseGood: foundReleaseGood,
199
- domain: foundReleaseGood.domain,
200
- bizplace: foundReleaseGood.bizplace,
201
- creator: foundReleaseGood.creator,
202
- updater: foundReleaseGood.updater
203
- }
204
-
205
- let savedOrderPackage: OrderPackage = await tx.getRepository(OrderPackage).save(orderPackage)
206
- const orderPackageItems: OrderPackageItem[] = foundReleaseGood.orderProducts.map(op => {
207
- return {
208
- name: OrderNoGenerator.orderPackageItem(),
209
- orderProduct: op,
189
+ const ecommerceCtrl: EcommerceController = new EcommerceController(tx, domain, user)
190
+ if (marketplaceStore?.isAutoUpdateShipment) {
191
+ await ecommerceCtrl.createOrderShip(foundReleaseGood, marketplaceStore, marketplaceOrder, companyDomain)
192
+ }
193
+ foundReleaseGood = await ecommerceCtrl.createOrderPackage(
194
+ tx,
195
+ marketplaceOrder,
196
+ companyDomain,
197
+ marketplaceStore,
198
+ foundReleaseGood
199
+ )
200
+ break
201
+
202
+ default:
203
+ let orderPackage: OrderPackage = {
204
+ name: OrderNoGenerator.orderPackage(),
205
+ packageId: null,
206
+ trackingNo: null,
207
+ transporter: null,
208
+ airwayBill: null,
210
209
  status: ORDER_STATUS.PROCESSING,
211
- releaseQty: op.releaseQty,
212
- orderPackage: savedOrderPackage,
210
+ releaseGood: foundReleaseGood,
213
211
  domain: foundReleaseGood.domain,
214
212
  bizplace: foundReleaseGood.bizplace,
215
213
  creator: foundReleaseGood.creator,
216
214
  updater: foundReleaseGood.updater
217
215
  }
218
- })
219
216
 
220
- await tx.getRepository(OrderPackageItem).save(orderPackageItems)
221
- orderPackage.orderPackageItems = orderPackageItems
222
- foundReleaseGood.orderPackages = [orderPackage]
223
- break
224
- }
217
+ let savedOrderPackage: OrderPackage = await tx.getRepository(OrderPackage).save(orderPackage)
218
+ const orderPackageItems: OrderPackageItem[] = foundReleaseGood.orderProducts.map(op => {
219
+ return {
220
+ name: OrderNoGenerator.orderPackageItem(),
221
+ orderProduct: op,
222
+ status: ORDER_STATUS.PROCESSING,
223
+ releaseQty: op.releaseQty,
224
+ orderPackage: savedOrderPackage,
225
+ domain: foundReleaseGood.domain,
226
+ bizplace: foundReleaseGood.bizplace,
227
+ creator: foundReleaseGood.creator,
228
+ updater: foundReleaseGood.updater
229
+ }
230
+ })
225
231
 
226
- const account: Account = await tx
227
- .getRepository(Account)
228
- .findOne({ where: { domain: foundReleaseGood.bizplace.domain, status: 'active' }, relations: ['domain'] })
229
-
230
- // Xilnex Create Sales Order and Post Sales Order to Sales Invoice
231
- const createSalesOrder = async (account, releaseGood, tx) => {
232
- if (account) {
233
- if (account.platform == 'xilnex') {
234
- try {
235
- delete releaseGood.orderPackages
236
- delete releaseGood.creator
237
- delete releaseGood.updater
238
-
239
- const body = {
240
- accessToken: account.accessToken,
241
- appId: account.accountId,
242
- accountInfo: account.accountInfo,
243
- req: JSON.stringify({
244
- releaseGood,
245
- orderProducts: releaseGood.orderProducts
232
+ await tx.getRepository(OrderPackageItem).save(orderPackageItems)
233
+ orderPackage.orderPackageItems = orderPackageItems
234
+ foundReleaseGood.orderPackages = [orderPackage]
235
+ break
236
+ }
237
+
238
+ const account: Account = await tx
239
+ .getRepository(Account)
240
+ .findOne({ where: { domain: foundReleaseGood.bizplace.domain, status: 'active' }, relations: ['domain'] })
241
+
242
+ // Xilnex Create Sales Order and Post Sales Order to Sales Invoice
243
+ const createSalesOrder = async (account, releaseGood, tx) => {
244
+ if (account) {
245
+ if (account.platform == 'xilnex') {
246
+ try {
247
+ delete releaseGood.orderPackages
248
+ delete releaseGood.creator
249
+ delete releaseGood.updater
250
+
251
+ const body = {
252
+ accessToken: account.accessToken,
253
+ appId: account.accountId,
254
+ accountInfo: account.accountInfo,
255
+ req: JSON.stringify({
256
+ releaseGood,
257
+ orderProducts: releaseGood.orderProducts
258
+ })
259
+ }
260
+
261
+ await sqsSendMessage({
262
+ body,
263
+ deduplicationId: releaseGood.refNo,
264
+ groupId: releaseGood.refNo,
265
+ queueName: 'prototypeQueue.fifo'
246
266
  })
267
+ } catch (e) {
268
+ throw new Error(e)
247
269
  }
248
-
249
- await sqsSendMessage({
250
- body,
251
- deduplicationId: releaseGood.refNo,
252
- groupId: releaseGood.refNo,
253
- queueName: 'prototypeQueue.fifo'
254
- })
255
- } catch (e) {
256
- throw new Error(e)
257
270
  }
258
271
  }
259
272
  }
260
- }
261
273
 
262
- if (foundReleaseGood.type == 'b2c') {
263
- createSalesOrder(account, foundReleaseGood, tx)
264
- }
274
+ if (foundReleaseGood.type == 'b2c') {
275
+ createSalesOrder(account, foundReleaseGood, tx)
276
+ }
265
277
 
266
- // trigger LMD API to create parcel
267
- if (
268
- foundReleaseGood?.orderPackages?.length &&
269
- foundReleaseGood.lmdOption &&
270
- foundReleaseGood.lastMileDelivery &&
271
- foundReleaseGood?.orderPackages?.some(op => op.parcelId == null)
272
- ) {
273
- await createLmdParcel([foundReleaseGood], tx)
274
- // trigger RTS
275
- try {
276
- const orderSource: string = foundReleaseGood.source
277
-
278
- if (orderSource == ApplicationType.SELLERCRAFT) {
279
- const sellercraft: Sellercraft = await tx.getRepository(Sellercraft).findOne({
280
- domain: foundReleaseGood.bizplace.domain,
281
- status: SellercraftStatus.ACTIVE
282
- })
283
- if (sellercraft) {
284
- const sellercraftCtrl: SellercraftController = new SellercraftController(tx, null, null)
285
- const rtsTriggerLevel: Setting = await tx.getRepository(Setting).findOne({
286
- where: { domain: foundReleaseGood.domain, category: 'id-rule', name: 'rts-trigger-level' }
278
+ // trigger LMD API to create parcel
279
+ if (
280
+ foundReleaseGood?.orderPackages?.length &&
281
+ foundReleaseGood.lmdOption &&
282
+ foundReleaseGood.lastMileDelivery &&
283
+ foundReleaseGood?.orderPackages?.some(op => op.parcelId == null)
284
+ ) {
285
+ await createLmdParcel([foundReleaseGood], tx)
286
+ // trigger RTS
287
+ try {
288
+ const orderSource: string = foundReleaseGood.source
289
+
290
+ if (orderSource == ApplicationType.SELLERCRAFT) {
291
+ const sellercraft: Sellercraft = await tx.getRepository(Sellercraft).findOne({
292
+ domain: foundReleaseGood.bizplace.domain,
293
+ status: SellercraftStatus.ACTIVE
287
294
  })
295
+ if (sellercraft) {
296
+ const sellercraftCtrl: SellercraftController = new SellercraftController(tx, null, null)
297
+ const rtsTriggerLevel: Setting = await tx.getRepository(Setting).findOne({
298
+ where: { domain: foundReleaseGood.domain, category: 'id-rule', name: 'rts-trigger-level' }
299
+ })
288
300
 
289
- if (rtsTriggerLevel && parseInt(rtsTriggerLevel?.value || 0) == 1) {
290
- sellercraftCtrl.initiateOrderShipment(sellercraft, foundReleaseGood)
301
+ if (rtsTriggerLevel && parseInt(rtsTriggerLevel?.value || 0) == 1) {
302
+ sellercraftCtrl.initiateOrderShipment(sellercraft, foundReleaseGood)
303
+ }
291
304
  }
292
305
  }
306
+ } catch (e) {
307
+ logger.error(`[get-lmd-awb-sof-trigger]: ${e}`)
293
308
  }
294
- } catch (e) {
295
- logger.error(`[get-lmd-awb-sof-trigger]: ${e}`)
309
+ //
296
310
  }
297
311
  //
298
312
  }
299
- //
300
- }
301
-
302
- const pickPackTargetInventories: OrderInventory[] = targetInventories
303
- .filter(targetInventory => targetInventory.releaseGood.packingOption)
304
- .map((pickPackTargetInventory: OrderInventory) => {
305
- return pickPackTargetInventory
306
- })
307
-
308
- const pickSortTargetInventories: OrderInventory[] = targetInventories
309
- .filter(targetInventory => !targetInventory.releaseGood.packingOption)
310
- .map((pickSortTargetInventory: OrderInventory) => {
311
- return pickSortTargetInventory
312
- })
313
-
314
- if (pickPackTargetInventories?.length) {
315
- // pull out all unique RO
316
- const roNoList: string[] = pickPackTargetInventories.map((oi: OrderInventory) => oi.releaseGood.name)
317
- let uniqueRoNo: string[] = []
318
- roNoList.forEach(roNo => {
319
- if (!uniqueRoNo.includes(roNo)) {
320
- uniqueRoNo.push(roNo)
321
- }
322
- })
323
313
 
324
- // loop to generate packing worksheet
325
- for (let i: number = 0; i < uniqueRoNo.length; i++) {
326
- const releaseGoodNo: string = uniqueRoNo[i]
314
+ // generate packing worksheet based on orders WITH packing option
315
+ const pickPackOrders: ReleaseGood[] = uniqueReleaseGoods.filter(rg => rg.packingOption)
327
316
 
317
+ if (pickPackOrders?.length) {
318
+ const roNoList: string[] = pickPackOrders.map((rg: ReleaseGood) => rg.name)
328
319
  const packingWSCtrl: PackingWorksheetController = new PackingWorksheetController(tx, domain, user)
329
- await packingWSCtrl.generatePackingWorksheet(releaseGoodNo)
320
+ await packingWSCtrl.generatePackingWorksheetByBulk(roNoList)
330
321
  }
331
- }
332
322
 
333
- // loop to generate sorting worksheet
334
- if (pickSortTargetInventories?.length) {
335
- const sortingWSCtrl: SortingWorksheetController = new SortingWorksheetController(tx, domain, user)
336
- await sortingWSCtrl.generateSortingWorksheet(worksheet, pickSortTargetInventories)
337
- }
338
- } else {
339
- let foundObsoleteInventories: any = worksheet
340
- await tx.getRepository(Inventory).update({ id: In(foundObsoleteInventories) }, { obsolete: true })
323
+ // generate packing worksheet with orderInventory based on orders WITHOUT packing option
324
+ const pickSortTargetInventories: OrderInventory[] = targetInventories
325
+ .filter(targetInventory => !targetInventory.releaseGood.packingOption)
326
+ .map((pickSortTargetInventory: OrderInventory) => {
327
+ return pickSortTargetInventory
328
+ })
329
+
330
+ // loop to generate sorting worksheet
331
+ if (pickSortTargetInventories?.length) {
332
+ const sortingWSCtrl: SortingWorksheetController = new SortingWorksheetController(tx, domain, user)
333
+ await sortingWSCtrl.generateSortingWorksheet(worksheet, pickSortTargetInventories)
334
+ }
341
335
 
342
- foundObsoleteInventories = foundObsoleteInventories
343
- .map(inv => {
344
- return "'" + inv.trim() + "'"
345
- })
346
- .join(',')
347
-
348
- //update any orders that is assigned with obsolete inventories
349
- await tx.getRepository(ReleaseGood).query(`
350
- update release_goods rg set status = 'OBSOLETE'
351
- from order_inventories oi
352
- where oi.release_good_id = rg.id
353
- and oi.inventory_id in (${foundObsoleteInventories})
354
- and rg.status in ('PICKING', 'PENDING_WORKSHEET', 'READY_TO_PICK')`)
355
-
356
- await tx.getRepository(Worksheet).query(`
357
- update worksheets w set status = 'DEACTIVATED', updater_id = '${user.id}', assignee_id = null
358
- from worksheet_details wd
359
- inner join order_inventories oi on oi.id = wd.target_inventory_id
360
- where wd.worksheet_id = w.id
361
- and oi.inventory_id in (${foundObsoleteInventories})
362
- `)
363
-
364
- return true
336
+ return false
337
+ } else {
338
+ let foundObsoleteInventories: any = worksheet
339
+ await tx.getRepository(Inventory).update({ id: In(foundObsoleteInventories) }, { obsolete: true })
340
+
341
+ foundObsoleteInventories = foundObsoleteInventories
342
+ .map(inv => {
343
+ return "'" + inv.trim() + "'"
344
+ })
345
+ .join(',')
346
+
347
+ //update any orders that is assigned with obsolete inventories
348
+ await tx.getRepository(ReleaseGood).query(`
349
+ update release_goods rg set status = 'OBSOLETE'
350
+ from order_inventories oi
351
+ where oi.release_good_id = rg.id
352
+ and oi.inventory_id in (${foundObsoleteInventories})
353
+ and rg.status in ('PICKING', 'PENDING_WORKSHEET', 'READY_TO_PICK')
354
+ `)
355
+
356
+ await tx.getRepository(Worksheet).query(`
357
+ update worksheets w set status = 'DEACTIVATED', updater_id = '${user.id}', assignee_id = null
358
+ from worksheet_details wd
359
+ inner join order_inventories oi on oi.id = wd.target_inventory_id
360
+ where wd.worksheet_id = w.id
361
+ and oi.inventory_id in (${foundObsoleteInventories})
362
+ `)
363
+
364
+ return true
365
+ }
366
+ } catch (error) {
367
+ logger.error(error)
368
+ throw error
369
+ } finally {
370
+ console.timeEnd(`complete-batch-picking[completeBatchPicking]: {domain:${domain.id}, taskNo:${taskNo}}`)
365
371
  }
372
+
366
373
  }
@@ -146,7 +146,7 @@ export async function inventoriesByStrategy(
146
146
  })
147
147
  .andWhere('"INV"."obsolete" = false')
148
148
  .andWhere(
149
- 'case when "INV"."expiration_date" is not null and "PROD"."shelf_life" is not null then CURRENT_DATE < "INV"."expiration_date" - "PROD"."shelf_life" else true end'
149
+ 'case when "INV"."expiration_date" is not null and "PROD"."min_outbound_shelf_life" is not null then CURRENT_DATE < "INV"."expiration_date" - "PROD"."min_outbound_shelf_life" else true end'
150
150
  )
151
151
  .setParameters({
152
152
  domainId: domain.id,
@@ -6,7 +6,7 @@ import { Bizplace } from '@things-factory/biz-base'
6
6
  import { Board, headlessModel, pdf } from '@things-factory/board-service'
7
7
  import { logger } from '@things-factory/env'
8
8
  import { GeoArea } from '@things-factory/geography'
9
- import { LastMileAPI, LastMileDelivery } from '@things-factory/integration-lmd'
9
+ import { LastMileAPI, LastMileDelivery, refreshAccessTokenNinjavan} from '@things-factory/integration-lmd'
10
10
  import { OrderPackage, ReleaseGood } from '@things-factory/sales-base'
11
11
 
12
12
  // import { SellercraftController } from '../controllers'
@@ -35,6 +35,13 @@ export async function createLmdParcel(releaseGoods, tx) {
35
35
  .getRepository(GeoArea)
36
36
  .findOne({ where: { postalCode: releaseGood.postalCode } })
37
37
 
38
+ if(lmd?.platform=='NINJA_VAN'){
39
+ const currentTimestamp: number = Math.floor(Date.now() / 1000)
40
+ if (currentTimestamp >= lmd?.expiresIn) {
41
+ await refreshAccessTokenNinjavan(lmd?.id,{state:{ tx }})
42
+ }
43
+ }
44
+
38
45
  let data = {
39
46
  courier: lmd.platform,
40
47
  clientId: lmd?.clientId,