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