@barchart/portfolio-api-common 1.0.217 → 1.0.218
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.
- package/lib/data/Validator.js +103 -0
- package/package.json +1 -1
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
const assert = require('@barchart/common-js/lang/assert');
|
|
2
|
+
|
|
3
|
+
const InstrumentType = require('./InstrumentType'),
|
|
4
|
+
TransactionType = require('./TransactionType');
|
|
5
|
+
|
|
6
|
+
module.exports = (() => {
|
|
7
|
+
'use strict';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Static utilities for validating transactions.
|
|
11
|
+
*
|
|
12
|
+
* @public
|
|
13
|
+
*/
|
|
14
|
+
class Validator {
|
|
15
|
+
constructor() {
|
|
16
|
+
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Given an instrument type, returns all valid transaction types.
|
|
21
|
+
*
|
|
22
|
+
* @static
|
|
23
|
+
* @public
|
|
24
|
+
* @param {InstrumentType} instrumentType
|
|
25
|
+
* @param {Boolean=} userInitiated
|
|
26
|
+
* @return {Array.<TransactionType>}
|
|
27
|
+
*/
|
|
28
|
+
static getValidTransactionTypesFor(instrumentType, userInitiated) {
|
|
29
|
+
assert.argumentIsRequired(instrumentType, 'instrumentType', InstrumentType, 'InstrumentType');
|
|
30
|
+
assert.argumentIsOptional(userInitiated, 'userInitiated', Boolean);
|
|
31
|
+
|
|
32
|
+
let valid = validTransactionTypes[instrumentType.code] || [ ];
|
|
33
|
+
|
|
34
|
+
if (userInitiated) {
|
|
35
|
+
valid = valid.filter(d => d.user === userInitiated);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return valid.map(d => d.type);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Checks to see is an transaction type is applicable to an instrument type.
|
|
43
|
+
*
|
|
44
|
+
* @static
|
|
45
|
+
* @public
|
|
46
|
+
* @param {InstrumentType} instrumentType
|
|
47
|
+
* @param {Boolean=} userInitiated
|
|
48
|
+
* @return {Boolean}
|
|
49
|
+
*/
|
|
50
|
+
static validateTransactionTypeFor(instrumentType, transactionType, userInitiated) {
|
|
51
|
+
const transactionTypes = Validator.getValidTransactionTypesFor(instrumentType, userInitiated);
|
|
52
|
+
|
|
53
|
+
return transactionType.some(t => t === transactionType);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
toString() {
|
|
57
|
+
return '[Validator]';
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const validTransactionTypes = { };
|
|
62
|
+
|
|
63
|
+
function associateTypes(instrumentType, transactionType, userInitiated) {
|
|
64
|
+
const instrumentTypeCode = instrumentType.code;
|
|
65
|
+
|
|
66
|
+
if (!validTransactionTypes.hasOwnProperty(instrumentTypeCode)) {
|
|
67
|
+
validTransactionTypes[instrumentTypeCode] = [ ];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
validTransactionTypes[instrumentTypeCode].push({ type: transactionType, user: userInitiated });
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
associateTypes(InstrumentType.EQUITY, TransactionType.BUY, true);
|
|
74
|
+
associateTypes(InstrumentType.EQUITY, TransactionType.SELL, true);
|
|
75
|
+
associateTypes(InstrumentType.EQUITY, TransactionType.SELL_SHORT, true);
|
|
76
|
+
associateTypes(InstrumentType.EQUITY, TransactionType.BUY_SHORT, true);
|
|
77
|
+
associateTypes(InstrumentType.EQUITY, TransactionType.FEE, true);
|
|
78
|
+
associateTypes(InstrumentType.EQUITY, TransactionType.DIVIDEND, false);
|
|
79
|
+
associateTypes(InstrumentType.EQUITY, TransactionType.DIVIDEND_REINVEST, false);
|
|
80
|
+
associateTypes(InstrumentType.EQUITY, TransactionType.DIVIDEND_STOCK, false);
|
|
81
|
+
associateTypes(InstrumentType.EQUITY, TransactionType.SPLIT, false);
|
|
82
|
+
|
|
83
|
+
associateTypes(InstrumentType.FUND, TransactionType.BUY, true);
|
|
84
|
+
associateTypes(InstrumentType.FUND, TransactionType.SELL, true);
|
|
85
|
+
associateTypes(InstrumentType.FUND, TransactionType.FEE, true);
|
|
86
|
+
associateTypes(InstrumentType.FUND, TransactionType.FEE_UNITS, false);
|
|
87
|
+
associateTypes(InstrumentType.FUND, TransactionType.DISTRIBUTION_CASH, false);
|
|
88
|
+
associateTypes(InstrumentType.FUND, TransactionType.DISTRIBUTION_FUND, false);
|
|
89
|
+
|
|
90
|
+
associateTypes(InstrumentType.OTHER, TransactionType.BUY, true);
|
|
91
|
+
associateTypes(InstrumentType.OTHER, TransactionType.SELL, true);
|
|
92
|
+
associateTypes(InstrumentType.OTHER, TransactionType.INCOME, true);
|
|
93
|
+
associateTypes(InstrumentType.OTHER, TransactionType.FEE, true);
|
|
94
|
+
associateTypes(InstrumentType.OTHER, TransactionType.VALUATION, true);
|
|
95
|
+
|
|
96
|
+
associateTypes(InstrumentType.CASH, TransactionType.DEPOSIT, true);
|
|
97
|
+
associateTypes(InstrumentType.CASH, TransactionType.WITHDRAWAL, true);
|
|
98
|
+
associateTypes(InstrumentType.CASH, TransactionType.FEE, true);
|
|
99
|
+
associateTypes(InstrumentType.CASH, TransactionType.DEBIT, false);
|
|
100
|
+
associateTypes(InstrumentType.CASH, TransactionType.CREDIT, false);
|
|
101
|
+
|
|
102
|
+
return Validator;
|
|
103
|
+
})();
|