@things-factory/marketplace-base 3.8.10 → 3.8.19

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 (19) 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 +135 -25
  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 +144 -22
  6. package/dist-server/graphql/resolvers/marketplace-order/sync-marketplace-order.js.map +1 -1
  7. package/dist-server/graphql/types/marketplace-product-variation/marketplace-product-variation-patch.js +1 -0
  8. package/dist-server/graphql/types/marketplace-product-variation/marketplace-product-variation-patch.js.map +1 -1
  9. package/dist-server/graphql/types/marketplace-product-variation/marketplace-product-variation.js +1 -0
  10. package/dist-server/graphql/types/marketplace-product-variation/marketplace-product-variation.js.map +1 -1
  11. package/dist-server/graphql/types/marketplace-product-variation/new-marketplace-product-variation.js +1 -0
  12. package/dist-server/graphql/types/marketplace-product-variation/new-marketplace-product-variation.js.map +1 -1
  13. package/package.json +4 -4
  14. package/server/entities/marketplace-product-variation.ts +9 -2
  15. package/server/graphql/resolvers/marketplace-order/sync-all-marketplace-order.ts +229 -53
  16. package/server/graphql/resolvers/marketplace-order/sync-marketplace-order.ts +219 -49
  17. package/server/graphql/types/marketplace-product-variation/marketplace-product-variation-patch.ts +1 -0
  18. package/server/graphql/types/marketplace-product-variation/marketplace-product-variation.ts +1 -0
  19. package/server/graphql/types/marketplace-product-variation/new-marketplace-product-variation.ts +1 -0
@@ -124,26 +124,53 @@ 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,
131
133
  refNo: marketplaceOrder.orderNo
132
134
  })
133
135
 
134
- let releaseOrder = Object.assign({}, releaseOrders[0])
135
- let patch = {
136
- id: releaseOrder.id,
137
- marketplaceOrderStatus: marketplaceOrder.status,
138
- trackingNo: marketplaceOrder.trackingNo,
139
- transporter: dropPickProvider ? dropPickProvider : marketplaceOrder.shippingProvider
140
- }
136
+ if (existingMarketplaceOrder.isSplitted) {
137
+ for (let a = 0; a < releaseOrders.length; a++) {
138
+ const releaseOrder: any = releaseOrders[a]
139
+ const foundMarketplaceOrderShipping: MarketplaceOrderShipping = await tx
140
+ .getRepository(MarketplaceOrderShipping)
141
+ .findOne({
142
+ where: { domain: marketplaceStore.domain, subOrderNoRef: releaseOrder.refNo2 }
143
+ })
144
+ let patch = {
145
+ id: releaseOrder.id,
146
+ marketplaceOrderStatus: marketplaceOrder.status,
147
+ trackingNo: foundMarketplaceOrderShipping?.trackingNo
148
+ ? foundMarketplaceOrderShipping.trackingNo
149
+ : foundMarketplaceOrderShipping.ownTrackingNo,
150
+ transporter: dropPickProvider ? dropPickProvider : marketplaceOrder.shippingProvider
151
+ }
141
152
 
142
- await FulfillmentAPI.updateReleaseGoodDetails(fulfillmentCenter, {
143
- customerBizplaceId,
144
- releaseOrder: { ...patch },
145
- shippingOrder: null
146
- })
153
+ await FulfillmentAPI.updateReleaseGoodDetails(fulfillmentCenter, {
154
+ customerBizplaceId,
155
+ releaseOrder: { ...patch },
156
+ shippingOrder: null
157
+ })
158
+ }
159
+ } else {
160
+ let releaseOrder = Object.assign({}, releaseOrders[0])
161
+ let patch = {
162
+ id: releaseOrder.id,
163
+ marketplaceOrderStatus: marketplaceOrder.status,
164
+ trackingNo: marketplaceOrder.trackingNo,
165
+ transporter: dropPickProvider ? dropPickProvider : marketplaceOrder.shippingProvider
166
+ }
167
+
168
+ await FulfillmentAPI.updateReleaseGoodDetails(fulfillmentCenter, {
169
+ customerBizplaceId,
170
+ releaseOrder: { ...patch },
171
+ shippingOrder: null
172
+ })
173
+ }
147
174
 
148
175
  if (cancelStatuses.includes(marketplaceOrder.status)) {
149
176
  if (existingMarketplaceOrder.isSplitted) {
@@ -152,7 +179,7 @@ export const syncMarketplaceOrder = {
152
179
  })
153
180
 
154
181
  if (releaseOrders?.length) {
155
- for (var a = 0; a < releaseOrders.length; a++) {
182
+ for (let a = 0; a < releaseOrders.length; a++) {
156
183
  const releaseOrderId: string = releaseOrders[a].id
157
184
  await FulfillmentAPI.cancelReleaseOrder(fulfillmentCenter, { customerBizplaceId, releaseOrderId })
158
185
  }
@@ -285,9 +312,10 @@ export const syncMarketplaceOrder = {
285
312
  let foundVariation
286
313
  for (var j = 0; j < order.orderItems.length; j++) {
287
314
  var item = order.orderItems[j]
288
- foundVariation = await tx
289
- .getRepository(MarketplaceProductVariation)
290
- .findOne({ where: { domain: marketplaceStore.domain, variationId: item.variationId } })
315
+ foundVariation = await tx.getRepository(MarketplaceProductVariation).findOne({
316
+ where: { domain: marketplaceStore.domain, variationId: item.variationId },
317
+ relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
318
+ })
291
319
 
292
320
  if (!foundVariation) {
293
321
  let newVariation: any = {
@@ -373,43 +401,161 @@ export const syncMarketplaceOrder = {
373
401
  nonMatchedOrderItems.push({ ...item })
374
402
  }
375
403
  })
376
- if (nonMatchedOrderItems.length > 0 && !cancelStatuses.includes(marketplaceOrder.status))
404
+ if (nonMatchedOrderItems.length > 0 && !cancelStatuses.includes(marketplaceOrder.status)) {
405
+ await Promise.all(
406
+ nonMatchedOrderItems.map(async item => {
407
+ if (foundVariation.sku) {
408
+ let allVariations: MarketplaceProductVariation[] = await tx
409
+ .getRepository(MarketplaceProductVariation)
410
+ .find({
411
+ where: { domain: foundVariation.domain, sku: foundVariation.sku },
412
+ relations: [
413
+ 'domain',
414
+ 'marketplaceProduct',
415
+ 'marketplaceProduct.marketplaceStore',
416
+ 'marketplaceProduct.marketplaceStore.marketplaceDistributors'
417
+ ]
418
+ })
419
+
420
+ let variationsFromOtherStores: MarketplaceProductVariation[] = allVariations.filter(
421
+ variation =>
422
+ variation.id != foundVariation.id &&
423
+ variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
424
+ )
425
+
426
+ variationsFromOtherStores.map(async variation => {
427
+ variation.reserveQty -= item.qty
428
+
429
+ variation = await tx.getRepository(MarketplaceProductVariation).save(variation)
430
+ })
431
+ }
432
+
433
+ foundVariation.qty += item.qty
434
+ foundVariation = await tx.getRepository(MarketplaceProductVariation).save(foundVariation)
435
+ })
436
+ )
437
+
377
438
  await tx.getRepository(MarketplaceOrderItem).delete(nonMatchedOrderItems)
439
+ }
378
440
 
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
441
+ existingOrderItems = await Promise.all(
442
+ marketplaceOrderItems.map(async item => {
443
+ const matchedOrderItem = existingOrderItems.find(
444
+ itm =>
445
+ itm.name == item.name &&
446
+ itm.marketplaceProductVariation.variationId == item.marketplaceProductVariation.variationId
447
+ )
448
+ if (matchedOrderItem) {
449
+ if (foundVariation.sku) {
450
+ let allVariations: MarketplaceProductVariation[] = await tx
451
+ .getRepository(MarketplaceProductVariation)
452
+ .find({
453
+ where: { domain: foundVariation.domain, sku: foundVariation.sku },
454
+ relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
455
+ })
456
+
457
+ let variationsFromOtherStores: MarketplaceProductVariation[] = allVariations.filter(
458
+ variation =>
459
+ variation.id != foundVariation.id &&
460
+ variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
461
+ )
462
+
463
+ variationsFromOtherStores.map(async variation => {
464
+ variation.reserveQty = existingMarketplaceOrder?.releaseOrderId
465
+ ? variation.reserveQty + matchedOrderItem.qty
466
+ : variation.reserveQty - item.qty + matchedOrderItem.qty
467
+
468
+ variation = await tx.getRepository(MarketplaceProductVariation).save(variation)
469
+ })
470
+ }
471
+
472
+ foundVariation.qty = existingMarketplaceOrder?.releaseOrderId
473
+ ? foundVariation.qty - matchedOrderItem.qty
474
+ : foundVariation.qty + item.qty - matchedOrderItem.qty
475
+ foundVariation = await tx.getRepository(MarketplaceProductVariation).save(foundVariation)
476
+
477
+ return {
478
+ ...matchedOrderItem,
479
+ ...item,
480
+ updater: user
481
+ }
390
482
  }
391
- }
392
- })
483
+ })
484
+ )
393
485
  } 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
405
- }
406
- } else {
407
- return {
408
- ...item,
409
- updater: user
486
+ existingOrderItems = await Promise.all(
487
+ marketplaceOrderItems.map(async item => {
488
+ const matchedOrderItem = existingOrderItems.find(
489
+ itm =>
490
+ itm.name == item.name &&
491
+ itm.marketplaceProductVariation.variationId == item.marketplaceProductVariation.variationId
492
+ )
493
+ if (matchedOrderItem) {
494
+ if (foundVariation.sku) {
495
+ let allVariations: MarketplaceProductVariation[] = await tx
496
+ .getRepository(MarketplaceProductVariation)
497
+ .find({
498
+ where: { domain: foundVariation.domain, sku: foundVariation.sku },
499
+ relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
500
+ })
501
+
502
+ let variationsFromOtherStores: MarketplaceProductVariation[] = allVariations.filter(
503
+ variation =>
504
+ variation.id != foundVariation.id &&
505
+ variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
506
+ )
507
+
508
+ variationsFromOtherStores.map(async variation => {
509
+ variation.reserveQty = existingMarketplaceOrder?.releaseOrderId
510
+ ? variation.reserveQty + matchedOrderItem.qty
511
+ : variation.reserveQty - item.qty + matchedOrderItem.qty
512
+
513
+ variation = await tx.getRepository(MarketplaceProductVariation).save(variation)
514
+ })
515
+ }
516
+
517
+ foundVariation.qty = existingMarketplaceOrder?.releaseOrderId
518
+ ? foundVariation.qty - matchedOrderItem.qty
519
+ : foundVariation.qty + item.qty - matchedOrderItem.qty
520
+ foundVariation = await tx.getRepository(MarketplaceProductVariation).save(foundVariation)
521
+
522
+ return {
523
+ ...matchedOrderItem,
524
+ ...item,
525
+ updater: user
526
+ }
527
+ } else {
528
+ if (foundVariation.sku) {
529
+ let allVariations: MarketplaceProductVariation[] = await tx
530
+ .getRepository(MarketplaceProductVariation)
531
+ .find({
532
+ where: { domain: foundVariation.domain, sku: foundVariation.sku },
533
+ relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
534
+ })
535
+
536
+ let variationsFromOtherStores: MarketplaceProductVariation[] = allVariations.filter(
537
+ variation =>
538
+ variation.id != foundVariation.id &&
539
+ variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
540
+ )
541
+
542
+ variationsFromOtherStores.map(async variation => {
543
+ variation.reserveQty += item.qty
544
+
545
+ variation = await tx.getRepository(MarketplaceProductVariation).save(variation)
546
+ })
547
+ }
548
+
549
+ foundVariation.qty -= item.qty
550
+ foundVariation = await tx.getRepository(MarketplaceProductVariation).save(foundVariation)
551
+
552
+ return {
553
+ ...item,
554
+ updater: user
555
+ }
410
556
  }
411
- }
412
- })
557
+ })
558
+ )
413
559
  }
414
560
 
415
561
  marketplaceOrderItems = existingOrderItems
@@ -422,6 +568,30 @@ export const syncMarketplaceOrder = {
422
568
  }
423
569
  }
424
570
  } else {
571
+ if (foundVariation.sku) {
572
+ let allVariations: MarketplaceProductVariation[] = await tx
573
+ .getRepository(MarketplaceProductVariation)
574
+ .find({
575
+ where: { domain: foundVariation.domain, sku: foundVariation.sku },
576
+ relations: ['domain', 'marketplaceProduct', 'marketplaceProduct.marketplaceStore']
577
+ })
578
+
579
+ let variationsFromOtherStores: MarketplaceProductVariation[] = allVariations.filter(
580
+ variation =>
581
+ variation.id != foundVariation.id &&
582
+ variation.marketplaceProduct.marketplaceStore.status != 'TERMINATED'
583
+ )
584
+
585
+ variationsFromOtherStores.map(async variation => {
586
+ variation.reserveQty += item.qty
587
+
588
+ variation = await tx.getRepository(MarketplaceProductVariation).save(variation)
589
+ })
590
+ }
591
+
592
+ foundVariation.qty -= item.qty
593
+ foundVariation = await tx.getRepository(MarketplaceProductVariation).save(foundVariation)
594
+
425
595
  if (order?.orderShipping) {
426
596
  savedMarketplaceOrderShipping.totalWeight = orderTotalWeight
427
597
  savedMarketplaceOrderShipping = await tx
@@ -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