@dhyasama/totem-models 8.48.0 → 8.49.0
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/Investment.js +1 -6
- package/lib/Round.js +50 -116
- package/package.json +1 -1
package/lib/Investment.js
CHANGED
|
@@ -88,9 +88,8 @@ module.exports = function(mongoose, config) {
|
|
|
88
88
|
|
|
89
89
|
if (!investment) { return cb(new Error('investment is required'), null); }
|
|
90
90
|
investment.save(cb);
|
|
91
|
-
};
|
|
92
|
-
|
|
93
91
|
|
|
92
|
+
};
|
|
94
93
|
|
|
95
94
|
///////////////////////////////////////////////////////////////////////////////////////
|
|
96
95
|
// HOOKS
|
|
@@ -103,8 +102,6 @@ module.exports = function(mongoose, config) {
|
|
|
103
102
|
|
|
104
103
|
var self = this;
|
|
105
104
|
|
|
106
|
-
if (self.debtType) self.debt = true;
|
|
107
|
-
|
|
108
105
|
return next();
|
|
109
106
|
|
|
110
107
|
});
|
|
@@ -116,8 +113,6 @@ module.exports = function(mongoose, config) {
|
|
|
116
113
|
});
|
|
117
114
|
});
|
|
118
115
|
|
|
119
|
-
|
|
120
|
-
|
|
121
116
|
///////////////////////////////////////////////////////////////////////////////////////
|
|
122
117
|
// CONFIG
|
|
123
118
|
///////////////////////////////////////////////////////////////////////////////////////
|
package/lib/Round.js
CHANGED
|
@@ -64,38 +64,37 @@ module.exports = function(mongoose, config) {
|
|
|
64
64
|
// HELPERS
|
|
65
65
|
///////////////////////////////////////////////////////////////////////////////////////
|
|
66
66
|
|
|
67
|
-
var
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
var
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
67
|
+
var calculatePerformance = function calculatePerformance(rounds) {
|
|
68
|
+
|
|
69
|
+
var totalInvestment = 0;
|
|
70
|
+
var totalUnrealized = 0;
|
|
71
|
+
var totalRealized = 0;
|
|
72
|
+
|
|
73
|
+
_.each(rounds, function(round) {
|
|
74
|
+
_.each(round.vehicles, function(vehicle) {
|
|
75
|
+
_.each(vehicle.investments, function(investment) {
|
|
76
|
+
totalInvestment += investment.investment || 0;
|
|
77
|
+
totalUnrealized += investment.unrealized || 0;
|
|
78
|
+
totalRealized += investment.realized || 0;
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
});
|
|
80
82
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
83
|
+
// finally, calc the multiple
|
|
84
|
+
return {
|
|
85
|
+
investment: totalInvestment,
|
|
86
|
+
unrealized: totalUnrealized,
|
|
87
|
+
realized: totalRealized,
|
|
88
|
+
multiple: totalInvestment > 0 ? (totalUnrealized + totalRealized) / totalInvestment : 0
|
|
89
|
+
};
|
|
86
90
|
|
|
87
|
-
|
|
88
|
-
org.realized = _.reduce(rounds, function(memo, round) {
|
|
89
|
-
var sum = _.reduce(round.vehicles, function(memo, vehicle) { return memo + vehicle.realized; }, 0);
|
|
90
|
-
return memo + sum;
|
|
91
|
-
}, 0);
|
|
91
|
+
};
|
|
92
92
|
|
|
93
|
-
|
|
94
|
-
org.multiple = org.investment > 0 ? (org.unrealized + org.realized) / org.investment : 0;
|
|
93
|
+
var buildPortfolio = function buildPortfolio(rounds, fundIds) {
|
|
95
94
|
|
|
96
|
-
|
|
95
|
+
// Given a collection of rounds and funds, construct a portfolio of organizations
|
|
97
96
|
|
|
98
|
-
|
|
97
|
+
var portfolio = [];
|
|
99
98
|
|
|
100
99
|
rounds = filterVehicles(rounds, fundIds);
|
|
101
100
|
|
|
@@ -141,7 +140,11 @@ module.exports = function(mongoose, config) {
|
|
|
141
140
|
funds: funds
|
|
142
141
|
};
|
|
143
142
|
|
|
144
|
-
|
|
143
|
+
var performance = calculatePerformance(rounds);
|
|
144
|
+
org.investment = performance.investment;
|
|
145
|
+
org.unrealized = performance.unrealized;
|
|
146
|
+
org.realized = performance.realized;
|
|
147
|
+
org.multiple = performance.multiple;
|
|
145
148
|
|
|
146
149
|
portfolio.push(org);
|
|
147
150
|
|
|
@@ -508,7 +511,7 @@ module.exports = function(mongoose, config) {
|
|
|
508
511
|
|
|
509
512
|
};
|
|
510
513
|
|
|
511
|
-
Round.statics.getFundPerformance = function getFundPerformance(fundId, cb) {
|
|
514
|
+
Round.statics.getFundPerformance = function getFundPerformance(fundId, options, cb) {
|
|
512
515
|
|
|
513
516
|
if (!cb) { throw new Error('cb is required'); }
|
|
514
517
|
if (!fundId) { throw new Error('fundId is required'); }
|
|
@@ -518,38 +521,28 @@ module.exports = function(mongoose, config) {
|
|
|
518
521
|
|
|
519
522
|
let query = self.find({ 'vehicles.fund': fundId });
|
|
520
523
|
|
|
524
|
+
query.select('organization vehicles');
|
|
525
|
+
query.populate('organization', 'name slug logoUrl description ');
|
|
526
|
+
query.populate('vehicles.fund');
|
|
527
|
+
|
|
528
|
+
if (options.isWorkerProcess) {
|
|
529
|
+
query.populate('vehicles.investments');
|
|
530
|
+
}
|
|
531
|
+
else {
|
|
532
|
+
query.populate({
|
|
533
|
+
path: 'vehicles.investments',
|
|
534
|
+
match: { customer: options.CUSTOMER_ID }
|
|
535
|
+
});
|
|
536
|
+
}
|
|
537
|
+
|
|
521
538
|
query.exec(function(err, rounds) {
|
|
522
539
|
|
|
523
540
|
if (err) return cb(err, null);
|
|
524
541
|
else if (!rounds) return cb(null, null);
|
|
525
|
-
else if(rounds.length === 0) return cb(null, []);
|
|
542
|
+
else if (rounds.length === 0) return cb(null, []);
|
|
526
543
|
|
|
527
544
|
rounds = filterVehicles(rounds, [fundId]);
|
|
528
|
-
|
|
529
|
-
// we now have all the rounds the fund has participated in with other fund information removed from each round
|
|
530
|
-
|
|
531
|
-
let totalInvestment = _.reduce(rounds, function(memo, round) {
|
|
532
|
-
let roundInvestment = _.reduce(round.vehicles, function(memo, vehicle) { return memo + vehicle.investment; }, 0);
|
|
533
|
-
return memo + roundInvestment;
|
|
534
|
-
}, 0);
|
|
535
|
-
|
|
536
|
-
let totalUnrealized = _.reduce(rounds, function(memo, round) {
|
|
537
|
-
let roundUnrealized = _.reduce(round.vehicles, function(memo, vehicle) { return memo + vehicle.unrealized; }, 0);
|
|
538
|
-
return memo + roundUnrealized;
|
|
539
|
-
}, 0);
|
|
540
|
-
|
|
541
|
-
let totalRealized = _.reduce(rounds, function(memo, round) {
|
|
542
|
-
let roundRealized = _.reduce(round.vehicles, function(memo, vehicle) { return memo + vehicle.realized; }, 0);
|
|
543
|
-
return memo + roundRealized;
|
|
544
|
-
}, 0);
|
|
545
|
-
|
|
546
|
-
return cb(null, {
|
|
547
|
-
investment: totalInvestment,
|
|
548
|
-
unrealized: totalUnrealized,
|
|
549
|
-
realized: totalRealized,
|
|
550
|
-
value: totalUnrealized + totalRealized,
|
|
551
|
-
multiple: totalInvestment > 0 ? (totalUnrealized + totalRealized) / totalInvestment : 0
|
|
552
|
-
});
|
|
545
|
+
return cb(null, calculatePerformance(rounds));
|
|
553
546
|
|
|
554
547
|
});
|
|
555
548
|
|
|
@@ -710,11 +703,6 @@ module.exports = function(mongoose, config) {
|
|
|
710
703
|
// The preferred way to delete an investment is to delete it directly and this
|
|
711
704
|
// reference will be cleaned up by the investment post-remove hook.
|
|
712
705
|
|
|
713
|
-
// Note that hooks don't work on updates so that makes this more complicated because our pre-save hook to compute
|
|
714
|
-
// cost won't work on a straight update. The workaround is to pull a list of round ids that will have the investment
|
|
715
|
-
// removed, call update to remove the investments from rounds, retrieve the updated rounds and save them which will
|
|
716
|
-
// trigger the pre-save hook and update the cost.
|
|
717
|
-
|
|
718
706
|
if (!cb) { throw new Error('cb is required'); }
|
|
719
707
|
if (!investmentId) { throw new Error('investmentId is required'); }
|
|
720
708
|
if (!mongoose.Types.ObjectId.isValid(investmentId)) { return cb(new Error('investmentId is not a valid ObjectId'), null); }
|
|
@@ -736,25 +724,9 @@ module.exports = function(mongoose, config) {
|
|
|
736
724
|
query.exec(callback);
|
|
737
725
|
};
|
|
738
726
|
|
|
739
|
-
let updateCost = function updateCost(previous, callback) {
|
|
740
|
-
|
|
741
|
-
let ids = _.pluck(previous, '_id');
|
|
742
|
-
let query = self.find({_id: { $in: ids }});
|
|
743
|
-
|
|
744
|
-
query.exec(function (err, docs) {
|
|
745
|
-
async.map(docs, function (doc, saveCallback) {
|
|
746
|
-
doc.save(function (err) {
|
|
747
|
-
saveCallback(err, doc);
|
|
748
|
-
});
|
|
749
|
-
}, callback);
|
|
750
|
-
});
|
|
751
|
-
|
|
752
|
-
};
|
|
753
|
-
|
|
754
727
|
async.waterfall([
|
|
755
728
|
getRounds,
|
|
756
|
-
updateRounds
|
|
757
|
-
updateCost
|
|
729
|
+
updateRounds
|
|
758
730
|
], cb);
|
|
759
731
|
|
|
760
732
|
};
|
|
@@ -790,45 +762,7 @@ module.exports = function(mongoose, config) {
|
|
|
790
762
|
Round.statics.upsert = function(round, cb) {
|
|
791
763
|
|
|
792
764
|
if (!round) { return cb(new Error('round is required'), null); }
|
|
793
|
-
|
|
794
|
-
const compute = function compute(doc) {
|
|
795
|
-
|
|
796
|
-
// compute for each vehicle
|
|
797
|
-
|
|
798
|
-
_.each(doc.vehicles, function(vehicle) {
|
|
799
|
-
|
|
800
|
-
var investments = vehicle.investments;
|
|
801
|
-
|
|
802
|
-
if (investments.length >= 1) {
|
|
803
|
-
vehicle.investment = _.reduce(investments, function(memo, investment) { return memo + investment.investment; }, 0);
|
|
804
|
-
}
|
|
805
|
-
else {
|
|
806
|
-
vehicle.investment = 0;
|
|
807
|
-
}
|
|
808
|
-
|
|
809
|
-
});
|
|
810
|
-
|
|
811
|
-
};
|
|
812
|
-
|
|
813
|
-
round.save(function(err, result) {
|
|
814
|
-
|
|
815
|
-
if (err) return cb(err, null);
|
|
816
|
-
|
|
817
|
-
result.populate({
|
|
818
|
-
path: 'vehicles.investments',
|
|
819
|
-
select: 'investment'
|
|
820
|
-
}, function(err, doc) {
|
|
821
|
-
|
|
822
|
-
if (err) return cb(err, null);
|
|
823
|
-
else if (!doc) return cb(null, null);
|
|
824
|
-
|
|
825
|
-
compute(doc);
|
|
826
|
-
|
|
827
|
-
round.save(cb);
|
|
828
|
-
|
|
829
|
-
});
|
|
830
|
-
|
|
831
|
-
});
|
|
765
|
+
round.save(cb);
|
|
832
766
|
|
|
833
767
|
};
|
|
834
768
|
|