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