@barchart/portfolio-api-common 1.0.219 → 1.0.220

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.
@@ -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');
@@ -1,6 +1,7 @@
1
1
  const assert = require('@barchart/common-js/lang/assert');
2
2
 
3
3
  const InstrumentType = require('./InstrumentType'),
4
+ PositionDirection = require('./PositionDirection'),
4
5
  TransactionType = require('./TransactionType');
5
6
 
6
7
  module.exports = (() => {
@@ -11,7 +12,7 @@ module.exports = (() => {
11
12
  *
12
13
  * @public
13
14
  */
14
- class TypeValidator {
15
+ class TransactionValidator {
15
16
  constructor() {
16
17
 
17
18
  }
@@ -39,22 +40,57 @@ module.exports = (() => {
39
40
  }
40
41
 
41
42
  /**
42
- * Checks to see is an transaction type is applicable to an instrument type.
43
+ * Checks to see if an transaction type is applicable to an instrument type.
43
44
  *
44
45
  * @static
45
46
  * @public
46
47
  * @param {InstrumentType} instrumentType
48
+ * @param {TransactionType} transactionType
47
49
  * @param {Boolean=} userInitiated
48
50
  * @return {Boolean}
49
51
  */
50
- static validateTransactionTypeFor(instrumentType, transactionType, userInitiated) {
51
- const transactionTypes = TypeValidator.getTransactionTypesFor(instrumentType, userInitiated);
52
+ static validateTransactionType(instrumentType, transactionType, userInitiated) {
53
+ assert.argumentIsRequired(transactionType, 'transactionType', TransactionType, 'TransactionType');
54
+
55
+ const transactionTypes = TransactionValidator.getTransactionTypesFor(instrumentType, userInitiated);
52
56
 
53
57
  return transactionType.some(t => t === transactionType);
54
58
  }
55
59
 
60
+ /**
61
+ * Checks to see if a position for a given instrument type can exist in
62
+ * the given direction.
63
+ *
64
+ * @static
65
+ * @public
66
+ * @param {InstrumentType} instrumentType
67
+ * @param {PositionDirection} direction
68
+ * @return {Boolean}
69
+ */
70
+ static validateDirection(transactionType, direction) {
71
+ assert.argumentIsRequired(transactionType, 'transactionType', TransactionType, 'TransactionType');
72
+ assert.argumentIsRequired(direction, 'direction', PositionDirection, 'PositionDirection');
73
+
74
+ return validDirections[transactionType.code].some(d => d === direction);
75
+ }
76
+
77
+ /**
78
+ * Checks to see if the position switches direction and if the direction switch
79
+ * is valid.
80
+ *
81
+ * @static
82
+ * @public
83
+ * @param {InstrumentType} instrumentType
84
+ * @param {PositionDirection|null|undefined} currentDirection
85
+ * @param {PositionDirection} proposedDirection
86
+ * @return {Boolean}
87
+ */
88
+ static validateDirectionSwitch(instrumentType, currentDirection, proposedDirection) {
89
+ return currentDirection === null || instrumentType.canSwitchDirection || (!currentDirection.closed && !proposedDirection.closed && currentDirection.positive !== proposedDirection.positive);
90
+ }
91
+
56
92
  toString() {
57
- return '[TypeValidator]';
93
+ return '[TransactionValidator]';
58
94
  }
59
95
  }
60
96
 
@@ -99,5 +135,31 @@ module.exports = (() => {
99
135
  associateTypes(InstrumentType.CASH, TransactionType.DEBIT, false);
100
136
  associateTypes(InstrumentType.CASH, TransactionType.CREDIT, false);
101
137
 
102
- return TypeValidator;
138
+ const validDirections = { };
139
+
140
+ function associateDirections(instrumentType, positionDirection) {
141
+ const instrumentTypeCode = instrumentType.code;
142
+
143
+ if (!validDirections.hasOwnProperty(instrumentTypeCode)) {
144
+ validDirections[instrumentTypeCode] = [ ];
145
+ }
146
+
147
+ validDirections[instrumentTypeCode].push(positionDirection);
148
+ }
149
+
150
+ associateTypes(InstrumentType.EQUITY, PositionDirection.EVEN);
151
+ associateTypes(InstrumentType.EQUITY, PositionDirection.LONG);
152
+ associateTypes(InstrumentType.EQUITY, PositionDirection.SHORT);
153
+
154
+ associateTypes(InstrumentType.FUND, PositionDirection.EVEN);
155
+ associateTypes(InstrumentType.FUND, PositionDirection.LONG);
156
+
157
+ associateTypes(InstrumentType.OTHER, PositionDirection.EVEN);
158
+ associateTypes(InstrumentType.OTHER, PositionDirection.LONG);
159
+
160
+ associateTypes(InstrumentType.CASH, PositionDirection.EVEN);
161
+ associateTypes(InstrumentType.CASH, PositionDirection.LONG);
162
+ associateTypes(InstrumentType.CASH, PositionDirection.SHORT);
163
+
164
+ return TransactionValidator;
103
165
  })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.0.219",
3
+ "version": "1.0.220",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",