@things-factory/sales-base 4.3.131 → 4.3.135

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.
@@ -1,6 +1,7 @@
1
1
  import { FileUpload, GraphQLUpload } from 'graphql-upload'
2
2
  import { Arg, Ctx, Directive, Mutation, Resolver } from 'type-graphql'
3
3
  import { EntityManager, getConnection, getRepository, In, IsNull, Repository } from 'typeorm'
4
+ import { logger } from '@things-factory/env'
4
5
 
5
6
  import { Attachment, createAttachments, deleteAttachmentsByRef } from '@things-factory/attachment-base'
6
7
  import { User } from '@things-factory/auth-base'
@@ -185,240 +186,250 @@ export class DraftReleaseGoodMutation {
185
186
  // @Arg('bizplaceId', type => String) bizplaceId: string
186
187
  ): Promise<boolean> {
187
188
  const { domain, user, tx } = context.state
188
-
189
- const updatableDraftOrders = await tx.getRepository(DraftReleaseGood).find({
190
- where: {
191
- domain,
192
- id: In(ids),
193
- status: DRAFT_RELEASE_ORDER_STATUS.DRAFT
194
- },
195
- relations: [
196
- 'orderProducts',
197
- 'orderProducts.product',
198
- 'orderProducts.productBundle',
199
- 'shippingOrder',
200
- 'bizplace',
201
- 'bizplace.domain',
202
- 'domain',
203
- 'creator',
204
- 'updater'
205
- ]
206
- })
207
- if (updatableDraftOrders.length > 0) {
208
- let createdReleaseGoods: ReleaseGood[] = []
209
- const settingRepo: Repository<Setting> = tx?.getRepository(Setting) || getRepository(Setting)
210
-
211
- const roNoSetting: Setting = await settingRepo.findOne({
189
+ try {
190
+ const updatableDraftOrders = await tx.getRepository(DraftReleaseGood).find({
212
191
  where: {
213
192
  domain,
214
- name: ORDER_NUMBER_SETTING_KEY.RO_NUMBER_RULE
215
- }
193
+ id: In(ids),
194
+ status: DRAFT_RELEASE_ORDER_STATUS.DRAFT
195
+ },
196
+ relations: [
197
+ 'orderProducts',
198
+ 'orderProducts.product',
199
+ 'orderProducts.productBundle',
200
+ 'shippingOrder',
201
+ 'bizplace',
202
+ 'bizplace.domain',
203
+ 'domain',
204
+ 'creator',
205
+ 'updater'
206
+ ]
216
207
  })
217
208
 
218
- for (let index = 0; index < updatableDraftOrders.length; index++) {
219
- await getConnection().transaction(async (parentTx: EntityManager) => {
220
- const draftOrder = updatableDraftOrders[index]
221
-
222
- let foundPermittedBizplace: Bizplace
223
- let companyBizplace: Bizplace
209
+ if (updatableDraftOrders.length > 0) {
210
+ let createdReleaseGoods: ReleaseGood[] = []
211
+ const settingRepo: Repository<Setting> = tx?.getRepository(Setting) || getRepository(Setting)
224
212
 
225
- if (draftOrder?.bizplace?.id) {
226
- foundPermittedBizplace = await parentTx.getRepository(Bizplace).findOne(draftOrder.bizplace.id, {
227
- relations: ['company', 'company.domain']
228
- })
229
- const companyDomain: Domain = foundPermittedBizplace.company.domain
230
- companyBizplace = await parentTx.getRepository(Bizplace).findOne({ where: { domain: companyDomain } })
231
- } else {
232
- return false
213
+ const roNoSetting: Setting = await settingRepo.findOne({
214
+ where: {
215
+ domain,
216
+ name: ORDER_NUMBER_SETTING_KEY.RO_NUMBER_RULE
233
217
  }
218
+ })
234
219
 
235
- const bizplaces: Bizplace[] = [foundPermittedBizplace, companyBizplace]
220
+ for (let index = 0; index < updatableDraftOrders.length; index++) {
221
+ await getConnection().transaction(async (parentTx: EntityManager) => {
222
+ const draftOrder = updatableDraftOrders[index]
236
223
 
237
- let productInventory = await InventoryUtil.bizplaceProductInventory(
238
- bizplaces,
239
- {
240
- filters: [
241
- {
242
- name: 'productId',
243
- operator: 'in',
244
- value: [
245
- ...draftOrder.orderProducts
246
- .filter(itm => itm?.product)
247
- .map(itm => {
248
- return itm.product.id
249
- }),
250
- ...draftOrder.orderProducts
251
- .filter(itm => itm?.productBundle)
252
- .map(itm => {
253
- return itm.productBundle.id
254
- })
255
- ]
256
- }
257
- ]
258
- },
259
- context,
260
- parentTx
261
- )
262
-
263
- draftOrder.orderProducts = draftOrder.orderProducts.map(itm => {
264
- let foundProductInv = productInventory.items.find(
265
- i =>
266
- (i.productId == itm?.product?.id || i.productId == itm?.productBundle?.id) &&
267
- i.packingType == itm.packingType &&
268
- i.packingSize == itm.packingSize &&
269
- i.uom == itm.uom &&
270
- itm.releaseQty <= i.remainQty
271
- )
272
- return {
273
- ...itm,
274
- releaseUomValue: (foundProductInv.remainUomValue / foundProductInv.remainQty) * itm.releaseQty,
275
- status: foundProductInv ? 'suffice' : 'insufficient'
224
+ let foundPermittedBizplace: Bizplace
225
+ let companyBizplace: Bizplace
226
+
227
+ if (draftOrder?.bizplace?.id) {
228
+ foundPermittedBizplace = await parentTx.getRepository(Bizplace).findOne(draftOrder.bizplace.id, {
229
+ relations: ['company', 'company.domain']
230
+ })
231
+ const companyDomain: Domain = foundPermittedBizplace.company.domain
232
+ companyBizplace = await parentTx.getRepository(Bizplace).findOne({ where: { domain: companyDomain } })
233
+ } else {
234
+ return false
276
235
  }
277
- })
278
236
 
279
- let insufficient = draftOrder.orderProducts.find(op => op.status == 'insufficient')
280
- if (!insufficient) {
281
- //create RO
282
- let releaseGood = { ...draftOrder }
283
- delete releaseGood.id
284
- delete releaseGood.name
285
- delete releaseGood.createdAt
286
- delete releaseGood.createdBy
287
- delete releaseGood.updatedAt
288
- delete releaseGood.updatedBy
289
-
290
- let orderInventories = []
291
-
292
- for (let index = 0; index < draftOrder.orderProducts.length; index++) {
293
- const itm: Partial<OrderProduct> = draftOrder.orderProducts[index]
294
- let data = []
295
- if (itm.productBundle) {
296
- let bundle: ProductBundle = await parentTx.getRepository(ProductBundle).findOne({
297
- where: { id: itm.productBundle.id },
298
- relations: [
299
- 'productBundleSettings',
300
- 'productBundleSettings.productDetail',
301
- 'productBundleSettings.product'
302
- ]
303
- })
237
+ const bizplaces: Bizplace[] = [foundPermittedBizplace, companyBizplace]
304
238
 
305
- for (let index2 = 0; index2 < bundle.productBundleSettings.length; index2++) {
306
- const objProductBundleSetting = bundle.productBundleSettings[index2]
307
- data.push({
308
- sku: objProductBundleSetting.product.sku,
309
- packingType: objProductBundleSetting.productDetail.packingType,
310
- packingSize: objProductBundleSetting.productDetail.packingSize,
311
- uom: objProductBundleSetting.productDetail.uom,
312
- releaseQty: objProductBundleSetting.bundleQty * itm.releaseQty,
313
- assignedQty: undefined,
314
- assignedUomValue: undefined,
315
- releaseUomValue: objProductBundleSetting.bundleQty * itm.releaseQty * itm.releaseUomValue
316
- })
317
- }
318
- } else {
319
- data = [
320
- {
321
- sku: itm.product.sku,
322
- packingType: itm.packingType,
323
- packingSize: itm.packingSize,
324
- uom: itm.uom,
325
- releaseQty: itm.releaseQty,
326
- assignedQty: undefined,
327
- assignedUomValue: undefined,
328
- releaseUomValue: itm.releaseUomValue
329
- }
330
- ]
331
- }
239
+ let loggerJson = JSON.stringify({ refNo: draftOrder.refNo, bizplaceId: draftOrder.bizplace.id, domain })
240
+ try {
241
+ console.time(`creating_order:${loggerJson}`)
332
242
 
333
- data.forEach(data => {
334
- let existingOI = orderInventories.find(
335
- itm =>
336
- itm.sku == data.sku &&
337
- itm.packingType == data.packingType &&
338
- itm.packingSize == data.packingSize &&
339
- itm.uom == data.uom
243
+ let productInventory = await InventoryUtil.bizplaceProductInventory(
244
+ bizplaces,
245
+ {
246
+ filters: [
247
+ {
248
+ name: 'productId',
249
+ operator: 'in',
250
+ value: [
251
+ ...draftOrder.orderProducts
252
+ .filter(itm => itm?.product)
253
+ .map(itm => {
254
+ return itm.product.id
255
+ }),
256
+ ...draftOrder.orderProducts
257
+ .filter(itm => itm?.productBundle)
258
+ .map(itm => {
259
+ return itm.productBundle.id
260
+ })
261
+ ]
262
+ }
263
+ ]
264
+ },
265
+ context,
266
+ parentTx
267
+ )
268
+
269
+ draftOrder.orderProducts = draftOrder.orderProducts.map(itm => {
270
+ let foundProductInv = productInventory.items.find(
271
+ i =>
272
+ (i.productId == itm?.product?.id || i.productId == itm?.productBundle?.id) &&
273
+ i.packingType == itm.packingType &&
274
+ i.packingSize == itm.packingSize &&
275
+ i.uom == itm.uom &&
276
+ itm.releaseQty <= i.remainQty
340
277
  )
341
-
342
- if (existingOI) {
343
- existingOI.releaseQty = existingOI.releaseQty + data.releaseQty
344
- existingOI.releaseUomValue = existingOI.releaseUomValue + data.uomValue
345
- existingOI.uomValue = existingOI.uomValue + data.uomValue
346
- } else {
347
- orderInventories.push({ ...data, orderProduct: itm })
278
+ return {
279
+ ...itm,
280
+ releaseUomValue: (foundProductInv.remainUomValue / foundProductInv.remainQty) * itm.releaseQty,
281
+ status: foundProductInv ? 'suffice' : 'insufficient'
348
282
  }
349
283
  })
350
- }
351
- try {
352
- await getConnection().transaction(async (childTx: EntityManager) => {
353
- let availableItems: any[] = await bulkReleaseGoodsAvailableItemsFunction(
354
- orderInventories,
355
- draftOrder.bizplace.id,
356
- context,
357
- childTx
358
- )
359
- if (availableItems.some(item => !item.releaseQty || item.releaseQty > item.assignedQty))
360
- throw new ValidationError({
361
- ...ValidationError.ERROR_CODES.INSUFFICIENT_STOCK,
362
- detail: { data: JSON.stringify(availableItems) }
363
- })
364
- // update orderInventories if availableItems are valid
365
- releaseGood.orderInventories = availableItems.map(itm => {
366
- return {
367
- ...itm,
368
- releaseQty: itm.releaseQty,
369
- releaseUomValue: itm.releaseUomValue,
370
- uomValue: itm.uomValue
284
+
285
+ let insufficient = draftOrder.orderProducts.find(op => op.status == 'insufficient')
286
+ if (!insufficient) {
287
+ //create RO
288
+ let releaseGood = { ...draftOrder }
289
+ delete releaseGood.id
290
+ delete releaseGood.name
291
+ delete releaseGood.createdAt
292
+ delete releaseGood.createdBy
293
+ delete releaseGood.updatedAt
294
+ delete releaseGood.updatedBy
295
+
296
+ let orderInventories = []
297
+
298
+ for (let index = 0; index < draftOrder.orderProducts.length; index++) {
299
+ const itm: Partial<OrderProduct> = draftOrder.orderProducts[index]
300
+ let data = []
301
+ if (itm.productBundle) {
302
+ let bundle: ProductBundle = await parentTx.getRepository(ProductBundle).findOne({
303
+ where: { id: itm.productBundle.id },
304
+ relations: [
305
+ 'productBundleSettings',
306
+ 'productBundleSettings.productDetail',
307
+ 'productBundleSettings.product'
308
+ ]
309
+ })
310
+
311
+ for (let index2 = 0; index2 < bundle.productBundleSettings.length; index2++) {
312
+ const objProductBundleSetting = bundle.productBundleSettings[index2]
313
+ data.push({
314
+ sku: objProductBundleSetting.product.sku,
315
+ packingType: objProductBundleSetting.productDetail.packingType,
316
+ packingSize: objProductBundleSetting.productDetail.packingSize,
317
+ uom: objProductBundleSetting.productDetail.uom,
318
+ releaseQty: objProductBundleSetting.bundleQty * itm.releaseQty,
319
+ assignedQty: undefined,
320
+ assignedUomValue: undefined,
321
+ releaseUomValue: objProductBundleSetting.bundleQty * itm.releaseQty * itm.releaseUomValue
322
+ })
323
+ }
324
+ } else {
325
+ data = [
326
+ {
327
+ sku: itm.product.sku,
328
+ packingType: itm.packingType,
329
+ packingSize: itm.packingSize,
330
+ uom: itm.uom,
331
+ releaseQty: itm.releaseQty,
332
+ assignedQty: undefined,
333
+ assignedUomValue: undefined,
334
+ releaseUomValue: itm.releaseUomValue
335
+ }
336
+ ]
371
337
  }
372
- })
373
- let createdReleaseGood: ReleaseGood = await bulkGenerateReleaseGood(
374
- releaseGood,
375
- releaseGood.bizplace.id,
376
- roNoSetting,
377
- domain,
378
- user,
379
- childTx
380
- )
381
- await childTx
382
- .getRepository(DraftReleaseGood)
383
- .update(
384
- { id: draftOrder.id },
385
- { releaseGood: createdReleaseGood, status: DRAFT_RELEASE_ORDER_STATUS.SUBMITTED }
386
- )
387
338
 
388
- const directReceiveSetting: Setting = await tx.getRepository(Setting).findOne({
389
- where: { domain, category: 'id-rule', name: 'enable-direct-receive-release-order' }
390
- })
339
+ data.forEach(data => {
340
+ let existingOI = orderInventories.find(
341
+ itm =>
342
+ itm.sku == data.sku &&
343
+ itm.packingType == data.packingType &&
344
+ itm.packingSize == data.packingSize &&
345
+ itm.uom == data.uom
346
+ )
347
+
348
+ if (existingOI) {
349
+ existingOI.releaseQty = existingOI.releaseQty + data.releaseQty
350
+ existingOI.releaseUomValue = existingOI.releaseUomValue + data.uomValue
351
+ existingOI.uomValue = existingOI.uomValue + data.uomValue
352
+ } else {
353
+ orderInventories.push({ ...data, orderProduct: itm })
354
+ }
355
+ })
356
+ }
357
+ await getConnection().transaction(async (childTx: EntityManager) => {
358
+ let availableItems: any[] = await bulkReleaseGoodsAvailableItemsFunction(
359
+ orderInventories,
360
+ draftOrder.bizplace.id,
361
+ context,
362
+ childTx
363
+ )
364
+ if (availableItems.some(item => !item.releaseQty || item.releaseQty > item.assignedQty))
365
+ throw new ValidationError({
366
+ ...ValidationError.ERROR_CODES.INSUFFICIENT_STOCK,
367
+ detail: { data: JSON.stringify(availableItems) }
368
+ })
369
+ // update orderInventories if availableItems are valid
370
+ releaseGood.orderInventories = availableItems.map(itm => {
371
+ return {
372
+ ...itm,
373
+ releaseQty: itm.releaseQty,
374
+ releaseUomValue: itm.releaseUomValue,
375
+ uomValue: itm.uomValue
376
+ }
377
+ })
378
+ let createdReleaseGood: ReleaseGood = await bulkGenerateReleaseGood(
379
+ releaseGood,
380
+ releaseGood.bizplace.id,
381
+ roNoSetting,
382
+ domain,
383
+ user,
384
+ childTx
385
+ )
386
+ await childTx
387
+ .getRepository(DraftReleaseGood)
388
+ .update(
389
+ { id: draftOrder.id },
390
+ { releaseGood: createdReleaseGood, status: DRAFT_RELEASE_ORDER_STATUS.SUBMITTED }
391
+ )
392
+
393
+ const directReceiveSetting: Setting = await tx.getRepository(Setting).findOne({
394
+ where: { domain, category: 'id-rule', name: 'enable-direct-receive-release-order' }
395
+ })
391
396
 
392
- const partnerDirectReceiveSetting: PartnerSetting = await tx.getRepository(PartnerSetting).findOne({
393
- where: { setting: directReceiveSetting, domain, partnerDomain: draftOrder.bizplace?.domain }
394
- })
397
+ const partnerDirectReceiveSetting: PartnerSetting = await tx.getRepository(PartnerSetting).findOne({
398
+ where: { setting: directReceiveSetting, domain, partnerDomain: draftOrder.bizplace?.domain }
399
+ })
395
400
 
396
- let settingValue
401
+ let settingValue
397
402
 
398
- if (
399
- !isNaN(partnerDirectReceiveSetting?.value) &&
400
- !isNaN(parseFloat(partnerDirectReceiveSetting?.value))
401
- ) {
402
- settingValue = parseFloat(partnerDirectReceiveSetting.value)
403
- } else {
404
- settingValue = parseFloat(directReceiveSetting.value)
405
- }
403
+ if (
404
+ !isNaN(partnerDirectReceiveSetting?.value) &&
405
+ !isNaN(parseFloat(partnerDirectReceiveSetting?.value))
406
+ ) {
407
+ settingValue = parseFloat(partnerDirectReceiveSetting.value)
408
+ } else {
409
+ settingValue = parseFloat(directReceiveSetting.value)
410
+ }
406
411
 
407
- createdReleaseGood = await confirmReleaseGood(createdReleaseGood.name, context, childTx)
412
+ createdReleaseGood = await confirmReleaseGood(createdReleaseGood.name, context, childTx)
408
413
 
409
- if (settingValue > 1) {
410
- createdReleaseGood = await receiveReleaseGood(createdReleaseGood.name, context, childTx)
411
- }
412
- })
413
- } catch (error) {
414
- console.error(error)
414
+ if (settingValue > 1) {
415
+ createdReleaseGood = await receiveReleaseGood(createdReleaseGood.name, context, childTx)
416
+ }
417
+ })
418
+ }
419
+ } catch (e) {
420
+ logger.error(`draft-release-good-mutation[generateReleaseGoodsFromDraft:${loggerJson}]: ${e?.message}`)
421
+ } finally {
422
+ console.timeEnd(`creating_order:${loggerJson}`)
415
423
  }
416
- }
417
- })
424
+ })
425
+ }
418
426
  }
427
+
428
+ return true
429
+ } catch (e) {
430
+ logger.error(`draft-release-good-mutation[generateReleaseGoodsFromDraft]: ${e?.message}`)
419
431
  }
420
432
 
421
- return true
422
433
  }
423
434
  }
424
435
 
@@ -761,8 +772,8 @@ export async function upsertDraftReleaseGoodProducts(
761
772
  updatePatch['packingType'] = op?.packingType
762
773
  ? op.packingType
763
774
  : product?.packingType
764
- ? product.packingType
765
- : productBundle.packingType
775
+ ? product.packingType
776
+ : productBundle.packingType
766
777
  updatePatch['uom'] = op?.uom ? op.uom : product?.primaryUnit ? product.primaryUnit : 'UNIT'
767
778
  updatePatch['uomValue'] = op?.uomValue ? op.uomValue : product?.primaryValue ? product.primaryValue : 0
768
779
 
@@ -159,7 +159,7 @@ export class ReleaseGoodMutation {
159
159
  childTx
160
160
  )
161
161
  }
162
- } catch (error) {}
162
+ } catch (error) { }
163
163
  })
164
164
 
165
165
  if (errorsCaught.length)
@@ -652,8 +652,8 @@ export async function generateReleaseGoodFunction(
652
652
  orderMethod: releaseGood?.orderMethod
653
653
  ? releaseGood.orderMethod
654
654
  : releaseGood.orderInventories[0]?.inventory?.id
655
- ? ORDER_METHOD.SELECT_BY_PALLET
656
- : ORDER_METHOD.SELECT_BY_PRODUCT,
655
+ ? ORDER_METHOD.SELECT_BY_PALLET
656
+ : ORDER_METHOD.SELECT_BY_PRODUCT,
657
657
  status: ORDER_STATUS.PENDING,
658
658
  packageId: releaseGood?.packageId,
659
659
  storeName: releaseGood?.storeName,
@@ -931,26 +931,6 @@ export async function confirmReleaseGood(name: string, context: any, tx?: Entity
931
931
 
932
932
  const orderSource: string = foundReleaseGood.source
933
933
  switch (orderSource) {
934
- case ApplicationType.SELLERCRAFT:
935
- if (foundReleaseGood.type == 'b2c' && sellercraft) {
936
- try {
937
- let rg = await sellercraftCtrl.packOrder(sellercraft, {
938
- ...foundReleaseGood,
939
- orderProducts: foundOPs
940
- })
941
- foundReleaseGood = rg
942
- } catch (error) {
943
- logger.error(
944
- `release-good-mutation[confirmReleaseGood]:(data:${JSON.stringify({
945
- sellercraft,
946
- foundReleaseGood,
947
- domain,
948
- user
949
- })}) ${error} `
950
- )
951
- }
952
- }
953
- break
954
934
 
955
935
  case ApplicationType.MMS:
956
936
  // find for any existing marketplace store connections