@dhyasama/totem-models 9.9.1 → 9.11.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/Fund.js CHANGED
@@ -16,11 +16,6 @@ module.exports = function(mongoose, config) {
16
16
  closeDate: { type: Date },
17
17
  amount: { type: Number },
18
18
  crunchbase: { uuid: { type: String, default: '', unique: true } },
19
- transactions: [{
20
- type: { type: String, enum: ['fees', 'expenses', 'carry'] },
21
- amount: { type: Number, default: 0 },
22
- date: { type: Date, default: null },
23
- }],
24
19
  customFields: { type: Schema.Types.Mixed },
25
20
 
26
21
  // the stakeholders names we match to on cap tables
@@ -143,15 +138,44 @@ module.exports = function(mongoose, config) {
143
138
 
144
139
  };
145
140
 
146
- Fund.statics.removeTransactions = function removeTransactions(cb) {
141
+ Fund.statics.modify = function(filter, update, cb) {
147
142
 
148
- const self = this;
143
+ // VERY IMPORTANT NOTE
144
+ // findByIdAndUpdate and findOneAndUpdate do not trigger pre-save hook so that code will not run here
145
+ // at this time, the only known and approved use of this method is for adding and removing roles in totem web
146
+
147
+ if (!cb) throw new Error('cb is required');
148
+ if (!filter) { return cb(new Error('filter is required'), null); }
149
+ if (!update) { return cb(new Error('update is required'), null); }
150
+
151
+ var self = this;
152
+
153
+ // https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
154
+ // options runValidators defaults false which is ok since we have upsert false
155
+ // new returns the updated document
156
+
157
+ self.findOneAndUpdate(filter, update, { upsert: false, new: true }, cb);
158
+
159
+ };
160
+
161
+ Fund.statics.modifyById = function(id, update, cb) {
162
+
163
+ // VERY IMPORTANT NOTE
164
+ // findByIdAndUpdate and findOneAndUpdate do not trigger pre-save hook so that code will not run here
165
+ // at this time, the only known and approved use of this method is for the org edit form in totem web
166
+
167
+ if (!cb) throw new Error('cb is required');
168
+ if (!id) { return cb(new Error('id is required'), null); }
169
+ if (!mongoose.Types.ObjectId.isValid(id)) { return cb(new Error('id is not a valid ObjectId'), null); }
170
+ if (!update) { return cb(new Error('update is required'), null); }
171
+
172
+ var self = this;
173
+
174
+ // https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
175
+ // options runValidators defaults false which is ok since we have upsert false
176
+ // new returns the updated document
149
177
 
150
- let query = self.update(
151
- { $set: { transactions: [] } },
152
- { multi : true }
153
- );
154
- query.exec(cb);
178
+ self.findByIdAndUpdate(id, update, { upsert: false, new: true }, cb);
155
179
 
156
180
  };
157
181
 
@@ -39,9 +39,10 @@ module.exports = function(mongoose, config) {
39
39
 
40
40
  transactions: [{
41
41
  fund: {type: Schema.ObjectId, ref: 'Fund'},
42
- type: { type: String, enum: ['commitment', 'contribution', 'distribution'] },
42
+ type: { type: String, enum: ['commitment', 'contribution', 'distribution', 'fee', 'expense', 'carry', 'income', 'realized', 'unrealized'] },
43
43
  amount: { type: Number, default: 0 },
44
44
  date: { type: Date, default: null },
45
+ description: { type: String, default: '' },
45
46
  }],
46
47
 
47
48
  contact: {
@@ -134,6 +135,48 @@ module.exports = function(mongoose, config) {
134
135
  }, 0);
135
136
  });
136
137
 
138
+ LimitedPartner.virtual('totalFee').get(function () {
139
+ var self = this;
140
+ return _.reduce(self.transactions, function(memo, transaction) {
141
+ return memo + ((transaction.type === 'fee' && Math.abs(transaction.amount) > 0) ? transaction.amount : 0);
142
+ }, 0);
143
+ });
144
+
145
+ LimitedPartner.virtual('totalExpense').get(function () {
146
+ var self = this;
147
+ return _.reduce(self.transactions, function(memo, transaction) {
148
+ return memo + ((transaction.type === 'expense' && Math.abs(transaction.amount) > 0) ? transaction.amount : 0);
149
+ }, 0);
150
+ });
151
+
152
+ LimitedPartner.virtual('totalCarry').get(function () {
153
+ var self = this;
154
+ return _.reduce(self.transactions, function(memo, transaction) {
155
+ return memo + ((transaction.type === 'carry' && Math.abs(transaction.amount) > 0) ? transaction.amount : 0);
156
+ }, 0);
157
+ });
158
+
159
+ LimitedPartner.virtual('totalIncome').get(function () {
160
+ var self = this;
161
+ return _.reduce(self.transactions, function(memo, transaction) {
162
+ return memo + ((transaction.type === 'income' && Math.abs(transaction.amount) > 0) ? transaction.amount : 0);
163
+ }, 0);
164
+ });
165
+
166
+ LimitedPartner.virtual('totalRealized').get(function () {
167
+ var self = this;
168
+ return _.reduce(self.transactions, function(memo, transaction) {
169
+ return memo + ((transaction.type === 'realized' && Math.abs(transaction.amount) > 0) ? transaction.amount : 0);
170
+ }, 0);
171
+ });
172
+
173
+ LimitedPartner.virtual('totalUnrealized').get(function () {
174
+ var self = this;
175
+ return _.reduce(self.transactions, function(memo, transaction) {
176
+ return memo + ((transaction.type === 'unrealized' && Math.abs(transaction.amount) > 0) ? transaction.amount : 0);
177
+ }, 0);
178
+ });
179
+
137
180
  LimitedPartner.virtual('fundsInvested').get(function () {
138
181
 
139
182
  var self = this;
@@ -151,7 +194,13 @@ module.exports = function(mongoose, config) {
151
194
  abbreviation: transaction.fund.abbreviation,
152
195
  commitment: 0,
153
196
  contribution: 0,
154
- distribution: 0
197
+ distribution: 0,
198
+ fee: 0,
199
+ expense: 0,
200
+ carry: 0,
201
+ income: 0,
202
+ realized: 0,
203
+ unrealized: 0
155
204
  });
156
205
  fundMatch = _.last(fundsInvested);
157
206
  }
@@ -637,17 +686,38 @@ module.exports = function(mongoose, config) {
637
686
 
638
687
  };
639
688
 
689
+ LimitedPartner.statics.modify = function(filter, update, cb) {
690
+
691
+ // VERY IMPORTANT NOTE
692
+ // findByIdAndUpdate and findOneAndUpdate do not trigger pre-save hook so that code will not run here
693
+ // at this time, the only known and approved use of this method is for adding and removing roles in totem web
694
+
695
+ if (!cb) throw new Error('cb is required');
696
+ if (!filter) { return cb(new Error('filter is required'), null); }
697
+ if (!update) { return cb(new Error('update is required'), null); }
698
+
699
+ var self = this;
700
+
701
+ // https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
702
+ // options runValidators defaults false which is ok since we have upsert false
703
+ // new returns the updated document
704
+
705
+ self.findOneAndUpdate(filter, update, { upsert: false, new: true }, cb);
706
+
707
+ };
708
+
640
709
  LimitedPartner.statics.modifyById = function(id, update, cb) {
641
710
 
642
711
  // VERY IMPORTANT NOTE
643
712
  // findByIdAndUpdate and findOneAndUpdate do not trigger pre-save hook so that code will not run here
713
+ // at this time, the only known and approved use of this method is for the org edit form in totem web
644
714
 
645
- if (!cb) { throw new Error('cb is required'); }
715
+ if (!cb) throw new Error('cb is required');
646
716
  if (!id) { return cb(new Error('id is required'), null); }
647
717
  if (!mongoose.Types.ObjectId.isValid(id)) { return cb(new Error('id is not a valid ObjectId'), null); }
648
- if (!update) { return cb(new Error('options is required'), null); }
718
+ if (!update) { return cb(new Error('update is required'), null); }
649
719
 
650
- const self = this;
720
+ var self = this;
651
721
 
652
722
  // https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
653
723
  // options runValidators defaults false which is ok since we have upsert false
@@ -1649,6 +1649,7 @@ module.exports = function(mongoose, config) {
1649
1649
 
1650
1650
  if (!cb) throw new Error('cb is required');
1651
1651
  if (!id) { return cb(new Error('id is required'), null); }
1652
+ if (!mongoose.Types.ObjectId.isValid(id)) { return cb(new Error('id is not a valid ObjectId'), null); }
1652
1653
  if (!update) { return cb(new Error('update is required'), null); }
1653
1654
 
1654
1655
  var self = this;
package/lib/Person.js CHANGED
@@ -594,16 +594,38 @@ module.exports = function(mongoose, config) {
594
594
  });
595
595
  };
596
596
 
597
+ Person.statics.modify = function(filter, update, cb) {
598
+
599
+ // VERY IMPORTANT NOTE
600
+ // findByIdAndUpdate and findOneAndUpdate do not trigger pre-save hook so that code will not run here
601
+ // at this time, the only known and approved use of this method is for adding and removing roles in totem web
602
+
603
+ if (!cb) throw new Error('cb is required');
604
+ if (!filter) { return cb(new Error('filter is required'), null); }
605
+ if (!update) { return cb(new Error('update is required'), null); }
606
+
607
+ var self = this;
608
+
609
+ // https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
610
+ // options runValidators defaults false which is ok since we have upsert false
611
+ // new returns the updated document
612
+
613
+ self.findOneAndUpdate(filter, update, { upsert: false, new: true }, cb);
614
+
615
+ };
616
+
597
617
  Person.statics.modifyById = function(id, update, cb) {
598
618
 
599
619
  // VERY IMPORTANT NOTE
600
620
  // findByIdAndUpdate and findOneAndUpdate do not trigger pre-save hook so that code will not run here
621
+ // at this time, the only known and approved use of this method is for the org edit form in totem web
601
622
 
602
623
  if (!cb) throw new Error('cb is required');
603
624
  if (!id) { return cb(new Error('id is required'), null); }
625
+ if (!mongoose.Types.ObjectId.isValid(id)) { return cb(new Error('id is not a valid ObjectId'), null); }
604
626
  if (!update) { return cb(new Error('update is required'), null); }
605
627
 
606
- const self = this;
628
+ var self = this;
607
629
 
608
630
  // https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
609
631
  // options runValidators defaults false which is ok since we have upsert false
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dhyasama/totem-models",
3
- "version": "9.9.1",
3
+ "version": "9.11.0",
4
4
  "author": "Jason Reynolds",
5
5
  "license": "UNLICENSED",
6
6
  "description": "Models for Totem platform",