@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.
@@ -8,7 +8,6 @@ module.exports = function(mongoose, config) {
8
8
  Note = mongoose.model('Note'),
9
9
  Organization = mongoose.model('Organization'),
10
10
  _ = require('underscore'),
11
- async = require('async'),
12
11
  helpers = require('../helpers');
13
12
 
14
13
  let LimitedPartner = new Schema({
@@ -331,30 +330,23 @@ module.exports = function(mongoose, config) {
331
330
  Note.createForModel(this, lpId, note, cb);
332
331
  };
333
332
 
334
- LimitedPartner.statics.deleteNote = function(noteId, customerId, cb) {
333
+ LimitedPartner.statics.deleteNote = async function(noteId, customerId, cb) {
335
334
 
336
335
  // Delete the note itself along with any references
337
336
 
338
- const self = this;
339
-
340
- let removeReferences = function removeReferences(callback) {
341
- self.updateOne({}, {
337
+ try {
338
+ await Note.delete(noteId, customerId);
339
+ await this.updateOne({}, {
342
340
  $pull: { 'notes' : [noteId] }
343
- }).then(function(result) { callback(null, result); }).catch(function(err) { callback(err); });
344
- };
345
-
346
- async.series([
347
- Note.delete.bind(Note, noteId, customerId),
348
- removeReferences
349
- ], function(err, results) {
350
- return cb(err, null);
351
- });
341
+ });
342
+ return cb(null, null);
343
+ } catch(err) {
344
+ return cb(err);
345
+ }
352
346
 
353
347
  };
354
348
 
355
- LimitedPartner.statics.getById = function(id, options, cb) {
356
-
357
- const self = this;
349
+ LimitedPartner.statics.getById = async function(id, options, cb) {
358
350
 
359
351
  if (!cb) { throw new Error('cb is required'); }
360
352
  if (!id) { return cb(new Error('id is required'), null); }
@@ -365,28 +357,29 @@ module.exports = function(mongoose, config) {
365
357
 
366
358
  options = helpers.getDefaultOptions(options);
367
359
 
368
- let query;
360
+ try {
361
+ let query;
369
362
 
370
- if (options.isWorkerProcess) {
371
- query = self.findById(id, getReadFilterKeys(options.role))
372
- }
373
- else {
374
- query = self.findOne({ '_id': id, customer: options.CUSTOMER_ID }, getReadFilterKeys(options.role));
375
- }
363
+ if (options.isWorkerProcess) {
364
+ query = this.findById(id, getReadFilterKeys(options.role))
365
+ }
366
+ else {
367
+ query = this.findOne({ '_id': id, customer: options.CUSTOMER_ID }, getReadFilterKeys(options.role));
368
+ }
376
369
 
377
- query.populate('transactions.fund');
378
- query.populate({
379
- path: 'people',
380
- populate: { path: 'sources.person', select: 'name avatarUrl title' }
381
- });
382
- query.populate({
383
- path: 'notes',
384
- match: { customer: options.CUSTOMER_ID },
385
- populate: { path: 'createdBy', select: 'name avatarUrl' }
386
- });
387
- query.populate('documents');
370
+ query.populate('transactions.fund');
371
+ query.populate({
372
+ path: 'people',
373
+ populate: { path: 'sources.person', select: 'name avatarUrl title' }
374
+ });
375
+ query.populate({
376
+ path: 'notes',
377
+ match: { customer: options.CUSTOMER_ID },
378
+ populate: { path: 'createdBy', select: 'name avatarUrl' }
379
+ });
380
+ query.populate('documents');
388
381
 
389
- query.exec().then(function(lp) {
382
+ const lp = await query;
390
383
  if (!lp) { return cb(null, null); }
391
384
 
392
385
  lp.people = _.map(lp.people, function(person) {
@@ -409,13 +402,11 @@ module.exports = function(mongoose, config) {
409
402
 
410
403
  return cb(null, lp);
411
404
 
412
- }).catch(function(err) { cb(err); });
405
+ } catch(err) { return cb(err); }
413
406
 
414
407
  };
415
408
 
416
- LimitedPartner.statics.getByName = function(lpName, options, cb) {
417
-
418
- const self = this;
409
+ LimitedPartner.statics.getByName = async function(lpName, options, cb) {
419
410
 
420
411
  if (!cb) { throw new Error('cb is required'); }
421
412
  if (!options) { return cb(new Error('options is required'), null); }
@@ -424,24 +415,20 @@ module.exports = function(mongoose, config) {
424
415
 
425
416
  options = helpers.getDefaultOptions(options);
426
417
 
427
- let query;
428
-
429
418
  if (!options.isWorkerProcess) {
430
419
  return cb(null, []);
431
420
  }
432
421
 
433
- else {
434
- query = self.findOne({'name': lpName, customer: options.CUSTOMER_ID }, getReadFilterKeys(options.role));
435
- }
436
-
437
- query.populate('transactions.fund');
438
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
422
+ try {
423
+ let query = this.findOne({'name': lpName, customer: options.CUSTOMER_ID }, getReadFilterKeys(options.role));
424
+ query.populate('transactions.fund');
425
+ const result = await query;
426
+ return cb(null, result);
427
+ } catch(err) { return cb(err); }
439
428
 
440
429
  };
441
430
 
442
- LimitedPartner.statics.getByPersonId = function(personId, options, cb) {
443
-
444
- const self = this;
431
+ LimitedPartner.statics.getByPersonId = async function(personId, options, cb) {
445
432
 
446
433
  if (!cb) { throw new Error('cb is required'); }
447
434
  if (!personId) { return cb(new Error('personId is required'), null); }
@@ -452,30 +439,30 @@ module.exports = function(mongoose, config) {
452
439
  if (!options.CUSTOMER_ID) { return cb(new Error('options.CUSTOMER_ID is required'), null); }
453
440
  if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
454
441
  }
455
-
442
+
456
443
  options = helpers.getDefaultOptions(options);
457
444
 
458
- let query;
445
+ try {
446
+ let query;
459
447
 
460
- if (options.isWorkerProcess) {
461
- query = self.find({'people': personId}, getReadFilterKeys(options.role));
462
- }
463
- else {
464
- query = self.find({'people': personId, customer: options.CUSTOMER_ID }, getReadFilterKeys(options.role));
465
- }
448
+ if (options.isWorkerProcess) {
449
+ query = this.find({'people': personId}, getReadFilterKeys(options.role));
450
+ }
451
+ else {
452
+ query = this.find({'people': personId, customer: options.CUSTOMER_ID }, getReadFilterKeys(options.role));
453
+ }
466
454
 
467
- query.populate('transactions.fund');
468
- query.populate('people', 'name');
455
+ query.populate('transactions.fund');
456
+ query.populate('people', 'name');
469
457
 
470
- query.exec().then(function(lps) {lps = _.sortBy(lps, function(lp) { return lp.name ? lp.name.toLowerCase() : ''; });
458
+ let lps = await query;
459
+ lps = _.sortBy(lps, function(lp) { return lp.name ? lp.name.toLowerCase() : ''; });
471
460
  return cb(null, lps);
472
- }).catch(function(err) { cb(err); });
461
+ } catch(err) { return cb(err); }
473
462
 
474
463
  };
475
464
 
476
- LimitedPartner.statics.getByPersonIds = function(personIds, options, cb) {
477
-
478
- const self = this;
465
+ LimitedPartner.statics.getByPersonIds = async function(personIds, options, cb) {
479
466
 
480
467
  if (!cb) { throw new Error('cb is required'); }
481
468
  if (!personIds) { return cb(new Error('personIds is required'), null); }
@@ -488,24 +475,24 @@ module.exports = function(mongoose, config) {
488
475
 
489
476
  options = helpers.getDefaultOptions(options);
490
477
 
491
- let query;
478
+ try {
479
+ let query;
492
480
 
493
- if (options.isWorkerProcess) {
494
- query = self.find({'people': { $in : personIds }}, getReadFilterKeys(options.role));
495
- }
496
- else {
497
- query = self.find({'people': { $in : personIds }, customer: options.CUSTOMER_ID }, getReadFilterKeys(options.role));
498
- }
481
+ if (options.isWorkerProcess) {
482
+ query = this.find({'people': { $in : personIds }}, getReadFilterKeys(options.role));
483
+ }
484
+ else {
485
+ query = this.find({'people': { $in : personIds }, customer: options.CUSTOMER_ID }, getReadFilterKeys(options.role));
486
+ }
499
487
 
500
- query.exec().then(function(lps) {lps = _.sortBy(lps, function(lp) { return lp.name ? lp.name.toLowerCase() : ''; });
488
+ let lps = await query;
489
+ lps = _.sortBy(lps, function(lp) { return lp.name ? lp.name.toLowerCase() : ''; });
501
490
  return cb(null, lps);
502
- }).catch(function(err) { cb(err); });
491
+ } catch(err) { return cb(err); }
503
492
 
504
493
  };
505
494
 
506
- LimitedPartner.statics.getNotes = function getNotes(lpid, options, cb) {
507
-
508
- const self = this;
495
+ LimitedPartner.statics.getNotes = async function getNotes(lpid, options, cb) {
509
496
 
510
497
  if (!cb) { throw new Error('cb is required'); }
511
498
  if (!lpid) { return cb(new Error('lpid is required'), null); }
@@ -514,23 +501,24 @@ module.exports = function(mongoose, config) {
514
501
  if (!options.CUSTOMER_ID) { return cb(new Error('options.CUSTOMER_ID is required'), null); }
515
502
  if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
516
503
 
517
- let query = self.findOne({ '_id': lpid, customer: options.CUSTOMER_ID });
504
+ try {
505
+ let query = this.findOne({ '_id': lpid, customer: options.CUSTOMER_ID });
518
506
 
519
- query.select('notes people');
520
- query.populate({
521
- path: 'notes',
522
- match: { customer: options.CUSTOMER_ID },
523
- options: { sort: { createdOn: -1 } },
524
- populate: { path: 'createdBy', select: 'name avatarUrl title' }
525
- });
507
+ query.select('notes people');
508
+ query.populate({
509
+ path: 'notes',
510
+ match: { customer: options.CUSTOMER_ID },
511
+ options: { sort: { createdOn: -1 } },
512
+ populate: { path: 'createdBy', select: 'name avatarUrl title' }
513
+ });
526
514
 
527
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
515
+ const result = await query;
516
+ return cb(null, result);
517
+ } catch(err) { return cb(err); }
528
518
 
529
519
  };
530
520
 
531
- LimitedPartner.statics.getSources = function(lpid, options, cb) {
532
-
533
- const self = this;
521
+ LimitedPartner.statics.getSources = async function(lpid, options, cb) {
534
522
 
535
523
  if (!cb) { throw new Error('cb is required'); }
536
524
  if (!lpid) { return cb(new Error('lpid is required'), null); }
@@ -539,54 +527,53 @@ module.exports = function(mongoose, config) {
539
527
  if (!options.CUSTOMER_ID) { return cb(new Error('options.CUSTOMER_ID is required'), null); }
540
528
  if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
541
529
 
542
- let query = self.findById(lpid);
543
-
544
- query.populate({
545
- path: 'people',
546
- select: 'name avatarUrl title doNotDisplay sources',
547
- populate: { path: 'sources.person', select: 'name avatarUrl title doNotDisplay' }
548
- });
530
+ try {
531
+ let query = this.findById(lpid);
549
532
 
550
- query.exec().then(function(lp) {if (!lp) { return cb(null, null); }
533
+ query.populate({
534
+ path: 'people',
535
+ select: 'name avatarUrl title doNotDisplay sources',
536
+ populate: { path: 'sources.person', select: 'name avatarUrl title doNotDisplay' }
537
+ });
551
538
 
552
- Organization.findById(options.CUSTOMER_ID).then(function(customer) {if (!customer) { return cb(null, null); }
539
+ const lp = await query;
540
+ if (!lp) { return cb(null, null); }
553
541
 
554
- let current = [];
555
- let past = [];
556
- let all = [];
557
- let lpSources = helpers.getPeopleSources(lp.people);
542
+ const customer = await Organization.findById(options.CUSTOMER_ID);
543
+ if (!customer) { return cb(null, null); }
558
544
 
559
- _.each(customer.people, function(p) {
545
+ let current = [];
546
+ let past = [];
547
+ let all = [];
548
+ let lpSources = helpers.getPeopleSources(lp.people);
560
549
 
561
- let source = _.find(lpSources, function(ls) {
562
- if (!ls || !ls.person || !ls.person._id) return false;
563
- return ls.person._id.toString() === p.person.toString();
564
- });
550
+ _.each(customer.people, function(p) {
565
551
 
566
- if (!source) { return; }
552
+ let source = _.find(lpSources, function(ls) {
553
+ if (!ls || !ls.person || !ls.person._id) return false;
554
+ return ls.person._id.toString() === p.person.toString();
555
+ });
567
556
 
568
- if (p.current) { current.push(source); }
569
- if (!p.current) { past.push(source); }
570
- all.push(source);
557
+ if (!source) { return; }
571
558
 
572
- });
559
+ if (p.current) { current.push(source); }
560
+ if (!p.current) { past.push(source); }
561
+ all.push(source);
573
562
 
574
- // todo - fix sorting
575
- return cb(null, {
576
- all: all, //helpers.sortPeopleSources(all, options.CUSTOMER_ID),
577
- current: current, //helpers.sortPeopleSources(current, options.CUSTOMER_ID),
578
- past: past //helpers.sortPeopleSources(past, options.CUSTOMER_ID)
579
- });
563
+ });
580
564
 
581
- }).catch(function(err) { cb(err); });
565
+ // todo - fix sorting
566
+ return cb(null, {
567
+ all: all, //helpers.sortPeopleSources(all, options.CUSTOMER_ID),
568
+ current: current, //helpers.sortPeopleSources(current, options.CUSTOMER_ID),
569
+ past: past //helpers.sortPeopleSources(past, options.CUSTOMER_ID)
570
+ });
582
571
 
583
- }).catch(function(err) { cb(err); });
572
+ } catch(err) { return cb(err); }
584
573
 
585
574
  };
586
575
 
587
- LimitedPartner.statics.list = function (customerId, options, cb) {
588
-
589
- const self = this;
576
+ LimitedPartner.statics.list = async function (customerId, options, cb) {
590
577
 
591
578
  if (!cb) { throw new Error('cb is required'); }
592
579
  if (!customerId) { return cb(new Error('customerId is required'), null); }
@@ -595,19 +582,23 @@ module.exports = function(mongoose, config) {
595
582
 
596
583
  options = helpers.getDefaultOptions(options);
597
584
 
598
- let query;
585
+ try {
586
+ let query;
599
587
 
600
- if (options.isWorkerProcess) {
601
- query = self.find({}, getReadFilterKeys(options.role));
602
- }
603
- else {
604
- query = self.find({ 'transactions.fund': { $in : options.fundids }, customer: customerId }, getReadFilterKeys(options.role));
605
- }
588
+ if (options.isWorkerProcess) {
589
+ query = this.find({}, getReadFilterKeys(options.role));
590
+ }
591
+ else {
592
+ query = this.find({ 'transactions.fund': { $in : options.fundids }, customer: customerId }, getReadFilterKeys(options.role));
593
+ }
594
+
595
+ query.populate('transactions.fund');
596
+ query.populate('people');
597
+ query.sort({ 'name': -1 });
606
598
 
607
- query.populate('transactions.fund');
608
- query.populate('people');
609
- query.sort({ 'name': -1 });
610
- query.exec().then(function(result) {if (options.fundids) {
599
+ const result = await query;
600
+
601
+ if (options.fundids) {
611
602
  _.each(result, function(lp) {
612
603
  lp.transactions = _.filter(lp.transactions, function(transaction) {
613
604
  return _.find(options.fundids, function(f) {
@@ -619,13 +610,11 @@ module.exports = function(mongoose, config) {
619
610
 
620
611
  return cb(null, result);
621
612
 
622
- }).catch(function(err) { cb(err); });
613
+ } catch(err) { return cb(err); }
623
614
 
624
615
  };
625
616
 
626
- LimitedPartner.statics.listByFund = function (fundId, options, cb) {
627
-
628
- const self = this;
617
+ LimitedPartner.statics.listByFund = async function (fundId, options, cb) {
629
618
 
630
619
  if (!cb) { throw new Error('cb is required'); }
631
620
  if (!fundId) { return cb(new Error('fundId is required'), null); }
@@ -636,15 +625,19 @@ module.exports = function(mongoose, config) {
636
625
 
637
626
  options = helpers.getDefaultOptions(options);
638
627
 
639
- let query = self.find({ 'transactions.fund': fundId, customer: options.CUSTOMER_ID }, getReadFilterKeys(options.role));
628
+ try {
629
+ let query = this.find({ 'transactions.fund': fundId, customer: options.CUSTOMER_ID }, getReadFilterKeys(options.role));
630
+
631
+ query.populate('transactions.fund');
632
+ query.sort({ 'name': -1 });
640
633
 
641
- query.populate('transactions.fund');
642
- query.sort({ 'name': -1 });
643
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
634
+ const result = await query;
635
+ return cb(null, result);
636
+ } catch(err) { return cb(err); }
644
637
 
645
638
  };
646
639
 
647
- LimitedPartner.statics.modify = function(filter, update, cb) {
640
+ LimitedPartner.statics.modify = async function(filter, update, cb) {
648
641
 
649
642
  // VERY IMPORTANT NOTE
650
643
  // findByIdAndUpdate and findOneAndUpdate do not trigger pre-save hook so that code will not run here
@@ -654,17 +647,18 @@ module.exports = function(mongoose, config) {
654
647
  if (!filter) { return cb(new Error('filter is required'), null); }
655
648
  if (!update) { return cb(new Error('update is required'), null); }
656
649
 
657
- var self = this;
658
-
659
650
  // https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
660
651
  // options runValidators defaults false which is ok since we have upsert false
661
652
  // new returns the updated document
662
653
 
663
- self.findOneAndUpdate(filter, update, { upsert: false, new: true }).then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
654
+ try {
655
+ const result = await this.findOneAndUpdate(filter, update, { upsert: false, new: true });
656
+ return cb(null, result);
657
+ } catch(err) { return cb(err); }
664
658
 
665
659
  };
666
660
 
667
- LimitedPartner.statics.modifyById = function(id, update, cb) {
661
+ LimitedPartner.statics.modifyById = async function(id, update, cb) {
668
662
 
669
663
  // VERY IMPORTANT NOTE
670
664
  // findByIdAndUpdate and findOneAndUpdate do not trigger pre-save hook so that code will not run here
@@ -675,73 +669,72 @@ module.exports = function(mongoose, config) {
675
669
  if (!mongoose.Types.ObjectId.isValid(id)) { return cb(new Error('id is not a valid ObjectId'), null); }
676
670
  if (!update) { return cb(new Error('update is required'), null); }
677
671
 
678
- var self = this;
679
-
680
672
  // https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
681
673
  // options runValidators defaults false which is ok since we have upsert false
682
674
  // new returns the updated document
683
675
 
684
- self.findByIdAndUpdate(id, update, { upsert: false, new: true }).then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
676
+ try {
677
+ const result = await this.findByIdAndUpdate(id, update, { upsert: false, new: true });
678
+ return cb(null, result);
679
+ } catch(err) { return cb(err); }
685
680
 
686
681
  };
687
682
 
688
- LimitedPartner.statics.removeCustomerTransactions = function removeCustomerTransactions(customerId, cb) {
689
-
690
- const self = this;
683
+ LimitedPartner.statics.removeCustomerTransactions = async function removeCustomerTransactions(customerId, cb) {
691
684
 
692
685
  if (!cb) { throw new Error('cb is required'); }
693
686
  if (!customerId) { return cb(new Error('customerId is required'), null); }
694
687
  if (!mongoose.Types.ObjectId.isValid(customerId)) { return cb(new Error('customerId is not a valid ObjectId'), null); }
695
688
 
696
- let query = self.updateMany(
697
- { 'customer': customerId },
698
- { $set: { transactions: [] } },
699
- { multi : true }
700
- );
689
+ try {
690
+ let query = this.updateMany(
691
+ { 'customer': customerId },
692
+ { $set: { transactions: [] } }
693
+ );
701
694
 
702
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
695
+ const result = await query;
696
+ return cb(null, result);
697
+ } catch(err) { return cb(err); }
703
698
 
704
699
  };
705
700
 
706
- LimitedPartner.statics.removeCustomerTags = function removeCustomerTags(customerId, cb) {
707
-
708
- const self = this;
701
+ LimitedPartner.statics.removeCustomerTags = async function removeCustomerTags(customerId, cb) {
709
702
 
710
703
  if (!cb) { throw new Error('cb is required'); }
711
704
  if (!customerId) { return cb(new Error('customerId is required'), null); }
712
705
  if (!mongoose.Types.ObjectId.isValid(customerId)) { return cb(new Error('customerId is not a valid ObjectId'), null); }
713
706
 
714
- let query = self.updateMany(
715
- { 'customer': customerId },
716
- { $set: { details: [] } },
717
- { multi : true }
718
- );
707
+ try {
708
+ let query = this.updateMany(
709
+ { 'customer': customerId },
710
+ { $set: { details: [] } }
711
+ );
719
712
 
720
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
713
+ const result = await query;
714
+ return cb(null, result);
715
+ } catch(err) { return cb(err); }
721
716
 
722
717
  };
723
718
 
724
- LimitedPartner.statics.removeCustomerPeople = function removeCustomerPeople(customerId, cb) {
725
-
726
- const self = this;
719
+ LimitedPartner.statics.removeCustomerPeople = async function removeCustomerPeople(customerId, cb) {
727
720
 
728
721
  if (!cb) { throw new Error('cb is required'); }
729
722
  if (!customerId) { return cb(new Error('customerId is required'), null); }
730
723
  if (!mongoose.Types.ObjectId.isValid(customerId)) { return cb(new Error('customerId is not a valid ObjectId'), null); }
731
724
 
732
- let query = self.updateOne(
733
- { 'customer': customerId },
734
- { $set: { people: [] } },
735
- { multi : true }
736
- );
725
+ try {
726
+ let query = this.updateMany(
727
+ { 'customer': customerId },
728
+ { $set: { people: [] } }
729
+ );
737
730
 
738
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
731
+ const result = await query;
732
+ return cb(null, result);
733
+ } catch(err) { return cb(err); }
739
734
 
740
735
  };
741
736
 
742
- LimitedPartner.statics.search = function (terms, options, cb) {
743
-
744
- const self = this;
737
+ LimitedPartner.statics.search = async function (terms, options, cb) {
745
738
 
746
739
  if (!cb) { throw new Error('cb is required'); }
747
740
  if (!terms) { return cb(new Error('terms is required'), null); }
@@ -751,26 +744,28 @@ module.exports = function(mongoose, config) {
751
744
 
752
745
  options = helpers.getDefaultOptions(options);
753
746
 
754
- let query;
755
-
756
747
  if (options.role === 'none') { return cb(null, []); }
757
748
 
758
- if (options.isWorkerProcess) {
759
- query = self.find({'name': new RegExp(terms, 'i')}, getReadFilterKeys(options.role));
760
- }
761
- else {
762
- query = self.find({ 'name': new RegExp(terms, 'i'), customer: options.CUSTOMER_ID }, getReadFilterKeys(options.role));
763
- }
749
+ try {
750
+ let query;
751
+
752
+ if (options.isWorkerProcess) {
753
+ query = this.find({'name': new RegExp(terms, 'i')}, getReadFilterKeys(options.role));
754
+ }
755
+ else {
756
+ query = this.find({ 'name': new RegExp(terms, 'i'), customer: options.CUSTOMER_ID }, getReadFilterKeys(options.role));
757
+ }
764
758
 
765
- query.populate('transactions.fund');
766
- query.sort({ 'name': -1 });
767
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
759
+ query.populate('transactions.fund');
760
+ query.sort({ 'name': -1 });
768
761
 
769
- };
762
+ const result = await query;
763
+ return cb(null, result);
764
+ } catch(err) { return cb(err); }
770
765
 
771
- LimitedPartner.statics.search2 = function search2(value, options, cb) {
766
+ };
772
767
 
773
- const self = this;
768
+ LimitedPartner.statics.search2 = async function search2(value, options, cb) {
774
769
 
775
770
  if (!cb) { throw new Error('cb is required'); }
776
771
  if (!value) { return cb(new Error('value is required'), null); }
@@ -787,51 +782,54 @@ module.exports = function(mongoose, config) {
787
782
  // combine provided and default options
788
783
  options = _.defaults(options || {}, defaultOptions);
789
784
 
790
- self.aggregate([
791
- {
792
- "$search": {
793
- index: 'default',
794
- compound: {
795
- should: [
796
- {
797
- text: {
798
- query: value,
799
- path: [
800
- 'name',
801
- 'stakeholders',
802
- 'details.value'
803
- ],
804
- fuzzy: {
805
- maxEdits: options.maxEdits,
806
- maxExpansions: options.maxExpansions
807
- },
808
- score: { boost: { value: 3 } }
785
+ try {
786
+ const result = await this.aggregate([
787
+ {
788
+ "$search": {
789
+ index: 'default',
790
+ compound: {
791
+ should: [
792
+ {
793
+ text: {
794
+ query: value,
795
+ path: [
796
+ 'name',
797
+ 'stakeholders',
798
+ 'details.value'
799
+ ],
800
+ fuzzy: {
801
+ maxEdits: options.maxEdits,
802
+ maxExpansions: options.maxExpansions
803
+ },
804
+ score: { boost: { value: 3 } }
805
+ }
809
806
  }
810
- }
811
- ],
812
- must: [
813
- {
814
- equals: {
815
- path: 'customer',
816
- value: new mongoose.Types.ObjectId(options.CUSTOMER_ID)
807
+ ],
808
+ must: [
809
+ {
810
+ equals: {
811
+ path: 'customer',
812
+ value: new mongoose.Types.ObjectId(options.CUSTOMER_ID)
813
+ }
817
814
  }
818
- }
819
- ],
820
- minimumShouldMatch: 1
815
+ ],
816
+ minimumShouldMatch: 1
817
+ }
818
+ }
819
+ },
820
+ {
821
+ $limit: options.limit,
822
+ },
823
+ {
824
+ $project: {
825
+ name: 1,
826
+ transactions: 1,
827
+ score: { $meta: "searchScore" }
821
828
  }
822
829
  }
823
- },
824
- {
825
- $limit: options.limit,
826
- },
827
- {
828
- $project: {
829
- name: 1,
830
- transactions: 1,
831
- score: { $meta: "searchScore" }
832
- }
833
- }
834
- ]).exec().then(function(result) {_.each(result, function(item) {
830
+ ]);
831
+
832
+ _.each(result, function(item) {
835
833
  item.commitment = calculateTotalCommitment(item.transactions);
836
834
  item.contribution = calculateTotalContribution(item.transactions);
837
835
  delete item.transactions;
@@ -839,12 +837,11 @@ module.exports = function(mongoose, config) {
839
837
 
840
838
  return cb(null, result);
841
839
 
842
-
843
- }).catch(function(err) { cb(err); });
840
+ } catch(err) { return cb(err); }
844
841
 
845
842
  };
846
843
 
847
- LimitedPartner.statics.upsert = function(lp, username, options, cb) {
844
+ LimitedPartner.statics.upsert = async function(lp, username, options, cb) {
848
845
 
849
846
  if (!cb) { throw new Error('cb is required'); }
850
847
  if (!lp) { return cb(new Error('lp is required'), null); }
@@ -860,7 +857,10 @@ module.exports = function(mongoose, config) {
860
857
  lp.markModified('details');
861
858
  lp.markModified('customFields');
862
859
 
863
- lp.save().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
860
+ try {
861
+ const result = await lp.save();
862
+ return cb(null, result);
863
+ } catch(err) { return cb(err); }
864
864
 
865
865
  };
866
866