@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.
- 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 +5 -4
- package/server/graphql/resolvers/marketplace-order/sync-all-marketplace-order.ts +294 -172
- package/server/graphql/resolvers/marketplace-order/sync-marketplace-order.ts +274 -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 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,186 @@ 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 calculateReserveQtyForBundle(tx, variation, marketplaceOrder.domain, item.qty, '-')
|
|
468
|
+
|
|
469
|
+
variation.reserveQty -= item.qty
|
|
470
|
+
variation.qty += item.qty
|
|
471
|
+
|
|
472
|
+
variation = await tx.getRepository(MarketplaceProductVariation).save(variation)
|
|
473
|
+
})
|
|
474
|
+
)
|
|
475
|
+
}
|
|
438
476
|
}
|
|
439
|
-
|
|
440
|
-
foundVariation.qty += item.qty
|
|
441
|
-
foundVariation = await tx.getRepository(MarketplaceProductVariation).save(foundVariation)
|
|
442
477
|
})
|
|
443
478
|
)
|
|
444
479
|
|
|
445
480
|
await tx.getRepository(MarketplaceOrderItem).delete(nonMatchedOrderItems)
|
|
446
481
|
}
|
|
447
482
|
|
|
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
|
-
|
|
483
|
+
if (!cancelStatuses.includes(marketplaceOrder.status)) {
|
|
484
|
+
existingOrderItems = await Promise.all(
|
|
485
|
+
marketplaceOrderItems.map(async item => {
|
|
486
|
+
const matchedOrderItem = existingOrderItems.find(
|
|
487
|
+
itm =>
|
|
488
|
+
itm.name == item.name &&
|
|
489
|
+
itm.marketplaceProductVariation.variationId == item.marketplaceProductVariation.variationId
|
|
490
|
+
)
|
|
491
|
+
if (matchedOrderItem) {
|
|
492
|
+
for (var foundVariation of foundVariations) {
|
|
493
|
+
if (foundVariation.sku) {
|
|
494
|
+
let allVariations: MarketplaceProductVariation[] = await tx
|
|
495
|
+
.getRepository(MarketplaceProductVariation)
|
|
496
|
+
.find({
|
|
497
|
+
where: { domain: foundVariation.domain, sku: foundVariation.sku },
|
|
498
|
+
relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
|
|
499
|
+
})
|
|
500
|
+
|
|
501
|
+
let activeVariations: MarketplaceProductVariation[] = allVariations.filter(
|
|
502
|
+
variation => variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
|
|
503
|
+
)
|
|
504
|
+
|
|
505
|
+
await Promise.all(
|
|
506
|
+
activeVariations.map(async variation => {
|
|
507
|
+
if (!existingMarketplaceOrder?.releaseOrderId && matchedOrderItem.qty - item.qty != 0) {
|
|
508
|
+
await calculateReserveQtyForBundle(
|
|
509
|
+
tx,
|
|
510
|
+
variation,
|
|
511
|
+
marketplaceOrder.domain,
|
|
512
|
+
matchedOrderItem.qty - item.qty
|
|
513
|
+
)
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
variation.reserveQty = existingMarketplaceOrder?.releaseOrderId
|
|
517
|
+
? variation.reserveQty
|
|
518
|
+
: variation.reserveQty - item.qty + matchedOrderItem.qty
|
|
519
|
+
|
|
520
|
+
variation.qty = existingMarketplaceOrder?.releaseOrderId
|
|
521
|
+
? variation.qty
|
|
522
|
+
: variation.qty + item.qty - matchedOrderItem.qty
|
|
523
|
+
|
|
524
|
+
variation = await tx.getRepository(MarketplaceProductVariation).save(variation)
|
|
525
|
+
})
|
|
526
|
+
)
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
return {
|
|
530
|
+
...matchedOrderItem,
|
|
531
|
+
...item,
|
|
532
|
+
updater: user
|
|
533
|
+
}
|
|
488
534
|
}
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
|
|
535
|
+
})
|
|
536
|
+
)
|
|
537
|
+
}
|
|
492
538
|
} 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
|
-
|
|
539
|
+
if (!cancelStatuses.includes(marketplaceOrder.status)) {
|
|
540
|
+
existingOrderItems = await Promise.all(
|
|
541
|
+
marketplaceOrderItems.map(async item => {
|
|
542
|
+
const matchedOrderItem = existingOrderItems.find(
|
|
543
|
+
itm =>
|
|
544
|
+
itm.name == item.name &&
|
|
545
|
+
itm.marketplaceProductVariation.variationId == item.marketplaceProductVariation.variationId
|
|
546
|
+
)
|
|
547
|
+
if (matchedOrderItem) {
|
|
548
|
+
for (var foundVariation of foundVariations) {
|
|
549
|
+
if (foundVariation.sku) {
|
|
550
|
+
let allVariations: MarketplaceProductVariation[] = await tx
|
|
551
|
+
.getRepository(MarketplaceProductVariation)
|
|
552
|
+
.find({
|
|
553
|
+
where: { domain: foundVariation.domain, sku: foundVariation.sku },
|
|
554
|
+
relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
|
|
555
|
+
})
|
|
556
|
+
|
|
557
|
+
let activeVariations: MarketplaceProductVariation[] = allVariations.filter(
|
|
558
|
+
variation => variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
|
|
559
|
+
)
|
|
560
|
+
|
|
561
|
+
await Promise.all(
|
|
562
|
+
activeVariations.map(async variation => {
|
|
563
|
+
if (!existingMarketplaceOrder?.releaseOrderId && matchedOrderItem.qty - item.qty != 0) {
|
|
564
|
+
await calculateReserveQtyForBundle(
|
|
565
|
+
tx,
|
|
566
|
+
variation,
|
|
567
|
+
marketplaceOrder.domain,
|
|
568
|
+
matchedOrderItem.qty - item.qty
|
|
569
|
+
)
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
variation.reserveQty = existingMarketplaceOrder?.releaseOrderId
|
|
573
|
+
? variation.reserveQty
|
|
574
|
+
: variation.reserveQty - item.qty + matchedOrderItem.qty
|
|
575
|
+
|
|
576
|
+
variation.qty = existingMarketplaceOrder?.releaseOrderId
|
|
577
|
+
? variation.qty
|
|
578
|
+
: variation.qty + item.qty - matchedOrderItem.qty
|
|
579
|
+
|
|
580
|
+
variation = await tx.getRepository(MarketplaceProductVariation).save(variation)
|
|
581
|
+
})
|
|
582
|
+
)
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
return {
|
|
586
|
+
...matchedOrderItem,
|
|
587
|
+
...item,
|
|
588
|
+
updater: user
|
|
589
|
+
}
|
|
590
|
+
} else {
|
|
591
|
+
for (var foundVariation of foundVariations) {
|
|
592
|
+
if (foundVariation.sku) {
|
|
593
|
+
let allVariations: MarketplaceProductVariation[] = await tx
|
|
594
|
+
.getRepository(MarketplaceProductVariation)
|
|
595
|
+
.find({
|
|
596
|
+
where: { domain: foundVariation.domain, sku: foundVariation.sku },
|
|
597
|
+
relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
|
|
598
|
+
})
|
|
599
|
+
|
|
600
|
+
let activeVariations: MarketplaceProductVariation[] = allVariations.filter(
|
|
601
|
+
variation => variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
|
|
602
|
+
)
|
|
603
|
+
|
|
604
|
+
await Promise.all(
|
|
605
|
+
activeVariations.map(async variation => {
|
|
606
|
+
await calculateReserveQtyForBundle(tx, variation, marketplaceOrder.domain, item.qty)
|
|
607
|
+
|
|
608
|
+
variation.reserveQty += item.qty
|
|
609
|
+
|
|
610
|
+
if (variation.qty - item.qty >= 0) {
|
|
611
|
+
variation.qty -= item.qty
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
variation = await tx.getRepository(MarketplaceProductVariation).save(variation)
|
|
615
|
+
})
|
|
616
|
+
)
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
return {
|
|
620
|
+
...item,
|
|
621
|
+
updater: user
|
|
622
|
+
}
|
|
533
623
|
}
|
|
534
|
-
}
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
.getRepository(MarketplaceProductVariation)
|
|
538
|
-
.find({
|
|
539
|
-
where: { domain: foundVariation.domain, sku: foundVariation.sku },
|
|
540
|
-
relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
|
|
541
|
-
})
|
|
542
|
-
|
|
543
|
-
let variationsFromOtherStores: MarketplaceProductVariation[] = allVariations.filter(
|
|
544
|
-
variation =>
|
|
545
|
-
variation.id != foundVariation.id &&
|
|
546
|
-
variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
|
|
547
|
-
)
|
|
548
|
-
|
|
549
|
-
variationsFromOtherStores.map(async variation => {
|
|
550
|
-
variation.reserveQty += item.qty
|
|
551
|
-
|
|
552
|
-
variation = await tx.getRepository(MarketplaceProductVariation).save(variation)
|
|
553
|
-
})
|
|
554
|
-
}
|
|
555
|
-
|
|
556
|
-
foundVariation.qty -= item.qty
|
|
557
|
-
foundVariation = await tx.getRepository(MarketplaceProductVariation).save(foundVariation)
|
|
558
|
-
|
|
559
|
-
return {
|
|
560
|
-
...item,
|
|
561
|
-
updater: user
|
|
562
|
-
}
|
|
563
|
-
}
|
|
564
|
-
})
|
|
565
|
-
)
|
|
624
|
+
})
|
|
625
|
+
)
|
|
626
|
+
}
|
|
566
627
|
}
|
|
567
628
|
|
|
568
629
|
marketplaceOrderItems = existingOrderItems
|
|
@@ -575,35 +636,42 @@ export const syncMarketplaceOrder = {
|
|
|
575
636
|
}
|
|
576
637
|
}
|
|
577
638
|
} else {
|
|
578
|
-
if (
|
|
579
|
-
|
|
580
|
-
.
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
639
|
+
if (!cancelStatuses.includes(marketplaceOrder.status)) {
|
|
640
|
+
for (var foundVariation of foundVariations) {
|
|
641
|
+
if (foundVariation.sku) {
|
|
642
|
+
let allVariations: MarketplaceProductVariation[] = await tx
|
|
643
|
+
.getRepository(MarketplaceProductVariation)
|
|
644
|
+
.find({
|
|
645
|
+
where: { domain: foundVariation.domain, sku: foundVariation.sku },
|
|
646
|
+
relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
|
|
647
|
+
})
|
|
585
648
|
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
649
|
+
let activeVariations: MarketplaceProductVariation[] = allVariations.filter(
|
|
650
|
+
variation => variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
|
|
651
|
+
)
|
|
652
|
+
let item = order.orderItems.find(e => e.variationId == foundVariation.variationId)
|
|
653
|
+
await Promise.all(
|
|
654
|
+
activeVariations.map(async variation => {
|
|
655
|
+
await calculateReserveQtyForBundle(tx, variation, marketplaceOrder.domain, item.qty)
|
|
591
656
|
|
|
592
|
-
|
|
593
|
-
variation.reserveQty += item.qty
|
|
657
|
+
variation.reserveQty += item.qty
|
|
594
658
|
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
659
|
+
if (variation.qty - item.qty >= 0) {
|
|
660
|
+
variation.qty -= item.qty
|
|
661
|
+
}
|
|
598
662
|
|
|
599
|
-
|
|
600
|
-
|
|
663
|
+
variation = await tx.getRepository(MarketplaceProductVariation).save(variation)
|
|
664
|
+
})
|
|
665
|
+
)
|
|
666
|
+
}
|
|
601
667
|
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
668
|
+
if (order?.orderShipping) {
|
|
669
|
+
savedMarketplaceOrderShipping.totalWeight = orderTotalWeight
|
|
670
|
+
savedMarketplaceOrderShipping = await tx
|
|
671
|
+
.getRepository(MarketplaceOrderShipping)
|
|
672
|
+
.save(savedMarketplaceOrderShipping)
|
|
673
|
+
}
|
|
674
|
+
}
|
|
607
675
|
}
|
|
608
676
|
}
|
|
609
677
|
|
|
@@ -652,3 +720,41 @@ export const syncMarketplaceOrder = {
|
|
|
652
720
|
return true
|
|
653
721
|
}
|
|
654
722
|
}
|
|
723
|
+
|
|
724
|
+
async function calculateReserveQtyForBundle(tx, variation, domain, qty, operator = '+') {
|
|
725
|
+
const productBundle: ProductBundle = await tx.getRepository(ProductBundle).findOne({
|
|
726
|
+
where: { sku: variation.sku, domain }
|
|
727
|
+
})
|
|
728
|
+
|
|
729
|
+
if (productBundle) {
|
|
730
|
+
const productBundleSettings: ProductBundleSetting[] = await tx.getRepository(ProductBundleSetting).find({
|
|
731
|
+
where: { productBundle },
|
|
732
|
+
relations: ['productBundle', 'product']
|
|
733
|
+
})
|
|
734
|
+
|
|
735
|
+
for (let setting of productBundleSettings) {
|
|
736
|
+
const product: Product = await tx.getRepository(Product).findOne({
|
|
737
|
+
where: { id: setting.product.id }
|
|
738
|
+
})
|
|
739
|
+
|
|
740
|
+
const mpvs: MarketplaceProductVariation[] = await tx.getRepository(MarketplaceProductVariation).find({
|
|
741
|
+
where: { domain, sku: product.sku },
|
|
742
|
+
relations: ['domain']
|
|
743
|
+
})
|
|
744
|
+
|
|
745
|
+
for (let mpv of mpvs) {
|
|
746
|
+
if (operator === '+') {
|
|
747
|
+
mpv.reserveQty += setting.bundleQty * qty
|
|
748
|
+
if (mpv.qty - setting.bundleQty * qty >= 0) {
|
|
749
|
+
mpv.qty -= setting.bundleQty * qty
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
if (operator === '-') {
|
|
753
|
+
mpv.reserveQty -= setting.bundleQty * qty
|
|
754
|
+
mpv.qty += setting.bundleQty * qty
|
|
755
|
+
}
|
|
756
|
+
await tx.getRepository(MarketplaceProductVariation).save(mpv)
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
}
|