@things-factory/marketplace-base 3.8.28 → 3.8.32

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.
@@ -5,6 +5,7 @@ import { Bizplace, getCustomerBizplaces } from '@things-factory/biz-base'
5
5
  import { FulfillmentAPI, FulfillmentCenter } from '@things-factory/integration-fulfillment'
6
6
  import { MarketplaceStore, StoreAPI } from '@things-factory/integration-marketplace'
7
7
  import { Domain } from '@things-factory/shell'
8
+ import { Product, ProductBundle, ProductBundleSetting } from '@things-factory/product-base'
8
9
 
9
10
  import {
10
11
  MarketplaceOrder,
@@ -134,6 +135,36 @@ export const syncAllMarketplaceOrder = {
134
135
  updater: user
135
136
  }
136
137
 
138
+ if (
139
+ !marketplaceOrder?.releaseOrderId &&
140
+ cancelStatuses.includes(marketplaceOrder.status) &&
141
+ !cancelStatuses.includes(existingMarketplaceOrder.status)
142
+ ) {
143
+ for (let item of marketplaceOrder.marketplaceOrderItems) {
144
+ let foundVariations = await getRepository(MarketplaceProductVariation).find({
145
+ where: {
146
+ domain: marketplaceOrder.domain,
147
+ sku: item.marketplaceProductVariation.sku
148
+ },
149
+ relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
150
+ })
151
+
152
+ let activeVariations: MarketplaceProductVariation[] = foundVariations.filter(
153
+ variation =>
154
+ variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED' && variation.sku != null
155
+ )
156
+
157
+ await Promise.all(
158
+ activeVariations.map(async variation => {
159
+ await calculateReserveQtyForBundle(variation, marketplaceOrder.domain, item.qty, '-')
160
+
161
+ variation.reserveQty -= item.qty
162
+ variation.qty += item.qty
163
+ await getRepository(MarketplaceProductVariation).save(variation)
164
+ })
165
+ )
166
+ }
167
+ }
137
168
  if (marketplaceOrder?.releaseOrderId) {
138
169
  const fulfillmentCenter: FulfillmentCenter = marketplaceOrder.fulfillmentCenter
139
170
  const centerId: string = fulfillmentCenter.centerId
@@ -330,13 +361,17 @@ export const syncAllMarketplaceOrder = {
330
361
  }
331
362
 
332
363
  let orderTotalWeight: number = 0
333
- let foundVariation
364
+ let foundVariations = []
334
365
  for (var j = 0; j < order.orderItems.length; j++) {
335
366
  var item = order.orderItems[j]
336
- foundVariation = await getRepository(MarketplaceProductVariation).findOne({
367
+ let foundVariation = await getRepository(MarketplaceProductVariation).findOne({
337
368
  where: { domain: marketplaceStore.domain, variationId: item.variationId }
338
369
  })
339
370
 
371
+ if (foundVariation) {
372
+ foundVariations.push(foundVariation)
373
+ }
374
+
340
375
  if (!foundVariation) {
341
376
  let newVariation: any = {
342
377
  variationId: item.variationId,
@@ -424,158 +459,199 @@ export const syncAllMarketplaceOrder = {
424
459
  if (nonMatchedOrderItems.length > 0 && !cancelStatuses.includes(marketplaceOrder.status)) {
425
460
  await Promise.all(
426
461
  nonMatchedOrderItems.map(async item => {
427
- if (foundVariation.sku) {
428
- let allVariations: MarketplaceProductVariation[] = await getRepository(
429
- MarketplaceProductVariation
430
- ).find({
431
- where: { domain: foundVariation.domain, sku: foundVariation.sku },
432
- relations: [
433
- 'domain',
434
- 'marketplaceProduct',
435
- 'marketplaceProduct.marketplaceStore',
436
- 'marketplaceProduct.marketplaceStore.marketplaceDistributors'
437
- ]
438
- })
439
-
440
- let variationsFromOtherStores: MarketplaceProductVariation[] = allVariations.filter(
441
- variation =>
442
- variation.id != foundVariation.id &&
443
- variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
444
- )
445
-
446
- variationsFromOtherStores.map(async variation => {
447
- variation.reserveQty -= item.qty
448
-
449
- variation = await getRepository(MarketplaceProductVariation).save(variation)
450
- })
462
+ for (var foundVariation of foundVariations) {
463
+ if (foundVariation.sku) {
464
+ let allVariations: MarketplaceProductVariation[] = await getRepository(
465
+ MarketplaceProductVariation
466
+ ).find({
467
+ where: { domain: foundVariation.domain, sku: foundVariation.sku },
468
+ relations: [
469
+ 'domain',
470
+ 'marketplaceProduct',
471
+ 'marketplaceProduct.marketplaceStore',
472
+ 'marketplaceProduct.marketplaceStore.marketplaceDistributors'
473
+ ]
474
+ })
475
+
476
+ let activeVariations: MarketplaceProductVariation[] = allVariations.filter(
477
+ variation => variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
478
+ )
479
+
480
+ await Promise.all(
481
+ activeVariations.map(async variation => {
482
+ await calculateReserveQtyForBundle(
483
+ variation,
484
+ marketplaceOrder.domain,
485
+ item.qty,
486
+ '-'
487
+ )
488
+
489
+ variation.reserveQty -= item.qty
490
+ variation.qty += item.qty
491
+
492
+ variation = await getRepository(MarketplaceProductVariation).save(variation)
493
+ })
494
+ )
495
+ }
451
496
  }
452
-
453
- foundVariation.qty += item.qty
454
- foundVariation = await getRepository(MarketplaceProductVariation).save(foundVariation)
455
497
  })
456
498
  )
457
499
 
458
500
  await getRepository(MarketplaceOrderItem).delete(nonMatchedOrderItems)
459
501
  }
460
502
 
461
- existingOrderItems = await Promise.all(
462
- marketplaceOrderItems.map(async item => {
463
- const matchedOrderItem = existingOrderItems.find(
464
- itm =>
465
- itm.name == item.name &&
466
- itm.marketplaceProductVariation.variationId == item.marketplaceProductVariation.variationId
467
- )
468
- if (matchedOrderItem) {
469
- if (foundVariation.sku) {
470
- let allVariations: MarketplaceProductVariation[] = await getRepository(
471
- MarketplaceProductVariation
472
- ).find({
473
- where: { domain: foundVariation.domain, sku: foundVariation.sku },
474
- relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
475
- })
476
-
477
- let variationsFromOtherStores: MarketplaceProductVariation[] = allVariations.filter(
478
- variation =>
479
- variation.id != foundVariation.id &&
480
- variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
481
- )
482
-
483
- variationsFromOtherStores.map(async variation => {
484
- variation.reserveQty = existingMarketplaceOrder?.releaseOrderId
485
- ? variation.reserveQty + matchedOrderItem.qty
486
- : variation.reserveQty - item.qty + matchedOrderItem.qty
487
-
488
- variation = await getRepository(MarketplaceProductVariation).save(variation)
489
- })
503
+ if (!cancelStatuses.includes(marketplaceOrder.status)) {
504
+ existingOrderItems = await Promise.all(
505
+ marketplaceOrderItems.map(async item => {
506
+ const matchedOrderItem = existingOrderItems.find(
507
+ itm =>
508
+ itm.name == item.name &&
509
+ itm.marketplaceProductVariation.variationId ==
510
+ item.marketplaceProductVariation.variationId
511
+ )
512
+ if (matchedOrderItem) {
513
+ for (var foundVariation of foundVariations) {
514
+ if (foundVariation.sku) {
515
+ let allVariations: MarketplaceProductVariation[] = await getRepository(
516
+ MarketplaceProductVariation
517
+ ).find({
518
+ where: { domain: foundVariation.domain, sku: foundVariation.sku },
519
+ relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
520
+ })
521
+
522
+ let activeVariations: MarketplaceProductVariation[] = allVariations.filter(
523
+ variation => variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
524
+ )
525
+
526
+ await Promise.all(
527
+ activeVariations.map(async variation => {
528
+ if (
529
+ !existingMarketplaceOrder?.releaseOrderId &&
530
+ matchedOrderItem.qty - item.qty != 0
531
+ ) {
532
+ await calculateReserveQtyForBundle(
533
+ variation,
534
+ marketplaceOrder.domain,
535
+ matchedOrderItem.qty - item.qty
536
+ )
537
+ }
538
+
539
+ variation.reserveQty = existingMarketplaceOrder?.releaseOrderId
540
+ ? variation.reserveQty
541
+ : variation.reserveQty - item.qty + matchedOrderItem.qty
542
+
543
+ variation.qty = existingMarketplaceOrder?.releaseOrderId
544
+ ? variation.qty
545
+ : variation.qty + item.qty - matchedOrderItem.qty
546
+
547
+ variation = await getRepository(MarketplaceProductVariation).save(variation)
548
+ })
549
+ )
550
+ }
551
+ }
552
+ return {
553
+ ...matchedOrderItem,
554
+ ...item,
555
+ updater: user
556
+ }
490
557
  }
491
-
492
- foundVariation.qty = existingMarketplaceOrder?.releaseOrderId
493
- ? foundVariation.qty - matchedOrderItem.qty
494
- : foundVariation.qty + item.qty - matchedOrderItem.qty
495
- foundVariation = await getRepository(MarketplaceProductVariation).save(foundVariation)
496
-
497
- return {
498
- ...matchedOrderItem,
499
- ...item,
500
- updater: user
501
- }
502
- }
503
- })
504
- )
558
+ })
559
+ )
560
+ }
505
561
  } else {
506
- existingOrderItems = await Promise.all(
507
- marketplaceOrderItems.map(async item => {
508
- const matchedOrderItem = existingOrderItems.find(
509
- itm =>
510
- itm.name == item.name &&
511
- itm.marketplaceProductVariation.variationId == item.marketplaceProductVariation.variationId
512
- )
513
- if (matchedOrderItem) {
514
- if (foundVariation.sku) {
515
- let allVariations: MarketplaceProductVariation[] = await getRepository(
516
- MarketplaceProductVariation
517
- ).find({
518
- where: { domain: foundVariation.domain, sku: foundVariation.sku },
519
- relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
520
- })
521
-
522
- let variationsFromOtherStores: MarketplaceProductVariation[] = allVariations.filter(
523
- variation =>
524
- variation.id != foundVariation.id &&
525
- variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
526
- )
527
-
528
- variationsFromOtherStores.map(async variation => {
529
- variation.reserveQty = existingMarketplaceOrder?.releaseOrderId
530
- ? variation.reserveQty + matchedOrderItem.qty
531
- : variation.reserveQty - item.qty + matchedOrderItem.qty
532
-
533
- variation = await getRepository(MarketplaceProductVariation).save(variation)
534
- })
535
- }
536
-
537
- foundVariation.qty = existingMarketplaceOrder?.releaseOrderId
538
- ? foundVariation.qty - matchedOrderItem.qty
539
- : foundVariation.qty + item.qty - matchedOrderItem.qty
540
- foundVariation = await getRepository(MarketplaceProductVariation).save(foundVariation)
541
-
542
- return {
543
- ...matchedOrderItem,
544
- ...item,
545
- updater: user
562
+ if (!cancelStatuses.includes(marketplaceOrder.status)) {
563
+ existingOrderItems = await Promise.all(
564
+ marketplaceOrderItems.map(async item => {
565
+ const matchedOrderItem = existingOrderItems.find(
566
+ itm =>
567
+ itm.name == item.name &&
568
+ itm.marketplaceProductVariation.variationId ==
569
+ item.marketplaceProductVariation.variationId
570
+ )
571
+ if (matchedOrderItem) {
572
+ for (var foundVariation of foundVariations) {
573
+ if (foundVariation.sku) {
574
+ let allVariations: MarketplaceProductVariation[] = await getRepository(
575
+ MarketplaceProductVariation
576
+ ).find({
577
+ where: { domain: foundVariation.domain, sku: foundVariation.sku },
578
+ relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
579
+ })
580
+
581
+ let activeVariations: MarketplaceProductVariation[] = allVariations.filter(
582
+ variation => variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
583
+ )
584
+
585
+ await Promise.all(
586
+ activeVariations.map(async variation => {
587
+ if (
588
+ !existingMarketplaceOrder?.releaseOrderId &&
589
+ matchedOrderItem.qty - item.qty != 0
590
+ ) {
591
+ await calculateReserveQtyForBundle(
592
+ variation,
593
+ marketplaceOrder.domain,
594
+ matchedOrderItem.qty - item.qty
595
+ )
596
+ }
597
+
598
+ variation.reserveQty = existingMarketplaceOrder?.releaseOrderId
599
+ ? variation.reserveQty
600
+ : variation.reserveQty - item.qty + matchedOrderItem.qty
601
+
602
+ variation.qty = existingMarketplaceOrder?.releaseOrderId
603
+ ? variation.qty
604
+ : variation.qty + item.qty - matchedOrderItem.qty
605
+
606
+ variation = await getRepository(MarketplaceProductVariation).save(variation)
607
+ })
608
+ )
609
+ }
610
+ }
611
+ return {
612
+ ...matchedOrderItem,
613
+ ...item,
614
+ updater: user
615
+ }
616
+ } else {
617
+ for (var foundVariation of foundVariations) {
618
+ if (foundVariation.sku) {
619
+ let allVariations: MarketplaceProductVariation[] = await getRepository(
620
+ MarketplaceProductVariation
621
+ ).find({
622
+ where: { domain: foundVariation.domain, sku: foundVariation.sku },
623
+ relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
624
+ })
625
+
626
+ let activeVariations: MarketplaceProductVariation[] = allVariations.filter(
627
+ variation => variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
628
+ )
629
+
630
+ await Promise.all(
631
+ activeVariations.map(async variation => {
632
+ await calculateReserveQtyForBundle(
633
+ variation,
634
+ marketplaceOrder.domain,
635
+ item.qty
636
+ )
637
+
638
+ variation.reserveQty += item.qty
639
+ if (variation.qty - item.qty >= 0) {
640
+ variation.qty -= item.qty
641
+ }
642
+ variation = await getRepository(MarketplaceProductVariation).save(variation)
643
+ })
644
+ )
645
+ }
646
+ }
647
+ return {
648
+ ...item,
649
+ updater: user
650
+ }
546
651
  }
547
- } else {
548
- if (foundVariation.sku) {
549
- let allVariations: MarketplaceProductVariation[] = await getRepository(
550
- MarketplaceProductVariation
551
- ).find({
552
- where: { domain: foundVariation.domain, sku: foundVariation.sku },
553
- relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
554
- })
555
-
556
- let variationsFromOtherStores: MarketplaceProductVariation[] = allVariations.filter(
557
- variation =>
558
- variation.id != foundVariation.id &&
559
- variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
560
- )
561
-
562
- variationsFromOtherStores.map(async variation => {
563
- variation.reserveQty += item.qty
564
-
565
- variation = await getRepository(MarketplaceProductVariation).save(variation)
566
- })
567
- }
568
-
569
- foundVariation.qty -= item.qty
570
- foundVariation = await getRepository(MarketplaceProductVariation).save(foundVariation)
571
-
572
- return {
573
- ...item,
574
- updater: user
575
- }
576
- }
577
- })
578
- )
652
+ })
653
+ )
654
+ }
579
655
  }
580
656
 
581
657
  marketplaceOrderItems = existingOrderItems
@@ -588,35 +664,43 @@ export const syncAllMarketplaceOrder = {
588
664
  }
589
665
  }
590
666
  } else {
591
- if (foundVariation.sku) {
592
- let allVariations: MarketplaceProductVariation[] = await getRepository(
593
- MarketplaceProductVariation
594
- ).find({
595
- where: { domain: foundVariation.domain, sku: foundVariation.sku },
596
- relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
597
- })
667
+ if (!cancelStatuses.includes(marketplaceOrder.status)) {
668
+ for (var foundVariation of foundVariations) {
669
+ if (foundVariation.sku) {
670
+ let allVariations: MarketplaceProductVariation[] = await getRepository(
671
+ MarketplaceProductVariation
672
+ ).find({
673
+ where: { domain: foundVariation.domain, sku: foundVariation.sku },
674
+ relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
675
+ })
598
676
 
599
- let variationsFromOtherStores: MarketplaceProductVariation[] = allVariations.filter(
600
- variation =>
601
- variation.id != foundVariation.id &&
602
- variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
603
- )
677
+ let item = order.orderItems.find(e => e.variationId == foundVariation.variationId)
678
+ let activeVariations: MarketplaceProductVariation[] = allVariations.filter(
679
+ variation => variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
680
+ )
604
681
 
605
- variationsFromOtherStores.map(async variation => {
606
- variation.reserveQty += item.qty
682
+ await Promise.all(
683
+ activeVariations.map(async variation => {
684
+ await calculateReserveQtyForBundle(variation, marketplaceOrder.domain, item.qty)
685
+ variation.reserveQty += item.qty
686
+ if (variation.qty - item.qty >= 0) {
687
+ variation.qty -= item.qty
688
+ }
607
689
 
608
- variation = await getRepository(MarketplaceProductVariation).save(variation)
609
- })
610
- }
690
+ variation = await getRepository(MarketplaceProductVariation).save(variation)
691
+ })
692
+ )
693
+ }
611
694
 
612
- foundVariation.qty -= item.qty
613
- foundVariation = await getRepository(MarketplaceProductVariation).save(foundVariation)
695
+ foundVariation = await getRepository(MarketplaceProductVariation).save(foundVariation)
614
696
 
615
- if (order?.orderShipping) {
616
- savedMarketplaceOrderShipping.totalWeight = orderTotalWeight
617
- savedMarketplaceOrderShipping = await getRepository(MarketplaceOrderShipping).save(
618
- savedMarketplaceOrderShipping
619
- )
697
+ if (order?.orderShipping) {
698
+ savedMarketplaceOrderShipping.totalWeight = orderTotalWeight
699
+ savedMarketplaceOrderShipping = await getRepository(MarketplaceOrderShipping).save(
700
+ savedMarketplaceOrderShipping
701
+ )
702
+ }
703
+ }
620
704
  }
621
705
  }
622
706
 
@@ -640,19 +724,19 @@ export const syncAllMarketplaceOrder = {
640
724
  let hasMore = true
641
725
  let perPage: number = 100
642
726
  var payoutDateList = []
643
-
727
+
644
728
  while (hasMore) {
645
729
  const { result: payoutDates, more } = await StoreAPI.getStoreOrderPayoutDates(marketplaceStore, {
646
730
  fromDate,
647
731
  toDate,
648
732
  pagination: { page: pageNo, limit: perPage }
649
733
  })
650
-
734
+
651
735
  payoutDateList.push(...payoutDates)
652
736
  if (more) pageNo++
653
737
  hasMore = more
654
738
  }
655
-
739
+
656
740
  await Promise.all(
657
741
  payoutDateList.map(async item => {
658
742
  await getRepository(MarketplaceOrder).update(
@@ -665,9 +749,47 @@ export const syncAllMarketplaceOrder = {
665
749
  )
666
750
  })
667
751
  )
668
- }
752
+ }
669
753
  }
670
754
 
671
755
  return true
672
756
  }
673
757
  }
758
+
759
+ async function calculateReserveQtyForBundle(variation, domain, qty, operator = '+') {
760
+ const productBundle: ProductBundle = await getRepository(ProductBundle).findOne({
761
+ where: { sku: variation.sku, domain }
762
+ })
763
+
764
+ if (productBundle) {
765
+ const productBundleSettings: ProductBundleSetting[] = await getRepository(ProductBundleSetting).find({
766
+ where: { productBundle },
767
+ relations: ['productBundle', 'product']
768
+ })
769
+
770
+ for (let setting of productBundleSettings) {
771
+ const product: Product = await getRepository(Product).findOne({
772
+ where: { id: setting.product.id }
773
+ })
774
+
775
+ const mpvs: MarketplaceProductVariation[] = await getRepository(MarketplaceProductVariation).find({
776
+ where: { domain, sku: product.sku },
777
+ relations: ['domain']
778
+ })
779
+
780
+ for (let mpv of mpvs) {
781
+ if (operator === '+') {
782
+ mpv.reserveQty += setting.bundleQty * qty
783
+ if (mpv.qty - setting.bundleQty * qty >= 0) {
784
+ mpv.qty -= setting.bundleQty * qty
785
+ }
786
+ }
787
+ if (operator === '-') {
788
+ mpv.reserveQty -= setting.bundleQty * qty
789
+ mpv.qty += setting.bundleQty * qty
790
+ }
791
+ await getRepository(MarketplaceProductVariation).save(mpv)
792
+ }
793
+ }
794
+ }
795
+ }