@dhyasama/totem-models 12.10.0 → 12.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.
@@ -213,23 +213,26 @@ module.exports = function(mongoose, config) {
213
213
  });
214
214
 
215
215
  LimitedPartner.virtual('email.primary').get(function () {
216
- let primary = _.find(this.contact.email, function(email) {
217
- return email.primary;
216
+ let emails = this && this.contact && this.contact.email ? this.contact.email : [];
217
+ let primary = _.find(emails, function(email) {
218
+ return email && email.primary;
218
219
  });
219
220
  return primary ? primary.email : '';
220
221
  });
221
222
 
222
223
  LimitedPartner.virtual('phone.primary').get(function () {
223
- let primary = _.find(this.contact.phone, function(phone) {
224
- return phone.primary;
224
+ let phones = this && this.contact && this.contact.phone ? this.contact.phone : [];
225
+ let primary = _.find(phones, function(phone) {
226
+ return phone && phone.primary;
225
227
  });
226
228
  return primary ? primary.number : '';
227
229
  });
228
230
 
229
231
  LimitedPartner.virtual('address.primary').get(function () {
230
232
 
231
- let primary = _.find(this.contact.address, function(address) {
232
- return address.primary;
233
+ let addresses = this && this.contact && this.contact.address ? this.contact.address : [];
234
+ let primary = _.find(addresses, function(address) {
235
+ return address && address.primary;
233
236
  });
234
237
 
235
238
  if (!primary) {
package/lib/Person.js CHANGED
@@ -166,17 +166,21 @@ module.exports = function(mongoose, config) {
166
166
  // Absent that, use first email
167
167
  // Absent that, return empty string
168
168
  var self = this;
169
+ var emails = self && self.contact && self.contact.email ? self.contact.email : [];
169
170
  var result;
170
- var primary = _.find(self.contact.email, function(email) { return email.primary; });
171
+ var primary = _.find(emails, function(email) {
172
+ return email && email.primary;
173
+ });
171
174
  if (primary) result = primary.email;
172
- else if (self.contact.email && self.contact.email.length >= 1) result = self.contact.email[0].email;
175
+ else if (emails.length >= 1 && emails[0]) result = emails[0].email;
173
176
  else result = '';
174
177
  return result;
175
178
  });
176
179
 
177
180
  Person.virtual('phone.primary').get(function () {
178
- var primary = _.find(this.contact.phone, function(phone) {
179
- return phone.primary;
181
+ var phones = this && this.contact && this.contact.phone ? this.contact.phone : [];
182
+ var primary = _.find(phones, function(phone) {
183
+ return phone && phone.primary;
180
184
  });
181
185
  return primary ? primary.number : '';
182
186
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dhyasama/totem-models",
3
- "version": "12.10.0",
3
+ "version": "12.11.0",
4
4
  "author": "Jason Reynolds",
5
5
  "license": "UNLICENSED",
6
6
  "description": "Models for Totem platform",
@@ -398,6 +398,25 @@ describe('Limited Partner', function() {
398
398
 
399
399
  });
400
400
 
401
+ it('does not throw when contact is omitted from a lean lp projection', function(done) {
402
+
403
+ LimitedPartner.findById(lp.id)
404
+ .select('name')
405
+ .lean({ virtuals: true })
406
+ .then(function(result) {
407
+ should.exist(result);
408
+ should.exist(result.email);
409
+ should.exist(result.phone);
410
+ should.exist(result.address);
411
+ result.email.primary.should.equal('');
412
+ result.phone.primary.should.equal('');
413
+ result.address.primary.postalCode.should.equal('');
414
+ done();
415
+ })
416
+ .catch(done);
417
+
418
+ });
419
+
401
420
  it('adds a flag', function(done) {
402
421
 
403
422
  LimitedPartner.addFlag(lp.id, person._id, 'incorrect phone', organization._id, function(err, result) {
package/test/Person.js CHANGED
@@ -718,6 +718,23 @@ describe('Person', function() {
718
718
 
719
719
  });
720
720
 
721
+ it('does not throw when contact is omitted from a lean person projection', function(done) {
722
+
723
+ Person.findById(person.id)
724
+ .select('name')
725
+ .lean({ virtuals: true })
726
+ .then(function(result) {
727
+ should.exist(result);
728
+ should.exist(result.email);
729
+ should.exist(result.phone);
730
+ result.email.primary.should.equal('');
731
+ result.phone.primary.should.equal('');
732
+ done();
733
+ })
734
+ .catch(done);
735
+
736
+ });
737
+
721
738
  it('gets sources', function(done) {
722
739
 
723
740
  Person.getSources(person.id, { CUSTOMER_ID: org._id }, function(err, result) {