@things-factory/marketplace-base 4.0.24 → 4.0.28

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 (22) hide show
  1. package/dist-server/entities/marketplace-product-variation.js +6 -1
  2. package/dist-server/entities/marketplace-product-variation.js.map +1 -1
  3. package/dist-server/graphql/resolvers/marketplace-order/sync-all-marketplace-order.js +100 -13
  4. package/dist-server/graphql/resolvers/marketplace-order/sync-all-marketplace-order.js.map +1 -1
  5. package/dist-server/graphql/resolvers/marketplace-order/sync-marketplace-order.js +106 -9
  6. package/dist-server/graphql/resolvers/marketplace-order/sync-marketplace-order.js.map +1 -1
  7. package/dist-server/graphql/resolvers/marketplace-order-shipping/marketplace-order-shippings.js +1 -1
  8. package/dist-server/graphql/resolvers/marketplace-order-shipping/marketplace-order-shippings.js.map +1 -1
  9. package/dist-server/graphql/types/marketplace-product-variation/marketplace-product-variation-patch.js +1 -0
  10. package/dist-server/graphql/types/marketplace-product-variation/marketplace-product-variation-patch.js.map +1 -1
  11. package/dist-server/graphql/types/marketplace-product-variation/marketplace-product-variation.js +1 -0
  12. package/dist-server/graphql/types/marketplace-product-variation/marketplace-product-variation.js.map +1 -1
  13. package/dist-server/graphql/types/marketplace-product-variation/new-marketplace-product-variation.js +1 -0
  14. package/dist-server/graphql/types/marketplace-product-variation/new-marketplace-product-variation.js.map +1 -1
  15. package/package.json +5 -5
  16. package/server/entities/marketplace-product-variation.ts +9 -2
  17. package/server/graphql/resolvers/marketplace-order/sync-all-marketplace-order.ts +192 -41
  18. package/server/graphql/resolvers/marketplace-order/sync-marketplace-order.ts +181 -36
  19. package/server/graphql/resolvers/marketplace-order-shipping/marketplace-order-shippings.ts +1 -1
  20. package/server/graphql/types/marketplace-product-variation/marketplace-product-variation-patch.ts +1 -0
  21. package/server/graphql/types/marketplace-product-variation/marketplace-product-variation.ts +1 -0
  22. package/server/graphql/types/marketplace-product-variation/new-marketplace-product-variation.ts +1 -0
@@ -15,6 +15,7 @@ exports.NewMarketplaceProductVariation = (0, graphql_tag_1.default) `
15
15
  variationSku: String
16
16
  channelSku: String
17
17
  qty: Float
18
+ reserveQty: Float
18
19
  bufferQty: Float
19
20
  thresholdQty: Float
20
21
  costPrice: Float
@@ -1 +1 @@
1
- {"version":3,"file":"new-marketplace-product-variation.js","sourceRoot":"","sources":["../../../../server/graphql/types/marketplace-product-variation/new-marketplace-product-variation.ts"],"names":[],"mappings":";;;;;;AAAA,8DAA6B;AAEhB,QAAA,8BAA8B,GAAG,IAAA,qBAAG,EAAA;;;;;;;;;;;;;;;;;;;;;;CAsBhD,CAAA"}
1
+ {"version":3,"file":"new-marketplace-product-variation.js","sourceRoot":"","sources":["../../../../server/graphql/types/marketplace-product-variation/new-marketplace-product-variation.ts"],"names":[],"mappings":";;;;;;AAAA,8DAA6B;AAEhB,QAAA,8BAA8B,GAAG,IAAA,qBAAG,EAAA;;;;;;;;;;;;;;;;;;;;;;;CAuBhD,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@things-factory/marketplace-base",
3
- "version": "4.0.24",
3
+ "version": "4.0.28",
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.24",
28
- "@things-factory/integration-marketplace": "^4.0.24",
29
- "@things-factory/shell": "^4.0.24"
27
+ "@things-factory/integration-fulfillment": "^4.0.28",
28
+ "@things-factory/integration-marketplace": "^4.0.28",
29
+ "@things-factory/shell": "^4.0.28"
30
30
  },
31
- "gitHead": "ba94f3effd113a235e78a34391dbcd259ed410b0"
31
+ "gitHead": "fc526e27ae290a6fa3f001ce7ee75a2904b1d363"
32
32
  }
@@ -1,5 +1,3 @@
1
- import { User } from '@things-factory/auth-base'
2
- import { Domain } from '@things-factory/shell'
3
1
  import {
4
2
  Column,
5
3
  CreateDateColumn,
@@ -10,8 +8,13 @@ import {
10
8
  PrimaryGeneratedColumn,
11
9
  UpdateDateColumn
12
10
  } from 'typeorm'
11
+
12
+ import { User } from '@things-factory/auth-base'
13
+ import { Domain } from '@things-factory/shell'
14
+
13
15
  import { MarketplaceProduct, MarketplaceProductVariationPrice } from '../entities'
14
16
  import { NoGenerator } from '../utils'
17
+
15
18
  @Entity()
16
19
  @Index(
17
20
  'ix_marketplace-product-variation_0',
@@ -33,6 +36,7 @@ export class MarketplaceProductVariation {
33
36
  this.name = productVariation.name ? productVariation.name.toString() : NoGenerator.id()
34
37
  this.description = productVariation.description
35
38
  this.qty = productVariation.qty
39
+ this.reserveQty = productVariation.reserveQty
36
40
  this.type = productVariation.type
37
41
  this.mrpPrice = productVariation.mrpPrice
38
42
  this.costPrice = productVariation.costPrice
@@ -91,6 +95,9 @@ export class MarketplaceProductVariation {
91
95
  @Column('float', { nullable: true })
92
96
  qty: number
93
97
 
98
+ @Column('float', { nullable: true })
99
+ reserveQty: number
100
+
94
101
  @Column({ nullable: true })
95
102
  type: string
96
103
 
@@ -1,4 +1,4 @@
1
- import { EntityManager, getRepository } from 'typeorm'
1
+ import { getRepository } from 'typeorm'
2
2
 
3
3
  import { User } from '@things-factory/auth-base'
4
4
  import { Bizplace, getCustomerBizplaces } from '@things-factory/biz-base'
@@ -16,7 +16,7 @@ import { NoGenerator } from '../../../utils'
16
16
 
17
17
  export const syncAllMarketplaceOrder = {
18
18
  async syncAllMarketplaceOrder(_: any, { companyDomainId, fromDate, toDate, blackListStores }, context: any) {
19
- const { user, tx }: { user: User; tx: EntityManager } = context.state
19
+ const { user }: { user: User } = context.state
20
20
 
21
21
  const companyDomain: Domain = await getRepository(Domain).findOne(companyDomainId)
22
22
 
@@ -127,14 +127,23 @@ export const syncAllMarketplaceOrder = {
127
127
  otherInfoJSON.shippingProvider = deliveryProvider
128
128
  }
129
129
 
130
+ marketplaceOrder = {
131
+ ...existingMarketplaceOrder,
132
+ ...marketplaceOrder,
133
+ otherInfoJSON: otherInfoJSON ? JSON.stringify(otherInfoJSON) : null,
134
+ updater: user
135
+ }
136
+
130
137
  if (marketplaceOrder?.releaseOrderId) {
131
138
  const fulfillmentCenter: FulfillmentCenter = marketplaceOrder.fulfillmentCenter
132
139
  const centerId: string = fulfillmentCenter.centerId
133
- const warehouseDomain: Domain = await tx
134
- .getRepository(Domain)
135
- .findOne({ where: { subdomain: centerId } })
140
+ const warehouseDomain: Domain = await getRepository(Domain).findOne({
141
+ where: { subdomain: centerId }
142
+ })
136
143
  const customerBizplaces: Bizplace[] = await getCustomerBizplaces(warehouseDomain)
137
- const customerBizplaceId: string = customerBizplaces[0].id
144
+ const customerBizplaceId: string = customerBizplaces.find(
145
+ customerBizplace => customerBizplace.company.domain.id == domain.id
146
+ ).id
138
147
 
139
148
  let { items: releaseOrders }: any = await FulfillmentAPI.getOutboundOrders(fulfillmentCenter, {
140
149
  customerBizplaceId,
@@ -174,9 +183,9 @@ export const syncAllMarketplaceOrder = {
174
183
  const fulfillmentCenter: FulfillmentCenter = marketplaceOrder.fulfillmentCenter
175
184
  const centerId: string = fulfillmentCenter.centerId
176
185
 
177
- const warehouseDomain: Domain = await tx
178
- .getRepository(Domain)
179
- .findOne({ where: { subdomain: centerId } })
186
+ const warehouseDomain: Domain = await getRepository(Domain).findOne({
187
+ where: { subdomain: centerId }
188
+ })
180
189
  const customerBizplaces: Bizplace[] = await getCustomerBizplaces(warehouseDomain)
181
190
  const customerBizplaceId: string = customerBizplaces[0].id
182
191
 
@@ -380,43 +389,161 @@ export const syncAllMarketplaceOrder = {
380
389
  nonMatchedOrderItems.push({ ...item })
381
390
  }
382
391
  })
383
- if (nonMatchedOrderItems.length > 0 && !cancelStatuses.includes(marketplaceOrder.status))
392
+ if (nonMatchedOrderItems.length > 0 && !cancelStatuses.includes(marketplaceOrder.status)) {
393
+ await Promise.all(
394
+ nonMatchedOrderItems.map(async item => {
395
+ if (foundVariation.sku) {
396
+ let allVariations: MarketplaceProductVariation[] = await getRepository(
397
+ MarketplaceProductVariation
398
+ ).find({
399
+ where: { domain: foundVariation.domain, sku: foundVariation.sku },
400
+ relations: [
401
+ 'domain',
402
+ 'marketplaceProduct',
403
+ 'marketplaceProduct.marketplaceStore',
404
+ 'marketplaceProduct.marketplaceStore.marketplaceDistributors'
405
+ ]
406
+ })
407
+
408
+ let variationsFromOtherStores: MarketplaceProductVariation[] = allVariations.filter(
409
+ variation =>
410
+ variation.id != foundVariation.id &&
411
+ variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
412
+ )
413
+
414
+ variationsFromOtherStores.map(async variation => {
415
+ variation.reserveQty -= item.qty
416
+
417
+ variation = await getRepository(MarketplaceProductVariation).save(variation)
418
+ })
419
+ }
420
+
421
+ foundVariation.qty += item.qty
422
+ foundVariation = await getRepository(MarketplaceProductVariation).save(foundVariation)
423
+ })
424
+ )
425
+
384
426
  await getRepository(MarketplaceOrderItem).delete(nonMatchedOrderItems)
427
+ }
385
428
 
386
- existingOrderItems = marketplaceOrderItems.map(item => {
387
- const matchedOrderItem = existingOrderItems.find(
388
- itm =>
389
- itm.name == item.name &&
390
- itm.marketplaceProductVariation.variationId == item.marketplaceProductVariation.variationId
391
- )
392
- if (matchedOrderItem) {
393
- return {
394
- ...matchedOrderItem,
395
- ...item,
396
- updater: user
429
+ existingOrderItems = await Promise.all(
430
+ marketplaceOrderItems.map(async item => {
431
+ const matchedOrderItem = existingOrderItems.find(
432
+ itm =>
433
+ itm.name == item.name &&
434
+ itm.marketplaceProductVariation.variationId == item.marketplaceProductVariation.variationId
435
+ )
436
+ if (matchedOrderItem) {
437
+ if (foundVariation.sku) {
438
+ let allVariations: MarketplaceProductVariation[] = await getRepository(
439
+ MarketplaceProductVariation
440
+ ).find({
441
+ where: { domain: foundVariation.domain, sku: foundVariation.sku },
442
+ relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
443
+ })
444
+
445
+ let variationsFromOtherStores: MarketplaceProductVariation[] = allVariations.filter(
446
+ variation =>
447
+ variation.id != foundVariation.id &&
448
+ variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
449
+ )
450
+
451
+ variationsFromOtherStores.map(async variation => {
452
+ variation.reserveQty = existingMarketplaceOrder?.releaseOrderId
453
+ ? variation.reserveQty + matchedOrderItem.qty
454
+ : variation.reserveQty - item.qty + matchedOrderItem.qty
455
+
456
+ variation = await getRepository(MarketplaceProductVariation).save(variation)
457
+ })
458
+ }
459
+
460
+ foundVariation.qty = existingMarketplaceOrder?.releaseOrderId
461
+ ? foundVariation.qty - matchedOrderItem.qty
462
+ : foundVariation.qty + item.qty - matchedOrderItem.qty
463
+ foundVariation = await getRepository(MarketplaceProductVariation).save(foundVariation)
464
+
465
+ return {
466
+ ...matchedOrderItem,
467
+ ...item,
468
+ updater: user
469
+ }
397
470
  }
398
- }
399
- })
471
+ })
472
+ )
400
473
  } else {
401
- existingOrderItems = marketplaceOrderItems.map(item => {
402
- const matchedOrderItem = existingOrderItems.find(
403
- itm =>
404
- itm.name == item.name &&
405
- itm.marketplaceProductVariation.variationId == item.marketplaceProductVariation.variationId
406
- )
407
- if (matchedOrderItem) {
408
- return {
409
- ...matchedOrderItem,
410
- ...item,
411
- updater: user
474
+ existingOrderItems = await Promise.all(
475
+ marketplaceOrderItems.map(async item => {
476
+ const matchedOrderItem = existingOrderItems.find(
477
+ itm =>
478
+ itm.name == item.name &&
479
+ itm.marketplaceProductVariation.variationId == item.marketplaceProductVariation.variationId
480
+ )
481
+ if (matchedOrderItem) {
482
+ if (foundVariation.sku) {
483
+ let allVariations: MarketplaceProductVariation[] = await getRepository(
484
+ MarketplaceProductVariation
485
+ ).find({
486
+ where: { domain: foundVariation.domain, sku: foundVariation.sku },
487
+ relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
488
+ })
489
+
490
+ let variationsFromOtherStores: MarketplaceProductVariation[] = allVariations.filter(
491
+ variation =>
492
+ variation.id != foundVariation.id &&
493
+ variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
494
+ )
495
+
496
+ variationsFromOtherStores.map(async variation => {
497
+ variation.reserveQty = existingMarketplaceOrder?.releaseOrderId
498
+ ? variation.reserveQty + matchedOrderItem.qty
499
+ : variation.reserveQty - item.qty + matchedOrderItem.qty
500
+
501
+ variation = await getRepository(MarketplaceProductVariation).save(variation)
502
+ })
503
+ }
504
+
505
+ foundVariation.qty = existingMarketplaceOrder?.releaseOrderId
506
+ ? foundVariation.qty - matchedOrderItem.qty
507
+ : foundVariation.qty + item.qty - matchedOrderItem.qty
508
+ foundVariation = await getRepository(MarketplaceProductVariation).save(foundVariation)
509
+
510
+ return {
511
+ ...matchedOrderItem,
512
+ ...item,
513
+ updater: user
514
+ }
515
+ } else {
516
+ if (foundVariation.sku) {
517
+ let allVariations: MarketplaceProductVariation[] = await getRepository(
518
+ MarketplaceProductVariation
519
+ ).find({
520
+ where: { domain: foundVariation.domain, sku: foundVariation.sku },
521
+ relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
522
+ })
523
+
524
+ let variationsFromOtherStores: MarketplaceProductVariation[] = allVariations.filter(
525
+ variation =>
526
+ variation.id != foundVariation.id &&
527
+ variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
528
+ )
529
+
530
+ variationsFromOtherStores.map(async variation => {
531
+ variation.reserveQty += item.qty
532
+
533
+ variation = await getRepository(MarketplaceProductVariation).save(variation)
534
+ })
535
+ }
536
+
537
+ foundVariation.qty -= item.qty
538
+ foundVariation = await getRepository(MarketplaceProductVariation).save(foundVariation)
539
+
540
+ return {
541
+ ...item,
542
+ updater: user
543
+ }
412
544
  }
413
- } else {
414
- return {
415
- ...item,
416
- updater: user
417
- }
418
- }
419
- })
545
+ })
546
+ )
420
547
  }
421
548
 
422
549
  marketplaceOrderItems = existingOrderItems
@@ -429,6 +556,30 @@ export const syncAllMarketplaceOrder = {
429
556
  }
430
557
  }
431
558
  } else {
559
+ if (foundVariation.sku) {
560
+ let allVariations: MarketplaceProductVariation[] = await getRepository(
561
+ MarketplaceProductVariation
562
+ ).find({
563
+ where: { domain: foundVariation.domain, sku: foundVariation.sku },
564
+ relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
565
+ })
566
+
567
+ let variationsFromOtherStores: MarketplaceProductVariation[] = allVariations.filter(
568
+ variation =>
569
+ variation.id != foundVariation.id &&
570
+ variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
571
+ )
572
+
573
+ variationsFromOtherStores.map(async variation => {
574
+ variation.reserveQty += item.qty
575
+
576
+ variation = await getRepository(MarketplaceProductVariation).save(variation)
577
+ })
578
+ }
579
+
580
+ foundVariation.qty -= item.qty
581
+ foundVariation = await getRepository(MarketplaceProductVariation).save(foundVariation)
582
+
432
583
  if (order?.orderShipping) {
433
584
  savedMarketplaceOrderShipping.totalWeight = orderTotalWeight
434
585
  savedMarketplaceOrderShipping = await getRepository(MarketplaceOrderShipping).save(
@@ -124,7 +124,9 @@ export const syncMarketplaceOrder = {
124
124
  const centerId: string = fulfillmentCenter.centerId
125
125
  const warehouseDomain: Domain = await tx.getRepository(Domain).findOne({ where: { subdomain: centerId } })
126
126
  const customerBizplaces: Bizplace[] = await getCustomerBizplaces(warehouseDomain)
127
- const customerBizplaceId: string = customerBizplaces[0].id
127
+ const customerBizplaceId: string = customerBizplaces.find(
128
+ customerBizplace => customerBizplace.company.domain.id == domain.id
129
+ ).id
128
130
 
129
131
  let { items: releaseOrders }: any = await FulfillmentAPI.getOutboundOrders(fulfillmentCenter, {
130
132
  customerBizplaceId,
@@ -285,9 +287,10 @@ export const syncMarketplaceOrder = {
285
287
  let foundVariation
286
288
  for (var j = 0; j < order.orderItems.length; j++) {
287
289
  var item = order.orderItems[j]
288
- foundVariation = await tx
289
- .getRepository(MarketplaceProductVariation)
290
- .findOne({ where: { domain: marketplaceStore.domain, variationId: item.variationId } })
290
+ foundVariation = await tx.getRepository(MarketplaceProductVariation).findOne({
291
+ where: { domain: marketplaceStore.domain, variationId: item.variationId },
292
+ relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
293
+ })
291
294
 
292
295
  if (!foundVariation) {
293
296
  let newVariation: any = {
@@ -373,43 +376,161 @@ export const syncMarketplaceOrder = {
373
376
  nonMatchedOrderItems.push({ ...item })
374
377
  }
375
378
  })
376
- if (nonMatchedOrderItems.length > 0 && !cancelStatuses.includes(marketplaceOrder.status))
379
+ if (nonMatchedOrderItems.length > 0 && !cancelStatuses.includes(marketplaceOrder.status)) {
380
+ await Promise.all(
381
+ nonMatchedOrderItems.map(async item => {
382
+ if (foundVariation.sku) {
383
+ let allVariations: MarketplaceProductVariation[] = await tx
384
+ .getRepository(MarketplaceProductVariation)
385
+ .find({
386
+ where: { domain: foundVariation.domain, sku: foundVariation.sku },
387
+ relations: [
388
+ 'domain',
389
+ 'marketplaceProduct',
390
+ 'marketplaceProduct.marketplaceStore',
391
+ 'marketplaceProduct.marketplaceStore.marketplaceDistributors'
392
+ ]
393
+ })
394
+
395
+ let variationsFromOtherStores: MarketplaceProductVariation[] = allVariations.filter(
396
+ variation =>
397
+ variation.id != foundVariation.id &&
398
+ variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
399
+ )
400
+
401
+ variationsFromOtherStores.map(async variation => {
402
+ variation.reserveQty -= item.qty
403
+
404
+ variation = await tx.getRepository(MarketplaceProductVariation).save(variation)
405
+ })
406
+ }
407
+
408
+ foundVariation.qty += item.qty
409
+ foundVariation = await tx.getRepository(MarketplaceProductVariation).save(foundVariation)
410
+ })
411
+ )
412
+
377
413
  await tx.getRepository(MarketplaceOrderItem).delete(nonMatchedOrderItems)
414
+ }
378
415
 
379
- existingOrderItems = marketplaceOrderItems.map(item => {
380
- const matchedOrderItem = existingOrderItems.find(
381
- itm =>
382
- itm.name == item.name &&
383
- itm.marketplaceProductVariation.variationId == item.marketplaceProductVariation.variationId
384
- )
385
- if (matchedOrderItem) {
386
- return {
387
- ...matchedOrderItem,
388
- ...item,
389
- updater: user
416
+ existingOrderItems = await Promise.all(
417
+ marketplaceOrderItems.map(async item => {
418
+ const matchedOrderItem = existingOrderItems.find(
419
+ itm =>
420
+ itm.name == item.name &&
421
+ itm.marketplaceProductVariation.variationId == item.marketplaceProductVariation.variationId
422
+ )
423
+ if (matchedOrderItem) {
424
+ if (foundVariation.sku) {
425
+ let allVariations: MarketplaceProductVariation[] = await tx
426
+ .getRepository(MarketplaceProductVariation)
427
+ .find({
428
+ where: { domain: foundVariation.domain, sku: foundVariation.sku },
429
+ relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
430
+ })
431
+
432
+ let variationsFromOtherStores: MarketplaceProductVariation[] = allVariations.filter(
433
+ variation =>
434
+ variation.id != foundVariation.id &&
435
+ variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
436
+ )
437
+
438
+ variationsFromOtherStores.map(async variation => {
439
+ variation.reserveQty = existingMarketplaceOrder?.releaseOrderId
440
+ ? variation.reserveQty + matchedOrderItem.qty
441
+ : variation.reserveQty - item.qty + matchedOrderItem.qty
442
+
443
+ variation = await tx.getRepository(MarketplaceProductVariation).save(variation)
444
+ })
445
+ }
446
+
447
+ foundVariation.qty = existingMarketplaceOrder?.releaseOrderId
448
+ ? foundVariation.qty - matchedOrderItem.qty
449
+ : foundVariation.qty + item.qty - matchedOrderItem.qty
450
+ foundVariation = await tx.getRepository(MarketplaceProductVariation).save(foundVariation)
451
+
452
+ return {
453
+ ...matchedOrderItem,
454
+ ...item,
455
+ updater: user
456
+ }
390
457
  }
391
- }
392
- })
458
+ })
459
+ )
393
460
  } else {
394
- existingOrderItems = marketplaceOrderItems.map(item => {
395
- const matchedOrderItem = existingOrderItems.find(
396
- itm =>
397
- itm.name == item.name &&
398
- itm.marketplaceProductVariation.variationId == item.marketplaceProductVariation.variationId
399
- )
400
- if (matchedOrderItem) {
401
- return {
402
- ...matchedOrderItem,
403
- ...item,
404
- updater: user
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 tx
471
+ .getRepository(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 tx.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 tx.getRepository(MarketplaceProductVariation).save(foundVariation)
496
+
497
+ return {
498
+ ...matchedOrderItem,
499
+ ...item,
500
+ updater: user
501
+ }
502
+ } else {
503
+ if (foundVariation.sku) {
504
+ let allVariations: MarketplaceProductVariation[] = await tx
505
+ .getRepository(MarketplaceProductVariation)
506
+ .find({
507
+ where: { domain: foundVariation.domain, sku: foundVariation.sku },
508
+ relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
509
+ })
510
+
511
+ let variationsFromOtherStores: MarketplaceProductVariation[] = allVariations.filter(
512
+ variation =>
513
+ variation.id != foundVariation.id &&
514
+ variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
515
+ )
516
+
517
+ variationsFromOtherStores.map(async variation => {
518
+ variation.reserveQty += item.qty
519
+
520
+ variation = await tx.getRepository(MarketplaceProductVariation).save(variation)
521
+ })
522
+ }
523
+
524
+ foundVariation.qty -= item.qty
525
+ foundVariation = await tx.getRepository(MarketplaceProductVariation).save(foundVariation)
526
+
527
+ return {
528
+ ...item,
529
+ updater: user
530
+ }
405
531
  }
406
- } else {
407
- return {
408
- ...item,
409
- updater: user
410
- }
411
- }
412
- })
532
+ })
533
+ )
413
534
  }
414
535
 
415
536
  marketplaceOrderItems = existingOrderItems
@@ -422,6 +543,30 @@ export const syncMarketplaceOrder = {
422
543
  }
423
544
  }
424
545
  } else {
546
+ if (foundVariation.sku) {
547
+ let allVariations: MarketplaceProductVariation[] = await tx
548
+ .getRepository(MarketplaceProductVariation)
549
+ .find({
550
+ where: { domain: foundVariation.domain, sku: foundVariation.sku },
551
+ relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
552
+ })
553
+
554
+ let variationsFromOtherStores: MarketplaceProductVariation[] = allVariations.filter(
555
+ variation =>
556
+ variation.id != foundVariation.id &&
557
+ variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
558
+ )
559
+
560
+ variationsFromOtherStores.map(async variation => {
561
+ variation.reserveQty += item.qty
562
+
563
+ variation = await tx.getRepository(MarketplaceProductVariation).save(variation)
564
+ })
565
+ }
566
+
567
+ foundVariation.qty -= item.qty
568
+ foundVariation = await tx.getRepository(MarketplaceProductVariation).save(foundVariation)
569
+
425
570
  if (order?.orderShipping) {
426
571
  savedMarketplaceOrderShipping.totalWeight = orderTotalWeight
427
572
  savedMarketplaceOrderShipping = await tx
@@ -17,7 +17,7 @@ export const marketplaceOrderShippingsResolver = {
17
17
  .innerJoinAndSelect('MOI.marketplaceProductVariation', 'MPV')
18
18
  .innerJoinAndSelect('MOS.marketplaceStore', 'MS')
19
19
  .innerJoinAndSelect('MOS.domain', 'domain')
20
- .innerJoinAndSelect('MOS.creator', 'creator')
20
+ .leftJoinAndSelect('MOS.creator', 'creator')
21
21
  .leftJoinAndSelect('MOS.updater', 'updater')
22
22
 
23
23
  buildQuery(qb, params, context, false)
@@ -14,6 +14,7 @@ export const MarketplaceProductVariationPatch = gql`
14
14
  type: String
15
15
  description: String
16
16
  qty: Float
17
+ reserveQty: Float
17
18
  bufferQty: Float
18
19
  thresholdQty: Float
19
20
  costPrice: Float
@@ -11,6 +11,7 @@ export const MarketplaceProductVariation = gql`
11
11
  variationSku: String
12
12
  channelSku: String
13
13
  qty: Float
14
+ reserveQty: Float
14
15
  bufferQty: Float
15
16
  thresholdQty: Float
16
17
  name: String
@@ -10,6 +10,7 @@ export const NewMarketplaceProductVariation = gql`
10
10
  variationSku: String
11
11
  channelSku: String
12
12
  qty: Float
13
+ reserveQty: Float
13
14
  bufferQty: Float
14
15
  thresholdQty: Float
15
16
  costPrice: Float