@dhyasama/totem-models 11.13.0 → 11.14.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 +56 -0
- package/package.json +1 -1
package/lib/Investment.js
CHANGED
|
@@ -62,6 +62,62 @@ 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 investmentWithOrg = _.map(investments, function(inv) {
|
|
97
|
+
|
|
98
|
+
var round = _.find(rounds, function(r) {
|
|
99
|
+
return _.some(r.vehicles, function(v) {
|
|
100
|
+
return _.some(v.investments, function(investmentId) {
|
|
101
|
+
return investmentId.toString() === inv._id.toString();
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
return _.extend({}, inv.toObject(), {
|
|
107
|
+
organization: round ? round.organization : null,
|
|
108
|
+
round: round ? round.roundName : null
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
cb(null, investmentWithOrg);
|
|
114
|
+
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
};
|
|
120
|
+
|
|
65
121
|
Investment.statics.removeCustomerInvestments = function removeCustomerInvestments(customerId, cb) {
|
|
66
122
|
|
|
67
123
|
// Note that rounds hold references to investments, so when an investment is deleted we need
|