@dhyasama/totem-models 12.9.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) {
@@ -639,10 +639,11 @@ module.exports = function(mongoose, config) {
639
639
  ///////////////////////////////////////////////////////////////////////////////////////
640
640
 
641
641
  Organization.virtual('address.primary').get(function () {
642
- var primary = _.find(this.contact.address, function(address) {
643
- return address.primary;
642
+ var addresses = this && this.contact && this.contact.address ? this.contact.address : [];
643
+ var primary = _.find(addresses, function(address) {
644
+ return address && address.primary;
644
645
  });
645
- return primary ? primary.address : null;
646
+ return primary || null;
646
647
  });
647
648
 
648
649
  Organization.virtual('board.all').get(function () {
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.9.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) {
@@ -1127,6 +1127,87 @@ describe('Organization', function() {
1127
1127
  });
1128
1128
  });
1129
1129
 
1130
+ it('finds the primary address for an org', function(done) {
1131
+
1132
+ var o = new Organization();
1133
+ o.name = 'Addressable';
1134
+ o.slug = 'addressable';
1135
+ o.website = 'addressable.org';
1136
+
1137
+ o.contact.address = [];
1138
+ o.contact.address.push({
1139
+ street: '1 Main Street',
1140
+ city: 'Portland',
1141
+ state: 'ME',
1142
+ country: 'USA',
1143
+ postalCode: '04101',
1144
+ primary: false
1145
+ });
1146
+ o.contact.address.push({
1147
+ street: '2 Center Street',
1148
+ city: 'Bangor',
1149
+ state: 'ME',
1150
+ country: 'USA',
1151
+ postalCode: '04412',
1152
+ primary: true
1153
+ });
1154
+
1155
+ Organization.upsert(o, 'test-user', function(err, org) {
1156
+
1157
+ should.not.exist(err);
1158
+ should.exist(org);
1159
+
1160
+ Organization.findById(org._id).lean({ virtuals: true })
1161
+ .then(function(result) {
1162
+ should.exist(result);
1163
+ should.exist(result.address);
1164
+ should.exist(result.address.primary);
1165
+ result.address.primary.postalCode.should.equal('04412');
1166
+ done();
1167
+ })
1168
+ .catch(done);
1169
+
1170
+ });
1171
+
1172
+ });
1173
+
1174
+ it('does not throw when contact is omitted from a lean projection', function(done) {
1175
+
1176
+ var o = new Organization();
1177
+ o.name = 'Projected';
1178
+ o.slug = 'projected';
1179
+ o.website = 'projected.org';
1180
+
1181
+ o.contact.address = [];
1182
+ o.contact.address.push({
1183
+ street: '10 Broad Street',
1184
+ city: 'New York',
1185
+ state: 'NY',
1186
+ country: 'USA',
1187
+ postalCode: '10004',
1188
+ primary: true
1189
+ });
1190
+
1191
+ Organization.upsert(o, 'test-user', function(err, org) {
1192
+
1193
+ should.not.exist(err);
1194
+ should.exist(org);
1195
+
1196
+ Organization.findById(org._id)
1197
+ .select('name')
1198
+ .lean({ virtuals: true })
1199
+ .then(function(result) {
1200
+ should.exist(result);
1201
+ should.exist(result.address);
1202
+ should.equal(result.address.primary, null);
1203
+ done();
1204
+ })
1205
+ .catch(done);
1206
+
1207
+ });
1208
+
1209
+ });
1210
+
1130
1211
  it('cleans people', function (done) {
1131
1212
 
1132
1213
  var littleDirtyOrg = new Organization();
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) {