@barchart/portfolio-api-common 1.2.12 → 1.2.16

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.
@@ -73,6 +73,28 @@ module.exports = (() => {
73
73
  return valid.map(d => d.type);
74
74
  }
75
75
 
76
+ /**
77
+ * Returns transaction types which can be initiated by the user, regardless
78
+ * of instrument type.
79
+ *
80
+ * @public
81
+ * @static
82
+ * @return {Array.<TransactionType>}
83
+ */
84
+ static getUserInitiatedTransactionTypes() {
85
+ return array.unique(Object.keys(validTransactionTypes).reduce((types, key) => {
86
+ const instrumentTypes = validTransactionTypes[key];
87
+
88
+ instrumentTypes.forEach((data) => {
89
+ if (data.user) {
90
+ types.push(data.type);
91
+ }
92
+ });
93
+
94
+ return types;
95
+ }, [ ]));
96
+ }
97
+
76
98
  /**
77
99
  * Checks to see if an transaction type is applicable to an instrument type.
78
100
  *
@@ -197,8 +197,18 @@ module.exports = (() => {
197
197
  });
198
198
 
199
199
  formatters.set(TransactionType.VALUATION, (t) => {
200
+ let rate;
201
+
202
+ if (t.valuation.rate) {
203
+ rate = t.valuation.rate;
204
+ } else if (t.snapshot.open.getIsZero()) {
205
+ rate = null;
206
+ } else {
207
+ rate = t.valuation.value.divide(t.snapshot.open)
208
+ }
209
+
200
210
  return {
201
- price: t.valuation.value
211
+ price: rate
202
212
  };
203
213
  });
204
214
 
@@ -406,32 +406,57 @@ module.exports = (() => {
406
406
 
407
407
  const summary = item.currentSummary;
408
408
 
409
- if (summary && price) {
410
- const period = summary.period;
409
+ let priceToUse;
411
410
 
412
- let unrealized = summary.end.open.multiply(price).add(summary.end.basis);
413
- let unrealizedChange;
411
+ if (price) {
412
+ priceToUse = price;
413
+ } else {
414
414
 
415
- if (data.unrealizedToday !== null) {
416
- unrealizedChange = unrealized.subtract(data.unrealized);
415
+ }
416
+
417
+ if (summary) {
418
+ let priceToUse;
419
+
420
+ if (price) {
421
+ priceToUse = price;
422
+ } else if (!summary.end.open.getIsZero()) {
423
+ priceToUse = summary.end.value.divide(summary.end.open);
417
424
  } else {
418
- unrealizedChange = Decimal.ZERO;
425
+ priceToUse = null;
419
426
  }
420
427
 
421
- let summaryTotalCurrent = period.realized.add(period.income).add(unrealized);
422
- let summaryTotalCurrentChange;
428
+ if (priceToUse !== null) {
429
+ const period = summary.period;
423
430
 
424
- if (data.summaryTotalCurrent !== null) {
425
- summaryTotalCurrentChange = summaryTotalCurrent.subtract(data.summaryTotalCurrent);
426
- } else {
427
- summaryTotalCurrentChange = Decimal.ZERO;
428
- }
431
+ let unrealized = summary.end.open.multiply(priceToUse).add(summary.end.basis);
432
+ let unrealizedChange;
433
+
434
+ if (data.unrealizedToday !== null) {
435
+ unrealizedChange = unrealized.subtract(data.unrealized);
436
+ } else {
437
+ unrealizedChange = Decimal.ZERO;
438
+ }
439
+
440
+ let summaryTotalCurrent = period.realized.add(period.income).add(unrealized);
441
+ let summaryTotalCurrentChange;
442
+
443
+ if (data.summaryTotalCurrent !== null) {
444
+ summaryTotalCurrentChange = summaryTotalCurrent.subtract(data.summaryTotalCurrent);
445
+ } else {
446
+ summaryTotalCurrentChange = Decimal.ZERO;
447
+ }
429
448
 
430
- data.summaryTotalCurrent = summaryTotalCurrent;
431
- data.summaryTotalCurrentChange = summaryTotalCurrentChange;
449
+ data.summaryTotalCurrent = summaryTotalCurrent;
450
+ data.summaryTotalCurrentChange = summaryTotalCurrentChange;
432
451
 
433
- data.unrealized = unrealized;
434
- data.unrealizedChange = unrealizedChange;
452
+ data.unrealized = unrealized;
453
+ data.unrealizedChange = unrealizedChange;
454
+ } else {
455
+ data.summaryTotalCurrentChange = Decimal.ZERO;
456
+
457
+ data.unrealized = Decimal.ZERO;
458
+ data.unrealizedChange = Decimal.ZERO;
459
+ }
435
460
  } else {
436
461
  data.summaryTotalCurrentChange = Decimal.ZERO;
437
462
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.2.12",
3
+ "version": "1.2.16",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",
@@ -1062,6 +1062,28 @@ module.exports = (() => {
1062
1062
  return valid.map(d => d.type);
1063
1063
  }
1064
1064
 
1065
+ /**
1066
+ * Returns transaction types which can be initiated by the user, regardless
1067
+ * of instrument type.
1068
+ *
1069
+ * @public
1070
+ * @static
1071
+ * @return {Array.<TransactionType>}
1072
+ */
1073
+ static getUserInitiatedTransactionTypes() {
1074
+ return array.unique(Object.keys(validTransactionTypes).reduce((types, key) => {
1075
+ const instrumentTypes = validTransactionTypes[key];
1076
+
1077
+ instrumentTypes.forEach((data) => {
1078
+ if (data.user) {
1079
+ types.push(data.type);
1080
+ }
1081
+ });
1082
+
1083
+ return types;
1084
+ }, [ ]));
1085
+ }
1086
+
1065
1087
  /**
1066
1088
  * Checks to see if an transaction type is applicable to an instrument type.
1067
1089
  *
@@ -3402,32 +3424,57 @@ module.exports = (() => {
3402
3424
 
3403
3425
  const summary = item.currentSummary;
3404
3426
 
3405
- if (summary && price) {
3406
- const period = summary.period;
3427
+ let priceToUse;
3407
3428
 
3408
- let unrealized = summary.end.open.multiply(price).add(summary.end.basis);
3409
- let unrealizedChange;
3429
+ if (price) {
3430
+ priceToUse = price;
3431
+ } else {
3410
3432
 
3411
- if (data.unrealizedToday !== null) {
3412
- unrealizedChange = unrealized.subtract(data.unrealized);
3433
+ }
3434
+
3435
+ if (summary) {
3436
+ let priceToUse;
3437
+
3438
+ if (price) {
3439
+ priceToUse = price;
3440
+ } else if (!summary.end.open.getIsZero()) {
3441
+ priceToUse = summary.end.value.divide(summary.end.open);
3413
3442
  } else {
3414
- unrealizedChange = Decimal.ZERO;
3443
+ priceToUse = null;
3415
3444
  }
3416
3445
 
3417
- let summaryTotalCurrent = period.realized.add(period.income).add(unrealized);
3418
- let summaryTotalCurrentChange;
3446
+ if (priceToUse !== null) {
3447
+ const period = summary.period;
3419
3448
 
3420
- if (data.summaryTotalCurrent !== null) {
3421
- summaryTotalCurrentChange = summaryTotalCurrent.subtract(data.summaryTotalCurrent);
3422
- } else {
3423
- summaryTotalCurrentChange = Decimal.ZERO;
3424
- }
3449
+ let unrealized = summary.end.open.multiply(priceToUse).add(summary.end.basis);
3450
+ let unrealizedChange;
3451
+
3452
+ if (data.unrealizedToday !== null) {
3453
+ unrealizedChange = unrealized.subtract(data.unrealized);
3454
+ } else {
3455
+ unrealizedChange = Decimal.ZERO;
3456
+ }
3457
+
3458
+ let summaryTotalCurrent = period.realized.add(period.income).add(unrealized);
3459
+ let summaryTotalCurrentChange;
3425
3460
 
3426
- data.summaryTotalCurrent = summaryTotalCurrent;
3427
- data.summaryTotalCurrentChange = summaryTotalCurrentChange;
3461
+ if (data.summaryTotalCurrent !== null) {
3462
+ summaryTotalCurrentChange = summaryTotalCurrent.subtract(data.summaryTotalCurrent);
3463
+ } else {
3464
+ summaryTotalCurrentChange = Decimal.ZERO;
3465
+ }
3466
+
3467
+ data.summaryTotalCurrent = summaryTotalCurrent;
3468
+ data.summaryTotalCurrentChange = summaryTotalCurrentChange;
3469
+
3470
+ data.unrealized = unrealized;
3471
+ data.unrealizedChange = unrealizedChange;
3472
+ } else {
3473
+ data.summaryTotalCurrentChange = Decimal.ZERO;
3428
3474
 
3429
- data.unrealized = unrealized;
3430
- data.unrealizedChange = unrealizedChange;
3475
+ data.unrealized = Decimal.ZERO;
3476
+ data.unrealizedChange = Decimal.ZERO;
3477
+ }
3431
3478
  } else {
3432
3479
  data.summaryTotalCurrentChange = Decimal.ZERO;
3433
3480
 
@@ -9226,6 +9273,20 @@ describe('When validating transaction order', () => {
9226
9273
  });
9227
9274
  });
9228
9275
 
9276
+ describe('When requesting all the user-initiated transaction types', () => {
9277
+ 'use strict';
9278
+
9279
+ let userInitiated;
9280
+
9281
+ beforeEach(() => {
9282
+ userInitiated = TransactionValidator.getUserInitiatedTransactionTypes();
9283
+ });
9284
+
9285
+ it('Only nine types should be returned', () => {
9286
+ expect(userInitiated.length).toEqual(9);
9287
+ });
9288
+ });
9289
+
9229
9290
  },{"./../../../lib/data/TransactionValidator":5,"@barchart/common-js/lang/Day":18}],37:[function(require,module,exports){
9230
9291
  const Currency = require('@barchart/common-js/lang/Currency'),
9231
9292
  Decimal = require('@barchart/common-js/lang/Decimal');
@@ -41,3 +41,17 @@ describe('When validating transaction order', () => {
41
41
  expect(TransactionValidator.validateOrder([ build(1, '2018-05-02'), build(2, '2018-05-01'), build(3, '2018-04-30') ])).toEqual(false);
42
42
  });
43
43
  });
44
+
45
+ describe('When requesting all the user-initiated transaction types', () => {
46
+ 'use strict';
47
+
48
+ let userInitiated;
49
+
50
+ beforeEach(() => {
51
+ userInitiated = TransactionValidator.getUserInitiatedTransactionTypes();
52
+ });
53
+
54
+ it('Only nine types should be returned', () => {
55
+ expect(userInitiated.length).toEqual(9);
56
+ });
57
+ });