@dhyasama/totem-models 5.4.1 → 5.5.1

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.
@@ -165,6 +165,33 @@ module.exports = function(mongoose, config) {
165
165
 
166
166
  });
167
167
 
168
+ var getSources = function getSources(people) {
169
+
170
+ var result = _.compact(_.pluck(people, 'sources'));
171
+ if (result.length == 0) return [];
172
+
173
+ result = _.flatten(result);
174
+ result = _.compact(result);
175
+
176
+ if (result.length == 0) return [];
177
+ if (mongoose.Types.ObjectId.isValid(result[0].person)) return []; // not populated
178
+
179
+ result = _.uniq(result, function(item) {
180
+ if (!item || !item.person) return '';
181
+ else return item.person._id ? item.person._id.toString() : '';
182
+ });
183
+ result = _.compact(result);
184
+ result = _.reject(result, function(item) { return !item.person; });
185
+ result = _.sortBy(result, 'interactions.count');
186
+ result.reverse();
187
+ result = _.sortBy(result, 'name.full');
188
+
189
+ console.log(result);
190
+
191
+ return result;
192
+
193
+ };
194
+
168
195
  /////////////////////
169
196
 
170
197
  LimitedPartner.virtual('totalCommitted').get(function () {
@@ -208,34 +235,6 @@ module.exports = function(mongoose, config) {
208
235
 
209
236
  });
210
237
 
211
- LimitedPartner.virtual('sources').get(function () {
212
-
213
- // Aggregate sources from all people
214
- // Sort by interactions descending, full name ascending
215
-
216
- var self = this;
217
- var result = self.people || [];
218
-
219
- if (result.length == 0) return [];
220
- if (mongoose.Types.ObjectId.isValid(result[0])) return []; // not populated
221
-
222
- result = _.pluck(result, 'sources');
223
- result = _.flatten(result);
224
- result = _.compact(result);
225
- result = _.uniq(result, function(item) {
226
- if (!item.person) return null;
227
- var personId = utils.isValidObjectId(item.person) ? item.person : item.person._id;
228
- return personId.toString();
229
- });
230
- result = _.compact(result);
231
- result = _.sortBy(result, 'interactions.count');
232
- result.reverse();
233
- result = _.sortBy(result, 'name.full');
234
-
235
- return result;
236
-
237
- });
238
-
239
238
  /////////////////////
240
239
 
241
240
  /************* BASIC FIELDS **********************/
@@ -477,6 +476,62 @@ module.exports = function(mongoose, config) {
477
476
 
478
477
  };
479
478
 
479
+ LimitedPartner.statics.getSources = function(lpid, cb) {
480
+
481
+ var self = this;
482
+
483
+ self
484
+ .findById(lpid)
485
+ .populate('people', 'name avatarUrl title doNotDisplay sources')
486
+ .deepPopulate([
487
+ 'people.sources.person'
488
+ ], {
489
+ populate: {
490
+ 'people.sources.person': { select: 'name avatarUrl title doNotDisplay' }
491
+ }
492
+ })
493
+ .exec(function(err, lp) {
494
+
495
+ if (err) return cb(err, null);
496
+ if (!lp) return cb(null, null);
497
+
498
+ Organization.findById(config.CUSTOMER_ID, function(err, customer) {
499
+
500
+ if (err) return cb(err, null);
501
+ if (!customer) return cb(null, null);
502
+
503
+ var current = [];
504
+ var past = [];
505
+ var all = [];
506
+ var lpSources = getSources(lp.people);
507
+
508
+ _.each(customer.people, function(p) {
509
+
510
+ var source = _.find(lpSources, function(ls) {
511
+ if (!ls || !ls.person || !ls.person._id) return false;
512
+ return ls.person._id.toString() == p.person.toString();
513
+ });
514
+
515
+ if (!source) return;
516
+
517
+ if (p.current) current.push(source);
518
+ if (!p.current) past.push(source);
519
+ all.push(source);
520
+
521
+ });
522
+
523
+ return cb(null, {
524
+ all: all,
525
+ current: current,
526
+ past: past
527
+ });
528
+
529
+ });
530
+
531
+ });
532
+
533
+ };
534
+
480
535
  LimitedPartner.statics.list = function (role, cb) {
481
536
 
482
537
  // sorted by name ascending
@@ -1560,6 +1560,10 @@ module.exports = function(mongoose, config) {
1560
1560
 
1561
1561
  var self = this;
1562
1562
 
1563
+ // https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
1564
+ // options runValidators defaults false which is ok since we have upsert false
1565
+ // new returns the updated document
1566
+
1563
1567
  self.findOneAndUpdate(filter, update, { upsert: false, new: true }, cb);
1564
1568
 
1565
1569
  };
package/lib/Person.js CHANGED
@@ -7,6 +7,7 @@ module.exports = function(mongoose, config) {
7
7
  var
8
8
 
9
9
  Schema = mongoose.Schema,
10
+ Organization = mongoose.model('Organization'),
10
11
  LimitedPartner = mongoose.model('LimitedPartner'),
11
12
  Flag = mongoose.model('Flag'),
12
13
  Note = mongoose.model('Note'),
@@ -857,7 +858,44 @@ module.exports = function(mongoose, config) {
857
858
  this
858
859
  .findById(id)
859
860
  .populate('sources.person')
860
- .exec(cb);
861
+ .exec(function(err, person) {
862
+
863
+ if (err) return cb(err, null);
864
+ if (!person) return cb(err, null);
865
+
866
+ Organization.findById(config.CUSTOMER_ID, function(err, customer) {
867
+
868
+ if (err) return cb(err, null);
869
+ if (!customer) return cb(null, null);
870
+
871
+ var current = [];
872
+ var past = [];
873
+ var all = [];
874
+
875
+ _.each(customer.people, function(p) {
876
+
877
+ var source = _.find(person.sources, function(ps) {
878
+ if (!ps || !ps.person || !ps.person._id) return false;
879
+ return ps.person._id.toString() == p.person.toString();
880
+ });
881
+
882
+ if (!source) return;
883
+
884
+ if (p.current) current.push(source);
885
+ if (!p.current) past.push(source);
886
+ all.push(source);
887
+
888
+ });
889
+
890
+ return cb(null, {
891
+ all: all,
892
+ current: current,
893
+ past: past
894
+ });
895
+
896
+ });
897
+
898
+ });
861
899
  };
862
900
 
863
901
  Person.statics.listAllPeople = function (cb) {
package/package-lock.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dhyasama/totem-models",
3
- "version": "5.3.4",
3
+ "version": "5.5.0",
4
4
  "lockfileVersion": 1,
5
5
  "requires": true,
6
6
  "dependencies": {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dhyasama/totem-models",
3
- "version": "5.4.1",
3
+ "version": "5.5.1",
4
4
  "author": "Jason Reynolds",
5
5
  "license": "UNLICENSED",
6
6
  "description": "Models for Totem platform",