@dhyasama/totem-models 1.0.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.
@@ -0,0 +1,750 @@
1
+ "use strict";
2
+
3
+ var
4
+
5
+ should = require("should"),
6
+ config = require('./config')['test'],
7
+ mongoose = require('mongoose'),
8
+ clearDB = require('mocha-mongoose')(config.db.uri, {noClear: true}),
9
+ Organization = mongoose.model('Organization'),
10
+ Person = mongoose.model('Person'),
11
+ Note = mongoose.model('Note');
12
+
13
+ var person, person1, person2;
14
+ var noteId;
15
+ var eventNote;
16
+
17
+ describe('Organization', function() {
18
+
19
+ var org1, org2, org3, org4, org5;
20
+ var mergeId = new mongoose.Types.ObjectId();
21
+
22
+ before(function (done) {
23
+ if (mongoose.connection.db) return done();
24
+ mongoose.connect(config.db.uri, done);
25
+ });
26
+
27
+ before(function (done) {
28
+ clearDB(done);
29
+ });
30
+
31
+ before(function (done) {
32
+
33
+ org2 = new Organization();
34
+ org2.name = 'No Thanks 2';
35
+ org2.slug = 'org2-slug';
36
+ org2.website = 'nothanks2.org';
37
+ org2.websiteAliases = [ 'myolddomain.tld' ];
38
+ org2.related = [];
39
+ //org2.investedIn = ['123', '456'];
40
+ org2.aliases.push('NT2');
41
+ org2.crunchbase.uuid = 'abc';
42
+ org2.crunchbase.url = 'abc';
43
+
44
+ org3 = new Organization();
45
+ org3.name = 'Fudge 3';
46
+ org3.slug = 'fudge-3';
47
+ org3.website = 'fromthesong.org';
48
+ org3.related = [];
49
+ org3.crunchbase.uuid = 'def';
50
+ org3.crunchbase.url = 'def';
51
+
52
+ org4 = new Organization();
53
+ org4.name = 'Lemonade 4';
54
+ org4.slug = 'lemonade-4';
55
+ org4.website = 'fromthesong.net';
56
+ org4.deleted = false;
57
+ org4.crunchbase.uuid = 'ghi';
58
+ org4.crunchbase.url = 'ghi';
59
+
60
+ org5 = new Organization();
61
+ org5.name = 'Fudgesicle';
62
+ org5.slug = 'fudgesicle';
63
+ org5.website = 'yum.io';
64
+ org5.deleted = false;
65
+ org5.aliases.push('sicle');
66
+ org5.crunchbase.uuid = 'jkl';
67
+ org5.crunchbase.url = 'jkl';
68
+
69
+ Organization.upsert(org2, 'test-user', function(err, no2) {
70
+ Organization.upsert(org3, 'test-user', function(err, no3) {
71
+ Organization.upsert(org4, 'test-user', function(err, no4) {
72
+ Organization.upsert(org5, 'test-user', function(err, no5) {
73
+ done();
74
+ });
75
+ });
76
+ });
77
+ });
78
+
79
+ });
80
+
81
+ before(function(done) {
82
+
83
+ person1 = new Person();
84
+ person1.name.first = 'Mischa';
85
+ person1.name.last = 'Barton';
86
+ person1.slug = 'mischa-barton';
87
+
88
+ // {
89
+ // calendarEventId: { type: Schema.ObjectId, ref: 'CalendarEvent', required: true },
90
+ // providerEventId: { type: String, required: true, index: true },
91
+ // totemCustomerId: { type: Schema.ObjectId, ref: 'Investor', required: true }, // todo - becomes org ref
92
+ // summary: { type: String, required: true },
93
+ // startTime: { type: Date },
94
+ // attendees: {
95
+ // internal: [{ type: Schema.ObjectId, ref: 'Person' }],
96
+ // external: [{ type: Schema.ObjectId, ref: 'Person' }]
97
+ // },
98
+ // notes: [ { type: Schema.ObjectId, ref: 'Note' } ]
99
+ // }
100
+
101
+ person1.calendarEventSummaries.push({
102
+ calendarEventId: new mongoose.Types.ObjectId(),
103
+ providerEventId: 'abc123',
104
+ totemCustomerId: new mongoose.Types.ObjectId(),
105
+ summary: 'Test 1'
106
+ });
107
+
108
+ Person.upsert(person1, 'test', function (err, newPerson) {
109
+
110
+ should.not.exist(err);
111
+ should.exist(newPerson);
112
+
113
+ person1 = newPerson;
114
+
115
+ return done();
116
+
117
+ });
118
+
119
+ });
120
+
121
+ before(function(done) {
122
+
123
+ person2 = new Person();
124
+ person2.name.first = 'Carrie';
125
+ person2.name.last = 'Coon';
126
+ person2.slug = 'carrie-coon';
127
+
128
+ person2.calendarEventSummaries.push({
129
+ calendarEventId: new mongoose.Types.ObjectId(),
130
+ providerEventId: 'def456',
131
+ totemCustomerId: new mongoose.Types.ObjectId(),
132
+ summary: 'Test 2'
133
+ });
134
+
135
+ Person.upsert(person2, 'test', function (err, newPerson) {
136
+
137
+ should.not.exist(err);
138
+ should.exist(newPerson);
139
+
140
+ person2 = newPerson;
141
+
142
+ return done();
143
+
144
+ });
145
+
146
+ });
147
+
148
+ before(function(done) {
149
+
150
+ person = new Person();
151
+ person.name.first = 'Stan';
152
+ person.name.last = 'Beeman';
153
+ person.slug = 'stan-beeman';
154
+ person.avatarUrl = 'https://hello.com';
155
+
156
+ Person.upsert(person, 'test', function(err, newPerson) {
157
+
158
+ should.not.exist(err);
159
+ should.exist(newPerson);
160
+
161
+ var eventSummary = {
162
+ calendarEventId: new mongoose.Types.ObjectId(),
163
+ providerEventId: 'some_other_random_data',
164
+ totemCustomerId: config.CUSTOMER_ID,
165
+ summary: 'The Russians are coming',
166
+ startTime: new Date(),
167
+ attendees: {
168
+ internal: [person1],
169
+ external: [person2]
170
+ }
171
+ };
172
+
173
+ newPerson.calendarEventSummaries.push(eventSummary);
174
+
175
+ Person.upsert(newPerson, 'test', function(err, newNewPerson) {
176
+
177
+ should.not.exist(err);
178
+ should.exist(newNewPerson);
179
+
180
+ person = newNewPerson;
181
+
182
+ person.calendarEventSummaries.length.should.equal(1);
183
+
184
+ return done();
185
+
186
+ });
187
+
188
+ });
189
+
190
+
191
+ });
192
+
193
+ before(function(done) {
194
+
195
+ var providerEventId = 'some_other_random_data';
196
+ var creator = person._id;
197
+ var text = 'Sweet note bro!';
198
+
199
+ Note.createForCalendarEvent(providerEventId, creator, text, function(err, result) {
200
+
201
+ should.not.exist(err);
202
+ should.exist(result);
203
+
204
+ //result.notes.length.should.equal(1);
205
+ //result.id.toString().should.equal(person2.id.toString());
206
+
207
+ eventNote = result;
208
+
209
+ return done();
210
+
211
+ });
212
+
213
+ });
214
+
215
+ it('it soft deletes an org', function(done) {
216
+
217
+ org4.deleteOrg();
218
+
219
+ Organization.upsert(org4, 'test-user', function(err, result) {
220
+ should.not.exist(err);
221
+ should.exist(result);
222
+ org4 = result;
223
+ org4.deleted.should.equal(true);
224
+ done();
225
+ });
226
+ }); // end soft delete org
227
+
228
+ it('it relates an org and an org', function(done) {
229
+
230
+ org3.relateOrg(org2.id);
231
+
232
+ Organization.upsert(org3, 'test-user', function(err, result) {
233
+ should.not.exist(err);
234
+ should.exist(result);
235
+ org3 = result;
236
+ org3.related.length.should.equal(1);
237
+ done();
238
+ });
239
+ }); // end relate 2 orgs
240
+
241
+ it('adds an org', function(done) {
242
+
243
+ org1 = new Organization();
244
+ org1.name = 'Testing Org 1';
245
+ org1.slug = 'testing-org-1';
246
+ org1.website = 'testingorg1.org';
247
+ org1.contact.email = [];
248
+ org1.contact.email.push({
249
+ email: 'work@org1.com',
250
+ type: 'work',
251
+ primary: true
252
+ });
253
+ org1.crunchbase.uuid = 'mno';
254
+ org1.crunchbase.url = 'mno';
255
+
256
+ Organization.upsert(org1, 'test-user', function(err, org) {
257
+ should.not.exist(err);
258
+ should.exist(org);
259
+ org.name.should.equal('Testing Org 1');
260
+ org1 = org;
261
+ done();
262
+ });
263
+ }); // end adds an org
264
+
265
+ it('adds a basic field to org', function(done) {
266
+
267
+ org1.newBasic('slug', 'org1-slug');
268
+
269
+ Organization.upsert(org1, 'test-user', function(err, org) {
270
+ should.not.exist(err);
271
+ should.exist(org);
272
+ org.slug.should.equal('org1-slug');
273
+ org1 = org;
274
+ done();
275
+ });
276
+ }); // end adds a basic field
277
+
278
+ it('updates a basic field on org', function(done) {
279
+
280
+ org1.updateBasic('website', 'updatedorg1.com');
281
+
282
+ Organization.upsert(org1, 'test-user', function(err, org) {
283
+ should.not.exist(err);
284
+ should.exist(org);
285
+ org.website.should.equal('updatedorg1.com');
286
+ org1 = org;
287
+ done();
288
+ });
289
+ }); // end update a basic field
290
+
291
+ it('finds a fuzzy match by name', function(done) {
292
+
293
+ Organization.search({
294
+ name: 'org 1'
295
+ }, { admin: true, pcs: [] }, function(err, results) {
296
+ should.not.exist(err);
297
+ should.exist(results);
298
+ results.length.should.equal(1);
299
+ done();
300
+ });
301
+
302
+ });
303
+
304
+ it('finds a fuzzy match by first word of name', function(done) {
305
+
306
+ Organization.search({
307
+ name: 'org 99'
308
+ }, { queryFirstWord: true, admin: true, pcs: [] }, function(err, results) {
309
+ should.not.exist(err);
310
+ should.exist(results);
311
+ results.length.should.equal(1);
312
+ done();
313
+ });
314
+
315
+ });
316
+
317
+ it('does not find a fuzzy match by name', function(done) {
318
+
319
+ Organization.search({
320
+ name: 'quick brown fox'
321
+ }, { admin: true, pcs: [] }, function(err, results) {
322
+ should.not.exist(err);
323
+ should.exist(results);
324
+ results.length.should.equal(0);
325
+ done();
326
+ });
327
+
328
+ });
329
+
330
+ it('does not find an exact match by name', function(done) {
331
+
332
+ Organization.search({
333
+ name: 'org 1'
334
+ }, { fuzzy: false, admin: true, pcs: [] }, function(err, results) {
335
+ should.not.exist(err);
336
+ should.exist(results);
337
+ results.length.should.equal(0);
338
+ done();
339
+ });
340
+
341
+ });
342
+
343
+ it('finds an exact match by name', function(done) {
344
+
345
+ Organization.search({
346
+ name: 'testing org 1'
347
+ }, { fuzzy: false, admin: true, pcs: [] }, function(err, results) {
348
+ should.not.exist(err);
349
+ should.exist(results);
350
+ results.length.should.equal(1);
351
+ done();
352
+ });
353
+
354
+ });
355
+
356
+ it('finds a fuzzy match by domain', function(done) {
357
+
358
+ Organization.search({
359
+ domain: 'thesong'
360
+ }, { admin: true }, function(err, results) {
361
+ should.not.exist(err);
362
+ should.exist(results);
363
+ results.length.should.equal(1);
364
+ done();
365
+ });
366
+
367
+ });
368
+
369
+ it('does not find a fuzzy match by domain', function(done) {
370
+
371
+ Organization.search({
372
+ domain: 'somethingthatisnotthere.io'
373
+ }, { admin: true }, function(err, results) {
374
+ should.not.exist(err);
375
+ should.exist(results);
376
+ results.length.should.equal(0);
377
+ done();
378
+ });
379
+
380
+ });
381
+
382
+ it('finds an exact match by domain', function(done) {
383
+
384
+ Organization.search({
385
+ domain: 'fromTHEsong.org'
386
+ }, { fuzzy: false, admin: true }, function(err, results) {
387
+ should.not.exist(err);
388
+ should.exist(results);
389
+ results.length.should.equal(1);
390
+ done();
391
+ });
392
+
393
+ });
394
+
395
+ it('does not find an exact match by domain', function(done) {
396
+
397
+ Organization.search({
398
+ domain: 'somethingthatisnotthere.io'
399
+ }, { fuzzy: false, admin: true }, function(err, results) {
400
+ should.not.exist(err);
401
+ should.exist(results);
402
+ results.length.should.equal(0);
403
+ done();
404
+ });
405
+
406
+ });
407
+
408
+ it('searches by name with a fuzzy alias match', function(done) {
409
+
410
+ Organization.search({
411
+ name: 'sicle'
412
+ }, { fuzzy: true, admin: true }, function(err, results) {
413
+ should.not.exist(err);
414
+ should.exist(results);
415
+ results.length.should.equal(1);
416
+ done();
417
+ });
418
+
419
+ });
420
+
421
+ it('searches by name with a strict alias match', function(done) {
422
+
423
+ Organization.search({
424
+ name: 'NT2'
425
+ }, { fuzzy: false, admin: true }, function(err, results) {
426
+ should.not.exist(err);
427
+ should.exist(results);
428
+ results.length.should.equal(1);
429
+ done();
430
+ });
431
+
432
+ });
433
+
434
+ it('finds org by slug', function(done) {
435
+
436
+ Organization.findBySlug('org2-slug', function(err, org) {
437
+ should.not.exist(err);
438
+ should.exist(org);
439
+ org.length.should.equal(1);
440
+ done();
441
+ });
442
+
443
+ });
444
+
445
+ it('finds org by slugs', function(done) {
446
+
447
+ Organization.findBySlugs(['org1-slug', 'org2-slug'], function(err, orgs) {
448
+ should.not.exist(err);
449
+ should.exist(orgs);
450
+ orgs.length.should.equal(2);
451
+ done();
452
+ });
453
+
454
+ });
455
+
456
+ it('adds a person to people', function(done) {
457
+
458
+ org1.addPerson({ person: person._id });
459
+ Organization.upsert(org1, 'test-user', function(err, updatedOrg) {
460
+ should.not.exist(err);
461
+ should.exist(updatedOrg);
462
+ updatedOrg.people.length.should.equal(1);
463
+ return done();
464
+ });
465
+
466
+ });
467
+
468
+ it('gets an org by id', function(done) {
469
+
470
+ Organization.getById(org1.id, function(err, org) {
471
+ should.not.exist(err);
472
+ should.exist(org);
473
+ org1 = org;
474
+ org1.id.should.equal(org.id);
475
+ org1.sources.length.should.equal(1);
476
+ return done();
477
+ });
478
+
479
+ });
480
+
481
+ it('updates a company', function(done) {
482
+
483
+ org1.name = 'Update';
484
+
485
+ Organization.upsert(org1, 'test-user', function(err, org) {
486
+ should.not.exist(err);
487
+ should.exist(org);
488
+ org.name.should.equal('Update');
489
+ done();
490
+ });
491
+
492
+ });
493
+
494
+ it('merges an org', function(done) {
495
+
496
+ var merged = org1.merge(mergeId);
497
+ merged.should.equal(true);
498
+ done();
499
+
500
+ });
501
+
502
+ it('does not merge an org that has already been merged', function(done) {
503
+
504
+ var merged = org1.merge(mergeId);
505
+ merged.should.equal(false);
506
+ done();
507
+
508
+ });
509
+
510
+ it('finds orgs by person', function(done) {
511
+
512
+ Organization.findByPerson(person.id, function(err, result) {
513
+ should.not.exist(err);
514
+ should.exist(result);
515
+ result.length.should.equal(1);
516
+ result[0].id.should.equal(org1.id);
517
+ done();
518
+ });
519
+
520
+ });
521
+
522
+ it('removes a person from people (unpopulated)', function(done) {
523
+
524
+ // remove person
525
+ org1.removePerson(person.id);
526
+ Organization.upsert(org1, 'test-user', function(err, updatedOrg) {
527
+ should.not.exist(err);
528
+ should.exist(updatedOrg);
529
+ updatedOrg.people.length.should.equal(0);
530
+ done();
531
+ });
532
+
533
+ });
534
+
535
+ it('removes a person from people (populated)', function(done) {
536
+
537
+ // add person to be removed
538
+ org1.addPerson({ person: person.id });
539
+ Organization.upsert(org1, 'test-user', function(err, updatedOrg) {
540
+ should.not.exist(err);
541
+ should.exist(updatedOrg);
542
+ updatedOrg.people.length.should.equal(1);
543
+
544
+ // get populated org
545
+ Organization.getById(org1.id, function(err, org) {
546
+
547
+ should.not.exist(err);
548
+ should.exist(org);
549
+
550
+ // remove person
551
+ org.removePerson(person.id);
552
+ Organization.upsert(org, 'test-user', function(err, updatedOrg) {
553
+ should.not.exist(err);
554
+ should.exist(updatedOrg);
555
+ updatedOrg.people.length.should.equal(0);
556
+ done();
557
+ });
558
+
559
+ });
560
+
561
+ });
562
+
563
+ });
564
+
565
+ it('it flags an org', function(done) {
566
+
567
+ Organization.flag(org4.id, false, 'flagging org4', 'test-user', function(err, org) {
568
+ should.not.exist(err);
569
+ should.exist(org);
570
+ org.flagged.text.should.equal('flagging org4');
571
+ done();
572
+ });
573
+ }); // end flag an org
574
+
575
+ it('finds dupes by querying first word of name', function(done) {
576
+
577
+ var options = {
578
+ fuzzy: true,
579
+ queryFirstWord: true,
580
+ admin: true,
581
+ pcs: []
582
+ };
583
+
584
+ Organization.findDupes(org3, options, function(err, dupes) {
585
+ should.not.exist(err);
586
+ should.exist(dupes);
587
+ dupes.length.should.equal(1);
588
+ done();
589
+ });
590
+
591
+ });
592
+
593
+ it('does not find dupes by querying full name', function(done) {
594
+
595
+ var options = {
596
+ fuzzy: true,
597
+ queryFirstWord: false,
598
+ admin: true,
599
+ pcs: []
600
+ };
601
+
602
+ Organization.findDupes(org3, options, function(err, dupes) {
603
+ should.not.exist(err);
604
+ should.exist(dupes);
605
+ dupes.length.should.equal(0);
606
+ done();
607
+ });
608
+
609
+ });
610
+
611
+ it('finds orgs by domain', function(done) {
612
+ Organization.findByDomains(['nothanks2.org', 'fromthesong.org'], function(err, result) {
613
+ should.not.exist(err);
614
+ should.exist(result);
615
+ result.length.should.equal(2);
616
+ return done();
617
+ });
618
+ });
619
+
620
+ it('finds org by domain alias', function(done) {
621
+ Organization.findByDomains(['doesnotexist.io', 'myolddomain.tld'], function(err, result) {
622
+ should.not.exist(err);
623
+ should.exist(result);
624
+ result.length.should.equal(1);
625
+ return done();
626
+ });
627
+ });
628
+
629
+ it('adds a note', function(done) {
630
+
631
+ Organization.addNote(org4.id, person._id, 'Sweet note bro!', function(err, result) {
632
+
633
+ should.not.exist(err);
634
+ should.exist(result);
635
+ should.exist(result.note);
636
+ should.exist(result.addedTo);
637
+ result.addedTo.notes.length.should.equal(1);
638
+
639
+ noteId = result.addedTo.notes[0];
640
+
641
+ return done();
642
+
643
+ });
644
+
645
+ });
646
+
647
+ it('gets an org by id with a populated note', function(done) {
648
+
649
+ Organization.getById(org4.id, function(err, result) {
650
+
651
+ should.not.exist(err);
652
+ should.exist(result);
653
+ result.notes.length.should.equal(1);
654
+ should.exist(result.notes[0].createdBy);
655
+ should.exist(result.notes[0].createdBy.avatarUrl);
656
+ result.notes[0].createdBy.avatarUrl.length.should.be.above(0);
657
+ should.exist(result.notes[0].createdBy.name);
658
+ should.exist(result.notes[0].createdBy.name.first);
659
+ result.notes[0].createdBy.name.first.length.should.be.above(0);
660
+
661
+ return done();
662
+
663
+ });
664
+
665
+ });
666
+
667
+ it('deletes a simple note', function(done) {
668
+
669
+ Organization.deleteNote(noteId, function(err, result) {
670
+
671
+ should.not.exist(err);
672
+
673
+ return done();
674
+
675
+ });
676
+
677
+ });
678
+
679
+ it('deletes an event note', function(done) {
680
+
681
+ Organization.deleteNote(eventNote.id, function(err, result) {
682
+
683
+ should.not.exist(err);
684
+
685
+ return done();
686
+
687
+ });
688
+
689
+ });
690
+
691
+ it('formats website domains', function(done) {
692
+
693
+ var temp = new Organization();
694
+ temp.name = 'I Love Poorly Formatted Domains';
695
+ temp.slug = 'i-love';
696
+ temp.deleted = false;
697
+ temp.website = 'https://www.yum.net/';
698
+ temp.websiteAliases = [
699
+ 'http://yum.co',
700
+ 'anglophile.co.uk',
701
+ 'abc.subdomain.com',
702
+ 'trailingslash.edu/'
703
+ ];
704
+
705
+ Organization.upsert(temp, 'test-user', function(err, result) {
706
+ should.not.exist(err);
707
+ should.exist(result);
708
+ result.website.should.equal('yum.net');
709
+ result.websiteAliases.length.should.equal(4);
710
+ result.websiteAliases.indexOf('yum.co').should.not.equal(-1);
711
+ result.websiteAliases.indexOf('anglophile.co.uk').should.not.equal(-1);
712
+ result.websiteAliases.indexOf('subdomain.com').should.not.equal(-1);
713
+ result.websiteAliases.indexOf('trailingslash.edu').should.not.equal(-1);
714
+ return done();
715
+ });
716
+
717
+ });
718
+
719
+ // it('does not duplicate website domains with investors', function(done) {
720
+ //
721
+ // var investor = new Investor();
722
+ // investor.name = 'I Love Unique Domains';
723
+ // investor.slug = 'i-love-unique-domains';
724
+ // investor.type = 'venture_capital';
725
+ // investor.deleted = false;
726
+ // investor.website = 'myuniquedomain.com';
727
+ // investor.websiteAliases = [];
728
+ //
729
+ // Investor.upsert(investor, 'test-user', function(err, result) {
730
+ //
731
+ // should.not.exist(err);
732
+ // should.exist(result);
733
+ //
734
+ // var temp = new Organization();
735
+ // temp.name = 'I Love to Duplicate Domains';
736
+ // temp.slug = 'i-love-to-duplicate';
737
+ // temp.deleted = false;
738
+ // temp.website = 'myuniquedomain.com';
739
+ // temp.websiteAliases = [];
740
+ //
741
+ // Organization.upsert(temp, 'test-user', function(err, result) {
742
+ // should.exist(err);
743
+ // return done();
744
+ // });
745
+ //
746
+ // });
747
+ //
748
+ // });
749
+
750
+ });