@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.
@@ -5,7 +5,6 @@ module.exports = function(mongoose, config) {
5
5
  let
6
6
 
7
7
  _ = require('underscore'),
8
- async = require('async'),
9
8
  utilities = require('@dhyasama/totem-utilities'),
10
9
  helpers = require('../helpers'),
11
10
  Schema = mongoose.Schema,
@@ -1040,30 +1039,30 @@ module.exports = function(mongoose, config) {
1040
1039
  Note.createForModel(this, organizationId, note, cb);
1041
1040
  };
1042
1041
 
1043
- Organization.statics.deleteNote = function(noteId, customerId, cb) {
1042
+ Organization.statics.deleteNote = async function(noteId, customerId, cb) {
1044
1043
 
1045
1044
  // Delete the note itself along with any references
1046
1045
 
1047
- const self = this;
1046
+ try {
1047
+ await new Promise(function(resolve, reject) {
1048
+ Note.delete(noteId, customerId, function(err, result) {
1049
+ if (err) return reject(err);
1050
+ resolve(result);
1051
+ });
1052
+ });
1048
1053
 
1049
- let removeReferences = function removeReferences(callback) {
1050
- self.updateMany({}, {
1054
+ await this.updateMany({}, {
1051
1055
  $pull: { 'notes' : [noteId] }
1052
- }).then(function(result) { callback(null, result); }).catch(function(err) { callback(err); });
1053
- };
1056
+ });
1054
1057
 
1055
- async.series([
1056
- Note.delete.bind(Note, noteId, customerId),
1057
- removeReferences
1058
- ], function(err, results) {
1059
- return cb(err, null);
1060
- });
1058
+ return cb(null, null);
1059
+ } catch(err) {
1060
+ return cb(err);
1061
+ }
1061
1062
 
1062
1063
  };
1063
1064
 
1064
- Organization.statics.findByDomains = function findByDomains(domains, options, cb) {
1065
-
1066
- const self = this;
1065
+ Organization.statics.findByDomains = async function findByDomains(domains, options, cb) {
1067
1066
 
1068
1067
  if (!cb) { throw new Error('cb is required'); }
1069
1068
  if (!domains) { return cb(new Error('domains is required'), null); }
@@ -1099,34 +1098,38 @@ module.exports = function(mongoose, config) {
1099
1098
 
1100
1099
  domains = domains.concat(alternativeDomains);
1101
1100
 
1102
- let query = self.find({ $or: [ {'website': { $in : domains }}, {'websiteAliases': { $in : domains }} ], 'deleted': {$ne: true} });
1101
+ try {
1102
+ let query = this.find({ $or: [ {'website': { $in : domains }}, {'websiteAliases': { $in : domains }} ], 'deleted': {$ne: true} });
1103
1103
 
1104
- query.exec().then(function(orgs) {
1104
+ const orgs = await query;
1105
1105
  if (!orgs) { return cb(null, []); }
1106
1106
 
1107
+ let result = orgs;
1107
1108
  if (!options.isWorkerProcess) {
1108
- orgs = _.map(orgs, function(org) {
1109
+ result = _.map(orgs, function(org) {
1109
1110
  return helpers.cleanOrg(org, options.CUSTOMER_ID);
1110
1111
  });
1111
1112
  }
1112
1113
 
1113
- return cb(null, orgs);
1114
-
1115
- }).catch(function(err) { cb(err); });
1114
+ return cb(null, result);
1115
+ } catch(err) {
1116
+ return cb(err);
1117
+ }
1116
1118
 
1117
1119
  };
1118
1120
 
1119
- Organization.statics.findByFunds = function findByFunds(fundids, cb) {
1120
- const self = this;
1121
- self
1122
- .find({ 'funds': { $in : fundids }, 'deleted': {$ne: true} })
1123
- .select('name logoUrl funds contact')
1124
- .exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
1121
+ Organization.statics.findByFunds = async function findByFunds(fundids, cb) {
1122
+ try {
1123
+ const result = await this
1124
+ .find({ 'funds': { $in : fundids }, 'deleted': {$ne: true} })
1125
+ .select('name logoUrl funds contact');
1126
+ return cb(null, result);
1127
+ } catch(err) {
1128
+ return cb(err);
1129
+ }
1125
1130
  };
1126
1131
 
1127
- Organization.statics.findByIdsUnpopulated = function findByIdsUnpopulated(ids, options, cb) {
1128
-
1129
- const self = this;
1132
+ Organization.statics.findByIdsUnpopulated = async function findByIdsUnpopulated(ids, options, cb) {
1130
1133
 
1131
1134
  if (!cb) { throw new Error('cb is required'); }
1132
1135
  if (!ids) { return cb(new Error('ids is required'), null); }
@@ -1139,26 +1142,25 @@ module.exports = function(mongoose, config) {
1139
1142
  if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
1140
1143
  }
1141
1144
 
1142
- self
1143
- .find({ '_id': { $in : ids }})
1144
- .exec().then(function(orgs) {
1145
+ try {
1146
+ const orgs = await this.find({ '_id': { $in : ids }});
1145
1147
  if (!orgs) { return cb(null, []); }
1146
1148
 
1147
- if (!options.isWorkerProcess) {
1148
- orgs = _.map(orgs, function(org) {
1149
- return helpers.cleanOrg(org, options.CUSTOMER_ID);
1150
- });
1151
- }
1152
-
1153
- return cb(null, orgs);
1149
+ let result = orgs;
1150
+ if (!options.isWorkerProcess) {
1151
+ result = _.map(orgs, function(org) {
1152
+ return helpers.cleanOrg(org, options.CUSTOMER_ID);
1153
+ });
1154
+ }
1154
1155
 
1155
- }).catch(function(err) { cb(err); });
1156
+ return cb(null, result);
1157
+ } catch(err) {
1158
+ return cb(err);
1159
+ }
1156
1160
 
1157
1161
  };
1158
1162
 
1159
- Organization.statics.findByIds = function findByIds(ids, options, cb) {
1160
-
1161
- const self = this;
1163
+ Organization.statics.findByIds = async function findByIds(ids, options, cb) {
1162
1164
 
1163
1165
  if (!cb) { throw new Error('cb is required'); }
1164
1166
  if (!ids) { return cb(new Error('ids is required'), null); }
@@ -1166,98 +1168,112 @@ module.exports = function(mongoose, config) {
1166
1168
  if (!options.CUSTOMER_ID) { return cb(new Error('options.CUSTOMER_ID is required'), null); }
1167
1169
  if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
1168
1170
 
1169
- self
1170
- .find({ '_id': { $in : ids }})
1171
- .populate({
1172
- path: 'people.person',
1173
- select: 'name avatarUrl title doNotDisplay sources',
1174
- populate: [
1175
- { path: 'sources.person', select: 'name avatarUrl title doNotDisplay' }
1176
- ]
1177
- })
1178
- .populate('related', 'name logoUrl')
1179
- .populate('chairs.first', 'name avatarUrl title')
1180
- .populate('chairs.second', 'name avatarUrl title')
1181
- .populate('funds', 'name hexColorCode abbreviation')
1182
- .exec().then(function(orgs) {
1171
+ try {
1172
+ const orgs = await this
1173
+ .find({ '_id': { $in : ids }})
1174
+ .populate({
1175
+ path: 'people.person',
1176
+ select: 'name avatarUrl title doNotDisplay sources',
1177
+ populate: [
1178
+ { path: 'sources.person', select: 'name avatarUrl title doNotDisplay' }
1179
+ ]
1180
+ })
1181
+ .populate('related', 'name logoUrl')
1182
+ .populate('chairs.first', 'name avatarUrl title')
1183
+ .populate('chairs.second', 'name avatarUrl title')
1184
+ .populate('funds', 'name hexColorCode abbreviation');
1183
1185
  if (!orgs) { return cb(null, []); }
1184
1186
 
1185
- orgs = _.map(orgs, function(org) {
1186
- return helpers.cleanOrg(org, options.CUSTOMER_ID);
1187
- });
1188
-
1189
- return cb(null, orgs);
1187
+ const result = _.map(orgs, function(org) {
1188
+ return helpers.cleanOrg(org, options.CUSTOMER_ID);
1189
+ });
1190
1190
 
1191
- }).catch(function(err) { cb(err); });
1191
+ return cb(null, result);
1192
+ } catch(err) {
1193
+ return cb(err);
1194
+ }
1192
1195
 
1193
1196
  };
1194
1197
 
1195
- Organization.statics.findByLimitedPartner = function findByLimitedPartner(lpid, options, cb) {
1196
-
1197
- const self = this;
1198
+ Organization.statics.findByLimitedPartner = async function findByLimitedPartner(lpid, options, cb) {
1198
1199
 
1199
1200
  options = helpers.getDefaultOptions(options);
1200
1201
 
1201
1202
  if (options.role === 'none') { return cb(new Error('Access denied'), null); }
1202
1203
 
1203
- self
1204
- .find({ 'lps': lpid, 'deleted': {$ne: true} })
1205
- .select('name logoUrl')
1206
- .exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
1204
+ try {
1205
+ const result = await this
1206
+ .find({ 'lps': lpid, 'deleted': {$ne: true} })
1207
+ .select('name logoUrl');
1208
+ return cb(null, result);
1209
+ } catch(err) {
1210
+ return cb(err);
1211
+ }
1207
1212
 
1208
1213
  };
1209
1214
 
1210
- Organization.statics.findByPerson = function findByPerson(personId, cb) {
1215
+ Organization.statics.findByPerson = async function findByPerson(personId, cb) {
1211
1216
 
1212
1217
  // Given a person id, get all organizations with that person
1213
1218
 
1214
- const self = this;
1215
-
1216
1219
  if (!cb) { throw new Error('cb is required'); }
1217
1220
  if (!personId) { return cb(new Error('ids is required'), null); }
1218
1221
  if (!mongoose.Types.ObjectId.isValid(personId)) { return cb(new Error('personId is not a valid ObjectId'), null); }
1219
1222
 
1220
- self
1221
- .find({ $or:
1222
- [
1223
- { 'people.person': personId },
1224
- { 'chairs.first': personId },
1225
- { 'chairs.second': personId }
1226
- ],
1227
- 'deleted': { $ne: true }
1228
- })
1229
- .select('-sources -customer')
1230
- .exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
1223
+ try {
1224
+ const result = await this
1225
+ .find({ $or:
1226
+ [
1227
+ { 'people.person': personId },
1228
+ { 'chairs.first': personId },
1229
+ { 'chairs.second': personId }
1230
+ ],
1231
+ 'deleted': { $ne: true }
1232
+ })
1233
+ .select('-sources -customer');
1234
+ return cb(null, result);
1235
+ } catch(err) {
1236
+ return cb(err);
1237
+ }
1231
1238
 
1232
1239
  };
1233
1240
 
1234
- Organization.statics.getByPersonIds = function getByPersonIds(personIds, cb) {
1235
-
1236
- const self = this;
1241
+ Organization.statics.getByPersonIds = async function getByPersonIds(personIds, cb) {
1237
1242
 
1238
1243
  if (!cb) { throw new Error('cb is required'); }
1239
1244
  if (!personIds) { return cb(new Error('personIds is required'), null); }
1240
1245
 
1241
- self
1242
- .find({'people.person': { $in : personIds } })
1243
- .exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
1246
+ try {
1247
+ const result = await this.find({'people.person': { $in : personIds } });
1248
+ return cb(null, result);
1249
+ } catch(err) {
1250
+ return cb(err);
1251
+ }
1244
1252
 
1245
1253
  };
1246
1254
 
1247
- Organization.statics.findBySlug = function findBySlug(slug, cb) {
1248
- this.findOne({ slug: slug, 'deleted': {$ne: true} }).exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
1255
+ Organization.statics.findBySlug = async function findBySlug(slug, cb) {
1256
+ try {
1257
+ const result = await this.findOne({ slug: slug, 'deleted': {$ne: true} });
1258
+ return cb(null, result);
1259
+ } catch(err) {
1260
+ return cb(err);
1261
+ }
1249
1262
  };
1250
1263
 
1251
- Organization.statics.findBySlugs = function findBySlugs(slugs, cb) {
1252
- this.find({ 'slug': { $in : slugs } }).exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
1264
+ Organization.statics.findBySlugs = async function findBySlugs(slugs, cb) {
1265
+ try {
1266
+ const result = await this.find({ 'slug': { $in : slugs } });
1267
+ return cb(null, result);
1268
+ } catch(err) {
1269
+ return cb(err);
1270
+ }
1253
1271
  };
1254
1272
 
1255
- Organization.statics.findBySocial2 = function findBySocial2(value, options, cb) {
1273
+ Organization.statics.findBySocial2 = async function findBySocial2(value, options, cb) {
1256
1274
 
1257
1275
  // Extract stand-alone username and append it to our standardized domains
1258
1276
 
1259
- const self = this;
1260
-
1261
1277
  if (!cb) { throw new Error('cb is required'); }
1262
1278
  if (!value) { return cb(new Error('value is required'), null); }
1263
1279
  if (!options) { return cb(new Error('options is required'), null); }
@@ -1270,71 +1286,73 @@ module.exports = function(mongoose, config) {
1270
1286
 
1271
1287
  if (!username) { return cb(null, []); }
1272
1288
 
1273
- self.aggregate([
1274
- {
1275
- "$search": {
1276
- index: 'organizations_search_autocomplete',
1277
- compound: {
1278
- should: [
1279
- {
1280
- autocomplete: {
1281
- query: username,
1282
- path: 'social_handle.facebook',
1283
- fuzzy: {
1284
- maxEdits: defaultMaxEdits,
1285
- maxExpansions: defaultMaxExpansions
1289
+ try {
1290
+ const orgs = await this.aggregate([
1291
+ {
1292
+ "$search": {
1293
+ index: 'organizations_search_autocomplete',
1294
+ compound: {
1295
+ should: [
1296
+ {
1297
+ autocomplete: {
1298
+ query: username,
1299
+ path: 'social_handle.facebook',
1300
+ fuzzy: {
1301
+ maxEdits: defaultMaxEdits,
1302
+ maxExpansions: defaultMaxExpansions
1303
+ }
1286
1304
  }
1287
- }
1288
- },
1289
- {
1290
- autocomplete: {
1291
- query: username,
1292
- path: 'social_handle.linkedin',
1293
- fuzzy: {
1294
- maxEdits: defaultMaxEdits,
1295
- maxExpansions: defaultMaxExpansions
1305
+ },
1306
+ {
1307
+ autocomplete: {
1308
+ query: username,
1309
+ path: 'social_handle.linkedin',
1310
+ fuzzy: {
1311
+ maxEdits: defaultMaxEdits,
1312
+ maxExpansions: defaultMaxExpansions
1313
+ }
1296
1314
  }
1297
- }
1298
- },
1299
- {
1300
- autocomplete: {
1301
- query: username,
1302
- path: 'social_handle.twitter',
1303
- fuzzy: {
1304
- maxEdits: defaultMaxEdits,
1305
- maxExpansions: defaultMaxExpansions
1315
+ },
1316
+ {
1317
+ autocomplete: {
1318
+ query: username,
1319
+ path: 'social_handle.twitter',
1320
+ fuzzy: {
1321
+ maxEdits: defaultMaxEdits,
1322
+ maxExpansions: defaultMaxExpansions
1323
+ }
1306
1324
  }
1307
1325
  }
1308
- }
1309
- ],
1310
- must: [
1311
- {
1312
- equals: {
1313
- path: 'deleted',
1314
- value: false
1326
+ ],
1327
+ must: [
1328
+ {
1329
+ equals: {
1330
+ path: 'deleted',
1331
+ value: false
1332
+ }
1315
1333
  }
1316
- }
1317
- ],
1318
- minimumShouldMatch: 1
1334
+ ],
1335
+ minimumShouldMatch: 1
1336
+ }
1319
1337
  }
1320
1338
  }
1321
- }
1322
- ]).exec().then(function(orgs) {orgs = _.map(orgs, function(org) {
1339
+ ]);
1340
+
1341
+ const result = _.map(orgs, function(org) {
1323
1342
  return helpers.cleanOrg(org, options.CUSTOMER_ID);
1324
1343
  });
1325
1344
 
1326
- return cb(null, orgs);
1327
-
1328
- }).catch(function(err) { cb(err); });
1345
+ return cb(null, result);
1346
+ } catch(err) {
1347
+ return cb(err);
1348
+ }
1329
1349
 
1330
1350
  };
1331
1351
 
1332
- Organization.statics.findBySocial = function findBySocial(value, options, cb) {
1352
+ Organization.statics.findBySocial = async function findBySocial(value, options, cb) {
1333
1353
 
1334
1354
  // Extract stand-alone username and append it to our standardized domains
1335
1355
 
1336
- const self = this;
1337
-
1338
1356
  if (!cb) { throw new Error('cb is required'); }
1339
1357
  if (!value) { return cb(new Error('value is required'), null); }
1340
1358
  if (!options) { return cb(new Error('options is required'), null); }
@@ -1345,33 +1363,34 @@ module.exports = function(mongoose, config) {
1345
1363
 
1346
1364
  if (!username) { return cb(null, []); }
1347
1365
 
1348
- self
1349
- .find({ $or:
1350
- [
1351
- { 'social.facebook': new RegExp('facebook.com/' + username + '\/?$', 'i') },
1352
- { 'social.linkedin': new RegExp('linkedin.com/company/' + username + '\/?$', 'i') },
1353
- { 'social.twitter': new RegExp('twitter.com/' + username + '\/?$', 'i') },
1354
- { 'crunchbase.url': new RegExp('crunchbase.com/organization/' + username + '\/?$', 'i') },
1355
- { 'pitchbook.url': new RegExp('pitchbook.com/company/' + username + '\/?$', 'i') },
1356
- ],
1357
- 'deleted': { $ne: true }
1358
- })
1359
- .exec().then(function(orgs) {orgs = _.map(orgs, function(org) {
1360
- return helpers.cleanOrg(org, options.CUSTOMER_ID);
1366
+ try {
1367
+ const orgs = await this
1368
+ .find({ $or:
1369
+ [
1370
+ { 'social.facebook': new RegExp('facebook.com/' + username + '\/?$', 'i') },
1371
+ { 'social.linkedin': new RegExp('linkedin.com/company/' + username + '\/?$', 'i') },
1372
+ { 'social.twitter': new RegExp('twitter.com/' + username + '\/?$', 'i') },
1373
+ { 'crunchbase.url': new RegExp('crunchbase.com/organization/' + username + '\/?$', 'i') },
1374
+ { 'pitchbook.url': new RegExp('pitchbook.com/company/' + username + '\/?$', 'i') },
1375
+ ],
1376
+ 'deleted': { $ne: true }
1361
1377
  });
1362
1378
 
1363
- return cb(null, orgs);
1379
+ const result = _.map(orgs, function(org) {
1380
+ return helpers.cleanOrg(org, options.CUSTOMER_ID);
1381
+ });
1364
1382
 
1365
- }).catch(function(err) { cb(err); });
1383
+ return cb(null, result);
1384
+ } catch(err) {
1385
+ return cb(err);
1386
+ }
1366
1387
 
1367
1388
  };
1368
1389
 
1369
- Organization.statics.findBySocials = function findBySocials(values, options, cb) {
1390
+ Organization.statics.findBySocials = async function findBySocials(values, options, cb) {
1370
1391
 
1371
1392
  // Extract usernames and append it to our standardized domains
1372
1393
 
1373
- const self = this;
1374
-
1375
1394
  if (!cb) { throw new Error('cb is required'); }
1376
1395
  if (!values) { return cb(new Error('values is required'), null); }
1377
1396
  if (!options) { return cb(new Error('options is required'), null); }
@@ -1384,37 +1403,49 @@ module.exports = function(mongoose, config) {
1384
1403
  let crunchbaseRegexes = _.map(values, function(value) { return new RegExp('crunchbase.com/organization/' + utilities.getUsernameFromUrl(value) + '\/?$', 'i') });
1385
1404
  let pitchbookRegexes = _.map(values, function(value) { return new RegExp('pitchbook.com/company/' + utilities.getUsernameFromUrl(value) + '\/?$', 'i') });
1386
1405
 
1387
- self
1388
- .find({ $or:
1389
- [
1390
- { 'social.facebook': {$in: facebookRegexes }},
1391
- { 'social.linkedin': {$in: linkedinRegexes }},
1392
- { 'social.twitter': {$in: twitterRegexes }},
1393
- { 'crunchbase.url': {$in: crunchbaseRegexes }},
1394
- { 'pitchbook.url': {$in: pitchbookRegexes }}
1395
- ],
1396
- 'deleted': { $ne: true }
1397
- }).exec().then(function(orgs) {orgs = _.map(orgs, function(org) {
1406
+ try {
1407
+ const orgs = await this
1408
+ .find({ $or:
1409
+ [
1410
+ { 'social.facebook': {$in: facebookRegexes }},
1411
+ { 'social.linkedin': {$in: linkedinRegexes }},
1412
+ { 'social.twitter': {$in: twitterRegexes }},
1413
+ { 'crunchbase.url': {$in: crunchbaseRegexes }},
1414
+ { 'pitchbook.url': {$in: pitchbookRegexes }}
1415
+ ],
1416
+ 'deleted': { $ne: true }
1417
+ });
1418
+
1419
+ const result = _.map(orgs, function(org) {
1398
1420
  return helpers.cleanOrg(org, options.CUSTOMER_ID);
1399
1421
  });
1400
1422
 
1401
- return cb(null, orgs);
1402
-
1403
- }).catch(function(err) { cb(err); });
1423
+ return cb(null, result);
1424
+ } catch(err) {
1425
+ return cb(err);
1426
+ }
1404
1427
 
1405
1428
  };
1406
1429
 
1407
- Organization.statics.findByDocument = function findByDocument(id, cb) {
1408
- this.findOne({ 'documents': id }).exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
1430
+ Organization.statics.findByDocument = async function findByDocument(id, cb) {
1431
+ try {
1432
+ const result = await this.findOne({ 'documents': id });
1433
+ return cb(null, result);
1434
+ } catch(err) {
1435
+ return cb(err);
1436
+ }
1409
1437
  };
1410
1438
 
1411
- Organization.statics.findByTotemUrl = function findByTotemUrl(url, cb) {
1412
- this.findOne({ 'customer.totemUrl': url }).exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
1439
+ Organization.statics.findByTotemUrl = async function findByTotemUrl(url, cb) {
1440
+ try {
1441
+ const result = await this.findOne({ 'customer.totemUrl': url });
1442
+ return cb(null, result);
1443
+ } catch(err) {
1444
+ return cb(err);
1445
+ }
1413
1446
  };
1414
1447
 
1415
- Organization.statics.getAcquisitions = function getAcquisitions(orgid, options, cb) {
1416
-
1417
- const self = this;
1448
+ Organization.statics.getAcquisitions = async function getAcquisitions(orgid, options, cb) {
1418
1449
 
1419
1450
  if (!cb) { throw new Error('cb is required'); }
1420
1451
  if (!orgid) { return cb(new Error('orgid is required'), null); }
@@ -1423,48 +1454,55 @@ module.exports = function(mongoose, config) {
1423
1454
  if (!options.CUSTOMER_ID) { return cb(new Error('options.CUSTOMER_ID is required'), null); }
1424
1455
  if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
1425
1456
 
1426
- self
1427
- .find(
1428
- { $or: [
1429
- { 'operating.acquired.private.by': orgid, 'operating.acquired.private.customer': options.CUSTOMER_ID, 'deleted': { $ne: true } },
1430
- { 'operating.acquired.public.by': orgid, 'deleted': { $ne: true } },
1431
- ]}
1432
- )
1433
- .select('name logoUrl')
1434
- .exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
1457
+ try {
1458
+ const result = await this
1459
+ .find(
1460
+ { $or: [
1461
+ { 'operating.acquired.private.by': orgid, 'operating.acquired.private.customer': options.CUSTOMER_ID, 'deleted': { $ne: true } },
1462
+ { 'operating.acquired.public.by': orgid, 'deleted': { $ne: true } },
1463
+ ]}
1464
+ )
1465
+ .select('name logoUrl');
1466
+ return cb(null, result);
1467
+ } catch(err) {
1468
+ return cb(err);
1469
+ }
1435
1470
  };
1436
1471
 
1437
- Organization.statics.getBoardsOfPerson = function getBoardsOfPerson(personId, cb) {
1438
-
1439
- var self = this;
1440
-
1441
- self
1442
- .find({
1443
- 'deleted': { $ne: true },
1444
- 'people': {
1445
- $elemMatch: {
1446
- 'person': personId,
1447
- 'board': { $in: ['chairman', 'director', 'observer'] },
1448
- 'current': true
1472
+ Organization.statics.getBoardsOfPerson = async function getBoardsOfPerson(personId, cb) {
1473
+
1474
+ try {
1475
+ const result = await this
1476
+ .find({
1477
+ 'deleted': { $ne: true },
1478
+ 'people': {
1479
+ $elemMatch: {
1480
+ 'person': personId,
1481
+ 'board': { $in: ['chairman', 'director', 'observer'] },
1482
+ 'current': true
1483
+ }
1449
1484
  }
1450
- }
1451
- })
1452
- .select('name logoUrl people')
1453
- .exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
1485
+ })
1486
+ .select('name logoUrl people');
1487
+ return cb(null, result);
1488
+ } catch(err) {
1489
+ return cb(err);
1490
+ }
1454
1491
 
1455
1492
  };
1456
1493
 
1457
- Organization.statics.getByFund = function getByFund(id, cb) {
1458
- var self = this;
1459
- self
1460
- .findOne({ 'funds': id, 'deleted': {$ne: true} })
1461
- .select('name logoUrl')
1462
- .exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
1494
+ Organization.statics.getByFund = async function getByFund(id, cb) {
1495
+ try {
1496
+ const result = await this
1497
+ .findOne({ 'funds': id, 'deleted': {$ne: true} })
1498
+ .select('name logoUrl');
1499
+ return cb(null, result);
1500
+ } catch(err) {
1501
+ return cb(err);
1502
+ }
1463
1503
  };
1464
1504
 
1465
- Organization.statics.getById = function getById(id, options, cb) {
1466
-
1467
- const self = this;
1505
+ Organization.statics.getById = async function getById(id, options, cb) {
1468
1506
 
1469
1507
  if (!cb) { throw new Error('cb is required'); }
1470
1508
  if (!id) { return cb(new Error('id is required'), null); }
@@ -1478,59 +1516,63 @@ module.exports = function(mongoose, config) {
1478
1516
  if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
1479
1517
  }
1480
1518
 
1481
- let lpPopulateOptions = { path: 'lps' };
1482
- if (!options.isWorkerProcess) { lpPopulateOptions.match = { customer: options.CUSTOMER_ID }; }
1483
-
1484
- var dealPopulateOptions = { path: 'deals' };
1485
- if (!options.isWorkerProcess) { dealPopulateOptions.match = { customer: options.CUSTOMER_ID }; }
1486
-
1487
- let query = self.findById(id);
1488
-
1489
- query.populate('people.person', 'name avatarUrl title contact doNotDisplay');
1490
- query.populate('related', 'name logoUrl');
1491
- query.populate('chairs.first', 'name avatarUrl title');
1492
- query.populate('chairs.second', 'name avatarUrl title');
1493
- query.populate('funds', 'name shortName hexColorCode abbreviation closeDate');
1494
- query.populate('operating.acquired.public.by', 'name logoUrl');
1495
- query.populate('operating.acquired.private.by', 'name logoUrl');
1496
- query.populate('operating.merged.public.with', 'name logoUrl');
1497
- query.populate('operating.merged.private.with', 'name logoUrl');
1498
- dealPopulateOptions.populate = [
1499
- { path: 'source.person' },
1500
- { path: 'history.account' }
1501
- ];
1502
- query.populate(dealPopulateOptions);
1503
- query.populate('documents');
1504
-
1505
- if (options.role !== 'none') {
1506
- lpPopulateOptions.populate = { path: 'transactions.fund', select: 'shortName' };
1507
- query.populate(lpPopulateOptions);
1508
- }
1519
+ try {
1520
+ let lpPopulateOptions = { path: 'lps' };
1521
+ if (!options.isWorkerProcess) { lpPopulateOptions.match = { customer: options.CUSTOMER_ID }; }
1522
+
1523
+ var dealPopulateOptions = { path: 'deals' };
1524
+ if (!options.isWorkerProcess) { dealPopulateOptions.match = { customer: options.CUSTOMER_ID }; }
1525
+
1526
+ let query = this.findById(id);
1527
+
1528
+ query.populate('people.person', 'name avatarUrl title contact doNotDisplay');
1529
+ query.populate('related', 'name logoUrl');
1530
+ query.populate('chairs.first', 'name avatarUrl title');
1531
+ query.populate('chairs.second', 'name avatarUrl title');
1532
+ query.populate('funds', 'name shortName hexColorCode abbreviation closeDate');
1533
+ query.populate('operating.acquired.public.by', 'name logoUrl');
1534
+ query.populate('operating.acquired.private.by', 'name logoUrl');
1535
+ query.populate('operating.merged.public.with', 'name logoUrl');
1536
+ query.populate('operating.merged.private.with', 'name logoUrl');
1537
+ dealPopulateOptions.populate = [
1538
+ { path: 'source.person' },
1539
+ { path: 'history.account' }
1540
+ ];
1541
+ query.populate(dealPopulateOptions);
1542
+ query.populate('documents');
1543
+
1544
+ if (options.role !== 'none') {
1545
+ lpPopulateOptions.populate = { path: 'transactions.fund', select: 'shortName' };
1546
+ query.populate(lpPopulateOptions);
1547
+ }
1509
1548
 
1510
- query.exec().then(function(result) {if (options.isWorkerProcess) { return cb(null, result); }
1549
+ const result = await query;
1550
+ if (options.isWorkerProcess) { return cb(null, result); }
1511
1551
  else { return cb(null, helpers.cleanOrg(result, options.CUSTOMER_ID)); }
1512
-
1513
- }).catch(function(err) { cb(err); });
1552
+ } catch(err) {
1553
+ return cb(err);
1554
+ }
1514
1555
 
1515
1556
  };
1516
1557
 
1517
- Organization.statics.getByIdSkinny = function getByIdSkinny(id, cb) {
1518
-
1519
- const self = this;
1558
+ Organization.statics.getByIdSkinny = async function getByIdSkinny(id, cb) {
1520
1559
 
1521
1560
  if (!cb) { throw new Error('cb is required'); }
1522
1561
  if (!id) { return cb(new Error('id is required'), null); }
1523
1562
 
1524
- const query = self.findById(id);
1525
- query.select('name aliases website websiteAliases logoUrl');
1563
+ try {
1564
+ const query = this.findById(id);
1565
+ query.select('name aliases website websiteAliases logoUrl');
1526
1566
 
1527
- return query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
1567
+ const result = await query;
1568
+ return cb(null, result);
1569
+ } catch(err) {
1570
+ return cb(err);
1571
+ }
1528
1572
 
1529
1573
  };
1530
1574
 
1531
- Organization.statics.getByIds = function getByIds(ids, options, cb) {
1532
-
1533
- const self = this;
1575
+ Organization.statics.getByIds = async function getByIds(ids, options, cb) {
1534
1576
 
1535
1577
  if (!cb) { throw new Error('cb is required'); }
1536
1578
  if (!ids) { return cb(new Error('ids is required'), null); }
@@ -1543,19 +1585,20 @@ module.exports = function(mongoose, config) {
1543
1585
  if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
1544
1586
  }
1545
1587
 
1546
- let query = self.find( {'_id': { $in : ids } } );
1547
- query.populate('documents');
1588
+ try {
1589
+ let query = this.find( {'_id': { $in : ids } } );
1590
+ query.populate('documents');
1548
1591
 
1549
- query.exec().then(function(result) {if (options.isWorkerProcess) { return cb(null, result); }
1592
+ const result = await query;
1593
+ if (options.isWorkerProcess) { return cb(null, result); }
1550
1594
  else { return cb(null, _.map(result, function(r) { return helpers.cleanOrg(r, options.CUSTOMER_ID) })) }
1551
-
1552
- }).catch(function(err) { cb(err); });
1595
+ } catch(err) {
1596
+ return cb(err);
1597
+ }
1553
1598
 
1554
1599
  };
1555
1600
 
1556
- Organization.statics.getChairsOfPerson = function getChairsOfPerson(personId, options, cb) {
1557
-
1558
- const self = this;
1601
+ Organization.statics.getChairsOfPerson = async function getChairsOfPerson(personId, options, cb) {
1559
1602
 
1560
1603
  if (!cb) { throw new Error('cb is required'); }
1561
1604
  if (!personId) { return cb(new Error('personId is required'), null); }
@@ -1564,27 +1607,28 @@ module.exports = function(mongoose, config) {
1564
1607
  if (!options.CUSTOMER_ID) { return cb(new Error('options.CUSTOMER_ID is required'), null); }
1565
1608
  if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
1566
1609
 
1567
- self
1568
- .find({ $or:
1569
- [
1570
- { 'chairs.first': personId },
1571
- { 'chairs.second': personId }
1572
- ],
1573
- 'deleted': { $ne: true }
1574
- })
1575
- .exec().then(function(orgs) {orgs = _.map(orgs, function(org) {
1576
- return helpers.cleanOrg(org, options.CUSTOMER_ID);
1610
+ try {
1611
+ const orgs = await this
1612
+ .find({ $or:
1613
+ [
1614
+ { 'chairs.first': personId },
1615
+ { 'chairs.second': personId }
1616
+ ],
1617
+ 'deleted': { $ne: true }
1577
1618
  });
1578
1619
 
1579
- return cb(null, orgs);
1620
+ const result = _.map(orgs, function(org) {
1621
+ return helpers.cleanOrg(org, options.CUSTOMER_ID);
1622
+ });
1580
1623
 
1581
- }).catch(function(err) { cb(err); });
1624
+ return cb(null, result);
1625
+ } catch(err) {
1626
+ return cb(err);
1627
+ }
1582
1628
 
1583
1629
  };
1584
1630
 
1585
- Organization.statics.getDocumentsForOrgs = function getDocumentsForOrgs(orgIds, customerId, startDate, endDate, options, cb) {
1586
-
1587
- const self = this;
1631
+ Organization.statics.getDocumentsForOrgs = async function getDocumentsForOrgs(orgIds, customerId, startDate, endDate, options, cb) {
1588
1632
 
1589
1633
  if (!cb) {
1590
1634
  throw new Error('cb is required');
@@ -1605,30 +1649,31 @@ module.exports = function(mongoose, config) {
1605
1649
  return cb(new Error('endDate is required'), null);
1606
1650
  }
1607
1651
 
1608
- let query = self
1609
- .find( {'_id': { $in : orgIds } } )
1610
- .select('_id documents')
1611
- .populate({
1612
- path: 'documents',
1613
- match: {
1614
- customer: customerId,
1615
- createdOn: { $gte: startDate, $lte: endDate }
1616
- },
1617
- })
1652
+ try {
1653
+ let query = this
1654
+ .find( {'_id': { $in : orgIds } } )
1655
+ .select('_id documents')
1656
+ .populate({
1657
+ path: 'documents',
1658
+ match: {
1659
+ customer: customerId,
1660
+ createdOn: { $gte: startDate, $lte: endDate }
1661
+ },
1662
+ })
1618
1663
 
1619
- query.exec().then(function(result) {result = _.filter(result, function(org) {
1664
+ const result = await query;
1665
+ const filtered = _.filter(result, function(org) {
1620
1666
  return org.documents.length > 0;
1621
1667
  });
1622
1668
 
1623
- return cb(null, result);
1624
-
1625
- }).catch(function(err) { cb(err); });
1669
+ return cb(null, filtered);
1670
+ } catch(err) {
1671
+ return cb(err);
1672
+ }
1626
1673
 
1627
1674
  };
1628
1675
 
1629
- Organization.statics.getValuationsForOrgs = function getValuationForOrgs(orgIds, customerId, startDate, endDate, options, cb) {
1630
-
1631
- const self = this;
1676
+ Organization.statics.getValuationsForOrgs = async function getValuationForOrgs(orgIds, customerId, startDate, endDate, options, cb) {
1632
1677
 
1633
1678
  if (!cb) {
1634
1679
  throw new Error('cb is required');
@@ -1649,23 +1694,24 @@ module.exports = function(mongoose, config) {
1649
1694
  return cb(new Error('endDate is required'), null);
1650
1695
  }
1651
1696
 
1652
- let query = self
1653
- .find( {'_id': { $in : orgIds } } )
1654
- .select('_id valuations');
1697
+ try {
1698
+ let query = this
1699
+ .find( {'_id': { $in : orgIds } } )
1700
+ .select('_id valuations');
1655
1701
 
1656
- query.exec().then(function(orgs) {orgs = _.map(orgs, function(org) {
1702
+ const orgs = await query;
1703
+ const result = _.map(orgs, function(org) {
1657
1704
  return helpers.cleanOrg(org, customerId);
1658
1705
  });
1659
1706
 
1660
- return cb(null, orgs);
1661
-
1662
- }).catch(function(err) { cb(err); });
1707
+ return cb(null, result);
1708
+ } catch(err) {
1709
+ return cb(err);
1710
+ }
1663
1711
 
1664
1712
  };
1665
1713
 
1666
- Organization.statics.getNotesForOrgs = function getNotesForOrgs(orgIds, customerId, startDate, endDate, options, cb) {
1667
-
1668
- const self = this;
1714
+ Organization.statics.getNotesForOrgs = async function getNotesForOrgs(orgIds, customerId, startDate, endDate, options, cb) {
1669
1715
 
1670
1716
  if (!cb) {
1671
1717
  throw new Error('cb is required');
@@ -1686,46 +1732,49 @@ module.exports = function(mongoose, config) {
1686
1732
  return cb(new Error('endDate is required'), null);
1687
1733
  }
1688
1734
 
1689
- let query = self
1690
- .find( {'_id': { $in : orgIds } } )
1691
- .select('_id notes')
1692
- .populate({
1693
- path: 'notes',
1694
- match: {
1695
- customer: customerId,
1696
- createdOn: { $gte: startDate, $lte: endDate }
1697
- },
1698
- populate: {
1699
- path: 'createdBy',
1700
- select: 'name avatarUrl'
1701
- }
1702
- })
1735
+ try {
1736
+ let query = this
1737
+ .find( {'_id': { $in : orgIds } } )
1738
+ .select('_id notes')
1739
+ .populate({
1740
+ path: 'notes',
1741
+ match: {
1742
+ customer: customerId,
1743
+ createdOn: { $gte: startDate, $lte: endDate }
1744
+ },
1745
+ populate: {
1746
+ path: 'createdBy',
1747
+ select: 'name avatarUrl'
1748
+ }
1749
+ })
1703
1750
 
1704
- query.exec().then(function(result) {result = _.filter(result, function(org) {
1751
+ const result = await query;
1752
+ const filtered = _.filter(result, function(org) {
1705
1753
  return org.notes.length > 0;
1706
1754
  });
1707
1755
 
1708
- return cb(null, result);
1709
-
1710
- }).catch(function(err) { cb(err); });
1756
+ return cb(null, filtered);
1757
+ } catch(err) {
1758
+ return cb(err);
1759
+ }
1711
1760
 
1712
1761
  };
1713
1762
 
1714
- Organization.statics.getList = function getList(cb) {
1763
+ Organization.statics.getList = async function getList(cb) {
1715
1764
 
1716
- const self = this;
1717
-
1718
- self
1719
- .find({'deleted': {$ne: true}})
1720
- .select('name website websiteAliases')
1721
- .lean()
1722
- .exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
1765
+ try {
1766
+ const result = await this
1767
+ .find({'deleted': {$ne: true}})
1768
+ .select('name website websiteAliases')
1769
+ .lean();
1770
+ return cb(null, result);
1771
+ } catch(err) {
1772
+ return cb(err);
1773
+ }
1723
1774
 
1724
1775
  };
1725
1776
 
1726
- Organization.statics.getLpOrgs = function getLpOrgs(lpids, options, cb) {
1727
-
1728
- const self = this;
1777
+ Organization.statics.getLpOrgs = async function getLpOrgs(lpids, options, cb) {
1729
1778
 
1730
1779
  if (!cb) { throw new Error('cb is required'); }
1731
1780
  if (!lpids) { return cb(new Error('lpids is required'), null); }
@@ -1733,27 +1782,27 @@ module.exports = function(mongoose, config) {
1733
1782
  if (!options.CUSTOMER_ID) { return cb(new Error('options.CUSTOMER_ID is required'), null); }
1734
1783
  if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
1735
1784
 
1736
- let query = self.find({ 'lps': { $in : lpids }, 'deleted': {$ne: true} });
1785
+ try {
1786
+ let query = this.find({ 'lps': { $in : lpids }, 'deleted': {$ne: true} });
1737
1787
 
1738
- query.select('name logoUrl lps');
1739
- query.populate('lps', 'name customer');
1788
+ query.select('name logoUrl lps');
1789
+ query.populate('lps', 'name customer');
1740
1790
 
1741
- query.exec().then(function(orgs) {
1791
+ const orgs = await query;
1742
1792
  if (!orgs) { return cb(null, []); }
1743
1793
 
1744
- orgs = _.map(orgs, function(org) {
1794
+ const result = _.map(orgs, function(org) {
1745
1795
  return helpers.cleanOrg(org, options.CUSTOMER_ID);
1746
1796
  });
1747
1797
 
1748
- return cb(null, orgs);
1749
-
1750
- }).catch(function(err) { cb(err); });
1798
+ return cb(null, result);
1799
+ } catch(err) {
1800
+ return cb(err);
1801
+ }
1751
1802
 
1752
1803
  };
1753
1804
 
1754
- Organization.statics.getNotes = function getNotes(orgid, options, cb) {
1755
-
1756
- const self = this;
1805
+ Organization.statics.getNotes = async function getNotes(orgid, options, cb) {
1757
1806
 
1758
1807
  if (!cb) { throw new Error('cb is required'); }
1759
1808
  if (!orgid) { return cb(new Error('orgid is required'), null); }
@@ -1762,34 +1811,34 @@ module.exports = function(mongoose, config) {
1762
1811
  if (!options.CUSTOMER_ID) { return cb(new Error('options.CUSTOMER_ID is required'), null); }
1763
1812
  if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
1764
1813
 
1765
- let query = self.findById(orgid);
1814
+ try {
1815
+ let query = this.findById(orgid);
1766
1816
 
1767
- query.select('notes people.person');
1768
- query.populate({
1769
- path: 'people.person',
1770
- select: 'name avatarUrl title'
1771
- });
1772
- query.populate({
1773
- path: 'notes',
1774
- match: { customer: options.CUSTOMER_ID },
1775
- populate: { path: 'createdBy', select: 'name avatarUrl title' }
1776
- });
1777
- query.sort({'createdOn':-1});
1817
+ query.select('notes people.person');
1818
+ query.populate({
1819
+ path: 'people.person',
1820
+ select: 'name avatarUrl title'
1821
+ });
1822
+ query.populate({
1823
+ path: 'notes',
1824
+ match: { customer: options.CUSTOMER_ID },
1825
+ populate: { path: 'createdBy', select: 'name avatarUrl title' }
1826
+ });
1827
+ query.sort({'createdOn':-1});
1778
1828
 
1779
- query.exec().then(function(org) {
1829
+ const org = await query;
1780
1830
  if (!org) { return cb(null, []); }
1781
1831
 
1782
- org = helpers.cleanOrg(org, options.CUSTOMER_ID);
1783
-
1784
- return cb(null, org);
1832
+ const result = helpers.cleanOrg(org, options.CUSTOMER_ID);
1785
1833
 
1786
- }).catch(function(err) { cb(err); });
1834
+ return cb(null, result);
1835
+ } catch(err) {
1836
+ return cb(err);
1837
+ }
1787
1838
 
1788
1839
  };
1789
1840
 
1790
- Organization.statics.getSources = function(id, options, cb) {
1791
-
1792
- const self = this;
1841
+ Organization.statics.getSources = async function(id, options, cb) {
1793
1842
 
1794
1843
  if (!cb) { throw new Error('cb is required'); }
1795
1844
  if (!id) { return cb(new Error('id is required'), null); }
@@ -1798,58 +1847,60 @@ module.exports = function(mongoose, config) {
1798
1847
  if (!options.CUSTOMER_ID) { return cb(new Error('options.CUSTOMER_ID is required'), null); }
1799
1848
  if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
1800
1849
 
1801
- let query = self.findById(id);
1802
- query.select('people');
1803
- query.populate({
1804
- path: 'people.person',
1805
- select: 'name avatarUrl title doNotDisplay sources',
1806
- populate: { path: 'sources.person', select: 'name avatarUrl title doNotDisplay' }
1807
- });
1850
+ try {
1851
+ let query = this.findById(id);
1852
+ query.select('people');
1853
+ query.populate({
1854
+ path: 'people.person',
1855
+ select: 'name avatarUrl title doNotDisplay sources',
1856
+ populate: { path: 'sources.person', select: 'name avatarUrl title doNotDisplay' }
1857
+ });
1808
1858
 
1809
- // get the org requested
1810
- query.exec().then(function(org) {if (!org) { return cb(null, null); }
1859
+ // get the org requested
1860
+ const org = await query;
1861
+ if (!org) { return cb(null, null); }
1811
1862
 
1812
1863
  // get the customer requesting org
1813
- self.findById(options.CUSTOMER_ID).then(function(customer) {if (!customer) { return cb(null, null); }
1814
-
1815
- let current = [];
1816
- let past = [];
1817
- let all = [];
1818
-
1819
- // combined list of sources from each person at org
1820
- let orgSources = helpers.getPeopleSources(_.pluck(org.people, 'person'));
1821
-
1822
- // loop through customer's people and categorize sources
1823
- _.each(customer.people, function(p) {
1864
+ const customer = await this.findById(options.CUSTOMER_ID);
1865
+ if (!customer) { return cb(null, null); }
1824
1866
 
1825
- var source = _.find(orgSources, function(os) {
1826
- if (!os || !os.person || !os.person._id) return false;
1827
- return os.person._id.toString() === p.person.toString();
1828
- });
1867
+ let current = [];
1868
+ let past = [];
1869
+ let all = [];
1829
1870
 
1830
- if (!source) return;
1871
+ // combined list of sources from each person at org
1872
+ let orgSources = helpers.getPeopleSources(_.pluck(org.people, 'person'));
1831
1873
 
1832
- if (p.current) current.push(source);
1833
- if (!p.current) past.push(source);
1834
- all.push(source);
1874
+ // loop through customer's people and categorize sources
1875
+ _.each(customer.people, function(p) {
1835
1876
 
1877
+ var source = _.find(orgSources, function(os) {
1878
+ if (!os || !os.person || !os.person._id) return false;
1879
+ return os.person._id.toString() === p.person.toString();
1836
1880
  });
1837
1881
 
1838
- // todo - fix sorting
1839
- // return categorized sources
1840
- return cb(null, {
1841
- all: all, //helpers.sortPeopleSources(all, options.CUSTOMER_ID),
1842
- current: current, //helpers.sortPeopleSources(current, options.CUSTOMER_ID),
1843
- past: past //helpers.sortPeopleSources(past, options.CUSTOMER_ID)
1844
- });
1882
+ if (!source) return;
1845
1883
 
1846
- }).catch(function(err) { cb(err); });
1884
+ if (p.current) current.push(source);
1885
+ if (!p.current) past.push(source);
1886
+ all.push(source);
1847
1887
 
1848
- }).catch(function(err) { cb(err); });
1888
+ });
1889
+
1890
+ // todo - fix sorting
1891
+ // return categorized sources
1892
+ return cb(null, {
1893
+ all: all, //helpers.sortPeopleSources(all, options.CUSTOMER_ID),
1894
+ current: current, //helpers.sortPeopleSources(current, options.CUSTOMER_ID),
1895
+ past: past //helpers.sortPeopleSources(past, options.CUSTOMER_ID)
1896
+ });
1897
+ } catch(err) {
1898
+ return cb(err);
1899
+ }
1849
1900
 
1850
1901
  };
1851
1902
 
1852
- Organization.statics.modify = function(filter, update, cb) {
1903
+ Organization.statics.modify = async function(filter, update, cb) {
1853
1904
 
1854
1905
  // VERY IMPORTANT NOTE
1855
1906
  // findByIdAndUpdate and findOneAndUpdate do not trigger pre-save hook so that code will not run here
@@ -1859,17 +1910,20 @@ module.exports = function(mongoose, config) {
1859
1910
  if (!filter) { return cb(new Error('filter is required'), null); }
1860
1911
  if (!update) { return cb(new Error('update is required'), null); }
1861
1912
 
1862
- var self = this;
1863
-
1864
1913
  // https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
1865
1914
  // options runValidators defaults false which is ok since we have upsert false
1866
1915
  // new returns the updated document
1867
1916
 
1868
- self.findOneAndUpdate(filter, update, { upsert: false, new: true }).then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
1917
+ try {
1918
+ const result = await this.findOneAndUpdate(filter, update, { upsert: false, new: true });
1919
+ return cb(null, result);
1920
+ } catch(err) {
1921
+ return cb(err);
1922
+ }
1869
1923
 
1870
1924
  };
1871
1925
 
1872
- Organization.statics.modifyById = function(id, update, cb) {
1926
+ Organization.statics.modifyById = async function(id, update, cb) {
1873
1927
 
1874
1928
  // VERY IMPORTANT NOTE
1875
1929
  // findByIdAndUpdate and findOneAndUpdate do not trigger pre-save hook so that code will not run here
@@ -1880,41 +1934,46 @@ module.exports = function(mongoose, config) {
1880
1934
  if (!mongoose.Types.ObjectId.isValid(id)) { return cb(new Error('id is not a valid ObjectId'), null); }
1881
1935
  if (!update) { return cb(new Error('update is required'), null); }
1882
1936
 
1883
- var self = this;
1884
-
1885
1937
  // https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
1886
1938
  // options runValidators defaults false which is ok since we have upsert false
1887
1939
  // new returns the updated document
1888
1940
 
1889
- self.findByIdAndUpdate(id, update, { upsert: false, new: true }).then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
1941
+ try {
1942
+ const result = await this.findByIdAndUpdate(id, update, { upsert: false, new: true });
1943
+ return cb(null, result);
1944
+ } catch(err) {
1945
+ return cb(err);
1946
+ }
1890
1947
 
1891
1948
  };
1892
1949
 
1893
- Organization.statics.removeCustomerFilters = function(customerId, cb) {
1894
-
1895
- var self = this;
1950
+ Organization.statics.removeCustomerFilters = async function(customerId, cb) {
1896
1951
 
1897
- self
1898
- .updateMany(
1899
- { 'filters.customer': customerId },
1900
- { $pull : { 'filters': { 'customer': customerId } } },
1901
- { multi : true }
1902
- )
1903
- .exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
1952
+ try {
1953
+ const result = await this
1954
+ .updateMany(
1955
+ { 'filters.customer': customerId },
1956
+ { $pull : { 'filters': { 'customer': customerId } } }
1957
+ );
1958
+ return cb(null, result);
1959
+ } catch(err) {
1960
+ return cb(err);
1961
+ }
1904
1962
 
1905
1963
  };
1906
1964
 
1907
- Organization.statics.removeCustomerValuations = function(customerId, cb) {
1965
+ Organization.statics.removeCustomerValuations = async function(customerId, cb) {
1908
1966
 
1909
- var self = this;
1910
-
1911
- self
1912
- .updateMany(
1913
- { 'valuations.customer': customerId },
1914
- { $pull : { 'valuations': { 'customer': customerId } } },
1915
- { multi : true }
1916
- )
1917
- .exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
1967
+ try {
1968
+ const result = await this
1969
+ .updateMany(
1970
+ { 'valuations.customer': customerId },
1971
+ { $pull : { 'valuations': { 'customer': customerId } } }
1972
+ );
1973
+ return cb(null, result);
1974
+ } catch(err) {
1975
+ return cb(err);
1976
+ }
1918
1977
 
1919
1978
  };
1920
1979
 
@@ -1959,7 +2018,7 @@ module.exports = function(mongoose, config) {
1959
2018
  //
1960
2019
  // };
1961
2020
 
1962
- Organization.statics.search = function search(data, options, cb) {
2021
+ Organization.statics.search = async function search(data, options, cb) {
1963
2022
 
1964
2023
  // search for orgs based on fields provided
1965
2024
  // data format, each field is optional
@@ -2092,31 +2151,27 @@ module.exports = function(mongoose, config) {
2092
2151
 
2093
2152
  var query = buildQuery(data, options);
2094
2153
 
2095
- this
2096
- .find(query)
2097
- .populate('merged')
2098
- .select('logoUrl name description related website')
2099
- .exec().then(function(orgs) {
2154
+ try {
2155
+ let orgs = await this
2156
+ .find(query)
2157
+ .populate('merged')
2158
+ .select('logoUrl name description related website');
2100
2159
  if (!orgs) return cb(null, null);
2101
- else {
2102
-
2103
- // remove related from top level
2104
- if (options.removeRelatedFromTopLevel) orgs = removeRelatedFromTopLevel(orgs);
2105
2160
 
2106
- // sort by name
2107
- orgs = _.sortBy(orgs, function(orgs) { return orgs.name; });
2161
+ // remove related from top level
2162
+ if (options.removeRelatedFromTopLevel) orgs = removeRelatedFromTopLevel(orgs);
2108
2163
 
2109
- return cb(null, orgs);
2164
+ // sort by name
2165
+ orgs = _.sortBy(orgs, function(orgs) { return orgs.name; });
2110
2166
 
2111
- }
2112
-
2113
- }).catch(function(err) { cb(err); });
2167
+ return cb(null, orgs);
2168
+ } catch(err) {
2169
+ return cb(err);
2170
+ }
2114
2171
 
2115
2172
  };
2116
2173
 
2117
- Organization.statics.search2 = function search2(value, options, cb) {
2118
-
2119
- const self = this;
2174
+ Organization.statics.search2 = async function search2(value, options, cb) {
2120
2175
 
2121
2176
  if (!cb) { throw new Error('cb is required'); }
2122
2177
  if (!value) { return cb(new Error('value is required'), null); }
@@ -2140,66 +2195,70 @@ module.exports = function(mongoose, config) {
2140
2195
  'stakeholders'
2141
2196
  ];
2142
2197
 
2143
- self.aggregate([
2144
- {
2145
- "$search": {
2146
- index: 'default',
2147
- compound: {
2148
- should: [
2149
- {
2150
- text: {
2151
- query: value,
2152
- path: path
2153
- }
2154
- },
2155
- {
2156
- autocomplete: {
2157
- query: value,
2158
- path: 'name'
2159
- }
2160
- },
2161
- {
2162
- autocomplete: {
2163
- query: value,
2164
- path: 'aliases'
2198
+ try {
2199
+ const orgs = await this.aggregate([
2200
+ {
2201
+ "$search": {
2202
+ index: 'default',
2203
+ compound: {
2204
+ should: [
2205
+ {
2206
+ text: {
2207
+ query: value,
2208
+ path: path
2209
+ }
2210
+ },
2211
+ {
2212
+ autocomplete: {
2213
+ query: value,
2214
+ path: 'name'
2215
+ }
2216
+ },
2217
+ {
2218
+ autocomplete: {
2219
+ query: value,
2220
+ path: 'aliases'
2221
+ }
2165
2222
  }
2166
- }
2167
- ],
2168
- must: [
2169
- {
2170
- equals: {
2171
- path: 'deleted',
2172
- value: false
2223
+ ],
2224
+ must: [
2225
+ {
2226
+ equals: {
2227
+ path: 'deleted',
2228
+ value: false
2229
+ }
2173
2230
  }
2174
- }
2175
- ],
2176
- minimumShouldMatch: 1
2231
+ ],
2232
+ minimumShouldMatch: 1
2233
+ }
2234
+ }
2235
+ },
2236
+ {
2237
+ $limit: options.limit,
2238
+ },
2239
+ {
2240
+ $project: {
2241
+ name: 1,
2242
+ logoUrl: 1,
2243
+ website: 1,
2244
+ websiteAliases: 1,
2245
+ score: { $meta: "searchScore" }
2177
2246
  }
2178
2247
  }
2179
- },
2180
- {
2181
- $limit: options.limit,
2182
- },
2183
- {
2184
- $project: {
2185
- name: 1,
2186
- logoUrl: 1,
2187
- website: 1,
2188
- websiteAliases: 1,
2189
- score: { $meta: "searchScore" }
2190
- }
2191
- }
2192
- ]).exec().then(function(orgs) {orgs = _.map(orgs, function(org) {
2248
+ ]);
2249
+
2250
+ const result = _.map(orgs, function(org) {
2193
2251
  return helpers.cleanOrg(org, options.CUSTOMER_ID);
2194
2252
  });
2195
2253
 
2196
- return cb(null, orgs);
2197
-
2198
- }).catch(function(err) { cb(err); });
2254
+ return cb(null, result);
2255
+ } catch(err) {
2256
+ return cb(err);
2257
+ }
2199
2258
 
2200
2259
  };
2201
2260
 
2202
- Organization.statics.upsert = function(org, username, cb) {
2261
+ Organization.statics.upsert = async function(org, username, cb) {
2203
2262
 
2204
2263
  if (!org) { return cb(new Error('Organization is required'), null); }
2205
2264
  if (!username) { return cb(new Error('Username is required'), null); }
@@ -2210,7 +2269,12 @@ module.exports = function(mongoose, config) {
2210
2269
  // Clean up missing references
2211
2270
  //org.people = _.reject(org.people, function(item) { return !item.person; });
2212
2271
 
2213
- org.save().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
2272
+ try {
2273
+ const result = await org.save();
2274
+ return cb(null, result);
2275
+ } catch(err) {
2276
+ return cb(err);
2277
+ }
2214
2278
 
2215
2279
  };
2216
2280