@dhyasama/totem-models 1.0.0 → 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.
package/lib/Person.js CHANGED
@@ -150,10 +150,16 @@ module.exports = function(mongoose, config) {
150
150
  });
151
151
 
152
152
  Person.virtual('email.primary').get(function () {
153
- var primary = _.find(this.contact.email, function(email) {
154
- return email.primary;
155
- });
156
- return primary ? primary.email : '';
153
+ // Email marked primary is best
154
+ // Absent that, use first email
155
+ // Absent that, return empty string
156
+ var self = this;
157
+ var result;
158
+ var primary = _.find(self.contact.email, function(email) { return email.primary; });
159
+ if (primary) result = primary.email;
160
+ else if (self.contact.email && self.contact.email.length >= 1) result = self.contact.email[0].email;
161
+ else result = '';
162
+ return result;
157
163
  });
158
164
 
159
165
  Person.virtual('phone.primary').get(function () {
@@ -684,31 +690,6 @@ module.exports = function(mongoose, config) {
684
690
 
685
691
  Person.statics.getById = function (id, cb) {
686
692
 
687
- this
688
- .findById(id)
689
- .populate('account')
690
- .populate('chairs.first.company')
691
- .populate('chairs.second.company')
692
- .populate('notes')
693
- .populate('related')
694
- .populate('sources.person')
695
- .exec(function (err, person) {
696
-
697
- if (err) return cb(err, null);
698
- else if (!person) return cb(null, null);
699
-
700
- person.sources = _.sortBy(person.sources, function(source) {
701
- return source.interactions.calendar + source.interactions.email;
702
- }).reverse();
703
-
704
- return cb(null, person);
705
-
706
- });
707
-
708
- };
709
-
710
- Person.statics.getByIdCustomer = function (id, customer, cb) {
711
-
712
693
  // this would speed it up
713
694
  // still need a way to optionally return all though
714
695
  //.findById(id, { 'calendarEventSummaries': { $slice: 5 } })
@@ -719,8 +700,8 @@ module.exports = function(mongoose, config) {
719
700
  .populate('calendarEventSummaries.attendees.external', 'avatarUrl name title')
720
701
  .populate('calendarEventSummaries.attendees.internal', 'avatarUrl name title')
721
702
  .populate('calendarEventSummaries.notes')
722
- .populate('chairs.first.company', 'logoUrl name')
723
- .populate('chairs.second.company', 'logoUrl name')
703
+ .populate('notes')
704
+ .populate('related')
724
705
  .populate('sources.person', 'avatarUrl name title')
725
706
  .deepPopulate([
726
707
  'notes.createdBy',
@@ -787,27 +768,6 @@ module.exports = function(mongoose, config) {
787
768
  .exec(cb);
788
769
  };
789
770
 
790
- // TODO - make dynamic
791
- Person.statics.getPartners = function (cb) {
792
-
793
- var partnerEmails = [
794
- 'john@ffvc.com',
795
- 'adam@ffvc.com',
796
- 'alex@ffvc.com',
797
- 'david@ffvc.com',
798
- 'michael@ffvc.com',
799
- 'ryan@ffvc.com',
800
- 'herb@ffvc.com',
801
- 'paul@ffvc.com'
802
- ];
803
-
804
- this
805
- .find({ 'contact.email.email': { $in: partnerEmails }, 'deleted': { $ne: true } } )
806
- .populate('account boards.company chairs.first.company chairs.second.company')
807
- .exec(cb);
808
-
809
- };
810
-
811
771
  Person.statics.findByProviderEventId = function (id, cb) {
812
772
  this
813
773
  .find({ 'calendarEventSummaries.providerEventId': id })
@@ -829,7 +789,8 @@ module.exports = function(mongoose, config) {
829
789
  Person.statics.findByUniqueInfo = function (email, cb) {
830
790
 
831
791
  // useful for not inserting dupes
832
- // todo: expand this to check other fields
792
+ // could expand this to check other fields
793
+ // but really should be replaced by search
833
794
 
834
795
  this.findOne({
835
796
  $or: [
@@ -841,6 +802,13 @@ module.exports = function(mongoose, config) {
841
802
 
842
803
  };
843
804
 
805
+ Person.statics.getSources = function (id, cb) {
806
+ this
807
+ .findById(id)
808
+ .populate('sources.person')
809
+ .exec(cb);
810
+ };
811
+
844
812
  Person.statics.listAllPeople = function (cb) {
845
813
 
846
814
  // sort is done post retrieval because query sort is done pre-decryption
@@ -1042,16 +1010,6 @@ module.exports = function(mongoose, config) {
1042
1010
 
1043
1011
  ///////////////
1044
1012
 
1045
- // Person.pre('save', function(next) {
1046
- // var self = this;
1047
- // var PersonHistory = mongoose.model('PersonHistory');
1048
- // var snapshot = new PersonHistory();
1049
- // snapshot.create(self, 'jason', function(err, result) {
1050
- // if (err) {} // todo: alert admin to fix; ok to proceed;
1051
- // return next();
1052
- // });
1053
- // });
1054
-
1055
1013
  Person.post('remove', function(doc) {
1056
1014
  // Account.person
1057
1015
  // Investor.people
@@ -1070,36 +1028,6 @@ module.exports = function(mongoose, config) {
1070
1028
  // PortfolioCompany.formerPeople
1071
1029
  });
1072
1030
 
1073
- var protectCustomerData = function(context) {
1074
-
1075
- var CUSTOMER_ID = config.CUSTOMER_ID;
1076
-
1077
- var protectCalendarEventData = function protectCalendarEventData(person) {
1078
-
1079
- if (CUSTOMER_ID == 'GLOBAL_PROCESS') return;
1080
-
1081
- person.calendarEventSummaries = _.reject(person.calendarEventSummaries, function(event) {
1082
- return event.totemCustomerId != CUSTOMER_ID;
1083
- });
1084
-
1085
- };
1086
-
1087
- var protectSources = function protectSources(person) {
1088
-
1089
- if (CUSTOMER_ID == 'GLOBAL_PROCESS') return;
1090
-
1091
- person.sources = _.reject(person.sources, function(source) {
1092
- var customerId = mongoose.Types.ObjectId.isValid(source.customer) ? source.customer : source.customer._id;
1093
- return customerId.toString() != CUSTOMER_ID.toString();
1094
- });
1095
-
1096
- };
1097
-
1098
- protectCalendarEventData(context);
1099
- protectSources(context);
1100
-
1101
- };
1102
-
1103
1031
  Person.pre('save', function(next) {
1104
1032
 
1105
1033
  var self = this;
@@ -1147,7 +1075,34 @@ module.exports = function(mongoose, config) {
1147
1075
  });
1148
1076
 
1149
1077
  Person.post('init', function() {
1150
- protectCustomerData(this);
1078
+
1079
+ var self = this;
1080
+ var CUSTOMER_ID = config.CUSTOMER_ID;
1081
+
1082
+ var protectCalendarEventData = function protectCalendarEventData(person) {
1083
+
1084
+ if (CUSTOMER_ID == 'GLOBAL_PROCESS') return;
1085
+
1086
+ person.calendarEventSummaries = _.reject(person.calendarEventSummaries, function(event) {
1087
+ return event.totemCustomerId != CUSTOMER_ID;
1088
+ });
1089
+
1090
+ };
1091
+
1092
+ var protectSources = function protectSources(person) {
1093
+
1094
+ if (CUSTOMER_ID == 'GLOBAL_PROCESS') return;
1095
+
1096
+ person.sources = _.reject(person.sources, function(source) {
1097
+ var customerId = mongoose.Types.ObjectId.isValid(source.customer) ? source.customer : source.customer._id;
1098
+ return customerId.toString() != CUSTOMER_ID.toString();
1099
+ });
1100
+
1101
+ };
1102
+
1103
+ protectCalendarEventData(self);
1104
+ protectSources(self);
1105
+
1151
1106
  });
1152
1107
 
1153
1108
  var deepPopulate = require('mongoose-deep-populate')(mongoose);