@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/Round.js CHANGED
@@ -258,12 +258,10 @@ module.exports = function(mongoose, config) {
258
258
  // Statics operate on the entire collection
259
259
  //////////////////////////////////////////////////////\
260
260
 
261
- Round.statics.getByOrg = function getByOrg(orgId, options, cb) {
261
+ Round.statics.getByOrg = async function getByOrg(orgId, options, cb) {
262
262
 
263
263
  // Gets all the rounds an org has raised
264
264
 
265
- const self = this;
266
-
267
265
  if (!cb) { throw new Error('cb is required'); }
268
266
  if (!orgId) { throw new Error('orgId is required'); }
269
267
  if (!mongoose.Types.ObjectId.isValid(orgId)) { return cb(new Error('orgId is not a valid ObjectId'), null); }
@@ -276,25 +274,27 @@ module.exports = function(mongoose, config) {
276
274
  if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
277
275
  }
278
276
 
279
- let query = self.find({
280
- 'deleted': { $ne: true },
281
- 'organization': orgId
282
- });
277
+ try {
278
+ let query = this.find({
279
+ 'deleted': { $ne: true },
280
+ 'organization': orgId
281
+ });
283
282
 
284
- query.populate('organization', 'name logoUrl website websiteAliases');
285
- query.populate('vehicles.fund', 'name shortName');
283
+ query.populate('organization', 'name logoUrl website websiteAliases');
284
+ query.populate('vehicles.fund', 'name shortName');
286
285
 
287
- if (options.isWorkerProcess) {
288
- query.populate('vehicles.investments');
289
- }
290
- else {
291
- query.populate({
292
- path: 'vehicles.investments',
293
- match: { customer: options.CUSTOMER_ID }
294
- });
295
- }
286
+ if (options.isWorkerProcess) {
287
+ query.populate('vehicles.investments');
288
+ }
289
+ else {
290
+ query.populate({
291
+ path: 'vehicles.investments',
292
+ match: { customer: options.CUSTOMER_ID }
293
+ });
294
+ }
296
295
 
297
- query.exec().then(function(rounds) {if (!rounds || rounds.length === 0) { return cb(null, []); }
296
+ let rounds = await query;
297
+ if (!rounds || rounds.length === 0) { return cb(null, []); }
298
298
 
299
299
  let error = checkForUnpopulatedData(rounds, 'Round.getByOrg');
300
300
  if (error) { return cb(error, null); }
@@ -313,14 +313,13 @@ module.exports = function(mongoose, config) {
313
313
  }).reverse();
314
314
 
315
315
  return cb(null, rounds);
316
-
317
- }).catch(function(err) { cb(err); });
316
+ } catch(err) {
317
+ return cb(err);
318
+ }
318
319
 
319
320
  };
320
321
 
321
- Round.statics.getByOrgs = function getByOrg(orgIds, options, cb) {
322
-
323
- const self = this;
322
+ Round.statics.getByOrgs = async function getByOrg(orgIds, options, cb) {
324
323
 
325
324
  if (!cb) { throw new Error('cb is required'); }
326
325
  if (!orgIds) { throw new Error('orgIds is required'); }
@@ -333,25 +332,27 @@ module.exports = function(mongoose, config) {
333
332
  if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
334
333
  }
335
334
 
336
- let query = self.find({
337
- 'deleted': { $ne: true },
338
- 'organization': { $in: orgIds }
339
- });
335
+ try {
336
+ let query = this.find({
337
+ 'deleted': { $ne: true },
338
+ 'organization': { $in: orgIds }
339
+ });
340
340
 
341
- query.populate('organization', 'name logoUrl website websiteAliases operating');
342
- query.populate('vehicles.fund');
341
+ query.populate('organization', 'name logoUrl website websiteAliases operating');
342
+ query.populate('vehicles.fund');
343
343
 
344
- if (options.isWorkerProcess) {
345
- query.populate('vehicles.investments');
346
- }
347
- else {
348
- query.populate({
349
- path: 'vehicles.investments',
350
- match: { customer: options.CUSTOMER_ID }
351
- });
352
- }
344
+ if (options.isWorkerProcess) {
345
+ query.populate('vehicles.investments');
346
+ }
347
+ else {
348
+ query.populate({
349
+ path: 'vehicles.investments',
350
+ match: { customer: options.CUSTOMER_ID }
351
+ });
352
+ }
353
353
 
354
- query.exec().then(function(rounds) {if (!rounds || rounds.length === 0) return cb(null, []);
354
+ let rounds = await query;
355
+ if (!rounds || rounds.length === 0) return cb(null, []);
355
356
 
356
357
  let error = checkForUnpopulatedData(rounds, 'Round.getByOrgs');
357
358
  if (error) return cb(error, null);
@@ -370,14 +371,13 @@ module.exports = function(mongoose, config) {
370
371
  }).reverse();
371
372
 
372
373
  return cb(null, rounds);
373
-
374
- }).catch(function(err) { cb(err); });
374
+ } catch(err) {
375
+ return cb(err);
376
+ }
375
377
 
376
378
  };
377
379
 
378
- Round.statics.getByOrgs2 = function getByOrg2(orgIds, options, cb) {
379
-
380
- const self = this;
380
+ Round.statics.getByOrgs2 = async function getByOrg2(orgIds, options, cb) {
381
381
 
382
382
  if (!cb) { throw new Error('cb is required'); }
383
383
  if (!orgIds) { throw new Error('orgIds is required'); }
@@ -390,29 +390,30 @@ module.exports = function(mongoose, config) {
390
390
  if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
391
391
  }
392
392
 
393
- let query = self.find({
394
- 'organization': { $in: orgIds }
395
- });
396
-
397
- if (options.isWorkerProcess) {
398
- query.populate('vehicles.investments');
399
- }
400
- else {
401
- query.populate({
402
- path: 'vehicles.investments',
403
- match: { customer: options.CUSTOMER_ID }
393
+ try {
394
+ let query = this.find({
395
+ 'organization': { $in: orgIds }
404
396
  });
405
- }
406
397
 
407
- query.exec().then(function(rounds) {return cb(null, rounds);
398
+ if (options.isWorkerProcess) {
399
+ query.populate('vehicles.investments');
400
+ }
401
+ else {
402
+ query.populate({
403
+ path: 'vehicles.investments',
404
+ match: { customer: options.CUSTOMER_ID }
405
+ });
406
+ }
408
407
 
409
- }).catch(function(err) { cb(err); });
408
+ const rounds = await query;
409
+ return cb(null, rounds);
410
+ } catch(err) {
411
+ return cb(err);
412
+ }
410
413
 
411
414
  };
412
415
 
413
- Round.statics.getFundInvestments = function getFundInvestments(fundId, options, cb) {
414
-
415
- const self = this;
416
+ Round.statics.getFundInvestments = async function getFundInvestments(fundId, options, cb) {
416
417
 
417
418
  if (!cb) { throw new Error('cb is required'); }
418
419
  if (!fundId) { throw new Error('fundId is required'); }
@@ -426,21 +427,23 @@ module.exports = function(mongoose, config) {
426
427
  if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
427
428
  }
428
429
 
429
- let query = self.find({ 'vehicles.fund': fundId });
430
- query.select('organization roundName vehicles');
431
- query.populate('organization', 'name aliases description logoUrl website websiteAliases filters status ipo closed acquired operating');
430
+ try {
431
+ let query = this.find({ 'vehicles.fund': fundId });
432
+ query.select('organization roundName vehicles');
433
+ query.populate('organization', 'name aliases description logoUrl website websiteAliases filters status ipo closed acquired operating');
432
434
 
433
- if (options.isWorkerProcess) {
434
- query.populate('vehicles.investments');
435
- }
436
- else {
437
- query.populate({
438
- path: 'vehicles.investments',
439
- match: { customer: options.CUSTOMER_ID }
440
- });
441
- }
435
+ if (options.isWorkerProcess) {
436
+ query.populate('vehicles.investments');
437
+ }
438
+ else {
439
+ query.populate({
440
+ path: 'vehicles.investments',
441
+ match: { customer: options.CUSTOMER_ID }
442
+ });
443
+ }
442
444
 
443
- query.exec().then(function(rounds) {if (!rounds) return cb(null, []);
445
+ const rounds = await query;
446
+ if (!rounds) return cb(null, []);
444
447
 
445
448
  if (!options.isWorkerProcess) {
446
449
  _.each(rounds, function(round) {
@@ -461,14 +464,13 @@ module.exports = function(mongoose, config) {
461
464
  });
462
465
 
463
466
  return cb(null, fundInvestments);
464
-
465
- }).catch(function(err) { cb(err); });
467
+ } catch(err) {
468
+ return cb(err);
469
+ }
466
470
 
467
471
  };
468
-
469
- Round.statics.getPortfolioInvestments = function getPortfolioInvestments(options, cb) {
470
472
 
471
- const self = this;
473
+ Round.statics.getPortfolioInvestments = async function getPortfolioInvestments(options, cb) {
472
474
 
473
475
  if (!cb) { throw new Error('cb is required'); }
474
476
  if (!options) { return cb(new Error('options is required'), null); }
@@ -481,7 +483,7 @@ module.exports = function(mongoose, config) {
481
483
  }
482
484
 
483
485
  // First get the customer's fund IDs
484
- Organization.getById(options.CUSTOMER_ID, { CUSTOMER_ID: options.CUSTOMER_ID }, function(err, customer) {
486
+ Organization.getById(options.CUSTOMER_ID, { CUSTOMER_ID: options.CUSTOMER_ID }, async (err, customer) => {
485
487
 
486
488
  if (err) return cb(err, null);
487
489
  if (!customer) return cb(new Error('Customer not found'), null);
@@ -489,21 +491,23 @@ module.exports = function(mongoose, config) {
489
491
 
490
492
  const fundIds = _.pluck(customer.funds, '_id');
491
493
 
492
- let query = self.find({ 'vehicles.fund': { $in: fundIds } });
493
- query.select('organization roundName vehicles');
494
- query.populate('organization', 'name description logoUrl website websiteAliases filters status ipo closed acquired operating');
494
+ try {
495
+ let query = this.find({ 'vehicles.fund': { $in: fundIds } });
496
+ query.select('organization roundName vehicles');
497
+ query.populate('organization', 'name description logoUrl website websiteAliases filters status ipo closed acquired operating');
495
498
 
496
- if (options.isWorkerProcess) {
497
- query.populate('vehicles.investments');
498
- }
499
- else {
500
- query.populate({
501
- path: 'vehicles.investments',
502
- match: { customer: options.CUSTOMER_ID }
503
- });
504
- }
499
+ if (options.isWorkerProcess) {
500
+ query.populate('vehicles.investments');
501
+ }
502
+ else {
503
+ query.populate({
504
+ path: 'vehicles.investments',
505
+ match: { customer: options.CUSTOMER_ID }
506
+ });
507
+ }
505
508
 
506
- query.exec().then(function(rounds) {if (!rounds) return cb(null, []);
509
+ const rounds = await query;
510
+ if (!rounds) return cb(null, []);
507
511
 
508
512
  if (!options.isWorkerProcess) {
509
513
  _.each(rounds, function(round) {
@@ -524,38 +528,38 @@ module.exports = function(mongoose, config) {
524
528
  });
525
529
 
526
530
  return cb(null, portfolioInvestments);
527
-
528
- }).catch(function(err) { cb(err); });
531
+ } catch(err) {
532
+ return cb(err);
533
+ }
529
534
 
530
535
  });
531
536
 
532
537
  };
533
538
 
534
- Round.statics.getFundPerformance = function getFundPerformance(fundId, options, cb) {
539
+ Round.statics.getFundPerformance = async function getFundPerformance(fundId, options, cb) {
535
540
 
536
541
  if (!cb) { throw new Error('cb is required'); }
537
542
  if (!fundId) { throw new Error('fundId is required'); }
538
543
  if (!mongoose.Types.ObjectId.isValid(fundId)) { return cb(new Error('fundId is not a valid ObjectId'), null); }
539
544
 
540
- const self = this;
541
-
542
- let query = self.find({ 'vehicles.fund': fundId });
545
+ try {
546
+ let query = this.find({ 'vehicles.fund': fundId });
543
547
 
544
- query.select('organization vehicles');
545
- query.populate('organization', 'name slug logoUrl description ');
546
- query.populate('vehicles.fund');
548
+ query.select('organization vehicles');
549
+ query.populate('organization', 'name slug logoUrl description ');
550
+ query.populate('vehicles.fund');
547
551
 
548
- if (options.isWorkerProcess) {
549
- query.populate('vehicles.investments');
550
- }
551
- else {
552
- query.populate({
553
- path: 'vehicles.investments',
554
- match: { customer: options.CUSTOMER_ID }
555
- });
556
- }
552
+ if (options.isWorkerProcess) {
553
+ query.populate('vehicles.investments');
554
+ }
555
+ else {
556
+ query.populate({
557
+ path: 'vehicles.investments',
558
+ match: { customer: options.CUSTOMER_ID }
559
+ });
560
+ }
557
561
 
558
- query.exec().then(function(rounds) {
562
+ const rounds = await query;
559
563
  if (!rounds) return cb(null, null);
560
564
  else if (rounds.length === 0) return cb(null, []);
561
565
 
@@ -567,14 +571,13 @@ module.exports = function(mongoose, config) {
567
571
  }
568
572
 
569
573
  return cb(null, calculatePerformance(rounds));
570
-
571
- }).catch(function(err) { cb(err); });
574
+ } catch(err) {
575
+ return cb(err);
576
+ }
572
577
 
573
578
  };
574
579
 
575
- Round.statics.getPortfolioByFund = function getPortfolioByFund(fundId, options, cb) {
576
-
577
- const self = this;
580
+ Round.statics.getPortfolioByFund = async function getPortfolioByFund(fundId, options, cb) {
578
581
 
579
582
  if (!cb) { throw new Error('cb is required'); }
580
583
  if (!fundId) { throw new Error('fundId is required'); }
@@ -588,23 +591,25 @@ module.exports = function(mongoose, config) {
588
591
  if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
589
592
  }
590
593
 
591
- let query = self.find({ 'vehicles.fund': fundId });
594
+ try {
595
+ let query = this.find({ 'vehicles.fund': fundId });
592
596
 
593
- query.select('organization vehicles');
594
- query.populate('organization', 'name aliases slug logoUrl description contact filters status ipo closed acquired operating website websiteAliases');
595
- query.populate('vehicles.fund');
597
+ query.select('organization vehicles');
598
+ query.populate('organization', 'name aliases slug logoUrl description contact filters status ipo closed acquired operating website websiteAliases');
599
+ query.populate('vehicles.fund');
596
600
 
597
- if (options.isWorkerProcess) {
598
- query.populate('vehicles.investments');
599
- }
600
- else {
601
- query.populate({
602
- path: 'vehicles.investments',
603
- match: { customer: options.CUSTOMER_ID }
604
- });
605
- }
601
+ if (options.isWorkerProcess) {
602
+ query.populate('vehicles.investments');
603
+ }
604
+ else {
605
+ query.populate({
606
+ path: 'vehicles.investments',
607
+ match: { customer: options.CUSTOMER_ID }
608
+ });
609
+ }
606
610
 
607
- query.exec().then(function(rounds) {if (!rounds || rounds.length === 0) return cb(null, []);
611
+ const rounds = await query;
612
+ if (!rounds || rounds.length === 0) return cb(null, []);
608
613
 
609
614
  let error = checkForUnpopulatedData(rounds, 'Round.getPortfolioByFund');
610
615
  if (error) return cb(error, null);
@@ -617,18 +622,17 @@ module.exports = function(mongoose, config) {
617
622
  }
618
623
 
619
624
  return cb(null, buildPortfolio(rounds, [fundId]));
620
-
621
- }).catch(function(err) { cb(err); });
625
+ } catch(err) {
626
+ return cb(err);
627
+ }
622
628
 
623
629
  };
624
630
 
625
- Round.statics.getPortfolioByFunds = function getPortfolioByFunds(fundIds, options, cb) {
631
+ Round.statics.getPortfolioByFunds = async function getPortfolioByFunds(fundIds, options, cb) {
626
632
 
627
633
  // Returns a list of orgs that have rounds participated in by the given fund ids
628
634
  // Note retrieving multiple funds does not calculate fund stats. Use getPortfolioByFund for that.
629
635
 
630
- const self = this;
631
-
632
636
  if (!cb) { throw new Error('cb is required'); }
633
637
  if (!fundIds) { throw new Error('fundIds is required'); }
634
638
  if (!options) { return cb(new Error('options is required'), null); }
@@ -640,30 +644,32 @@ module.exports = function(mongoose, config) {
640
644
  if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
641
645
  }
642
646
 
643
- let query = self.find({ 'vehicles.fund': { $in: fundIds } });
647
+ try {
648
+ let query = this.find({ 'vehicles.fund': { $in: fundIds } });
644
649
 
645
- query.select('organization vehicles');
646
- query.populate({
647
- path: 'organization',
648
- select: 'name aliases slug logoUrl description contact chairs filters status ipo closed acquired operating website websiteAliases',
649
- populate: [
650
- { path: 'chairs.first', select: 'name avatarUrl title doNotDisplay' },
651
- { path: 'chairs.second', select: 'name avatarUrl title doNotDisplay' }
652
- ]
653
- });
654
- query.populate('vehicles.fund');
655
-
656
- if (options.isWorkerProcess) {
657
- query.populate('vehicles.investments');
658
- }
659
- else {
650
+ query.select('organization vehicles');
660
651
  query.populate({
661
- path: 'vehicles.investments',
662
- match: { customer: options.CUSTOMER_ID }
652
+ path: 'organization',
653
+ select: 'name aliases slug logoUrl description contact chairs filters status ipo closed acquired operating website websiteAliases',
654
+ populate: [
655
+ { path: 'chairs.first', select: 'name avatarUrl title doNotDisplay' },
656
+ { path: 'chairs.second', select: 'name avatarUrl title doNotDisplay' }
657
+ ]
663
658
  });
664
- }
659
+ query.populate('vehicles.fund');
660
+
661
+ if (options.isWorkerProcess) {
662
+ query.populate('vehicles.investments');
663
+ }
664
+ else {
665
+ query.populate({
666
+ path: 'vehicles.investments',
667
+ match: { customer: options.CUSTOMER_ID }
668
+ });
669
+ }
665
670
 
666
- query.exec().then(function(rounds) {if (!rounds || rounds.length === 0) return cb(null, []);
671
+ const rounds = await query;
672
+ if (!rounds || rounds.length === 0) return cb(null, []);
667
673
 
668
674
  let error = checkForUnpopulatedData(rounds, 'Round.getPortfolioByFunds');
669
675
  if (error) return cb(error, null);
@@ -676,18 +682,17 @@ module.exports = function(mongoose, config) {
676
682
  }
677
683
 
678
684
  return cb(null, buildPortfolio(rounds, fundIds));
679
-
680
- }).catch(function(err) { cb(err); });
685
+ } catch(err) {
686
+ return cb(err);
687
+ }
681
688
 
682
689
  };
683
690
 
684
- Round.statics.getPortfolioList = function getPortfolioList(fundIds, options, cb) {
691
+ Round.statics.getPortfolioList = async function getPortfolioList(fundIds, options, cb) {
685
692
 
686
693
  // Returns a list of orgs that have rounds participated in by the given fund ids
687
694
  // Note retrieving multiple funds does not calculate fund stats. Use getPortfolioByFund for that.
688
695
 
689
- const self = this;
690
-
691
696
  if (!cb) { throw new Error('cb is required'); }
692
697
  if (!fundIds) { throw new Error('fundIds is required'); }
693
698
  if (!options) { return cb(new Error('options is required'), null); }
@@ -696,16 +701,21 @@ module.exports = function(mongoose, config) {
696
701
 
697
702
  options = helpers.getDefaultOptions(options);
698
703
 
699
- let query = self.find({ 'vehicles.fund': { $in: fundIds } });
704
+ try {
705
+ let query = this.find({ 'vehicles.fund': { $in: fundIds } });
700
706
 
701
- query.select('organization vehicles');
702
- query.populate('organization', 'name website status ipo operating people');
707
+ query.select('organization vehicles');
708
+ query.populate('organization', 'name website status ipo operating people');
709
+ query.lean({ virtuals: true });
703
710
 
704
- query.exec().then(function(rounds) {if (!rounds || rounds.length === 0) return cb(null, []);
711
+ const rounds = await query;
712
+ if (!rounds || rounds.length === 0) {
713
+ process.nextTick(function() { cb(null, []); });
714
+ return;
715
+ }
705
716
 
706
717
  if (!options.isWorkerProcess) {
707
718
  _.each(rounds, function(round) {
708
- round.organization = round.organization.toObject({ virtuals: true });
709
719
  round = helpers.cleanRound(round, fundIds, options.CUSTOMER_ID);
710
720
  round.organization = helpers.cleanOrg(round.organization, options.CUSTOMER_ID);
711
721
  });
@@ -715,7 +725,7 @@ module.exports = function(mongoose, config) {
715
725
  const grouped = _.groupBy(rounds, function(r) { return r.organization._id; });
716
726
 
717
727
  _.each(grouped, function(rounds) {
718
-
728
+
719
729
  var fundList = _.uniq(_.flatten(_.map(rounds, function(round) {
720
730
  return round.vehicles ? _.map(round.vehicles, function(vehicle) { return vehicle.fund.toString(); }) : [];
721
731
  })));
@@ -734,13 +744,14 @@ module.exports = function(mongoose, config) {
734
744
 
735
745
  portfolio = _.sortBy(portfolio, function(p) { return p.name.toLowerCase(); });
736
746
 
737
- return cb(null, portfolio);
738
-
739
- }).catch(function(err) { cb(err); });
747
+ process.nextTick(function() { cb(null, portfolio); });
748
+ } catch(err) {
749
+ process.nextTick(function() { cb(err); });
750
+ }
740
751
 
741
752
  };
742
753
 
743
- Round.statics.removeInvestment = function(investmentId, cb) {
754
+ Round.statics.removeInvestment = async function(investmentId, cb) {
744
755
 
745
756
  // Do not call this directly. It will leave an orphaned investment document.
746
757
  // The preferred way to delete an investment is to delete it directly and this
@@ -750,48 +761,42 @@ module.exports = function(mongoose, config) {
750
761
  if (!investmentId) { throw new Error('investmentId is required'); }
751
762
  if (!mongoose.Types.ObjectId.isValid(investmentId)) { return cb(new Error('investmentId is not a valid ObjectId'), null); }
752
763
 
753
- const self = this;
754
-
755
- let getRounds = function getRounds(callback) {
756
- let query = self.find({ 'vehicles.investments': investmentId });
764
+ try {
765
+ let query = this.find({ 'vehicles.investments': investmentId });
757
766
  query.select('_id');
758
- query.exec().then(function(result) { callback(null, result); }).catch(function(err) { callback(err); });
759
- };
767
+ const rounds = await query;
760
768
 
761
- let updateRounds = function updateRounds(previous, callback) {
762
- let query = self.updateMany(
769
+ let updateQuery = this.updateMany(
763
770
  { 'vehicles.investments': investmentId },
764
- { $pull : { 'vehicles.$.investments': investmentId } },
765
- { multi : true }
771
+ { $pull : { 'vehicles.$.investments': investmentId } }
766
772
  );
767
- query.exec().then(function(result) { callback(null, result); }).catch(function(err) { callback(err); });
768
- };
773
+ const result = await updateQuery;
769
774
 
770
- async.waterfall([
771
- getRounds,
772
- updateRounds
773
- ], cb);
775
+ return cb(null, result);
776
+ } catch(err) {
777
+ return cb(err);
778
+ }
774
779
 
775
780
  };
776
781
 
777
- Round.statics.removeVehicle = function(roundId, fundId, cb) {
778
-
779
- const self = this;
780
-
781
- self
782
- .findOneAndUpdate(
783
- { '_id': roundId },
784
- { $pull : { 'vehicles': { 'fund': fundId } } },
785
- { multi : false }
786
- )
787
- .exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
782
+ Round.statics.removeVehicle = async function(roundId, fundId, cb) {
783
+
784
+ try {
785
+ const result = await this
786
+ .findOneAndUpdate(
787
+ { '_id': roundId },
788
+ { $pull : { 'vehicles': { 'fund': fundId } } },
789
+ { multi : false }
790
+ );
791
+ return cb(null, result);
792
+ } catch(err) {
793
+ return cb(err);
794
+ }
788
795
 
789
796
  };
790
797
 
791
- Round.statics.getRelated = function getRelated(orgid, options, cb) {
798
+ Round.statics.getRelated = async function getRelated(orgid, options, cb) {
792
799
 
793
- const self = this;
794
- const async = require('async');
795
800
  const Organization = mongoose.model('Organization');
796
801
 
797
802
  if (!cb) { throw new Error('cb is required'); }
@@ -805,109 +810,97 @@ module.exports = function(mongoose, config) {
805
810
  var customerFunds = options.customerFunds;
806
811
  if (!customerFunds) {
807
812
  const Fund = mongoose.model('Fund');
808
- return Fund.findByCustomer(options.CUSTOMER_ID, function(err, funds) {
813
+ return Fund.findByCustomer(options.CUSTOMER_ID, (err, funds) => {
809
814
  if (err) return cb(err);
810
815
  options.customerFunds = funds;
811
- return self.getRelated(orgid, options, cb);
816
+ return this.getRelated(orgid, options, cb);
812
817
  });
813
818
  }
814
819
 
815
- async.parallel([
816
-
820
+ try {
817
821
  // Find organizations acquired by this org (acquirees)
818
- function(callback) {
819
- Organization
820
- .find({
821
- $or: [
822
- { 'operating.acquired.private.by': orgid, 'operating.acquired.private.customer': options.CUSTOMER_ID, 'deleted': { $ne: true } },
823
- { 'operating.acquired.public.by': orgid, 'deleted': { $ne: true } }
824
- ]
825
- })
826
- .select('name logoUrl')
827
- .exec().then(function(result) { callback(null, result); }).catch(function(err) { callback(err); });
828
- },
829
-
822
+ const acquirees = await Organization
823
+ .find({
824
+ $or: [
825
+ { 'operating.acquired.private.by': orgid, 'operating.acquired.private.customer': options.CUSTOMER_ID, 'deleted': { $ne: true } },
826
+ { 'operating.acquired.public.by': orgid, 'deleted': { $ne: true } }
827
+ ]
828
+ })
829
+ .select('name logoUrl');
830
+
830
831
  // Find organizations merged with this org
831
- function(callback) {
832
- Organization
833
- .find({
834
- $or: [
835
- { 'operating.merged.private.with': orgid, 'operating.merged.private.customer': options.CUSTOMER_ID, 'deleted': { $ne: true } },
836
- { 'operating.merged.public.with': orgid, 'deleted': { $ne: true } }
837
- ]
838
- })
839
- .select('name logoUrl')
840
- .exec().then(function(result) { callback(null, result); }).catch(function(err) { callback(err); });
841
- },
842
-
832
+ const merged = await Organization
833
+ .find({
834
+ $or: [
835
+ { 'operating.merged.private.with': orgid, 'operating.merged.private.customer': options.CUSTOMER_ID, 'deleted': { $ne: true } },
836
+ { 'operating.merged.public.with': orgid, 'deleted': { $ne: true } }
837
+ ]
838
+ })
839
+ .select('name logoUrl');
840
+
843
841
  // Find organizations that acquired this org (acquirers) or merged with this org
844
- function(callback) {
845
- Organization
846
- .findById(orgid)
847
- .select('operating')
848
- .exec().then(function(mainOrg) {if (!mainOrg) return callback(null, []);
849
-
850
- var relatedIds = [];
851
-
852
- // Check for acquirer
853
- if (mainOrg.operating && mainOrg.operating.acquired) {
854
- // acquired.private is an array, iterate through it
855
- if (mainOrg.operating.acquired.private && Array.isArray(mainOrg.operating.acquired.private)) {
856
- mainOrg.operating.acquired.private.forEach(function(acquiredEntry) {
857
- if (acquiredEntry && acquiredEntry.by) {
858
- relatedIds.push(acquiredEntry.by);
859
- }
860
- });
861
- }
862
-
863
- if (mainOrg.operating.acquired.public && mainOrg.operating.acquired.public.by) {
864
- relatedIds.push(mainOrg.operating.acquired.public.by);
865
- }
866
- }
867
-
868
- // Check for merger partner
869
- if (mainOrg.operating && mainOrg.operating.merged) {
870
- // merged.private is an array, iterate through it
871
- if (mainOrg.operating.merged.private && Array.isArray(mainOrg.operating.merged.private)) {
872
- mainOrg.operating.merged.private.forEach(function(mergedEntry) {
873
- if (mergedEntry && mergedEntry.with) {
874
- relatedIds.push(mergedEntry.with);
875
- }
876
- });
842
+ const mainOrg = await Organization
843
+ .findById(orgid)
844
+ .select('operating');
845
+
846
+ let reverseRelated = [];
847
+ if (mainOrg) {
848
+ var relatedIds = [];
849
+
850
+ // Check for acquirer
851
+ if (mainOrg.operating && mainOrg.operating.acquired) {
852
+ // acquired.private is an array, iterate through it
853
+ if (mainOrg.operating.acquired.private && Array.isArray(mainOrg.operating.acquired.private)) {
854
+ mainOrg.operating.acquired.private.forEach(function(acquiredEntry) {
855
+ if (acquiredEntry && acquiredEntry.by) {
856
+ relatedIds.push(acquiredEntry.by);
877
857
  }
878
-
879
- if (mainOrg.operating.merged.public && mainOrg.operating.merged.public.with) {
880
- relatedIds.push(mainOrg.operating.merged.public.with);
858
+ });
859
+ }
860
+
861
+ if (mainOrg.operating.acquired.public && mainOrg.operating.acquired.public.by) {
862
+ relatedIds.push(mainOrg.operating.acquired.public.by);
863
+ }
864
+ }
865
+
866
+ // Check for merger partner
867
+ if (mainOrg.operating && mainOrg.operating.merged) {
868
+ // merged.private is an array, iterate through it
869
+ if (mainOrg.operating.merged.private && Array.isArray(mainOrg.operating.merged.private)) {
870
+ mainOrg.operating.merged.private.forEach(function(mergedEntry) {
871
+ if (mergedEntry && mergedEntry.with) {
872
+ relatedIds.push(mergedEntry.with);
881
873
  }
882
- }
883
-
884
- if (relatedIds.length === 0) return callback(null, []);
885
-
886
- Organization
887
- .find({
888
- '_id': { $in: relatedIds },
889
- 'deleted': { $ne: true }
890
- })
891
- .select('name logoUrl')
892
- .exec().then(function(result) { callback(null, result); }).catch(function(err) { callback(err); });
893
- }).catch(function(err) { callback(err); });
874
+ });
875
+ }
876
+
877
+ if (mainOrg.operating.merged.public && mainOrg.operating.merged.public.with) {
878
+ relatedIds.push(mainOrg.operating.merged.public.with);
879
+ }
880
+ }
881
+
882
+ if (relatedIds.length > 0) {
883
+ reverseRelated = await Organization
884
+ .find({
885
+ '_id': { $in: relatedIds },
886
+ 'deleted': { $ne: true }
887
+ })
888
+ .select('name logoUrl');
889
+ }
894
890
  }
895
-
896
- ], function(err, results) {
897
- if (err) return cb(err);
898
-
891
+
899
892
  // Flatten and combine all results
900
893
  var allResults = [];
901
- results.forEach(function(resultSet) {
894
+ [acquirees, merged, reverseRelated].forEach(function(resultSet) {
902
895
  if (Array.isArray(resultSet)) {
903
896
  allResults = allResults.concat(resultSet);
904
897
  }
905
898
  });
906
-
899
+
907
900
  // Remove duplicates and the org itself
908
901
  var uniqueResults = [];
909
902
  var seenIds = new Set();
910
-
903
+
911
904
  allResults.forEach(function(org) {
912
905
  if (!seenIds.has(org._id.toString()) && org._id.toString() !== orgid.toString()) {
913
906
  seenIds.add(org._id.toString());
@@ -916,11 +909,10 @@ module.exports = function(mongoose, config) {
916
909
  });
917
910
 
918
911
  // Now get rounds for each related organization
919
- async.map(uniqueResults, function(org, mapCallback) {
920
-
912
+ const relatedWithRounds = await Promise.all(uniqueResults.map(async (org) => {
921
913
  var fundIds = customerFunds.map(function(fund) { return fund._id; });
922
-
923
- self
914
+
915
+ let rounds = await this
924
916
  .find({
925
917
  'organization': org._id,
926
918
  'vehicles.fund': { $in: fundIds },
@@ -931,31 +923,37 @@ module.exports = function(mongoose, config) {
931
923
  .populate({
932
924
  path: 'vehicles.investments',
933
925
  match: { customer: options.CUSTOMER_ID }
934
- })
935
- .exec().then(function(rounds) {// Clean rounds to only show vehicles for customer funds
936
- if (rounds && !options.isWorkerProcess) {
937
- rounds = rounds.map(function(round) {
938
- return helpers.cleanRound(round, fundIds, options.CUSTOMER_ID);
939
- });
940
- }
941
-
942
- mapCallback(null, {
943
- org: org,
944
- rounds: rounds || []
945
- });
946
- }).catch(function(err) { mapCallback(err); });
947
-
948
- }, function(err, relatedWithRounds) {
949
- if (err) return cb(err);
950
- cb(null, relatedWithRounds);
951
- });
952
- });
926
+ });
927
+
928
+ // Clean rounds to only show vehicles for customer funds
929
+ if (rounds && !options.isWorkerProcess) {
930
+ rounds = rounds.map(function(round) {
931
+ return helpers.cleanRound(round, fundIds, options.CUSTOMER_ID);
932
+ });
933
+ }
934
+
935
+ return {
936
+ org: org,
937
+ rounds: rounds || []
938
+ };
939
+ }));
940
+
941
+ return cb(null, relatedWithRounds);
942
+ } catch(err) {
943
+ return cb(err);
944
+ }
953
945
  };
954
946
 
955
- Round.statics.upsert = function(round, cb) {
947
+ Round.statics.upsert = async function(round, cb) {
956
948
 
957
949
  if (!round) { return cb(new Error('round is required'), null); }
958
- round.save().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
950
+
951
+ try {
952
+ const result = await round.save();
953
+ return cb(null, result);
954
+ } catch(err) {
955
+ return cb(err);
956
+ }
959
957
 
960
958
  };
961
959
 
@@ -970,4 +968,4 @@ module.exports = function(mongoose, config) {
970
968
 
971
969
  mongoose.model('Round', Round);
972
970
 
973
- };
971
+ };