@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.
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',
@@ -821,6 +802,13 @@ module.exports = function(mongoose, config) {
821
802
 
822
803
  };
823
804
 
805
+ Person.statics.getSources = function (id, cb) {
806
+ this
807
+ .findById(id)
808
+ .populate('sources.person')
809
+ .exec(cb);
810
+ };
811
+
824
812
  Person.statics.listAllPeople = function (cb) {
825
813
 
826
814
  // sort is done post retrieval because query sort is done pre-decryption
@@ -1040,36 +1028,6 @@ module.exports = function(mongoose, config) {
1040
1028
  // PortfolioCompany.formerPeople
1041
1029
  });
1042
1030
 
1043
- var protectCustomerData = function(context) {
1044
-
1045
- var CUSTOMER_ID = config.CUSTOMER_ID;
1046
-
1047
- var protectCalendarEventData = function protectCalendarEventData(person) {
1048
-
1049
- if (CUSTOMER_ID == 'GLOBAL_PROCESS') return;
1050
-
1051
- person.calendarEventSummaries = _.reject(person.calendarEventSummaries, function(event) {
1052
- return event.totemCustomerId != CUSTOMER_ID;
1053
- });
1054
-
1055
- };
1056
-
1057
- var protectSources = function protectSources(person) {
1058
-
1059
- if (CUSTOMER_ID == 'GLOBAL_PROCESS') return;
1060
-
1061
- person.sources = _.reject(person.sources, function(source) {
1062
- var customerId = mongoose.Types.ObjectId.isValid(source.customer) ? source.customer : source.customer._id;
1063
- return customerId.toString() != CUSTOMER_ID.toString();
1064
- });
1065
-
1066
- };
1067
-
1068
- protectCalendarEventData(context);
1069
- protectSources(context);
1070
-
1071
- };
1072
-
1073
1031
  Person.pre('save', function(next) {
1074
1032
 
1075
1033
  var self = this;
@@ -1117,7 +1075,34 @@ module.exports = function(mongoose, config) {
1117
1075
  });
1118
1076
 
1119
1077
  Person.post('init', function() {
1120
- 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
+
1121
1106
  });
1122
1107
 
1123
1108
  var deepPopulate = require('mongoose-deep-populate')(mongoose);