@things-factory/marketplace-base 3.8.29 → 3.8.35

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
@@ -239,8 +270,6 @@ export const syncAllMarketplaceOrder = {
239
270
  }
240
271
  }
241
272
 
242
- marketplaceOrder = await getRepository(MarketplaceOrder).save(marketplaceOrder)
243
-
244
273
  let savedMarketplaceOrderShipping: MarketplaceOrderShipping = new MarketplaceOrderShipping()
245
274
  let savedMarketplaceOrderShippings: MarketplaceOrderShipping[] = []
246
275
 
@@ -330,13 +359,17 @@ export const syncAllMarketplaceOrder = {
330
359
  }
331
360
 
332
361
  let orderTotalWeight: number = 0
333
- let foundVariation
362
+ let foundVariations = []
334
363
  for (var j = 0; j < order.orderItems.length; j++) {
335
364
  var item = order.orderItems[j]
336
- foundVariation = await getRepository(MarketplaceProductVariation).findOne({
365
+ let foundVariation = await getRepository(MarketplaceProductVariation).findOne({
337
366
  where: { domain: marketplaceStore.domain, variationId: item.variationId }
338
367
  })
339
368
 
369
+ if (foundVariation) {
370
+ foundVariations.push(foundVariation)
371
+ }
372
+
340
373
  if (!foundVariation) {
341
374
  let newVariation: any = {
342
375
  variationId: item.variationId,
@@ -424,158 +457,190 @@ export const syncAllMarketplaceOrder = {
424
457
  if (nonMatchedOrderItems.length > 0 && !cancelStatuses.includes(marketplaceOrder.status)) {
425
458
  await Promise.all(
426
459
  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
- })
460
+ for (var foundVariation of foundVariations) {
461
+ if (foundVariation.sku) {
462
+ let allVariations: MarketplaceProductVariation[] = await getRepository(
463
+ MarketplaceProductVariation
464
+ ).find({
465
+ where: { domain: foundVariation.domain, sku: foundVariation.sku },
466
+ relations: [
467
+ 'domain',
468
+ 'marketplaceProduct',
469
+ 'marketplaceProduct.marketplaceStore',
470
+ 'marketplaceProduct.marketplaceStore.marketplaceDistributors'
471
+ ]
472
+ })
473
+
474
+ let activeVariations: MarketplaceProductVariation[] = allVariations.filter(
475
+ variation => variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
476
+ )
477
+
478
+ await Promise.all(
479
+ activeVariations.map(async variation => {
480
+ await calculateReserveQtyForBundle(variation, marketplaceOrder.domain, item.qty, '-')
481
+
482
+ variation.reserveQty -= item.qty
483
+ variation.qty += item.qty
484
+
485
+ variation = await getRepository(MarketplaceProductVariation).save(variation)
486
+ })
487
+ )
488
+ }
451
489
  }
452
-
453
- foundVariation.qty += item.qty
454
- foundVariation = await getRepository(MarketplaceProductVariation).save(foundVariation)
455
490
  })
456
491
  )
457
492
 
458
493
  await getRepository(MarketplaceOrderItem).delete(nonMatchedOrderItems)
459
494
  }
460
495
 
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
- })
490
- }
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
496
+ if (!cancelStatuses.includes(marketplaceOrder.status)) {
497
+ existingOrderItems = await Promise.all(
498
+ marketplaceOrderItems.map(async item => {
499
+ const matchedOrderItem = existingOrderItems.find(
500
+ itm =>
501
+ itm.name == item.name &&
502
+ itm.marketplaceProductVariation.variationId ==
503
+ item.marketplaceProductVariation.variationId
504
+ )
505
+ if (matchedOrderItem) {
506
+ for (var foundVariation of foundVariations) {
507
+ if (foundVariation.sku) {
508
+ let allVariations: MarketplaceProductVariation[] = await getRepository(
509
+ MarketplaceProductVariation
510
+ ).find({
511
+ where: { domain: foundVariation.domain, sku: foundVariation.sku },
512
+ relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
513
+ })
514
+
515
+ let activeVariations: MarketplaceProductVariation[] = allVariations.filter(
516
+ variation => variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
517
+ )
518
+
519
+ await Promise.all(
520
+ activeVariations.map(async variation => {
521
+ if (
522
+ !existingMarketplaceOrder?.releaseOrderId &&
523
+ matchedOrderItem.qty - item.qty != 0
524
+ ) {
525
+ await calculateReserveQtyForBundle(
526
+ variation,
527
+ marketplaceOrder.domain,
528
+ matchedOrderItem.qty - item.qty
529
+ )
530
+ }
531
+
532
+ variation.reserveQty = existingMarketplaceOrder?.releaseOrderId
533
+ ? variation.reserveQty
534
+ : variation.reserveQty - item.qty + matchedOrderItem.qty
535
+
536
+ variation.qty = existingMarketplaceOrder?.releaseOrderId
537
+ ? variation.qty
538
+ : variation.qty + item.qty - matchedOrderItem.qty
539
+
540
+ variation = await getRepository(MarketplaceProductVariation).save(variation)
541
+ })
542
+ )
543
+ }
544
+ }
545
+ return {
546
+ ...matchedOrderItem,
547
+ ...item,
548
+ updater: user
549
+ }
501
550
  }
502
- }
503
- })
504
- )
551
+ })
552
+ )
553
+ }
505
554
  } 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
546
- }
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
555
+ if (!cancelStatuses.includes(marketplaceOrder.status)) {
556
+ existingOrderItems = await Promise.all(
557
+ marketplaceOrderItems.map(async item => {
558
+ const matchedOrderItem = existingOrderItems.find(
559
+ itm =>
560
+ itm.name == item.name &&
561
+ itm.marketplaceProductVariation.variationId ==
562
+ item.marketplaceProductVariation.variationId
563
+ )
564
+ if (matchedOrderItem) {
565
+ for (var foundVariation of foundVariations) {
566
+ if (foundVariation.sku) {
567
+ let allVariations: MarketplaceProductVariation[] = await getRepository(
568
+ MarketplaceProductVariation
569
+ ).find({
570
+ where: { domain: foundVariation.domain, sku: foundVariation.sku },
571
+ relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
572
+ })
573
+
574
+ let activeVariations: MarketplaceProductVariation[] = allVariations.filter(
575
+ variation => variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
576
+ )
577
+
578
+ await Promise.all(
579
+ activeVariations.map(async variation => {
580
+ if (
581
+ !existingMarketplaceOrder?.releaseOrderId &&
582
+ matchedOrderItem.qty - item.qty != 0
583
+ ) {
584
+ await calculateReserveQtyForBundle(
585
+ variation,
586
+ marketplaceOrder.domain,
587
+ matchedOrderItem.qty - item.qty
588
+ )
589
+ }
590
+
591
+ variation.reserveQty = existingMarketplaceOrder?.releaseOrderId
592
+ ? variation.reserveQty
593
+ : variation.reserveQty - item.qty + matchedOrderItem.qty
594
+
595
+ variation.qty = existingMarketplaceOrder?.releaseOrderId
596
+ ? variation.qty
597
+ : variation.qty + item.qty - matchedOrderItem.qty
598
+
599
+ variation = await getRepository(MarketplaceProductVariation).save(variation)
600
+ })
601
+ )
602
+ }
603
+ }
604
+ return {
605
+ ...matchedOrderItem,
606
+ ...item,
607
+ updater: user
608
+ }
609
+ } else {
610
+ for (var foundVariation of foundVariations) {
611
+ if (foundVariation.sku) {
612
+ let allVariations: MarketplaceProductVariation[] = await getRepository(
613
+ MarketplaceProductVariation
614
+ ).find({
615
+ where: { domain: foundVariation.domain, sku: foundVariation.sku },
616
+ relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
617
+ })
618
+
619
+ let activeVariations: MarketplaceProductVariation[] = allVariations.filter(
620
+ variation => variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
621
+ )
622
+
623
+ await Promise.all(
624
+ activeVariations.map(async variation => {
625
+ await calculateReserveQtyForBundle(variation, marketplaceOrder.domain, item.qty)
626
+
627
+ variation.reserveQty += item.qty
628
+ if (variation.qty - item.qty >= 0) {
629
+ variation.qty -= item.qty
630
+ }
631
+ variation = await getRepository(MarketplaceProductVariation).save(variation)
632
+ })
633
+ )
634
+ }
635
+ }
636
+ return {
637
+ ...item,
638
+ updater: user
639
+ }
575
640
  }
576
- }
577
- })
578
- )
641
+ })
642
+ )
643
+ }
579
644
  }
580
645
 
581
646
  marketplaceOrderItems = existingOrderItems
@@ -588,38 +653,47 @@ export const syncAllMarketplaceOrder = {
588
653
  }
589
654
  }
590
655
  } 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
- })
656
+ if (!cancelStatuses.includes(marketplaceOrder.status)) {
657
+ for (var foundVariation of foundVariations) {
658
+ if (foundVariation.sku) {
659
+ let allVariations: MarketplaceProductVariation[] = await getRepository(
660
+ MarketplaceProductVariation
661
+ ).find({
662
+ where: { domain: foundVariation.domain, sku: foundVariation.sku },
663
+ relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
664
+ })
598
665
 
599
- let variationsFromOtherStores: MarketplaceProductVariation[] = allVariations.filter(
600
- variation =>
601
- variation.id != foundVariation.id &&
602
- variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
603
- )
666
+ let item = order.orderItems.find(e => e.variationId == foundVariation.variationId)
667
+ let activeVariations: MarketplaceProductVariation[] = allVariations.filter(
668
+ variation => variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
669
+ )
604
670
 
605
- variationsFromOtherStores.map(async variation => {
606
- variation.reserveQty += item.qty
671
+ await Promise.all(
672
+ activeVariations.map(async variation => {
673
+ await calculateReserveQtyForBundle(variation, marketplaceOrder.domain, item.qty)
674
+ variation.reserveQty += item.qty
675
+ if (variation.qty - item.qty >= 0) {
676
+ variation.qty -= item.qty
677
+ }
607
678
 
608
- variation = await getRepository(MarketplaceProductVariation).save(variation)
609
- })
610
- }
679
+ variation = await getRepository(MarketplaceProductVariation).save(variation)
680
+ })
681
+ )
682
+ }
611
683
 
612
- foundVariation.qty -= item.qty
613
- foundVariation = await getRepository(MarketplaceProductVariation).save(foundVariation)
684
+ foundVariation = await getRepository(MarketplaceProductVariation).save(foundVariation)
614
685
 
615
- if (order?.orderShipping) {
616
- savedMarketplaceOrderShipping.totalWeight = orderTotalWeight
617
- savedMarketplaceOrderShipping = await getRepository(MarketplaceOrderShipping).save(
618
- savedMarketplaceOrderShipping
619
- )
686
+ if (order?.orderShipping) {
687
+ savedMarketplaceOrderShipping.totalWeight = orderTotalWeight
688
+ savedMarketplaceOrderShipping = await getRepository(MarketplaceOrderShipping).save(
689
+ savedMarketplaceOrderShipping
690
+ )
691
+ }
692
+ }
620
693
  }
621
694
  }
622
695
 
696
+ await getRepository(MarketplaceOrder).save(marketplaceOrder)
623
697
  await getRepository(MarketplaceOrderItem).save(marketplaceOrderItems)
624
698
 
625
699
  if (m == marketplaceOrders.length - 1) {
@@ -640,19 +714,19 @@ export const syncAllMarketplaceOrder = {
640
714
  let hasMore = true
641
715
  let perPage: number = 100
642
716
  var payoutDateList = []
643
-
717
+
644
718
  while (hasMore) {
645
719
  const { result: payoutDates, more } = await StoreAPI.getStoreOrderPayoutDates(marketplaceStore, {
646
720
  fromDate,
647
721
  toDate,
648
722
  pagination: { page: pageNo, limit: perPage }
649
723
  })
650
-
724
+
651
725
  payoutDateList.push(...payoutDates)
652
726
  if (more) pageNo++
653
727
  hasMore = more
654
728
  }
655
-
729
+
656
730
  await Promise.all(
657
731
  payoutDateList.map(async item => {
658
732
  await getRepository(MarketplaceOrder).update(
@@ -665,9 +739,47 @@ export const syncAllMarketplaceOrder = {
665
739
  )
666
740
  })
667
741
  )
668
- }
742
+ }
669
743
  }
670
744
 
671
745
  return true
672
746
  }
673
747
  }
748
+
749
+ async function calculateReserveQtyForBundle(variation, domain, qty, operator = '+') {
750
+ const productBundle: ProductBundle = await getRepository(ProductBundle).findOne({
751
+ where: { sku: variation.sku, domain }
752
+ })
753
+
754
+ if (productBundle) {
755
+ const productBundleSettings: ProductBundleSetting[] = await getRepository(ProductBundleSetting).find({
756
+ where: { productBundle },
757
+ relations: ['productBundle', 'product']
758
+ })
759
+
760
+ for (let setting of productBundleSettings) {
761
+ const product: Product = await getRepository(Product).findOne({
762
+ where: { id: setting.product.id }
763
+ })
764
+
765
+ const mpvs: MarketplaceProductVariation[] = await getRepository(MarketplaceProductVariation).find({
766
+ where: { domain, sku: product.sku },
767
+ relations: ['domain']
768
+ })
769
+
770
+ for (let mpv of mpvs) {
771
+ if (operator === '+') {
772
+ mpv.reserveQty += setting.bundleQty * qty
773
+ if (mpv.qty - setting.bundleQty * qty >= 0) {
774
+ mpv.qty -= setting.bundleQty * qty
775
+ }
776
+ }
777
+ if (operator === '-') {
778
+ mpv.reserveQty -= setting.bundleQty * qty
779
+ mpv.qty += setting.bundleQty * qty
780
+ }
781
+ await getRepository(MarketplaceProductVariation).save(mpv)
782
+ }
783
+ }
784
+ }
785
+ }