@dhyasama/totem-models 1.0.1 → 1.6.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.
@@ -0,0 +1,143 @@
1
+ "use strict";
2
+
3
+ ///////////////////////////////////////////////////////////////////////////////////////
4
+ // NOTES
5
+ //
6
+ // This is private data
7
+ //
8
+ // If you're looking for investments into an org, go in via Rounds.
9
+ //
10
+ // If you're looking for investments made by an org, you could go in this way
11
+ // but note there isn't round information here so you're probably better off
12
+ // going through Rounds as well.
13
+ //
14
+ ///////////////////////////////////////////////////////////////////////////////////////
15
+
16
+ module.exports = function(mongoose, config) {
17
+
18
+ var Schema = mongoose.Schema;
19
+ var Round = mongoose.model('Round');
20
+ var async = require('async');
21
+ var _ = require('underscore');
22
+
23
+ var Investment = new Schema({
24
+
25
+ // Used by Round to selectively populate
26
+ customer: { type: Schema.ObjectId, ref: 'Organization', required: true, index: true },
27
+
28
+ investmentDate: { type: Date, required: true },
29
+ cost: { type: Number, default: 0 },
30
+ costPlusInterest: { type: Number, default: 0 },
31
+ pricePerShare: { type: Number, default: 0 },
32
+
33
+ debt: { type: Boolean, default: false },
34
+ debtType: {
35
+ type: String,
36
+ enum: [ 'Convertible Note', 'SAFE', 'SAFT', 'KISS', null ],
37
+ default: null,
38
+ required: false,
39
+ validate: {
40
+ validator: function(v) {
41
+ var self = this;
42
+ if (v && v.length && self.debt == false) return false;
43
+ else return true;
44
+ },
45
+ message: 'There is a debt type ({VALUE}) but debt is set to false! Either set debt to true or remove the debt type.'
46
+ }
47
+ },
48
+
49
+ // Catch-all for customer specific key-value pairs from sheet that aren't supported by our schema
50
+ details: [{
51
+ key: { type: String, trim: true, required: true },
52
+ value: { type: String, trim: true, required: true },
53
+ _id: false
54
+ }],
55
+
56
+ // note these fields are duplicative at the round level
57
+ // this is private data and will override the round level numbers where appropriate
58
+ preMoneyValuation: { type: Number, default: 0 },
59
+ dollarsRaised: { type: Number, default: 0 },
60
+ closingDate: { type: Date, default: null }
61
+
62
+ });
63
+
64
+
65
+
66
+ //////////////////////////////////////////////////////
67
+ // STATICS
68
+ // Statics operate on the entire collection
69
+ //////////////////////////////////////////////////////
70
+
71
+ Investment.statics.removeCustomerInvestments = function removeCustomerInvestments(customerId, cb) {
72
+
73
+ // Note that rounds hold references to investments, so when an investment is deleted we need
74
+ // to also remove any references to it. Investment has a post-remove hook which does that, but
75
+ // post-remove hooks only fire on document.remove not schema.remove, hence the loop below to call
76
+ // remove on each matching investment.
77
+
78
+ var self = this;
79
+
80
+ self
81
+ .find({customer: customerId})
82
+ .select('_id')
83
+ .exec(function(err, investments) {
84
+
85
+ if (err) return cb(err, null);
86
+
87
+ async.each(investments, function(investment, callback) {
88
+ investment.remove(callback);
89
+ }, function(err) {
90
+ return cb(err, null);
91
+ });
92
+
93
+ });
94
+
95
+ };
96
+
97
+ Investment.statics.upsert = function upsert(investment, cb) {
98
+ if (!investment) { return cb(new Error('investment is required'), null); }
99
+ investment.save(cb);
100
+ };
101
+
102
+
103
+
104
+ ///////////////////////////////////////////////////////////////////////////////////////
105
+ // HOOKS
106
+ // Pre-save: fired on every document before it is saved
107
+ // Post-init: fired on every document when it's returned from a query
108
+ // Post-remove: fired on every document after it's deleted
109
+ ///////////////////////////////////////////////////////////////////////////////////////
110
+
111
+ Investment.pre('save', function(next) {
112
+
113
+ var self = this;
114
+
115
+ if (self.debtType) self.debt = true;
116
+
117
+ return next();
118
+
119
+ });
120
+
121
+ Investment.post('remove', function(doc) {
122
+ // Clean up reference
123
+ Round.removeInvestment(doc._id, function(err, result) {
124
+ if (err) console.log(err);
125
+ });
126
+ });
127
+
128
+
129
+
130
+ ///////////////////////////////////////////////////////////////////////////////////////
131
+ // CONFIG
132
+ ///////////////////////////////////////////////////////////////////////////////////////
133
+
134
+ Investment.set('toJSON', { virtuals: true });
135
+ Investment.set('autoIndex', true);
136
+
137
+ Investment.on('index', function(err) { console.log('error building Investment indexes: ' + err); });
138
+
139
+ mongoose.model('Investment', Investment);
140
+
141
+ };
142
+
143
+
@@ -494,16 +494,6 @@ module.exports = function(mongoose, config) {
494
494
 
495
495
  if (err) return cb(err, null);
496
496
 
497
- lps = _.filter(lps, function(lp) {
498
- if (CUSTOMER_ID == 'GLOBAL_PROCESS') { return lp; }
499
- else {
500
- var match = _.find(lp.fundsInvested, function(fund) {
501
- return fund.fund.investor.toString() == CUSTOMER_ID.toString();
502
- });
503
- return typeof match != 'undefined';
504
- }
505
- });
506
-
507
497
  lps = _.sortBy(lps, function(lp) {
508
498
  return lp.name ? lp.name.toLowerCase() : '';
509
499
  });
package/lib/News.js CHANGED
@@ -54,6 +54,10 @@ module.exports = function(mongoose, config) {
54
54
 
55
55
  };
56
56
 
57
+ News.statics.listForCompanies = function listForCompanies(orgids, cb) {
58
+ this.find({ 'org': { $in : orgids }, 'deleted': {$ne: true} }).exec(cb);
59
+ };
60
+
57
61
  News.statics.listForCompany = function (id, cb) {
58
62
 
59
63
  this