@barchart/portfolio-api-common 1.2.66 → 1.2.70

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.
@@ -143,6 +143,17 @@ module.exports = (() => {
143
143
  return transactionCreateFailedReinvestPriceUnavailable;
144
144
  }
145
145
 
146
+ /**
147
+ * The transaction failed because the position has been locked.
148
+ *
149
+ * @public
150
+ * @static
151
+ * @returns {FailureType}
152
+ */
153
+ static get TRANSACTION_CREATE_FAILED_POSITION_LOCKED() {
154
+ return transactionCreateFailedPositionLocked;
155
+ }
156
+
146
157
  /**
147
158
  * Deleting any transaction except for the most recent requires
148
159
  * re-writing transaction history.
@@ -219,6 +230,7 @@ module.exports = (() => {
219
230
  const transactionCreateFailedInvalidInitialType = new FailureType('TRANSACTION_CREATE_FAILED_INVALID_INITIAL_TYPE', 'Unable to process operation because the first transaction would to be a {U|transactionType.description}, which is not allowed -- since {U|transactionType.description} transactions cannot open a position.');
220
231
  const transactionCreateFailedTypeReserved = new FailureType('TRANSACTION_CREATE_FAILED_TYPE_RESERVED', 'Unable to create {U|type.description} transaction, this type of transaction is managed by the system.');
221
232
  const transactionCreateFailedReinvestPriceUnavailable = new FailureType('TRANSACTION_CREATE_FAILED_REINVEST_PRICE_UNAVAILABLE', 'Unable to create transaction, a dividend was paid on {L|day}; however no historical price is available for this day. To successfully create this transaction, please turn off dividend reinvestment for this position.');
233
+ const transactionCreateFailedPositionLocked = new FailureType('TRANSACTION_CREATE_FAILED_POSITION_LOCKED', 'Unable to create transaction, are being recalculated for this position (or the linked cash position). Please re-enter the transaction in a minute or two.');
222
234
 
223
235
  const transactionDeleteFailedOutOfSequence = new FailureType('TRANSACTION_DELETE_FAILED_OUT_OF_SEQUENCE', 'Deleting any transaction, except for the most recent, will cause transaction history to be re-written. Please confirm your intent to re-write transaction history (which could take some time and alter the historical results for this position).');
224
236
  const transactionDeleteFailedNoTransaction = new FailureType('TRANSACTION_DELETE_FAILED_NO_TRANSACTION', 'Unable to delete transaction. The referenced transaction does not exist.');
@@ -46,7 +46,6 @@ module.exports = (() => {
46
46
  this._usesSymbols = usesSymbols;
47
47
  this._hasCorporateActions = hasCorporateActions;
48
48
  this._closeFractional = closeFractional;
49
- this._closeFractional = strictOrdering;
50
49
  this._roundQuantity = roundQuantity;
51
50
  this._strictOrdering = strictOrdering;
52
51
 
@@ -19,6 +19,25 @@ module.exports = (() => {
19
19
 
20
20
  }
21
21
 
22
+ /**
23
+ * Determines the desired sequence number for a transaction.
24
+ *
25
+ * @public
26
+ * @param {Object} transaction
27
+ * @return {Number}
28
+ */
29
+ static getSortSequence(transaction) {
30
+ let effective;
31
+
32
+ if (is.number(transaction.resequence)) {
33
+ effective = transaction.resequence;
34
+ } else {
35
+ effective = transaction.sequence;
36
+ }
37
+
38
+ return effective;
39
+ }
40
+
22
41
  /**
23
42
  * Given an array of transactions, ensures that all sequence numbers and dates
24
43
  * are properly ordered.
@@ -85,7 +104,7 @@ module.exports = (() => {
85
104
  assert.argumentIsArray(transactions, 'transactions');
86
105
  assert.argumentIsOptional(strict, 'strict', Boolean);
87
106
 
88
- return transactions.findIndex((t, i, a) => t.sequence !== (i + 1) || (i !== 0 && t.date.getIsBefore(a[ i - 1 ].date)) || (i !== 0 && is.boolean(strict) && strict && t.date.getIsEqual(a[i - 1].date) && t.type.sequence < a[i - 1].type.sequence));
107
+ return transactions.findIndex((t, i, a) => TransactionValidator.getSortSequence(t) !== (i + 1) || (i !== 0 && t.date.getIsBefore(a[ i - 1 ].date)) || (i !== 0 && is.boolean(strict) && strict && t.date.getIsEqual(a[i - 1].date) && t.type.sequence < a[i - 1].type.sequence));
89
108
  }
90
109
 
91
110
  /**
@@ -31,12 +31,14 @@ module.exports = (() => {
31
31
  * @param {Array.<Object>} transactions
32
32
  * @param {Array.<Object>} positions
33
33
  * @param {Boolean=} mutate
34
+ * @param {Boolean=} descending
34
35
  * @returns {Array}
35
36
  */
36
- static format(transactions, positions, mutate) {
37
+ static format(transactions, positions, mutate, descending) {
37
38
  assert.argumentIsArray(transactions, 'transactions');
38
39
  assert.argumentIsArray(positions, 'positions');
39
40
  assert.argumentIsOptional(mutate, 'mutate', Boolean);
41
+ assert.argumentIsOptional(descending, 'descending', Boolean);
40
42
 
41
43
  const instruments = positions.reduce((map, p) => {
42
44
  const instrument = Object.assign({ }, p.instrument || { });
@@ -84,6 +86,14 @@ module.exports = (() => {
84
86
  return list;
85
87
  }, [ ]);
86
88
 
89
+ let comparator;
90
+
91
+ if (is.boolean(descending) && descending) {
92
+ comparator = comparatorDescending;
93
+ } else {
94
+ comparator = comparatorAscending;
95
+ }
96
+
87
97
  a.sort(comparator);
88
98
 
89
99
  a.forEach((t) => {
@@ -99,9 +109,21 @@ module.exports = (() => {
99
109
  * @public
100
110
  * @static
101
111
  * @param {Array.<Object>} transactions
112
+ * @param {Boolean=} descending
102
113
  * @returns {Array}
103
114
  */
104
- sort(transactions) {
115
+ sort(transactions, descending) {
116
+ assert.argumentIsArray(transactions, 'transactions');
117
+ assert.argumentIsOptional(descending, 'descending', Boolean);
118
+
119
+ let comparator;
120
+
121
+ if (is.boolean(descending) && descending) {
122
+ comparator = comparatorDescending;
123
+ } else {
124
+ comparator = comparatorAscending;
125
+ }
126
+
105
127
  return transactions.sort(comparator);
106
128
  }
107
129
 
@@ -277,15 +299,14 @@ module.exports = (() => {
277
299
  }
278
300
  }
279
301
 
280
- const comparator = ComparatorBuilder.startWith((a, b) => {
281
- return Day.compareDays(a.date, b.date);
282
- }).thenBy((a, b) => {
283
- return comparators.compareNumbers(getInstrumentTypePriority(a.instrument.type), getInstrumentTypePriority(b.instrument.type));
284
- }).thenBy((a, b) => {
285
- return comparators.compareStrings(a.instrument.id, b.instrument.id);
286
- }).thenBy((a, b) => {
287
- return comparators.compareNumbers(a.sequence, b.sequence);
288
- }).toComparator();
302
+ const comparatorAscending = ComparatorBuilder.startWith((a, b) => Day.compareDays(a.date, b.date))
303
+ .thenBy((a, b) => comparators.compareNumbers(getInstrumentTypePriority(a.instrument.type), getInstrumentTypePriority(b.instrument.type)))
304
+ .thenBy((a, b) => comparators.compareStrings(a.instrument.id, b.instrument.id))
305
+ .thenBy((a, b) => comparators.compareNumbers(a.sequence, b.sequence))
306
+ .toComparator();
307
+
308
+ const comparatorDescending = ComparatorBuilder.startWith((a, b) => comparatorAscending(b, a))
309
+ .toComparator();
289
310
 
290
311
  return TransactionFormatter;
291
312
  })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.2.66",
3
+ "version": "1.2.70",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",
@@ -47,7 +47,6 @@ module.exports = (() => {
47
47
  this._usesSymbols = usesSymbols;
48
48
  this._hasCorporateActions = hasCorporateActions;
49
49
  this._closeFractional = closeFractional;
50
- this._closeFractional = strictOrdering;
51
50
  this._roundQuantity = roundQuantity;
52
51
  this._strictOrdering = strictOrdering;
53
52
 
@@ -1103,6 +1102,25 @@ module.exports = (() => {
1103
1102
 
1104
1103
  }
1105
1104
 
1105
+ /**
1106
+ * Determines the desired sequence number for a transaction.
1107
+ *
1108
+ * @public
1109
+ * @param {Object} transaction
1110
+ * @return {Number}
1111
+ */
1112
+ static getSortSequence(transaction) {
1113
+ let effective;
1114
+
1115
+ if (is.number(transaction.resequence)) {
1116
+ effective = transaction.resequence;
1117
+ } else {
1118
+ effective = transaction.sequence;
1119
+ }
1120
+
1121
+ return effective;
1122
+ }
1123
+
1106
1124
  /**
1107
1125
  * Given an array of transactions, ensures that all sequence numbers and dates
1108
1126
  * are properly ordered.
@@ -1169,7 +1187,7 @@ module.exports = (() => {
1169
1187
  assert.argumentIsArray(transactions, 'transactions');
1170
1188
  assert.argumentIsOptional(strict, 'strict', Boolean);
1171
1189
 
1172
- return transactions.findIndex((t, i, a) => t.sequence !== (i + 1) || (i !== 0 && t.date.getIsBefore(a[ i - 1 ].date)) || (i !== 0 && is.boolean(strict) && strict && t.date.getIsEqual(a[i - 1].date) && t.type.sequence < a[i - 1].type.sequence));
1190
+ return transactions.findIndex((t, i, a) => TransactionValidator.getSortSequence(t) !== (i + 1) || (i !== 0 && t.date.getIsBefore(a[ i - 1 ].date)) || (i !== 0 && is.boolean(strict) && strict && t.date.getIsEqual(a[i - 1].date) && t.type.sequence < a[i - 1].type.sequence));
1173
1191
  }
1174
1192
 
1175
1193
  /**