@barchart/portfolio-api-common 1.0.217 → 1.0.221

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.
@@ -35,15 +35,21 @@ module.exports = (() => {
35
35
  }
36
36
 
37
37
  /**
38
- * The transaction type is not valid, given the current position size
39
- * (e.g. sell is invalid for a short position, sell short must be used).
40
- *
41
38
  * @public
42
39
  * @static
43
40
  * @returns {FailureType}
44
41
  */
45
- static get TRANSACTION_CREATE_FAILED_TYPE_INVALID_FOR_POSITION() {
46
- return transactionCreateFailedTypeInvalidForPosition;
42
+ static get TRANSACTION_CREATE_FAILED_TYPE_INVALID_FOR_INSTRUMENT() {
43
+ return transactionCreateFailedTypeInvalidForInstrument;
44
+ }
45
+
46
+ /**
47
+ * @public
48
+ * @static
49
+ * @returns {FailureType}
50
+ */
51
+ static get TRANSACTION_CREATE_FAILED_TYPE_INVALID_FOR_DIRECTION() {
52
+ return transactionCreateFailedTypeInvalidForDirection;
47
53
  }
48
54
 
49
55
  /**
@@ -54,12 +60,8 @@ module.exports = (() => {
54
60
  * @static
55
61
  * @returns {FailureType}
56
62
  */
57
- static get TRANSACTION_CREATE_FAILED_DIRECTION_SWITCH() {
58
- return transactionCreateFailedDirectionSwitch;
59
- }
60
-
61
- static get TRANSACTION_CREATE_REWRITE_UNSUPPORTED() {
62
- return transactionCreateRewriteUnsupported;
63
+ static get TRANSACTION_CREATE_FAILED_INVALID_DIRECTION_SWITCH() {
64
+ return transactionCreateFailedInvalidDirectionSwitch;
63
65
  }
64
66
 
65
67
  /**
@@ -86,9 +88,9 @@ module.exports = (() => {
86
88
  const positionCreateFailedNoPortfolio = new FailureType('POSITION_CREATE_FAILED_NO_PORTFOLIO', 'Unable to create transaction. The referenced portfolio does not exist. Has it been deleted?');
87
89
 
88
90
  const transactionCreateFailedOutOfSequence = new FailureType('TRANSACTION_CREATE_FAILED_OUT_OF_SEQUENCE', 'Unable to create transaction, because the transaction date is out-of-sequence. In other words, it would occur before an existing transaction. Please confirm your intent to re-write transaction history (which could take some time and alter the historical results for this position).');
89
- const transactionCreateFailedTypeInvalidForPosition = new FailureType('TRANSACTION_CREATE_FAILED_INVALID_FOR_POSITION', 'A {L|type.description} are not allowed. At the time of the transaction, your position is {L|direction} (i.e. {L|sign}).');
90
- const transactionCreateFailedDirectionSwitch = new FailureType('TRANSACTION_CREATE_FAILED_DIRECTION_SWITCH', 'Unable to create transaction, because the position direction would be switched (from long to short or vice versa). Please close the position (to a zero balance), then enter a second transaction.');
91
- const transactionCreateRewriteUnsupported = new FailureType('TRANSACTION_CREATE_REWRITE_UNSUPPORTED', 'Unable to re-write transaction history. This operation is not currently supported (but will be implemented soon).');
91
+ const transactionCreateFailedTypeInvalidForInstrument = new FailureType('TRANSACTION_CREATE_FAILED_TYPE_INVALID_FOR_INSTRUMENT', '{L|transactionType.description} are not allowed for {L|instrumentType.description}');
92
+ const transactionCreateFailedTypeInvalidForDirection = new FailureType('TRANSACTION_CREATE_FAILED_TYPE_INVALID_FOR_DIRECTION', 'A {L|type.description} are not allowed. At the time of the transaction, your position is {L|direction} (i.e. {L|sign}).');
93
+ const transactionCreateFailedInvalidDirectionSwitch = new FailureType('TRANSACTION_CREATE_FAILED_INVALID_DIRECTION_SWITCH', 'Unable to create transaction, because the position direction would be switched (from long to short or vice versa). Please close the position (to a zero balance), then enter a second transaction.');
92
94
 
93
95
  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).');
94
96
  const transactionDeleteUnsupported = new FailureType('TRANSACTION_DELETE_UNSUPPORTED', 'Unable to delete transaction. This operation is not currently supported (but will be implemented soon).');
@@ -19,7 +19,7 @@ module.exports = (() => {
19
19
  constructor(code, description, sign) {
20
20
  super(code, description);
21
21
 
22
- assert.argumentIsString(sign, 'sign', String);
22
+ assert.argumentIsRequired(sign, 'sign', String);
23
23
 
24
24
  this._sign = sign;
25
25
  }
@@ -118,20 +118,6 @@ module.exports = (() => {
118
118
  return even;
119
119
  }
120
120
  }
121
-
122
- /**
123
- * Returns true of the direction switched (from long to short, or
124
- * short to long).
125
- *
126
- * @public
127
- * @static
128
- * @param {PositionDirection} a
129
- * @param {PositionDirection} b
130
- * @returns {Boolean}
131
- */
132
- static switched(a, b) {
133
- return !a.closed && !b.closed && a.positive !== b.positive;
134
- }
135
121
  }
136
122
 
137
123
  const long = new PositionDirection('LONG', 'Long', 'positive');
@@ -0,0 +1,166 @@
1
+ const assert = require('@barchart/common-js/lang/assert');
2
+
3
+ const InstrumentType = require('./InstrumentType'),
4
+ PositionDirection = require('./PositionDirection'),
5
+ TransactionType = require('./TransactionType');
6
+
7
+ module.exports = (() => {
8
+ 'use strict';
9
+
10
+ /**
11
+ * Static utilities for validating transactions.
12
+ *
13
+ * @public
14
+ */
15
+ class TransactionValidator {
16
+ constructor() {
17
+
18
+ }
19
+
20
+ /**
21
+ * Given an instrument type, returns all valid transaction types.
22
+ *
23
+ * @static
24
+ * @public
25
+ * @param {InstrumentType} instrumentType
26
+ * @param {Boolean=} userInitiated
27
+ * @pararm {PositionDirection=} currentDirection
28
+ * @return {Array.<TransactionType>}
29
+ */
30
+ static getTransactionTypesFor(instrumentType, userInitiated, currentDirection) {
31
+ assert.argumentIsRequired(instrumentType, 'instrumentType', InstrumentType, 'InstrumentType');
32
+ assert.argumentIsOptional(userInitiated, 'userInitiated', Boolean);
33
+
34
+ let valid = validTransactionTypes[instrumentType.code] || [ ];
35
+
36
+ if (userInitiated) {
37
+ valid = valid.filter(d => d.user === userInitiated);
38
+ }
39
+
40
+ return valid.map(d => d.type);
41
+ }
42
+
43
+ /**
44
+ * Checks to see if an transaction type is applicable to an instrument type.
45
+ *
46
+ * @static
47
+ * @public
48
+ * @param {InstrumentType} instrumentType
49
+ * @param {TransactionType} transactionType
50
+ * @param {Boolean=} userInitiated
51
+ * @return {Boolean}
52
+ */
53
+ static validateTransactionType(instrumentType, transactionType, userInitiated) {
54
+ assert.argumentIsRequired(transactionType, 'transactionType', TransactionType, 'TransactionType');
55
+
56
+ const transactionTypes = TransactionValidator.getTransactionTypesFor(instrumentType, userInitiated);
57
+
58
+ return transactionType.some(t => t === transactionType);
59
+ }
60
+
61
+ /**
62
+ * Checks to see if a position for a given instrument type can exist in
63
+ * the given direction.
64
+ *
65
+ * @static
66
+ * @public
67
+ * @param {InstrumentType} instrumentType
68
+ * @param {PositionDirection} direction
69
+ * @return {Boolean}
70
+ */
71
+ static validateDirection(transactionType, direction) {
72
+ assert.argumentIsRequired(transactionType, 'transactionType', TransactionType, 'TransactionType');
73
+ assert.argumentIsRequired(direction, 'direction', PositionDirection, 'PositionDirection');
74
+
75
+ return validDirections[transactionType.code].some(d => d === direction);
76
+ }
77
+
78
+ /**
79
+ * Checks to see if the position switches direction and if the direction switch
80
+ * is valid.
81
+ *
82
+ * @static
83
+ * @public
84
+ * @param {InstrumentType} instrumentType
85
+ * @param {PositionDirection|null|undefined} currentDirection
86
+ * @param {PositionDirection} proposedDirection
87
+ * @return {Boolean}
88
+ */
89
+ static validateDirectionSwitch(instrumentType, currentDirection, proposedDirection) {
90
+ return currentDirection === null || instrumentType.canSwitchDirection || (!currentDirection.closed && !proposedDirection.closed && currentDirection.positive !== proposedDirection.positive);
91
+ }
92
+
93
+ toString() {
94
+ return '[TransactionValidator]';
95
+ }
96
+ }
97
+
98
+ const validTransactionTypes = { };
99
+
100
+ function associateTypes(instrumentType, transactionType, userInitiated, directions) {
101
+ const instrumentTypeCode = instrumentType.code;
102
+
103
+ if (!validTransactionTypes.hasOwnProperty(instrumentTypeCode)) {
104
+ validTransactionTypes[instrumentTypeCode] = [ ];
105
+ }
106
+
107
+ validTransactionTypes[instrumentTypeCode].push({ type: transactionType, user: userInitiated });
108
+ }
109
+
110
+ associateTypes(InstrumentType.EQUITY, TransactionType.BUY, true);
111
+ associateTypes(InstrumentType.EQUITY, TransactionType.SELL, true);
112
+ associateTypes(InstrumentType.EQUITY, TransactionType.SELL_SHORT, true);
113
+ associateTypes(InstrumentType.EQUITY, TransactionType.BUY_SHORT, true);
114
+ associateTypes(InstrumentType.EQUITY, TransactionType.FEE, true);
115
+ associateTypes(InstrumentType.EQUITY, TransactionType.DIVIDEND, false);
116
+ associateTypes(InstrumentType.EQUITY, TransactionType.DIVIDEND_REINVEST, false);
117
+ associateTypes(InstrumentType.EQUITY, TransactionType.DIVIDEND_STOCK, false);
118
+ associateTypes(InstrumentType.EQUITY, TransactionType.SPLIT, false);
119
+
120
+ associateTypes(InstrumentType.FUND, TransactionType.BUY, true);
121
+ associateTypes(InstrumentType.FUND, TransactionType.SELL, true);
122
+ associateTypes(InstrumentType.FUND, TransactionType.FEE, true);
123
+ associateTypes(InstrumentType.FUND, TransactionType.FEE_UNITS, false);
124
+ associateTypes(InstrumentType.FUND, TransactionType.DISTRIBUTION_CASH, false);
125
+ associateTypes(InstrumentType.FUND, TransactionType.DISTRIBUTION_FUND, false);
126
+
127
+ associateTypes(InstrumentType.OTHER, TransactionType.BUY, true);
128
+ associateTypes(InstrumentType.OTHER, TransactionType.SELL, true);
129
+ associateTypes(InstrumentType.OTHER, TransactionType.INCOME, true);
130
+ associateTypes(InstrumentType.OTHER, TransactionType.FEE, true);
131
+ associateTypes(InstrumentType.OTHER, TransactionType.VALUATION, true);
132
+
133
+ associateTypes(InstrumentType.CASH, TransactionType.DEPOSIT, true);
134
+ associateTypes(InstrumentType.CASH, TransactionType.WITHDRAWAL, true);
135
+ associateTypes(InstrumentType.CASH, TransactionType.FEE, true);
136
+ associateTypes(InstrumentType.CASH, TransactionType.DEBIT, false);
137
+ associateTypes(InstrumentType.CASH, TransactionType.CREDIT, false);
138
+
139
+ const validDirections = { };
140
+
141
+ function associateDirections(instrumentType, positionDirection) {
142
+ const instrumentTypeCode = instrumentType.code;
143
+
144
+ if (!validDirections.hasOwnProperty(instrumentTypeCode)) {
145
+ validDirections[instrumentTypeCode] = [ ];
146
+ }
147
+
148
+ validDirections[instrumentTypeCode].push(positionDirection);
149
+ }
150
+
151
+ associateTypes(InstrumentType.EQUITY, PositionDirection.EVEN);
152
+ associateTypes(InstrumentType.EQUITY, PositionDirection.LONG);
153
+ associateTypes(InstrumentType.EQUITY, PositionDirection.SHORT);
154
+
155
+ associateTypes(InstrumentType.FUND, PositionDirection.EVEN);
156
+ associateTypes(InstrumentType.FUND, PositionDirection.LONG);
157
+
158
+ associateTypes(InstrumentType.OTHER, PositionDirection.EVEN);
159
+ associateTypes(InstrumentType.OTHER, PositionDirection.LONG);
160
+
161
+ associateTypes(InstrumentType.CASH, PositionDirection.EVEN);
162
+ associateTypes(InstrumentType.CASH, PositionDirection.LONG);
163
+ associateTypes(InstrumentType.CASH, PositionDirection.SHORT);
164
+
165
+ return TransactionValidator;
166
+ })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.0.217",
3
+ "version": "1.0.221",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",