@dhyasama/totem-models 11.13.0 → 11.15.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 +73 -0
- package/package.json +1 -1
package/lib/Investment.js
CHANGED
|
@@ -62,6 +62,79 @@ module.exports = function(mongoose, config) {
|
|
|
62
62
|
// Statics operate on the entire collection
|
|
63
63
|
//////////////////////////////////////////////////////
|
|
64
64
|
|
|
65
|
+
Investment.statics.getByCustomer = function(customerId, options, cb) {
|
|
66
|
+
|
|
67
|
+
if (!cb) { throw new Error('cb is required'); }
|
|
68
|
+
if (!options) { return cb(new Error('options is required'), null); }
|
|
69
|
+
if (!customerId) { return cb(new Error('options.CUSTOMER_ID is required'), null); }
|
|
70
|
+
if (!mongoose.Types.ObjectId.isValid(customerId)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
|
|
71
|
+
|
|
72
|
+
var self = this;
|
|
73
|
+
var query = self.find({ customer: customerId });
|
|
74
|
+
|
|
75
|
+
if (options.startDate) {
|
|
76
|
+
var startDate = options.startDate || moment().subtract(20, 'years').startOf('day').toISOString();
|
|
77
|
+
query.where('date').gte(startDate);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (options.endDate) {
|
|
81
|
+
var endDate = options.endDate || moment().toISOString();
|
|
82
|
+
query.where('date').lte(endDate);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
query.exec(function(err, investments) {
|
|
86
|
+
|
|
87
|
+
if (err) return cb(err, null);
|
|
88
|
+
|
|
89
|
+
Round.find({
|
|
90
|
+
'vehicles.investments': { $in: _.map(investments, function(inv) { return inv._id; }) }
|
|
91
|
+
})
|
|
92
|
+
.exec(function(err, rounds) {
|
|
93
|
+
|
|
94
|
+
if (err) return cb(err, null);
|
|
95
|
+
|
|
96
|
+
var investmentWithRoundDetails = _.map(investments, function(inv) {
|
|
97
|
+
|
|
98
|
+
var fund = null;
|
|
99
|
+
var organization = null;
|
|
100
|
+
var roundName = null;
|
|
101
|
+
|
|
102
|
+
var round = _.find(rounds, function(r) {
|
|
103
|
+
|
|
104
|
+
return _.some(r.vehicles, function(v) {
|
|
105
|
+
|
|
106
|
+
var investmentFound = _.some(v.investments, function(investmentId) {
|
|
107
|
+
return investmentId.toString() === inv._id.toString();
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
if (investmentFound) {
|
|
111
|
+
fund = v.fund;
|
|
112
|
+
organization = r.organization;
|
|
113
|
+
roundName = r.roundName;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return investmentFound;
|
|
117
|
+
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
return _.extend({}, inv.toObject(), {
|
|
123
|
+
organization: organization,
|
|
124
|
+
round: roundName,
|
|
125
|
+
fund: fund
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
cb(null, investmentWithRoundDetails);
|
|
131
|
+
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
};
|
|
137
|
+
|
|
65
138
|
Investment.statics.removeCustomerInvestments = function removeCustomerInvestments(customerId, cb) {
|
|
66
139
|
|
|
67
140
|
// Note that rounds hold references to investments, so when an investment is deleted we need
|