@etsoo/appscript 1.4.59 → 1.4.61

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.
@@ -67,6 +67,10 @@ export type ShoppingCartItemBase = {
67
67
  * 数量
68
68
  */
69
69
  qty: number;
70
+ /**
71
+ * Asset qty
72
+ */
73
+ assetQty?: number;
70
74
  /**
71
75
  * Product level promotions
72
76
  * 产品层次促销
@@ -317,6 +321,15 @@ export declare class ShoppingCart<T extends ShoppingCartItem> {
317
321
  * @param item Shopping cart item
318
322
  */
319
323
  updateDiscount(item: ShoppingCartItem): void;
324
+ /**
325
+ * Update asset item
326
+ * 更新资产项目
327
+ * @param id Product id
328
+ * @param qty Asset qty
329
+ * @param itemCreator New item creator
330
+ * @returns Updated or not
331
+ */
332
+ updateAssetItem(id: T['id'], assetQty: number | undefined, itemCreator?: () => Omit<T, 'id' | 'price' | 'assetQty' | 'subtotal' | 'discount' | 'promotions'>): boolean;
320
333
  /**
321
334
  * Update item
322
335
  * 更新项目
@@ -361,6 +361,56 @@ class ShoppingCart {
361
361
  updateDiscount(item) {
362
362
  item.discount = item.promotions.sum('amount');
363
363
  }
364
+ /**
365
+ * Update asset item
366
+ * 更新资产项目
367
+ * @param id Product id
368
+ * @param qty Asset qty
369
+ * @param itemCreator New item creator
370
+ * @returns Updated or not
371
+ */
372
+ updateAssetItem(id, assetQty, itemCreator) {
373
+ var _a;
374
+ if (assetQty == null || assetQty <= 0)
375
+ assetQty = 1;
376
+ const index = this.items.findIndex((item) => item.id === id);
377
+ if (index === -1) {
378
+ // New
379
+ if (itemCreator) {
380
+ const price = this.prices[id];
381
+ const data = itemCreator();
382
+ const qty = data.qty;
383
+ const newItem = {
384
+ ...data,
385
+ id,
386
+ price,
387
+ assetQty,
388
+ subtotal: price * qty * assetQty,
389
+ discount: 0,
390
+ promotions: Array()
391
+ };
392
+ this.addItem(newItem);
393
+ }
394
+ return false;
395
+ }
396
+ else {
397
+ // Update
398
+ const item = this.items[index];
399
+ // Price may be cached first
400
+ const price = (_a = this.prices[id]) !== null && _a !== void 0 ? _a : item.price;
401
+ const qty = item.qty;
402
+ const newItem = {
403
+ ...item,
404
+ price,
405
+ assetQty,
406
+ subtotal: price * qty * assetQty,
407
+ discount: 0
408
+ };
409
+ this.items.splice(index, 1, newItem);
410
+ this.doChange('update', [item, newItem]);
411
+ }
412
+ return true;
413
+ }
364
414
  /**
365
415
  * Update item
366
416
  * 更新项目
@@ -383,12 +433,13 @@ class ShoppingCart {
383
433
  // New
384
434
  if (itemCreator) {
385
435
  const price = this.prices[id];
436
+ const data = itemCreator();
386
437
  const newItem = {
387
- ...itemCreator(),
438
+ ...data,
388
439
  id,
389
440
  price,
390
441
  qty,
391
- subtotal: price * qty,
442
+ subtotal: price * qty * (data.assetQty || 1),
392
443
  discount: 0,
393
444
  promotions: Array()
394
445
  };
@@ -405,7 +456,7 @@ class ShoppingCart {
405
456
  ...item,
406
457
  qty,
407
458
  price,
408
- subtotal: price * qty,
459
+ subtotal: price * qty * (item.assetQty || 1),
409
460
  discount: 0
410
461
  };
411
462
  this.items.splice(index, 1, newItem);
@@ -424,7 +475,12 @@ class ShoppingCart {
424
475
  if (index !== -1) {
425
476
  const item = this.items[index];
426
477
  const qty = item.qty;
427
- const newItem = { ...item, price, subtotal: price * qty };
478
+ const assetQty = item.assetQty || 1;
479
+ const newItem = {
480
+ ...item,
481
+ price,
482
+ subtotal: price * qty * assetQty
483
+ };
428
484
  this.items.splice(index, 1, newItem);
429
485
  this.doChange('update', [item, newItem]);
430
486
  }
@@ -67,6 +67,10 @@ export type ShoppingCartItemBase = {
67
67
  * 数量
68
68
  */
69
69
  qty: number;
70
+ /**
71
+ * Asset qty
72
+ */
73
+ assetQty?: number;
70
74
  /**
71
75
  * Product level promotions
72
76
  * 产品层次促销
@@ -317,6 +321,15 @@ export declare class ShoppingCart<T extends ShoppingCartItem> {
317
321
  * @param item Shopping cart item
318
322
  */
319
323
  updateDiscount(item: ShoppingCartItem): void;
324
+ /**
325
+ * Update asset item
326
+ * 更新资产项目
327
+ * @param id Product id
328
+ * @param qty Asset qty
329
+ * @param itemCreator New item creator
330
+ * @returns Updated or not
331
+ */
332
+ updateAssetItem(id: T['id'], assetQty: number | undefined, itemCreator?: () => Omit<T, 'id' | 'price' | 'assetQty' | 'subtotal' | 'discount' | 'promotions'>): boolean;
320
333
  /**
321
334
  * Update item
322
335
  * 更新项目
@@ -358,6 +358,56 @@ export class ShoppingCart {
358
358
  updateDiscount(item) {
359
359
  item.discount = item.promotions.sum('amount');
360
360
  }
361
+ /**
362
+ * Update asset item
363
+ * 更新资产项目
364
+ * @param id Product id
365
+ * @param qty Asset qty
366
+ * @param itemCreator New item creator
367
+ * @returns Updated or not
368
+ */
369
+ updateAssetItem(id, assetQty, itemCreator) {
370
+ var _a;
371
+ if (assetQty == null || assetQty <= 0)
372
+ assetQty = 1;
373
+ const index = this.items.findIndex((item) => item.id === id);
374
+ if (index === -1) {
375
+ // New
376
+ if (itemCreator) {
377
+ const price = this.prices[id];
378
+ const data = itemCreator();
379
+ const qty = data.qty;
380
+ const newItem = {
381
+ ...data,
382
+ id,
383
+ price,
384
+ assetQty,
385
+ subtotal: price * qty * assetQty,
386
+ discount: 0,
387
+ promotions: Array()
388
+ };
389
+ this.addItem(newItem);
390
+ }
391
+ return false;
392
+ }
393
+ else {
394
+ // Update
395
+ const item = this.items[index];
396
+ // Price may be cached first
397
+ const price = (_a = this.prices[id]) !== null && _a !== void 0 ? _a : item.price;
398
+ const qty = item.qty;
399
+ const newItem = {
400
+ ...item,
401
+ price,
402
+ assetQty,
403
+ subtotal: price * qty * assetQty,
404
+ discount: 0
405
+ };
406
+ this.items.splice(index, 1, newItem);
407
+ this.doChange('update', [item, newItem]);
408
+ }
409
+ return true;
410
+ }
361
411
  /**
362
412
  * Update item
363
413
  * 更新项目
@@ -380,12 +430,13 @@ export class ShoppingCart {
380
430
  // New
381
431
  if (itemCreator) {
382
432
  const price = this.prices[id];
433
+ const data = itemCreator();
383
434
  const newItem = {
384
- ...itemCreator(),
435
+ ...data,
385
436
  id,
386
437
  price,
387
438
  qty,
388
- subtotal: price * qty,
439
+ subtotal: price * qty * (data.assetQty || 1),
389
440
  discount: 0,
390
441
  promotions: Array()
391
442
  };
@@ -402,7 +453,7 @@ export class ShoppingCart {
402
453
  ...item,
403
454
  qty,
404
455
  price,
405
- subtotal: price * qty,
456
+ subtotal: price * qty * (item.assetQty || 1),
406
457
  discount: 0
407
458
  };
408
459
  this.items.splice(index, 1, newItem);
@@ -421,7 +472,12 @@ export class ShoppingCart {
421
472
  if (index !== -1) {
422
473
  const item = this.items[index];
423
474
  const qty = item.qty;
424
- const newItem = { ...item, price, subtotal: price * qty };
475
+ const assetQty = item.assetQty || 1;
476
+ const newItem = {
477
+ ...item,
478
+ price,
479
+ subtotal: price * qty * assetQty
480
+ };
425
481
  this.items.splice(index, 1, newItem);
426
482
  this.doChange('update', [item, newItem]);
427
483
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/appscript",
3
- "version": "1.4.59",
3
+ "version": "1.4.61",
4
4
  "description": "Applications shared TypeScript framework",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -55,7 +55,7 @@
55
55
  "@etsoo/notificationbase": "^1.1.29",
56
56
  "@etsoo/restclient": "^1.0.93",
57
57
  "@etsoo/shared": "^1.2.17",
58
- "crypto-js": "^4.1.1"
58
+ "crypto-js": "^4.2.0"
59
59
  },
60
60
  "devDependencies": {
61
61
  "@babel/cli": "^7.23.0",
@@ -63,8 +63,8 @@
63
63
  "@babel/plugin-transform-runtime": "^7.23.2",
64
64
  "@babel/preset-env": "^7.23.2",
65
65
  "@babel/runtime-corejs3": "^7.23.2",
66
- "@types/crypto-js": "^4.1.2",
67
- "@types/jest": "^29.5.5",
66
+ "@types/crypto-js": "^4.1.3",
67
+ "@types/jest": "^29.5.6",
68
68
  "jest": "^29.7.0",
69
69
  "jest-environment-jsdom": "^29.7.0",
70
70
  "ts-jest": "^29.1.1",
@@ -77,6 +77,11 @@ export type ShoppingCartItemBase = {
77
77
  */
78
78
  qty: number;
79
79
 
80
+ /**
81
+ * Asset qty
82
+ */
83
+ assetQty?: number;
84
+
80
85
  /**
81
86
  * Product level promotions
82
87
  * 产品层次促销
@@ -571,6 +576,65 @@ export class ShoppingCart<T extends ShoppingCartItem> {
571
576
  item.discount = item.promotions.sum('amount');
572
577
  }
573
578
 
579
+ /**
580
+ * Update asset item
581
+ * 更新资产项目
582
+ * @param id Product id
583
+ * @param qty Asset qty
584
+ * @param itemCreator New item creator
585
+ * @returns Updated or not
586
+ */
587
+ updateAssetItem(
588
+ id: T['id'],
589
+ assetQty: number | undefined,
590
+ itemCreator?: () => Omit<
591
+ T,
592
+ 'id' | 'price' | 'assetQty' | 'subtotal' | 'discount' | 'promotions'
593
+ >
594
+ ) {
595
+ if (assetQty == null || assetQty <= 0) assetQty = 1;
596
+
597
+ const index = this.items.findIndex((item) => item.id === id);
598
+ if (index === -1) {
599
+ // New
600
+ if (itemCreator) {
601
+ const price = this.prices[id];
602
+ const data = itemCreator();
603
+ const qty = data.qty;
604
+ const newItem = {
605
+ ...data,
606
+ id,
607
+ price,
608
+ assetQty,
609
+ subtotal: price * qty * assetQty,
610
+ discount: 0,
611
+ promotions: Array<ShoppingPromotion>()
612
+ } as T;
613
+ this.addItem(newItem);
614
+ }
615
+ return false;
616
+ } else {
617
+ // Update
618
+ const item = this.items[index];
619
+
620
+ // Price may be cached first
621
+ const price = this.prices[id] ?? item.price;
622
+ const qty = item.qty;
623
+
624
+ const newItem = {
625
+ ...item,
626
+ price,
627
+ assetQty,
628
+ subtotal: price * qty * assetQty,
629
+ discount: 0
630
+ };
631
+ this.items.splice(index, 1, newItem);
632
+ this.doChange('update', [item, newItem]);
633
+ }
634
+
635
+ return true;
636
+ }
637
+
574
638
  /**
575
639
  * Update item
576
640
  * 更新项目
@@ -598,12 +662,13 @@ export class ShoppingCart<T extends ShoppingCartItem> {
598
662
  // New
599
663
  if (itemCreator) {
600
664
  const price = this.prices[id];
665
+ const data = itemCreator();
601
666
  const newItem = {
602
- ...itemCreator(),
667
+ ...data,
603
668
  id,
604
669
  price,
605
670
  qty,
606
- subtotal: price * qty,
671
+ subtotal: price * qty * (data.assetQty || 1),
607
672
  discount: 0,
608
673
  promotions: Array<ShoppingPromotion>()
609
674
  } as T;
@@ -620,7 +685,7 @@ export class ShoppingCart<T extends ShoppingCartItem> {
620
685
  ...item,
621
686
  qty,
622
687
  price,
623
- subtotal: price * qty,
688
+ subtotal: price * qty * (item.assetQty || 1),
624
689
  discount: 0
625
690
  };
626
691
  this.items.splice(index, 1, newItem);
@@ -642,7 +707,12 @@ export class ShoppingCart<T extends ShoppingCartItem> {
642
707
  if (index !== -1) {
643
708
  const item = this.items[index];
644
709
  const qty = item.qty;
645
- const newItem = { ...item, price, subtotal: price * qty };
710
+ const assetQty = item.assetQty || 1;
711
+ const newItem = {
712
+ ...item,
713
+ price,
714
+ subtotal: price * qty * assetQty
715
+ };
646
716
  this.items.splice(index, 1, newItem);
647
717
  this.doChange('update', [item, newItem]);
648
718
  }