@barchart/portfolio-api-common 1.0.4 → 1.0.9

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.
@@ -1,5 +1,6 @@
1
1
  const assert = require('@barchart/common-js/lang/assert'),
2
- is = require('@barchart/common-js/lang/is');
2
+ is = require('@barchart/common-js/lang/is'),
3
+ numberFormatter = require('@barchart/common-js/lang/formatter');
3
4
 
4
5
  const TransactionType = require('./../data/TransactionType');
5
6
 
@@ -36,7 +37,7 @@ module.exports = (() => {
36
37
 
37
38
  positions.map((p) => positionMap[p.position] = p.instrument);
38
39
 
39
- return transactions.map((transaction) => {
40
+ return transactions.filter((t) => positionMap[t.position]).map((transaction) => {
40
41
  transaction.instrument = positionMap[transaction.position];
41
42
 
42
43
  let formatted = getBasicTransaction(transaction);
@@ -44,16 +45,26 @@ module.exports = (() => {
44
45
  if (formatters.has(transaction.type)) {
45
46
  const formatterFunction = formatters.get(transaction.type);
46
47
 
47
- formatted = Object.assign({}, formatted, formatterFunction(transaction));
48
+ const formattedTransaction = formatterFunction(transaction);
49
+
50
+ Object.keys(formattedTransaction).map((key) => {
51
+ if (!is.undefined(formattedTransaction[key]) && is.fn(formattedTransaction[key].toFloat)) {
52
+ const precision = transaction.instrument.currency.precision;
53
+
54
+ formattedTransaction[key] = numberFormatter.numberToString(formattedTransaction[key].toFloat(), precision, ',');
55
+ }
56
+ });
57
+
58
+ formatted = Object.assign({}, formatted, formattedTransaction);
48
59
  }
49
60
 
50
61
  if (returnType === 'append') {
51
62
  transaction.formatted = formatted;
52
63
 
53
64
  return transaction;
65
+ } else {
66
+ return formatted;
54
67
  }
55
-
56
- return formatted;
57
68
  });
58
69
  }
59
70
 
@@ -63,12 +74,18 @@ module.exports = (() => {
63
74
  }
64
75
 
65
76
  const getBasicTransaction = (t) => {
66
- return {
77
+ const basic = {
67
78
  date: t.date,
68
79
  type: t.type,
69
80
  sequence: t.sequence,
70
81
  instrument: t.instrument
71
82
  };
83
+
84
+ if (basic.instrument) {
85
+ delete basic.instrument.id;
86
+ }
87
+
88
+ return basic;
72
89
  };
73
90
 
74
91
  const formatters = new Map();
@@ -120,14 +137,14 @@ module.exports = (() => {
120
137
  fee: t.fee,
121
138
  total: t.quantity,
122
139
  rate: t.dividend.rate
123
- }
140
+ };
124
141
  });
125
142
 
126
143
  formatters.set(TransactionType.DISTRIBUTION_FUND, (t) => {
127
144
  return {
128
145
  shares: t.snapshot.open.subtract(t.quantity),
129
146
  fee: t.fee
130
- }
147
+ };
131
148
  });
132
149
 
133
150
  formatters.set(TransactionType.INCOME, (t) => {
@@ -28,12 +28,12 @@ module.exports = (() => {
28
28
  return this._schema;
29
29
  }
30
30
 
31
- static get COMPLETE() {
32
- return complete;
31
+ static get CREATE() {
32
+ return create;
33
33
  }
34
34
 
35
- static get SIMPLE() {
36
- return simple;
35
+ static get COMPLETE() {
36
+ return complete;
37
37
  }
38
38
 
39
39
  toString() {
@@ -60,14 +60,19 @@ module.exports = (() => {
60
60
  .schema
61
61
  );
62
62
 
63
- const simple = new PortfolioSchema(SchemaBuilder.withName('Simple')
63
+ const create = new PortfolioSchema(SchemaBuilder.withName('Create')
64
64
  .withField('user', DataType.STRING)
65
65
  .withField('portfolio', DataType.STRING)
66
66
  .withField('name', DataType.STRING)
67
67
  .withField('timezone', DataType.forEnum(Timezones, 'Timezone'))
68
+ .withField('dates.create', DataType.DAY)
69
+ .withField('dates.cash', DataType.DAY, true)
68
70
  .withField('defaults.currency', DataType.forEnum(Currency, 'Currency'))
69
- .withField('legacy.warnings', DataType.NUMBER, true)
70
- .withField('legacy.drops', DataType.NUMBER, true)
71
+ .withField('defaults.reinvest', DataType.BOOLEAN, true)
72
+ .withField('defaults.valuation', DataType.forEnum(ValuationType, 'ValuationType'))
73
+ .withField('legacy.system', DataType.STRING, true)
74
+ .withField('legacy.user', DataType.STRING, true)
75
+ .withField('system.version', DataType.NUMBER, true)
71
76
  .schema
72
77
  );
73
78
 
@@ -31,10 +31,6 @@ module.exports = (() => {
31
31
  return complete;
32
32
  }
33
33
 
34
- static get SIMPLE() {
35
- return simple;
36
- }
37
-
38
34
  toString() {
39
35
  return '[PositionSchema]';
40
36
  }
@@ -72,25 +68,5 @@ module.exports = (() => {
72
68
  .schema
73
69
  );
74
70
 
75
- const simple = new PositionSchema(SchemaBuilder.withName('Simple')
76
- .withField('position', DataType.STRING)
77
- .withField('instrument.id', DataType.STRING)
78
- .withField('instrument.name', DataType.STRING)
79
- .withField('instrument.type', DataType.STRING)
80
- .withField('instrument.currency', DataType.forEnum(Currency, 'Currency'))
81
- .withField('instrument.delist', DataType.DAY, true)
82
- .withField('instrument.symbol.barchart', DataType.STRING, true)
83
- .withField('instrument.symbol.display', DataType.STRING, true)
84
- .withField('snapshot.date', DataType.DAY)
85
- .withField('snapshot.open', DataType.DECIMAL)
86
- .withField('snapshot.buys', DataType.DECIMAL)
87
- .withField('snapshot.sells', DataType.DECIMAL)
88
- .withField('snapshot.gain', DataType.DECIMAL)
89
- .withField('snapshot.basis', DataType.DECIMAL)
90
- .withField('snapshot.income', DataType.DECIMAL)
91
- .withField('snapshot.value', DataType.DECIMAL)
92
- .schema
93
- );
94
-
95
71
  return PositionSchema;
96
72
  })();
@@ -30,22 +30,6 @@ module.exports = (() => {
30
30
  return complete;
31
31
  }
32
32
 
33
- static get SIMPLE() {
34
- return simple;
35
- }
36
-
37
- static get DISPLAY() {
38
- return display;
39
- }
40
-
41
- static get DOWNLOAD() {
42
- return download;
43
- }
44
-
45
- static get SNAPSHOT() {
46
- return snapshot;
47
- }
48
-
49
33
  toString() {
50
34
  return '[TransactionSchema]';
51
35
  }
@@ -91,58 +75,5 @@ module.exports = (() => {
91
75
  .schema
92
76
  );
93
77
 
94
- const simple = new TransactionSchema(SchemaBuilder.withName('Simple')
95
- .withField('sequence', DataType.NUMBER)
96
- .withField('date', DataType.DAY)
97
- .withField('description', DataType.STRING)
98
- .withField('amount', DataType.DECIMAL)
99
- .schema
100
- );
101
-
102
- const display = new TransactionSchema(SchemaBuilder.withName('Display')
103
- .withField('sequence', DataType.NUMBER)
104
- .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
105
- .withField('date', DataType.DAY)
106
- .withField('amount', DataType.DECIMAL)
107
- .withField('quantity', DataType.DECIMAL)
108
- .withField('fee', DataType.DECIMAL)
109
- .withField('snapshot.open', DataType.DECIMAL)
110
- .withField('trade.price', DataType.DECIMAL, true)
111
- .withField('dividend.rate', DataType.DECIMAL, true)
112
- .withField('dividend.amount', DataType.DECIMAL, true)
113
- .withField('charge.amount', DataType.DECIMAL, true)
114
- .withField('income.amount', DataType.DECIMAL, true)
115
- .withField('valuation.value', DataType.DECIMAL, true)
116
- .schema
117
- );
118
-
119
- const download = new TransactionSchema(SchemaBuilder.withName('Download')
120
- .withField('sequence', DataType.NUMBER)
121
- .withField('type', DataType.forEnum(TransactionType, 'TransactionType'))
122
- .withField('date', DataType.DAY)
123
- .withField('amount', DataType.DECIMAL)
124
- .withField('quantity', DataType.DECIMAL)
125
- .withField('fee', DataType.DECIMAL)
126
- .withField('snapshot.open', DataType.DECIMAL)
127
- .withField('trade.price', DataType.DECIMAL, true)
128
- .withField('dividend.rate', DataType.DECIMAL, true)
129
- .withField('dividend.amount', DataType.DECIMAL, true)
130
- .withField('charge.amount', DataType.DECIMAL, true)
131
- .withField('income.amount', DataType.DECIMAL, true)
132
- .withField('valuation.value', DataType.DECIMAL, true)
133
- .schema);
134
-
135
- const snapshot = new TransactionSchema(SchemaBuilder.withName('Snapshot')
136
- .withField('position', DataType.STRING)
137
- .withField('sequence', DataType.NUMBER)
138
- .withField('date', DataType.DAY)
139
- .withField('snapshot.open', DataType.DECIMAL)
140
- .withField('snapshot.gain', DataType.DECIMAL)
141
- .withField('snapshot.basis', DataType.DECIMAL)
142
- .withField('snapshot.income', DataType.DECIMAL)
143
- .withField('snapshot.value', DataType.DECIMAL)
144
- .schema
145
- );
146
-
147
78
  return TransactionSchema;
148
79
  })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "1.0.4",
3
+ "version": "1.0.9",
4
4
  "description": "Common classes used by the Portfolio system",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",