@things-factory/marketplace-base 3.8.29 → 3.8.30
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/graphql/resolvers/marketplace-order/sync-all-marketplace-order.js +183 -101
- package/dist-server/graphql/resolvers/marketplace-order/sync-all-marketplace-order.js.map +1 -1
- package/dist-server/graphql/resolvers/marketplace-order/sync-marketplace-order.js +187 -113
- package/dist-server/graphql/resolvers/marketplace-order/sync-marketplace-order.js.map +1 -1
- package/package.json +3 -2
- package/server/graphql/resolvers/marketplace-order/sync-all-marketplace-order.ts +293 -172
- package/server/graphql/resolvers/marketplace-order/sync-marketplace-order.ts +279 -168
|
@@ -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,
|
|
@@ -119,6 +120,34 @@ export const syncMarketplaceOrder = {
|
|
|
119
120
|
updater: user
|
|
120
121
|
}
|
|
121
122
|
|
|
123
|
+
if (
|
|
124
|
+
!marketplaceOrder?.releaseOrderId &&
|
|
125
|
+
cancelStatuses.includes(marketplaceOrder.status) &&
|
|
126
|
+
!cancelStatuses.includes(existingMarketplaceOrder.status)
|
|
127
|
+
) {
|
|
128
|
+
for (let item of marketplaceOrder.marketplaceOrderItems) {
|
|
129
|
+
let foundVariations = await tx.getRepository(MarketplaceProductVariation).find({
|
|
130
|
+
where: { domain: marketplaceOrder.domain, sku: item.marketplaceProductVariation.sku },
|
|
131
|
+
relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
let activeVariations: MarketplaceProductVariation[] = foundVariations.filter(
|
|
135
|
+
variation =>
|
|
136
|
+
variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED' && variation.sku != null
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
await Promise.all(
|
|
140
|
+
activeVariations.map(async variation => {
|
|
141
|
+
await this.calculateReserveQtyForBundle(tx, variation, marketplaceOrder.domain, item.qty, '-')
|
|
142
|
+
|
|
143
|
+
variation.reserveQty -= item.qty
|
|
144
|
+
variation.qty += item.qty
|
|
145
|
+
await tx.getRepository(MarketplaceProductVariation).save(variation)
|
|
146
|
+
})
|
|
147
|
+
)
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
122
151
|
if (marketplaceOrder?.releaseOrderId) {
|
|
123
152
|
const fulfillmentCenter: FulfillmentCenter = marketplaceOrder.fulfillmentCenter
|
|
124
153
|
const centerId: string = fulfillmentCenter.centerId
|
|
@@ -316,14 +345,18 @@ export const syncMarketplaceOrder = {
|
|
|
316
345
|
}
|
|
317
346
|
|
|
318
347
|
let orderTotalWeight: number = 0
|
|
319
|
-
let
|
|
348
|
+
let foundVariations = []
|
|
320
349
|
for (var j = 0; j < order.orderItems.length; j++) {
|
|
321
350
|
var item = order.orderItems[j]
|
|
322
|
-
foundVariation = await tx.getRepository(MarketplaceProductVariation).findOne({
|
|
351
|
+
let foundVariation = await tx.getRepository(MarketplaceProductVariation).findOne({
|
|
323
352
|
where: { domain: marketplaceStore.domain, variationId: item.variationId },
|
|
324
353
|
relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
|
|
325
354
|
})
|
|
326
355
|
|
|
356
|
+
if (foundVariation) {
|
|
357
|
+
foundVariations.push(foundVariation)
|
|
358
|
+
}
|
|
359
|
+
|
|
327
360
|
if (!foundVariation) {
|
|
328
361
|
let newVariation: any = {
|
|
329
362
|
variationId: item.variationId,
|
|
@@ -411,158 +444,192 @@ export const syncMarketplaceOrder = {
|
|
|
411
444
|
if (nonMatchedOrderItems.length > 0 && !cancelStatuses.includes(marketplaceOrder.status)) {
|
|
412
445
|
await Promise.all(
|
|
413
446
|
nonMatchedOrderItems.map(async item => {
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
variation.
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
447
|
+
for (var foundVariation of foundVariations) {
|
|
448
|
+
if (foundVariation.sku) {
|
|
449
|
+
let allVariations: MarketplaceProductVariation[] = await tx
|
|
450
|
+
.getRepository(MarketplaceProductVariation)
|
|
451
|
+
.find({
|
|
452
|
+
where: { domain: foundVariation.domain, sku: foundVariation.sku },
|
|
453
|
+
relations: [
|
|
454
|
+
'domain',
|
|
455
|
+
'marketplaceProduct',
|
|
456
|
+
'marketplaceProduct.marketplaceStore',
|
|
457
|
+
'marketplaceProduct.marketplaceStore.marketplaceDistributors'
|
|
458
|
+
]
|
|
459
|
+
})
|
|
460
|
+
|
|
461
|
+
let activeVariations: MarketplaceProductVariation[] = allVariations.filter(
|
|
462
|
+
variation => variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
|
|
463
|
+
)
|
|
464
|
+
|
|
465
|
+
await Promise.all(
|
|
466
|
+
activeVariations.map(async variation => {
|
|
467
|
+
await this.calculateReserveQtyForBundle(
|
|
468
|
+
tx,
|
|
469
|
+
variation,
|
|
470
|
+
marketplaceOrder.domain,
|
|
471
|
+
item.qty,
|
|
472
|
+
'-'
|
|
473
|
+
)
|
|
474
|
+
|
|
475
|
+
variation.reserveQty -= item.qty
|
|
476
|
+
variation.qty += item.qty
|
|
477
|
+
|
|
478
|
+
variation = await tx.getRepository(MarketplaceProductVariation).save(variation)
|
|
479
|
+
})
|
|
480
|
+
)
|
|
481
|
+
}
|
|
438
482
|
}
|
|
439
|
-
|
|
440
|
-
foundVariation.qty += item.qty
|
|
441
|
-
foundVariation = await tx.getRepository(MarketplaceProductVariation).save(foundVariation)
|
|
442
483
|
})
|
|
443
484
|
)
|
|
444
485
|
|
|
445
486
|
await tx.getRepository(MarketplaceOrderItem).delete(nonMatchedOrderItems)
|
|
446
487
|
}
|
|
447
488
|
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
itm
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
if (
|
|
457
|
-
|
|
458
|
-
.
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
489
|
+
if (!cancelStatuses.includes(marketplaceOrder.status)) {
|
|
490
|
+
existingOrderItems = await Promise.all(
|
|
491
|
+
marketplaceOrderItems.map(async item => {
|
|
492
|
+
const matchedOrderItem = existingOrderItems.find(
|
|
493
|
+
itm =>
|
|
494
|
+
itm.name == item.name &&
|
|
495
|
+
itm.marketplaceProductVariation.variationId == item.marketplaceProductVariation.variationId
|
|
496
|
+
)
|
|
497
|
+
if (matchedOrderItem) {
|
|
498
|
+
for (var foundVariation of foundVariations) {
|
|
499
|
+
if (foundVariation.sku) {
|
|
500
|
+
let allVariations: MarketplaceProductVariation[] = await tx
|
|
501
|
+
.getRepository(MarketplaceProductVariation)
|
|
502
|
+
.find({
|
|
503
|
+
where: { domain: foundVariation.domain, sku: foundVariation.sku },
|
|
504
|
+
relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
|
|
505
|
+
})
|
|
506
|
+
|
|
507
|
+
let activeVariations: MarketplaceProductVariation[] = allVariations.filter(
|
|
508
|
+
variation => variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
|
|
509
|
+
)
|
|
510
|
+
|
|
511
|
+
await Promise.all(
|
|
512
|
+
activeVariations.map(async variation => {
|
|
513
|
+
if (!existingMarketplaceOrder?.releaseOrderId && matchedOrderItem.qty - item.qty != 0) {
|
|
514
|
+
await this.calculateReserveQtyForBundle(
|
|
515
|
+
tx,
|
|
516
|
+
variation,
|
|
517
|
+
marketplaceOrder.domain,
|
|
518
|
+
matchedOrderItem.qty - item.qty
|
|
519
|
+
)
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
variation.reserveQty = existingMarketplaceOrder?.releaseOrderId
|
|
523
|
+
? variation.reserveQty
|
|
524
|
+
: variation.reserveQty - item.qty + matchedOrderItem.qty
|
|
525
|
+
|
|
526
|
+
variation.qty = existingMarketplaceOrder?.releaseOrderId
|
|
527
|
+
? variation.qty
|
|
528
|
+
: variation.qty + item.qty - matchedOrderItem.qty
|
|
529
|
+
|
|
530
|
+
variation = await tx.getRepository(MarketplaceProductVariation).save(variation)
|
|
531
|
+
})
|
|
532
|
+
)
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
return {
|
|
536
|
+
...matchedOrderItem,
|
|
537
|
+
...item,
|
|
538
|
+
updater: user
|
|
539
|
+
}
|
|
488
540
|
}
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
|
|
541
|
+
})
|
|
542
|
+
)
|
|
543
|
+
}
|
|
492
544
|
} else {
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
itm
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
if (
|
|
502
|
-
|
|
503
|
-
.
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
545
|
+
if (!cancelStatuses.includes(marketplaceOrder.status)) {
|
|
546
|
+
existingOrderItems = await Promise.all(
|
|
547
|
+
marketplaceOrderItems.map(async item => {
|
|
548
|
+
const matchedOrderItem = existingOrderItems.find(
|
|
549
|
+
itm =>
|
|
550
|
+
itm.name == item.name &&
|
|
551
|
+
itm.marketplaceProductVariation.variationId == item.marketplaceProductVariation.variationId
|
|
552
|
+
)
|
|
553
|
+
if (matchedOrderItem) {
|
|
554
|
+
for (var foundVariation of foundVariations) {
|
|
555
|
+
if (foundVariation.sku) {
|
|
556
|
+
let allVariations: MarketplaceProductVariation[] = await tx
|
|
557
|
+
.getRepository(MarketplaceProductVariation)
|
|
558
|
+
.find({
|
|
559
|
+
where: { domain: foundVariation.domain, sku: foundVariation.sku },
|
|
560
|
+
relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
|
|
561
|
+
})
|
|
562
|
+
|
|
563
|
+
let activeVariations: MarketplaceProductVariation[] = allVariations.filter(
|
|
564
|
+
variation => variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
|
|
565
|
+
)
|
|
566
|
+
|
|
567
|
+
await Promise.all(
|
|
568
|
+
activeVariations.map(async variation => {
|
|
569
|
+
if (!existingMarketplaceOrder?.releaseOrderId && matchedOrderItem.qty - item.qty != 0) {
|
|
570
|
+
await this.calculateReserveQtyForBundle(
|
|
571
|
+
tx,
|
|
572
|
+
variation,
|
|
573
|
+
marketplaceOrder.domain,
|
|
574
|
+
matchedOrderItem.qty - item.qty
|
|
575
|
+
)
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
variation.reserveQty = existingMarketplaceOrder?.releaseOrderId
|
|
579
|
+
? variation.reserveQty
|
|
580
|
+
: variation.reserveQty - item.qty + matchedOrderItem.qty
|
|
581
|
+
|
|
582
|
+
variation.qty = existingMarketplaceOrder?.releaseOrderId
|
|
583
|
+
? variation.qty
|
|
584
|
+
: variation.qty + item.qty - matchedOrderItem.qty
|
|
585
|
+
|
|
586
|
+
variation = await tx.getRepository(MarketplaceProductVariation).save(variation)
|
|
587
|
+
})
|
|
588
|
+
)
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
return {
|
|
592
|
+
...matchedOrderItem,
|
|
593
|
+
...item,
|
|
594
|
+
updater: user
|
|
595
|
+
}
|
|
596
|
+
} else {
|
|
597
|
+
for (var foundVariation of foundVariations) {
|
|
598
|
+
if (foundVariation.sku) {
|
|
599
|
+
let allVariations: MarketplaceProductVariation[] = await tx
|
|
600
|
+
.getRepository(MarketplaceProductVariation)
|
|
601
|
+
.find({
|
|
602
|
+
where: { domain: foundVariation.domain, sku: foundVariation.sku },
|
|
603
|
+
relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
|
|
604
|
+
})
|
|
605
|
+
|
|
606
|
+
let activeVariations: MarketplaceProductVariation[] = allVariations.filter(
|
|
607
|
+
variation => variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
|
|
608
|
+
)
|
|
609
|
+
|
|
610
|
+
await Promise.all(
|
|
611
|
+
activeVariations.map(async variation => {
|
|
612
|
+
await this.calculateReserveQtyForBundle(tx, variation, marketplaceOrder.domain, item.qty)
|
|
613
|
+
|
|
614
|
+
variation.reserveQty += item.qty
|
|
615
|
+
|
|
616
|
+
if (variation.qty - item.qty >= 0) {
|
|
617
|
+
variation.qty -= item.qty
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
variation = await tx.getRepository(MarketplaceProductVariation).save(variation)
|
|
621
|
+
})
|
|
622
|
+
)
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
return {
|
|
626
|
+
...item,
|
|
627
|
+
updater: user
|
|
628
|
+
}
|
|
562
629
|
}
|
|
563
|
-
}
|
|
564
|
-
|
|
565
|
-
|
|
630
|
+
})
|
|
631
|
+
)
|
|
632
|
+
}
|
|
566
633
|
}
|
|
567
634
|
|
|
568
635
|
marketplaceOrderItems = existingOrderItems
|
|
@@ -575,35 +642,42 @@ export const syncMarketplaceOrder = {
|
|
|
575
642
|
}
|
|
576
643
|
}
|
|
577
644
|
} else {
|
|
578
|
-
if (
|
|
579
|
-
|
|
580
|
-
.
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
645
|
+
if (!cancelStatuses.includes(marketplaceOrder.status)) {
|
|
646
|
+
for (var foundVariation of foundVariations) {
|
|
647
|
+
if (foundVariation.sku) {
|
|
648
|
+
let allVariations: MarketplaceProductVariation[] = await tx
|
|
649
|
+
.getRepository(MarketplaceProductVariation)
|
|
650
|
+
.find({
|
|
651
|
+
where: { domain: foundVariation.domain, sku: foundVariation.sku },
|
|
652
|
+
relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
|
|
653
|
+
})
|
|
585
654
|
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
655
|
+
let activeVariations: MarketplaceProductVariation[] = allVariations.filter(
|
|
656
|
+
variation => variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
|
|
657
|
+
)
|
|
658
|
+
let item = order.orderItems.find(e => e.variationId == foundVariation.variationId)
|
|
659
|
+
await Promise.all(
|
|
660
|
+
activeVariations.map(async variation => {
|
|
661
|
+
await this.calculateReserveQtyForBundle(tx, variation, marketplaceOrder.domain, item.qty)
|
|
591
662
|
|
|
592
|
-
|
|
593
|
-
variation.reserveQty += item.qty
|
|
663
|
+
variation.reserveQty += item.qty
|
|
594
664
|
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
665
|
+
if (variation.qty - item.qty >= 0) {
|
|
666
|
+
variation.qty -= item.qty
|
|
667
|
+
}
|
|
598
668
|
|
|
599
|
-
|
|
600
|
-
|
|
669
|
+
variation = await tx.getRepository(MarketplaceProductVariation).save(variation)
|
|
670
|
+
})
|
|
671
|
+
)
|
|
672
|
+
}
|
|
601
673
|
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
674
|
+
if (order?.orderShipping) {
|
|
675
|
+
savedMarketplaceOrderShipping.totalWeight = orderTotalWeight
|
|
676
|
+
savedMarketplaceOrderShipping = await tx
|
|
677
|
+
.getRepository(MarketplaceOrderShipping)
|
|
678
|
+
.save(savedMarketplaceOrderShipping)
|
|
679
|
+
}
|
|
680
|
+
}
|
|
607
681
|
}
|
|
608
682
|
}
|
|
609
683
|
|
|
@@ -650,5 +724,42 @@ export const syncMarketplaceOrder = {
|
|
|
650
724
|
)
|
|
651
725
|
}
|
|
652
726
|
return true
|
|
727
|
+
},
|
|
728
|
+
async calculateReserveQtyForBundle(tx, variation, domain, qty, operator = '+') {
|
|
729
|
+
const productBundle: ProductBundle = await tx.getRepository(ProductBundle).findOne({
|
|
730
|
+
where: { sku: variation.sku, domain }
|
|
731
|
+
})
|
|
732
|
+
|
|
733
|
+
if (productBundle) {
|
|
734
|
+
const productBundleSettings: ProductBundleSetting[] = await tx.getRepository(ProductBundleSetting).find({
|
|
735
|
+
where: { productBundle },
|
|
736
|
+
relations: ['productBundle', 'product']
|
|
737
|
+
})
|
|
738
|
+
|
|
739
|
+
for (let setting of productBundleSettings) {
|
|
740
|
+
const product: Product = await tx.getRepository(Product).findOne({
|
|
741
|
+
where: { id: setting.product.id }
|
|
742
|
+
})
|
|
743
|
+
|
|
744
|
+
const mpvs: MarketplaceProductVariation[] = await tx.getRepository(MarketplaceProductVariation).find({
|
|
745
|
+
where: { domain, sku: product.sku },
|
|
746
|
+
relations: ['domain']
|
|
747
|
+
})
|
|
748
|
+
|
|
749
|
+
for (let mpv of mpvs) {
|
|
750
|
+
if (operator === '+') {
|
|
751
|
+
mpv.reserveQty += setting.bundleQty * qty
|
|
752
|
+
if (mpv.qty - setting.bundleQty * qty >= 0) {
|
|
753
|
+
mpv.qty -= setting.bundleQty * qty
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
if (operator === '-') {
|
|
757
|
+
mpv.reserveQty -= setting.bundleQty * qty
|
|
758
|
+
mpv.qty += setting.bundleQty * qty
|
|
759
|
+
}
|
|
760
|
+
await tx.getRepository(MarketplaceProductVariation).save(mpv)
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
}
|
|
653
764
|
}
|
|
654
765
|
}
|