@things-factory/marketplace-base 4.0.39 → 4.0.40

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.
Files changed (28) hide show
  1. package/dist-server/entities/marketplace-order-shipping.js +12 -3
  2. package/dist-server/entities/marketplace-order-shipping.js.map +1 -1
  3. package/dist-server/entities/marketplace-order.js +4 -0
  4. package/dist-server/entities/marketplace-order.js.map +1 -1
  5. package/dist-server/graphql/resolvers/marketplace-order/sync-all-marketplace-order.js +209 -102
  6. package/dist-server/graphql/resolvers/marketplace-order/sync-all-marketplace-order.js.map +1 -1
  7. package/dist-server/graphql/resolvers/marketplace-order/sync-marketplace-order.js +213 -114
  8. package/dist-server/graphql/resolvers/marketplace-order/sync-marketplace-order.js.map +1 -1
  9. package/dist-server/graphql/resolvers/marketplace-report/marketplace-sales-report.js +13 -2
  10. package/dist-server/graphql/resolvers/marketplace-report/marketplace-sales-report.js.map +1 -1
  11. package/dist-server/graphql/types/marketplace-order-shipping/marketplace-order-shipping-patch.js +1 -0
  12. package/dist-server/graphql/types/marketplace-order-shipping/marketplace-order-shipping-patch.js.map +1 -1
  13. package/dist-server/graphql/types/marketplace-order-shipping/marketplace-order-shipping.js +2 -0
  14. package/dist-server/graphql/types/marketplace-order-shipping/marketplace-order-shipping.js.map +1 -1
  15. package/dist-server/graphql/types/marketplace-order-shipping/new-marketplace-order-shipping.js +1 -0
  16. package/dist-server/graphql/types/marketplace-order-shipping/new-marketplace-order-shipping.js.map +1 -1
  17. package/dist-server/graphql/types/marketplace-report/marketplace-sales-report.js +9 -1
  18. package/dist-server/graphql/types/marketplace-report/marketplace-sales-report.js.map +1 -1
  19. package/package.json +5 -5
  20. package/server/entities/marketplace-order-shipping.ts +7 -0
  21. package/server/entities/marketplace-order.ts +3 -0
  22. package/server/graphql/resolvers/marketplace-order/sync-all-marketplace-order.ts +322 -168
  23. package/server/graphql/resolvers/marketplace-order/sync-marketplace-order.ts +311 -168
  24. package/server/graphql/resolvers/marketplace-report/marketplace-sales-report.ts +14 -2
  25. package/server/graphql/types/marketplace-order-shipping/marketplace-order-shipping-patch.ts +1 -0
  26. package/server/graphql/types/marketplace-order-shipping/marketplace-order-shipping.ts +2 -0
  27. package/server/graphql/types/marketplace-order-shipping/new-marketplace-order-shipping.ts +1 -0
  28. package/server/graphql/types/marketplace-report/marketplace-sales-report.ts +9 -1
@@ -12,7 +12,7 @@ exports.MarketplaceSalesReport = (0, graphql_tag_1.default) `
12
12
  shippingRebate: Float
13
13
  commissionFee: Float
14
14
  serviceFee: Float
15
- transactionFee: String
15
+ transactionFee: Float
16
16
  totalRealeasedAmount: Float
17
17
  voucherCode: String
18
18
  shippingMode: String
@@ -20,6 +20,7 @@ exports.MarketplaceSalesReport = (0, graphql_tag_1.default) `
20
20
  orderNo: String
21
21
  orderCreatedAt: String
22
22
  buyerUsername: String
23
+ payoutDate: String
23
24
  payTime: String
24
25
  totalAmount: Float
25
26
  refundAmount: Float
@@ -60,6 +61,13 @@ exports.MarketplaceSalesReport = (0, graphql_tag_1.default) `
60
61
  fulfillmentCenter: String
61
62
  variationSku: String
62
63
  itemCommissionFee: Float
64
+ address1: String
65
+ address2: String
66
+ address3: String
67
+ city: String
68
+ state: String
69
+ postcode: String
70
+ trackingNo: String
63
71
  }
64
72
  `;
65
73
  //# sourceMappingURL=marketplace-sales-report.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"marketplace-sales-report.js","sourceRoot":"","sources":["../../../../server/graphql/types/marketplace-report/marketplace-sales-report.ts"],"names":[],"mappings":";;;;;;AAAA,8DAA6B;AAEhB,QAAA,sBAAsB,GAAG,IAAA,qBAAG,EAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAwDxC,CAAA"}
1
+ {"version":3,"file":"marketplace-sales-report.js","sourceRoot":"","sources":["../../../../server/graphql/types/marketplace-report/marketplace-sales-report.ts"],"names":[],"mappings":";;;;;;AAAA,8DAA6B;AAEhB,QAAA,sBAAsB,GAAG,IAAA,qBAAG,EAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAgExC,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@things-factory/marketplace-base",
3
- "version": "4.0.39",
3
+ "version": "4.0.40",
4
4
  "main": "dist-server/index.js",
5
5
  "browser": "client/index.js",
6
6
  "things-factory": true,
@@ -24,9 +24,9 @@
24
24
  "migration:create": "node ../../node_modules/typeorm/cli.js migration:create -d ./server/migrations"
25
25
  },
26
26
  "dependencies": {
27
- "@things-factory/integration-fulfillment": "^4.0.39",
28
- "@things-factory/integration-marketplace": "^4.0.39",
29
- "@things-factory/shell": "^4.0.39"
27
+ "@things-factory/integration-fulfillment": "^4.0.40",
28
+ "@things-factory/integration-marketplace": "^4.0.40",
29
+ "@things-factory/shell": "^4.0.40"
30
30
  },
31
- "gitHead": "a4f93ea85a26cf70ae0aa2fcd298aa59b55dd039"
31
+ "gitHead": "f0697b5ab411e4aa1a52de19a6c135f39686eca1"
32
32
  }
@@ -10,6 +10,7 @@ import {
10
10
  } from 'typeorm'
11
11
 
12
12
  import { User } from '@things-factory/auth-base'
13
+ import { FulfillmentCenter } from '@things-factory/integration-fulfillment'
13
14
  import { MarketplaceStore } from '@things-factory/integration-marketplace'
14
15
  import { Domain } from '@things-factory/shell'
15
16
 
@@ -154,6 +155,12 @@ export class MarketplaceOrderShipping {
154
155
  @Column({ type: 'float', nullable: true })
155
156
  sellerShippingFeeVoucher: number
156
157
 
158
+ @Column({ nullable: true })
159
+ releaseOrderId: string
160
+
161
+ @ManyToOne(type => FulfillmentCenter, { nullable: true })
162
+ fulfillmentCenter: FulfillmentCenter
163
+
157
164
  @CreateDateColumn()
158
165
  createdAt: Date
159
166
 
@@ -131,6 +131,9 @@ export class MarketplaceOrder {
131
131
  @Column({ nullable: true })
132
132
  shippingMode: String
133
133
 
134
+ @Column({ nullable: true })
135
+ payoutDate: Date
136
+
134
137
  @CreateDateColumn()
135
138
  createdAt: Date
136
139
 
@@ -4,6 +4,7 @@ import { User } from '@things-factory/auth-base'
4
4
  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
+ import { Product, ProductBundle, ProductBundleSetting } from '@things-factory/product-base'
7
8
  import { Domain } from '@things-factory/shell'
8
9
 
9
10
  import {
@@ -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 this.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 this.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 this.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 this.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 this.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 this.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
 
@@ -634,8 +718,78 @@ export const syncAllMarketplaceOrder = {
634
718
  hasMorePage = false
635
719
  }
636
720
  }
721
+
722
+ if (marketplaceStore.platform === 'shopee') {
723
+ let pageNo: number = 0
724
+ let hasMore = true
725
+ let perPage: number = 100
726
+ var payoutDateList = []
727
+
728
+ while (hasMore) {
729
+ const { result: payoutDates, more } = await StoreAPI.getStoreOrderPayoutDates(marketplaceStore, {
730
+ fromDate,
731
+ toDate,
732
+ pagination: { page: pageNo, limit: perPage }
733
+ })
734
+
735
+ payoutDateList.push(...payoutDates)
736
+ if (more) pageNo++
737
+ hasMore = more
738
+ }
739
+
740
+ await Promise.all(
741
+ payoutDateList.map(async item => {
742
+ await getRepository(MarketplaceOrder).update(
743
+ {
744
+ orderNo: item.orderNo
745
+ },
746
+ {
747
+ payoutDate: item.payoutDate
748
+ }
749
+ )
750
+ })
751
+ )
752
+ }
637
753
  }
638
754
 
639
755
  return true
640
756
  }
641
757
  }
758
+
759
+ export async function calculateReserveQtyForBundle(tx, variation, domain, qty, operator = '+') {
760
+ const productBundle: ProductBundle = await tx.getRepository(ProductBundle).findOne({
761
+ where: { sku: variation.sku, domain }
762
+ })
763
+
764
+ if (productBundle) {
765
+ const productBundleSettings: ProductBundleSetting[] = await tx.getRepository(ProductBundleSetting).find({
766
+ where: { productBundle },
767
+ relations: ['productBundle', 'product']
768
+ })
769
+
770
+ for (let setting of productBundleSettings) {
771
+ const product: Product = await tx.getRepository(Product).findOne({
772
+ where: { id: setting.product.id }
773
+ })
774
+
775
+ const mpvs: MarketplaceProductVariation[] = await tx.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 tx.getRepository(MarketplaceProductVariation).save(mpv)
792
+ }
793
+ }
794
+ }
795
+ }