@dhyasama/totem-models 12.3.0 → 12.4.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
@@ -288,119 +288,127 @@ module.exports = function(mongoose, config) {
288
288
  Note.createForModel(this, personId, note, cb);
289
289
  };
290
290
 
291
- Person.statics.deleteNote = function(noteId, customerId, cb) {
291
+ Person.statics.deleteNote = async function(noteId, customerId, cb) {
292
292
 
293
293
  // Delete the note itself along with any references on people
294
294
 
295
- const self = this;
296
-
297
- let removePersonNoteReferences = function removePersonNoteReferences(callback) {
298
- self.updateMany({}, {
295
+ try {
296
+ await Note.delete(noteId, customerId);
297
+ await this.updateMany({}, {
299
298
  $pull: { 'notes' : [noteId] }
300
- }).then(function(result) { callback(null, result); }).catch(function(err) { callback(err); });
301
- };
302
-
303
- async.series([
304
- Note.delete.bind(Note, noteId, customerId),
305
- removePersonNoteReferences
306
- ], function(err, results) {
299
+ });
300
+ return cb(null, null);
301
+ } catch(err) {
307
302
  return cb(err, null);
308
- });
303
+ }
309
304
 
310
305
  };
311
306
 
312
307
  // Retrieve any person matching an email in the emails array parameter
313
- Person.statics.findByEmails = function (emails, cb) {
314
-
315
- this
316
- .find({ 'contact.email.email': { $in : emails }, 'deleted': {$ne: true}})
317
- .select('name avatarUrl title doNotDisplay contact')
318
- .sort('name.full')
319
- .exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
308
+ Person.statics.findByEmails = async function (emails, cb) {
309
+
310
+ try {
311
+ let query = this
312
+ .find({ 'contact.email.email': { $in : emails }, 'deleted': {$ne: true}})
313
+ .select('name avatarUrl title doNotDisplay contact')
314
+ .sort('name.full');
315
+ const result = await query;
316
+ return cb(null, result);
317
+ } catch(err) {
318
+ return cb(err);
319
+ }
320
320
 
321
321
  };
322
322
 
323
- Person.statics.findBySlug = function (slug, cb) {
324
-
325
- const self = this;
323
+ Person.statics.findBySlug = async function (slug, cb) {
326
324
 
327
325
  if (!cb) { throw new Error('cb is required'); }
328
326
  if (!slug) { return cb(new Error('slug must be provided'), null); }
329
327
 
330
- self
331
- .findOne({ slug: new RegExp(slug, 'i'), 'deleted': { $ne: true } })
332
- .exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
328
+ try {
329
+ const result = await this
330
+ .findOne({ slug: new RegExp(slug, 'i'), 'deleted': { $ne: true } });
331
+ return cb(null, result);
332
+ } catch(err) {
333
+ return cb(err);
334
+ }
333
335
 
334
336
  };
335
337
 
336
- Person.statics.findBySlugs = function findBySlugs(slugs, cb) {
337
-
338
- const self = this;
338
+ Person.statics.findBySlugs = async function findBySlugs(slugs, cb) {
339
339
 
340
340
  if (!cb) { throw new Error('cb is required'); }
341
341
  if (!slugs) { return cb(new Error('slug must be provided'), null); }
342
342
 
343
- self.find({ 'slug': { $in : slugs } }).exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
343
+ try {
344
+ const result = await this.find({ 'slug': { $in : slugs } });
345
+ return cb(null, result);
346
+ } catch(err) {
347
+ return cb(err);
348
+ }
344
349
 
345
350
  };
346
351
 
347
- Person.statics.findBySocial = function findBySocial(value, cb) {
352
+ Person.statics.findBySocial = async function findBySocial(value, cb) {
348
353
 
349
354
  // Extract stand-alone username and append it to our standardized domains
350
355
 
351
- const self = this;
352
-
353
356
  if (!value) { return cb(new Error('value must be provided'), null); }
354
357
 
355
358
  let username = utilities.getUsernameFromUrl(value);
356
359
 
357
360
  if (!username) { return cb(null, []); }
358
361
 
359
- self
360
- .find({ $or:
361
- [
362
- { 'social.facebook': new RegExp('facebook.com/' + username + '$/?', 'i') },
363
- { 'social.linkedin': new RegExp('linkedin.com/in/' + username + '\/?$', 'i') },
364
- { 'social.twitter': new RegExp('twitter.com/' + username + '/?$', 'i') }
365
- ],
366
- 'deleted': { $ne: true }
367
- })
368
- .exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
362
+ try {
363
+ const result = await this
364
+ .find({ $or:
365
+ [
366
+ { 'social.facebook': new RegExp('facebook.com/' + username + '$/?', 'i') },
367
+ { 'social.linkedin': new RegExp('linkedin.com/in/' + username + '\/?$', 'i') },
368
+ { 'social.twitter': new RegExp('twitter.com/' + username + '/?$', 'i') }
369
+ ],
370
+ 'deleted': { $ne: true }
371
+ });
372
+ return cb(null, result);
373
+ } catch(err) {
374
+ return cb(err);
375
+ }
369
376
 
370
377
  };
371
378
 
372
- Person.statics.findBySocials = function findBySocials(values, cb) {
379
+ Person.statics.findBySocials = async function findBySocials(values, cb) {
373
380
 
374
381
  // Extract usernames and append it to our standardized domains
375
382
 
376
- const self = this;
377
-
378
383
  if (!cb) { throw new Error('cb is required'); }
379
384
  if (!values) { return cb(new Error('values is required'), null); }
380
385
 
381
- let facebookRegexes = _.map(values, function(value) { return new RegExp('facebook.com/' + utilities.getUsernameFromUrl(value) + '\/?$', 'i') });
382
- let linkedinRegexes = _.map(values, function(value) { return new RegExp('linkedin.com/in/' + utilities.getUsernameFromUrl(value) + '\/?$', 'i') });
383
- let twitterRegexes = _.map(values, function(value) { return new RegExp('twitter.com/' + utilities.getUsernameFromUrl(value) + '\/?$', 'i') });
384
- let crunchbaseRegexes = _.map(values, function(value) { return new RegExp('crunchbase.com/person/' + utilities.getUsernameFromUrl(value) + '\/?$', 'i') });
385
- let pitchbookRegexes = _.map(values, function(value) { return new RegExp('pitchbook.com/profiles/person/' + utilities.getUsernameFromUrl(value) + '\/?$', 'i') });
386
-
387
- self
388
- .find({ $or:
389
- [
390
- { 'social.facebook': {$in: facebookRegexes }},
391
- { 'social.linkedin': {$in: linkedinRegexes }},
392
- { 'social.twitter': {$in: twitterRegexes }},
393
- { 'crunchbase.url': {$in: crunchbaseRegexes }},
394
- { 'pitchbook.url': {$in: pitchbookRegexes }},
395
- ],
396
- 'deleted': { $ne: true }
397
- }).exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
386
+ try {
387
+ let facebookRegexes = _.map(values, function(value) { return new RegExp('facebook.com/' + utilities.getUsernameFromUrl(value) + '\/?$', 'i') });
388
+ let linkedinRegexes = _.map(values, function(value) { return new RegExp('linkedin.com/in/' + utilities.getUsernameFromUrl(value) + '\/?$', 'i') });
389
+ let twitterRegexes = _.map(values, function(value) { return new RegExp('twitter.com/' + utilities.getUsernameFromUrl(value) + '\/?$', 'i') });
390
+ let crunchbaseRegexes = _.map(values, function(value) { return new RegExp('crunchbase.com/person/' + utilities.getUsernameFromUrl(value) + '\/?$', 'i') });
391
+ let pitchbookRegexes = _.map(values, function(value) { return new RegExp('pitchbook.com/profiles/person/' + utilities.getUsernameFromUrl(value) + '\/?$', 'i') });
392
+
393
+ const result = await this
394
+ .find({ $or:
395
+ [
396
+ { 'social.facebook': {$in: facebookRegexes }},
397
+ { 'social.linkedin': {$in: linkedinRegexes }},
398
+ { 'social.twitter': {$in: twitterRegexes }},
399
+ { 'crunchbase.url': {$in: crunchbaseRegexes }},
400
+ { 'pitchbook.url': {$in: pitchbookRegexes }},
401
+ ],
402
+ 'deleted': { $ne: true }
403
+ });
404
+ return cb(null, result);
405
+ } catch(err) {
406
+ return cb(err);
407
+ }
398
408
 
399
409
  };
400
410
 
401
- Person.statics.getById = function (id, options, cb) {
402
-
403
- const self = this;
411
+ Person.statics.getById = async function (id, options, cb) {
404
412
 
405
413
  if (!cb) { throw new Error('cb is required'); }
406
414
  if (!id) { return cb(new Error('id is required'), null); }
@@ -414,33 +422,34 @@ module.exports = function(mongoose, config) {
414
422
  if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
415
423
  }
416
424
 
417
- self
418
- .findById(id)
419
- .select('-previous')
420
- .populate('related')
421
- .populate('sources.person', 'avatarUrl name title doNotDisplay')
422
- .populate('documents')
423
- .exec().then(function(person) {
425
+ try {
426
+ let query = this
427
+ .findById(id)
428
+ .select('-previous')
429
+ .populate('related')
430
+ .populate('sources.person', 'avatarUrl name title doNotDisplay')
431
+ .populate('documents');
432
+ const person = await query;
424
433
  if (!person) return cb(null, null);
425
434
 
426
- if (!options.isWorkerProcess) { person = helpers.cleanPerson(person, options.CUSTOMER_ID); }
435
+ let cleanedPerson = person;
436
+ if (!options.isWorkerProcess) { cleanedPerson = helpers.cleanPerson(person, options.CUSTOMER_ID); }
427
437
 
428
- person.sources = _.sortBy(person.sources, function(source) {
429
- return source.interactions.calendar + source.interactions.email;
430
- }).reverse();
438
+ cleanedPerson.sources = _.sortBy(cleanedPerson.sources, function(source) {
439
+ return source.interactions.calendar + source.interactions.email;
440
+ }).reverse();
431
441
 
432
- return cb(null, person);
433
-
434
- }).catch(function(err) { cb(err); });
442
+ return cb(null, cleanedPerson);
443
+ } catch(err) {
444
+ return cb(err);
445
+ }
435
446
 
436
447
  };
437
448
 
438
- Person.statics.getByIdNoPopulate = function (id, options, cb) {
449
+ Person.statics.getByIdNoPopulate = async function (id, options, cb) {
439
450
 
440
451
  // only for worker
441
452
 
442
- const self = this;
443
-
444
453
  if (!cb) { throw new Error('cb is required'); }
445
454
  if (!id) { return cb(new Error('id is required'), null); }
446
455
  if (!mongoose.Types.ObjectId.isValid(id)) { return cb(new Error('id is not a valid ObjectId'), null); }
@@ -450,13 +459,16 @@ module.exports = function(mongoose, config) {
450
459
 
451
460
  if (!options.isWorkerProcess) { return cb(null, null); }
452
461
 
453
- self.findById(id).exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
462
+ try {
463
+ const result = await this.findById(id);
464
+ return cb(null, result);
465
+ } catch(err) {
466
+ return cb(err);
467
+ }
454
468
 
455
469
  };
456
470
 
457
- Person.statics.getByIds = function (ids, options, cb) {
458
-
459
- const self = this;
471
+ Person.statics.getByIds = async function (ids, options, cb) {
460
472
 
461
473
  if (!cb) { throw new Error('cb is required'); }
462
474
  if (!ids) { return cb(new Error('ids is required'), null); }
@@ -464,33 +476,36 @@ module.exports = function(mongoose, config) {
464
476
  if (!options.CUSTOMER_ID) { return cb(new Error('options.CUSTOMER_ID is required'), null); }
465
477
  if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
466
478
 
467
- self
468
- .find({ '_id': { $in : ids }, 'deleted': {$ne: true} })
469
- .exec().then(function(people) {
479
+ try {
480
+ const people = await this
481
+ .find({ '_id': { $in : ids }, 'deleted': {$ne: true} });
470
482
  if (!people) { return cb(null, []); }
471
483
 
472
- people = _.map(people, function(person) {
473
- return helpers.cleanPerson(person, options.CUSTOMER_ID);
474
- });
475
-
476
- return cb(null, people);
484
+ const cleaned = _.map(people, function(person) {
485
+ return helpers.cleanPerson(person, options.CUSTOMER_ID);
486
+ });
477
487
 
478
- }).catch(function(err) { cb(err); });
488
+ return cb(null, cleaned);
489
+ } catch(err) {
490
+ return cb(err);
491
+ }
479
492
  };
480
493
 
481
- Person.statics.getList = function getList(cb) {
494
+ Person.statics.getList = async function getList(cb) {
482
495
 
483
- var self = this;
484
-
485
- self
486
- .find({'deleted': {$ne: true}})
487
- .select('name contact.email')
488
- .lean()
489
- .exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
496
+ try {
497
+ const result = await this
498
+ .find({'deleted': {$ne: true}})
499
+ .select('name contact.email')
500
+ .lean();
501
+ return cb(null, result);
502
+ } catch(err) {
503
+ return cb(err);
504
+ }
490
505
 
491
506
  };
492
507
 
493
- Person.statics.getNotes = function getNotes(personId, options, cb) {
508
+ Person.statics.getNotes = async function getNotes(personId, options, cb) {
494
509
 
495
510
  if (!cb) { throw new Error('cb is required'); }
496
511
  if (!personId) { return cb(new Error('personId is required'), null); }
@@ -499,22 +514,26 @@ module.exports = function(mongoose, config) {
499
514
  if (!options.CUSTOMER_ID) { return cb(new Error('options.CUSTOMER_ID is required'), null); }
500
515
  if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
501
516
 
502
- const self = this;
503
- let query = self.findById(personId);
517
+ try {
518
+ let query = this.findById(personId);
504
519
 
505
- query.select('notes');
506
- query.populate({
507
- path: 'notes',
508
- match: { customer: options.CUSTOMER_ID },
509
- populate: { path: 'createdBy', select: 'name avatarUrl title' }
510
- });
511
- query.sort({'createdOn':-1});
520
+ query.select('notes');
521
+ query.populate({
522
+ path: 'notes',
523
+ match: { customer: options.CUSTOMER_ID },
524
+ populate: { path: 'createdBy', select: 'name avatarUrl title' }
525
+ });
526
+ query.sort({'createdOn':-1});
512
527
 
513
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
528
+ const result = await query;
529
+ return cb(null, result);
530
+ } catch(err) {
531
+ return cb(err);
532
+ }
514
533
 
515
534
  };
516
535
 
517
- Person.statics.getSources = function (id, options, cb) {
536
+ Person.statics.getSources = async function (id, options, cb) {
518
537
 
519
538
  if (!cb) { throw new Error('cb is required'); }
520
539
  if (!id) { return cb(new Error('id is required'), null); }
@@ -523,55 +542,53 @@ module.exports = function(mongoose, config) {
523
542
  if (!options.CUSTOMER_ID) { return cb(new Error('options.CUSTOMER_ID is required'), null); }
524
543
  if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
525
544
 
526
- const self = this;
527
-
528
- // get the person
529
- let query = self.findById(id);
545
+ try {
546
+ // get the person
547
+ let query = this.findById(id);
530
548
 
531
- query.populate('sources.person');
549
+ query.populate('sources.person');
532
550
 
533
- query.exec().then(function(person) {
551
+ const person = await query;
534
552
  if (!person) { return cb(null, null); }
535
553
 
536
554
  // get the customer's org
537
- Organization.findById(options.CUSTOMER_ID).then(function(customer) {
538
- if (!customer) { return cb(null, null); }
555
+ const customer = await Organization.findById(options.CUSTOMER_ID);
556
+ if (!customer) { return cb(null, null); }
539
557
 
540
- let current = [];
541
- let past = [];
542
- let all = [];
558
+ let current = [];
559
+ let past = [];
560
+ let all = [];
543
561
 
544
- // look at each person at customer
545
- // if person is a source, add to list(s)
546
- _.each(customer.people, function(p) {
562
+ // look at each person at customer
563
+ // if person is a source, add to list(s)
564
+ _.each(customer.people, function(p) {
547
565
 
548
- let source = _.find(person.sources, function(ps) {
549
- if (!ps || !ps.person || !ps.person._id) { return false; }
550
- return ps.person._id.toString() === p.person.toString();
551
- });
566
+ let source = _.find(person.sources, function(ps) {
567
+ if (!ps || !ps.person || !ps.person._id) { return false; }
568
+ return ps.person._id.toString() === p.person.toString();
569
+ });
552
570
 
553
- if (!source) { return; }
571
+ if (!source) { return; }
554
572
 
555
- if (p.current) { current.push(source); }
556
- if (!p.current) { past.push(source); }
573
+ if (p.current) { current.push(source); }
574
+ if (!p.current) { past.push(source); }
557
575
 
558
- all.push(source);
576
+ all.push(source);
559
577
 
560
- });
561
-
562
- // return categorized sources
563
- return cb(null, {
564
- all: all,
565
- current: current,
566
- past: past
567
- });
568
-
569
- }).catch(function(err) { cb(err); });
578
+ });
570
579
 
571
- }).catch(function(err) { cb(err); });
580
+ // return categorized sources
581
+ return cb(null, {
582
+ all: all,
583
+ current: current,
584
+ past: past
585
+ });
586
+ } catch(err) {
587
+ return cb(err);
588
+ }
572
589
  };
573
590
 
574
- Person.statics.modify = function(filter, update, cb) {
591
+ Person.statics.modify = async function(filter, update, cb) {
575
592
 
576
593
  // VERY IMPORTANT NOTE
577
594
  // findByIdAndUpdate and findOneAndUpdate do not trigger pre-save hook so that code will not run here
@@ -581,17 +598,20 @@ module.exports = function(mongoose, config) {
581
598
  if (!filter) { return cb(new Error('filter is required'), null); }
582
599
  if (!update) { return cb(new Error('update is required'), null); }
583
600
 
584
- var self = this;
585
-
586
- // https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
587
- // options runValidators defaults false which is ok since we have upsert false
588
- // new returns the updated document
601
+ try {
602
+ // https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
603
+ // options runValidators defaults false which is ok since we have upsert false
604
+ // new returns the updated document
589
605
 
590
- self.findOneAndUpdate(filter, update, { upsert: false, new: true }).then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
606
+ const result = await this.findOneAndUpdate(filter, update, { upsert: false, new: true });
607
+ return cb(null, result);
608
+ } catch(err) {
609
+ return cb(err);
610
+ }
591
611
 
592
612
  };
593
613
 
594
- Person.statics.modifyById = function(id, update, cb) {
614
+ Person.statics.modifyById = async function(id, update, cb) {
595
615
 
596
616
  // VERY IMPORTANT NOTE
597
617
  // findByIdAndUpdate and findOneAndUpdate do not trigger pre-save hook so that code will not run here
@@ -602,13 +622,16 @@ module.exports = function(mongoose, config) {
602
622
  if (!mongoose.Types.ObjectId.isValid(id)) { return cb(new Error('id is not a valid ObjectId'), null); }
603
623
  if (!update) { return cb(new Error('update is required'), null); }
604
624
 
605
- var self = this;
625
+ try {
626
+ // https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
627
+ // options runValidators defaults false which is ok since we have upsert false
628
+ // new returns the updated document
606
629
 
607
- // https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
608
- // options runValidators defaults false which is ok since we have upsert false
609
- // new returns the updated document
610
-
611
- self.findByIdAndUpdate(id, update, { upsert: false, new: true }).then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
630
+ const result = await this.findByIdAndUpdate(id, update, { upsert: false, new: true });
631
+ return cb(null, result);
632
+ } catch(err) {
633
+ return cb(err);
634
+ }
612
635
 
613
636
  };
614
637
 
@@ -631,13 +654,12 @@ module.exports = function(mongoose, config) {
631
654
  // both fuzzy and non are case-insensitive
632
655
  // fuzzy only applies to name, not email or phone
633
656
  // related people are removed from top-level results
634
- Person.statics.search = function (data, options, cb) {
657
+ Person.statics.search = async function (data, options, cb) {
635
658
 
636
659
  // validate input
637
660
  if (!cb) throw new Error('cb is required');
638
661
  if (!data || (!data.emails && !data.phones && !data.name)) return cb(null, []);
639
662
 
640
- var self = this;
641
663
  var query;
642
664
 
643
665
  var buildQuery = function buildQuery(data, options) {
@@ -927,28 +949,28 @@ module.exports = function(mongoose, config) {
927
949
 
928
950
  query = buildQuery(data, options);
929
951
 
930
- self
931
- .find(query)
932
- .select('avatarUrl name title related')
933
- .exec().then(function(people) {
952
+ try {
953
+ const people = await this
954
+ .find(query)
955
+ .select('avatarUrl name title related');
934
956
  if (!people) return cb(null, null);
935
- else {
936
-
937
- if (options.removeRelatedFromTopLevel) people = removeRelatedFromTopLevel(people);
957
+ else {
938
958
 
939
- people = _.sortBy(people, function(person) { return person.name.full; });
959
+ let result = people;
960
+ if (options.removeRelatedFromTopLevel) result = removeRelatedFromTopLevel(people);
940
961
 
941
- return cb(null, people);
962
+ result = _.sortBy(result, function(person) { return person.name.full; });
942
963
 
943
- }
964
+ return cb(null, result);
944
965
 
945
- }).catch(function(err) { cb(err); });
966
+ }
967
+ } catch(err) {
968
+ return cb(err);
969
+ }
946
970
 
947
971
  };
948
972
 
949
- Person.statics.search2 = function (query, options, cb) {
950
-
951
- const self = this;
973
+ Person.statics.search2 = async function (query, options, cb) {
952
974
 
953
975
  const defaultOptions = {
954
976
  limit: 10,
@@ -959,91 +981,99 @@ module.exports = function(mongoose, config) {
959
981
  // combine provided and default options
960
982
  options = _.defaults(options || {}, defaultOptions);
961
983
 
962
- self.aggregate([
963
- {
964
- "$search": {
965
- index: 'default',
966
- compound: {
967
- should: [
968
- {
969
- text: {
970
- query: query,
971
- path: [
972
- 'name.first',
973
- 'name.last',
974
- 'fullName',
975
- 'aliases',
976
- 'contact.email.email'
977
- ]
978
- }
979
- },
980
- {
981
- autocomplete: {
982
- query: query,
983
- path: 'name.first'
984
- }
985
- },
986
- {
987
- autocomplete: {
988
- query: query,
989
- path: 'name.last'
984
+ try {
985
+ const people = await this.aggregate([
986
+ {
987
+ "$search": {
988
+ index: 'default',
989
+ compound: {
990
+ should: [
991
+ {
992
+ text: {
993
+ query: query,
994
+ path: [
995
+ 'name.first',
996
+ 'name.last',
997
+ 'fullName',
998
+ 'aliases',
999
+ 'contact.email.email'
1000
+ ]
1001
+ }
1002
+ },
1003
+ {
1004
+ autocomplete: {
1005
+ query: query,
1006
+ path: 'name.first'
1007
+ }
1008
+ },
1009
+ {
1010
+ autocomplete: {
1011
+ query: query,
1012
+ path: 'name.last'
1013
+ }
1014
+ },
1015
+ {
1016
+ autocomplete: {
1017
+ query: query,
1018
+ path: 'fullName'
1019
+ }
990
1020
  }
991
- },
992
- {
993
- autocomplete: {
994
- query: query,
995
- path: 'fullName'
1021
+ ],
1022
+ must: [
1023
+ {
1024
+ equals: {
1025
+ path: 'deleted',
1026
+ value: false
1027
+ }
996
1028
  }
997
- }
998
- ],
999
- must: [
1000
- {
1001
- equals: {
1002
- path: 'deleted',
1003
- value: false
1004
- }
1005
- }
1006
- ]
1029
+ ]
1030
+ }
1031
+ }
1032
+ }, {
1033
+ $limit: options.limit,
1034
+ }, {
1035
+ "$project": {
1036
+ "_id": 1,
1037
+ "fullName": 1,
1038
+ "name": 1,
1039
+ "avatarUrl": 1,
1040
+ "sources": 1,
1041
+ "emails": "$contact.email",
1042
+ "score": { "$meta": "searchScore" }
1007
1043
  }
1008
1044
  }
1009
- }, {
1010
- $limit: options.limit,
1011
- }, {
1012
- "$project": {
1013
- "_id": 1,
1014
- "fullName": 1,
1015
- "name": 1,
1016
- "avatarUrl": 1,
1017
- "sources": 1,
1018
- "emails": "$contact.email",
1019
- "score": { "$meta": "searchScore" }
1020
- }
1021
- }
1022
- ]).exec().then(function(people) {people = _.map(people, function(person) {
1045
+ ]);
1046
+
1047
+ const cleaned = _.map(people, function(person) {
1023
1048
  return helpers.cleanPerson(person, options.CUSTOMER_ID);
1024
1049
  });
1025
1050
 
1026
- return cb(null, people);
1027
-
1028
- }).catch(function(err) { cb(err); });
1051
+ return cb(null, cleaned);
1052
+ } catch(err) {
1053
+ return cb(err);
1054
+ }
1029
1055
 
1030
1056
  };
1031
1057
 
1032
- Person.statics.searchByEmail = function(email, cb) {
1058
+ Person.statics.searchByEmail = async function(email, cb) {
1033
1059
 
1034
1060
  if (!cb) throw new Error('cb is required');
1035
1061
  if (!email) return cb(new Error('email is required to search by email'), null);
1036
1062
 
1037
1063
  email = email.toString().toLowerCase();
1038
1064
 
1039
- this
1040
- .find({ 'contact.email.email': email, 'deleted': {$ne: true} })
1041
- .select('name avatarUrl')
1042
- .exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
1065
+ try {
1066
+ const result = await this
1067
+ .find({ 'contact.email.email': email, 'deleted': {$ne: true} })
1068
+ .select('name avatarUrl');
1069
+ return cb(null, result);
1070
+ } catch(err) {
1071
+ return cb(err);
1072
+ }
1043
1073
 
1044
1074
  };
1045
1075
 
1046
- Person.statics.upsert = function(currentPerson, username, cb) {
1076
+ Person.statics.upsert = async function(currentPerson, username, cb) {
1047
1077
 
1048
1078
  if (!currentPerson) { return cb(new Error('Current Person is required'), null); }
1049
1079
  if (!username) { return cb(new Error('Username is required'), null); }
@@ -1053,7 +1083,12 @@ module.exports = function(mongoose, config) {
1053
1083
  on: new Date()
1054
1084
  };
1055
1085
 
1056
- currentPerson.save().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
1086
+ try {
1087
+ const result = await currentPerson.save();
1088
+ return cb(null, result);
1089
+ } catch(err) {
1090
+ return cb(err);
1091
+ }
1057
1092
 
1058
1093
  };
1059
1094