@dhyasama/totem-merge 6.0.1

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/test/index.js ADDED
@@ -0,0 +1,1159 @@
1
+ "use strict";
2
+
3
+ var config = require('./config');
4
+ var dbUri = config.db.uri;
5
+
6
+ if (dbUri.indexOf('@test-shard-') === -1) {
7
+ console.log('Not a test db. Quitting...');
8
+ process.exit();
9
+ }
10
+
11
+ var should = require("should");
12
+ var mongoose = require('mongoose');
13
+ var models = require('@dhyasama/totem-models')(mongoose, config);
14
+ var clearDB = require('mocha-mongoose')(config.db.uri, {noClear: true});
15
+ var Merge = require('../index.js');
16
+ var merge;
17
+ var Deal = mongoose.model('Deal');
18
+ var Interaction = mongoose.model('Interaction');
19
+ var List = mongoose.model('List');
20
+ var Message = mongoose.model('Message');
21
+ var News = mongoose.model('News');
22
+ var Organization = mongoose.model('Organization');
23
+ var Person = mongoose.model('Person');
24
+
25
+ var customer;
26
+ var org1, org2, org3, org4, org5, org6;
27
+ var person1, person2, person3;
28
+
29
+ describe('Merge', function() {
30
+
31
+ // connect to test db
32
+ before(function(done) {
33
+ if (mongoose.connection.db) return done();
34
+ mongoose.connect(config.db.uri, done);
35
+ });
36
+
37
+ // clear test db
38
+ before(function(done) {
39
+ clearDB(done);
40
+ });
41
+
42
+ // Customer prep
43
+ before(function(done) {
44
+
45
+ customer = new Organization();
46
+ customer.name = 'HUGE VC';
47
+ customer.slug = 'huge-vc';
48
+
49
+ Organization.upsert(customer, 'test', function(err, result) {
50
+
51
+ should.not.exist(err);
52
+ should.exist(result);
53
+
54
+ customer = result;
55
+
56
+ return done();
57
+
58
+ });
59
+
60
+ });
61
+
62
+ // People prep
63
+ before(function(done) {
64
+
65
+ var p3 = new Person();
66
+ p3.name.first = 'Toni';
67
+ p3.name.last = 'Braxton';
68
+ p3.slug = 'toni-braxton';
69
+
70
+ var p1 = new Person();
71
+ p1.name.first = 'George';
72
+ p1.name.last = 'Miller';
73
+ p1.slug = 'george-miller';
74
+ p1.bio = 'Hello World!';
75
+ p1.aliases.push('G-Money');
76
+ p1.sources.push({
77
+ person: p3._id,
78
+ customer: customer._id
79
+ });
80
+
81
+ var p2 = new Person();
82
+ p2.name.first = 'Braxton';
83
+ p2.name.last = 'Miller';
84
+ p2.slug = 'braxton-miller';
85
+ p2.sources.push(p1.sources[0]);
86
+
87
+ Person.upsert(p1, 'test', function(err, newPerson1) {
88
+
89
+ should.not.exist(err);
90
+ should.exist(newPerson1);
91
+ person1 = newPerson1;
92
+
93
+ Person.upsert(p2, 'test', function(err, newPerson2) {
94
+
95
+ should.not.exist(err);
96
+ should.exist(newPerson1);
97
+ person2 = newPerson2;
98
+
99
+ Person.upsert(p3, 'test', function(err, newPerson3) {
100
+
101
+ should.not.exist(err);
102
+ should.exist(newPerson3);
103
+ person3 = newPerson3;
104
+
105
+ return done();
106
+
107
+ });
108
+
109
+ });
110
+
111
+ });
112
+
113
+ });
114
+
115
+ // Org prep
116
+ before(function(done) {
117
+
118
+ var o1 = new Organization();
119
+ o1.name = 'Shiny and Chrome';
120
+ o1.slug = 'TEST 1';
121
+ o1.logoUrl = 'http://no.com';
122
+ o1.description = 'no';
123
+ o1.website = 'https://yes.com';
124
+ o1.websiteAliases.push('https://yup.com');
125
+ o1.social = {
126
+ 'twitter': 'dhyasamatwitter',
127
+ 'facebook': 'dhyasamafacebook',
128
+ 'linkedin': 'dhyasamalinkedin'
129
+ };
130
+ o1.addPerson({ person: person1._id, title: 'CEO' });
131
+ o1.addPerson({ person: person2._id, title: 'CTO' });
132
+
133
+ var o2 = new Organization();
134
+ o2.name = 'Furiosa Five';
135
+ o2.slug = 'TEST 2';
136
+ o2.logoUrl = 'http://yes.com';
137
+ o2.website = 'furiosafive.com';
138
+ o2.addPerson({ person: person2._id, title: 'CTO' });
139
+ o2.addPerson({ person: person1._id, title: 'CEO' });
140
+
141
+ var o3 = new Organization();
142
+ o3.name = 'Fab Five';
143
+ o3.slug = 'fab-five';
144
+ o3.addPerson({ person: person3._id, title: 'VP Engineering' });
145
+
146
+ var o4 = new Organization();
147
+ o4.name = 'ABC';
148
+ o4.slug = 'abc';
149
+ o4.logoUrl = 'http://abc.com';
150
+ o4.description = 'abc';
151
+ o4.website = 'https://abc.com';
152
+ o4.websiteAliases.push('https://abcdef.com');
153
+ o4.social = {
154
+ 'twitter': 'abc',
155
+ 'facebook': 'abc',
156
+ 'linkedin': 'abc'
157
+ };
158
+ o4.addPerson({ person: person1._id, title: 'abc' });
159
+ o4.addPerson({ person: person2._id, title: 'def' });
160
+
161
+ var o5 = new Organization();
162
+ o5.name = '123';
163
+ o5.slug = '123';
164
+ o5.logoUrl = 'http://123.com';
165
+ o5.website = '123.com';
166
+ o5.addPerson({ person: person2._id, title: '123' });
167
+ o5.addPerson({ person: person1._id, title: '456' });
168
+
169
+ var o6 = new Organization();
170
+ o6.name = 'jingles';
171
+ o6.slug = 'jingles';
172
+ o6.logoUrl = 'http://jingles.com';
173
+ o6.website = 'jingles.com';
174
+ o6.addPerson({ person: person2._id, title: 'jingles1' });
175
+ o6.addPerson({ person: person1._id, title: 'jingles2' });
176
+
177
+ Organization.upsert(o1, 'test', function(err, newOrg1) {
178
+
179
+ should.not.exist(err);
180
+ should.exist(newOrg1);
181
+
182
+ // getbyid to populate investedIn
183
+ Organization.getById(newOrg1.id, { CUSTOMER_ID: customer._id, role: 'none' }, function(err, result1) {
184
+
185
+ should.not.exist(err);
186
+ should.exist(result1);
187
+
188
+ org1 = result1;
189
+
190
+ Organization.upsert(o2, 'test', function(err, newOrg2) {
191
+
192
+ should.not.exist(err);
193
+ should.exist(newOrg2);
194
+
195
+ Organization.getById(newOrg2.id, { CUSTOMER_ID: customer._id, role: 'none' }, function(err, result2) {
196
+
197
+ should.not.exist(err);
198
+ should.exist(result2);
199
+
200
+ org2 = result2;
201
+
202
+ Organization.upsert(o3, 'test', function(err, newOrg3) {
203
+
204
+ should.not.exist(err);
205
+ should.exist(newOrg3);
206
+
207
+ Organization.getById(newOrg3.id, { CUSTOMER_ID: customer._id, role: 'none' }, function(err, result3) {
208
+
209
+ should.not.exist(err);
210
+ should.exist(result3);
211
+
212
+ org3 = result3;
213
+
214
+ Organization.upsert(o4, 'test', function(err, newOrg4) {
215
+
216
+ should.not.exist(err);
217
+ should.exist(newOrg4);
218
+
219
+ Organization.getById(newOrg4.id, { CUSTOMER_ID: customer._id, role: 'none' }, function(err, result4) {
220
+
221
+ should.not.exist(err);
222
+ should.exist(result4);
223
+
224
+ org4 = result4;
225
+
226
+ Organization.upsert(o5, 'test', function(err, newOrg5) {
227
+
228
+ should.not.exist(err);
229
+ should.exist(newOrg5);
230
+
231
+ Organization.getById(newOrg5.id, { CUSTOMER_ID: customer._id, role: 'none' }, function(err, result5) {
232
+
233
+ should.not.exist(err);
234
+ should.exist(result5);
235
+
236
+ org5 = result5;
237
+
238
+ Organization.upsert(o6, 'test', function(err, newOrg6) {
239
+
240
+ should.not.exist(err);
241
+ should.exist(newOrg6);
242
+
243
+ Organization.getById(newOrg6.id, { CUSTOMER_ID: customer._id, role: 'none' }, function(err, result6) {
244
+
245
+ should.not.exist(err);
246
+ should.exist(result6);
247
+
248
+ org6 = result6;
249
+
250
+ return done();
251
+
252
+ });
253
+
254
+ });
255
+
256
+ });
257
+
258
+ });
259
+
260
+ });
261
+
262
+ });
263
+
264
+ });
265
+
266
+ });
267
+
268
+ });
269
+
270
+ });
271
+
272
+ });
273
+
274
+ });
275
+
276
+ });
277
+
278
+ // List prep
279
+ before(function(done) {
280
+
281
+ List.createList('Test List', { organizations:[org1], people: [person1] }, person1, { CUSTOMER_ID: customer._id }, function(err, list) {
282
+ should.not.exist(err);
283
+ should.exist(list);
284
+ return done();
285
+ });
286
+
287
+ });
288
+
289
+ // News prep
290
+ before(function(done) {
291
+
292
+ var newsItem = new News({
293
+ title: 'Test News',
294
+ createdOn: new Date(),
295
+ createdBy: 'test',
296
+ link: 'http://totemvc.com',
297
+ org: org1
298
+ });
299
+
300
+ News.upsert(newsItem, function(err, result) {
301
+ should.not.exist(err);
302
+ should.exist(result);
303
+ return done();
304
+ });
305
+
306
+ });
307
+
308
+ // Interaction prep
309
+ before(function(done) {
310
+
311
+ var interaction = new Interaction({
312
+ calendarEventId: new mongoose.Types.ObjectId(),
313
+ providerEventId: 'ABC123',
314
+ totemCustomerId: customer._id,
315
+ summary: 'Test',
316
+ startTime: new Date(),
317
+ recurring: false,
318
+ attendees: {
319
+ internal: [person2],
320
+ external: [person1, person3]
321
+ }
322
+ });
323
+
324
+ Interaction.upsert(interaction, function(err, result) {
325
+ should.not.exist(err);
326
+ should.exist(result);
327
+ return done();
328
+ });
329
+
330
+ });
331
+
332
+ describe('Orgs', function() {
333
+
334
+ it('Flags organization to be merged', function (done) {
335
+
336
+ config.flag = true;
337
+ merge = new Merge(mongoose, config);
338
+
339
+ merge.organizations(org2, org1, 'testy', customer._id, function(err, result) {
340
+ should.not.exist(err);
341
+ should.exist(result);
342
+ result.addedTo.flags.length.should.equal(1);
343
+ return done();
344
+ });
345
+
346
+ });
347
+
348
+ it('Merges organizations', function (done) {
349
+
350
+ // reset to not flag
351
+ config.flag = false;
352
+ merge = new Merge(mongoose, config);
353
+
354
+ org1.investedIn = [];
355
+
356
+ Organization.upsert(org2, 'testy', function(err, result) {
357
+
358
+ should.not.exist(err);
359
+ should.exist(result);
360
+
361
+ merge.organizationsById(org2._id, org1._id, 'testy', customer._id, function(err, result) {
362
+
363
+ should.not.exist(err);
364
+ should.exist(result);
365
+
366
+ result.slug.should.equal('test 2');
367
+ result.id.should.equal(org2.id);
368
+ should.equal(result.logoUrl, 'http://yes.com');
369
+ result.aliases.should.containEql(org1.name);
370
+ result.people.length.should.equal(2);
371
+ result.website.should.equal('furiosafive.com');
372
+
373
+ org2 = result;
374
+
375
+ News.listForCompany(org1, function(err, result) {
376
+ should.not.exist(err);
377
+ should.exist(result);
378
+ result.length.should.equal(0);
379
+ return done();
380
+ });
381
+
382
+ });
383
+
384
+ });
385
+
386
+ });
387
+
388
+ it('Merges organizations with different people', function (done) {
389
+
390
+ // reset to not flag
391
+ config.flag = false;
392
+ merge = new Merge(mongoose, config);
393
+
394
+ org4.people = [];
395
+ org4.people.push({
396
+ person: new mongoose.Types.ObjectId(),
397
+ roles: [{
398
+ role: 'test 4'
399
+ }]
400
+ });
401
+
402
+ org5.people = [];
403
+ org5.people.push({
404
+ person: new mongoose.Types.ObjectId(),
405
+ roles: [{
406
+ role: 'test 5'
407
+ }]
408
+ });
409
+
410
+ Organization.upsert(org2, 'testy', function(err, result) {
411
+
412
+ should.not.exist(err);
413
+ should.exist(result);
414
+
415
+ Organization.upsert(org2, 'testy', function(err, result) {
416
+
417
+ should.not.exist(err);
418
+ should.exist(result);
419
+
420
+ merge.organizationsById(org5._id, org4._id, 'testy', customer._id, function(err, result) {
421
+ should.not.exist(err);
422
+ should.exist(result);
423
+ result.people.length.should.equal(2);
424
+ org5 = result;
425
+ return done();
426
+ });
427
+
428
+ });
429
+
430
+ });
431
+
432
+ });
433
+
434
+ it('Merges organizations with the same people', function (done) {
435
+
436
+ config.flag = false;
437
+ merge = new Merge(mongoose, config);
438
+
439
+ org2.people = [];
440
+ org2.people.push({
441
+ person: person2,
442
+ roles: [{
443
+ role: 'test 1'
444
+ }]
445
+ });
446
+
447
+ org5.people = [];
448
+ org5.people.push({
449
+ person: person2,
450
+ roles: [{
451
+ role: 'test 2'
452
+ }]
453
+ });
454
+
455
+ Organization.upsert(org2, 'testy', function(err, result) {
456
+
457
+ should.not.exist(err);
458
+ should.exist(result);
459
+
460
+ org2 = result;
461
+
462
+ Organization.upsert(org5, 'testy', function (err, result) {
463
+
464
+ should.not.exist(err);
465
+ should.exist(result);
466
+
467
+ org5 = result;
468
+
469
+ merge.organizationsById(org5._id, org2._id, 'testy', customer._id, function(err, result) {
470
+ should.not.exist(err);
471
+ should.exist(result);
472
+ result.people.length.should.equal(1);
473
+ return done();
474
+ });
475
+
476
+ });
477
+
478
+ });
479
+
480
+ });
481
+
482
+ it('Merges organizations when source has a deal', function (done) {
483
+
484
+ config.flag = false;
485
+ merge = new Merge(mongoose, config);
486
+
487
+ var deal = new Deal({
488
+ customer: customer._id,
489
+ organization: org5._id,
490
+ description: 'aok',
491
+ stage: 'New'
492
+ });
493
+
494
+ Deal.upsert(deal, { account: new mongoose.Types.ObjectId() }, function(err, result1) {
495
+
496
+ should.not.exist(err);
497
+ should.exist(result1);
498
+
499
+ merge.organizationsById(org6._id, org5._id, new mongoose.Types.ObjectId(), customer._id, function(err, result2) {
500
+
501
+ should.not.exist(err);
502
+ should.exist(result2);
503
+
504
+ org6 = result2;
505
+
506
+ Deal.getById(deal.id, { CUSTOMER_ID: customer._id }, function(err, result3) {
507
+
508
+ should.not.exist(err);
509
+ should.exist(result3);
510
+
511
+ result3.organization._id.toString().should.equal(org6._id.toString());
512
+
513
+ return done();
514
+
515
+ });
516
+
517
+ });
518
+
519
+ });
520
+
521
+ });
522
+
523
+ it('Merges organizations when target has a list', function (done) {
524
+
525
+ config.flag = false;
526
+ merge = new Merge(mongoose, config);
527
+
528
+ var list = new List({
529
+ customer: customer._id,
530
+ name: 'my list',
531
+ key: 'my-list',
532
+ createdOn: new Date(),
533
+ createdBy: new mongoose.Types.ObjectId(),
534
+ organizations: [org3]
535
+ });
536
+
537
+ List.upsert(list, function(err, result1) {
538
+
539
+ should.not.exist(err);
540
+ should.exist(result1);
541
+
542
+ merge.organizationsById(org6._id, org3._id, new mongoose.Types.ObjectId(), customer._id, function(err, result2) {
543
+
544
+ should.not.exist(err);
545
+ should.exist(result2);
546
+
547
+ org6 = result2;
548
+
549
+ List.getById(list.id, { CUSTOMER_ID: customer._id }, function(err, result3) {
550
+
551
+ should.not.exist(err);
552
+ should.exist(result3);
553
+
554
+ result3.customer.toString().should.equal(customer._id.toString());
555
+ result3.organizations.length.should.equal(1);
556
+ result3.organizations[0]._id.toString().should.equal(org6._id.toString());
557
+
558
+ return done();
559
+
560
+ });
561
+
562
+ });
563
+
564
+ });
565
+
566
+ });
567
+
568
+ it('Merges organizations when target has a message', function (done) {
569
+
570
+ config.flag = false;
571
+ merge = new Merge(mongoose, config);
572
+
573
+ var newOrg = new Organization();
574
+ newOrg.name = 'New Org';
575
+ newOrg.slug = 'neworg';
576
+
577
+ Organization.upsert(newOrg, 'testy', function(err, result) {
578
+
579
+ should.not.exist(err);
580
+ should.exist(result);
581
+
582
+ var message = new Message({
583
+ createdOn: new Date(),
584
+ updatedOn: new Date(),
585
+ webhook: new mongoose.Types.ObjectId(),
586
+ type: 'email',
587
+ subtype: 'updates',
588
+ customer: customer._id,
589
+ organization: org6._id,
590
+ raw: {}
591
+ });
592
+
593
+ Message.upsert(message, function(err, result1) {
594
+
595
+ should.not.exist(err);
596
+ should.exist(result1);
597
+
598
+ merge.organizationsById(org6._id, newOrg._id, new mongoose.Types.ObjectId(), customer._id, function(err, result2) {
599
+
600
+ should.not.exist(err);
601
+ should.exist(result2);
602
+
603
+ org6 = result2;
604
+
605
+ Message.getById(message.id, { CUSTOMER_ID: customer._id }, function(err, result3) {
606
+
607
+ should.not.exist(err);
608
+ should.exist(result3);
609
+
610
+ result3.customer.toString().should.equal(customer._id.toString());
611
+ result3.organization._id.toString().should.equal(org6._id.toString());
612
+
613
+ return done();
614
+
615
+ });
616
+
617
+ });
618
+
619
+ });
620
+
621
+ });
622
+
623
+ });
624
+
625
+ it('Merges organizations when target has news', function (done) {
626
+
627
+ config.flag = false;
628
+ merge = new Merge(mongoose, config);
629
+
630
+ var newOrg = new Organization();
631
+ newOrg.name = 'Another New Org';
632
+ newOrg.slug = 'anotherneworg';
633
+
634
+ Organization.upsert(newOrg, 'testy', function(err, result) {
635
+
636
+ should.not.exist(err);
637
+ should.exist(result);
638
+
639
+ var news = new News({
640
+ title: 'Newsy News',
641
+ createdOn: new Date(),
642
+ createdBy: 'tester',
643
+ link: 'http://somenewssite.com/somenewsitem',
644
+ org: org6._id
645
+ });
646
+
647
+ News.upsert(news, function(err, result1) {
648
+
649
+ should.not.exist(err);
650
+ should.exist(result1);
651
+
652
+ merge.organizationsById(org6._id, newOrg._id, new mongoose.Types.ObjectId(), customer._id, function(err, result2) {
653
+
654
+ should.not.exist(err);
655
+ should.exist(result2);
656
+
657
+ org6 = result2;
658
+
659
+ News.findById(news.id, function(err, result3) {
660
+
661
+ should.not.exist(err);
662
+ should.exist(result3);
663
+
664
+ result3.org.toString().should.equal(org6._id.toString());
665
+
666
+ return done();
667
+
668
+ });
669
+
670
+ });
671
+
672
+ });
673
+
674
+ });
675
+
676
+ });
677
+
678
+ it('Will not merge customer', function (done) {
679
+
680
+ config.flag = false;
681
+ merge = new Merge(mongoose, config);
682
+
683
+ var newOrg = new Organization();
684
+ newOrg.name = 'Yet Another New Org';
685
+ newOrg.slug = 'yetanotherneworg';
686
+
687
+ Organization.upsert(newOrg, 'testy', function(err, result) {
688
+
689
+ should.not.exist(err);
690
+ should.exist(result);
691
+
692
+ org6.customer.totemUrl = 'http://test.totemvc.com';
693
+
694
+ Organization.upsert(org6, 'testy', function(err, result) {
695
+
696
+ should.not.exist(err);
697
+ should.exist(result);
698
+
699
+ merge.organizationsById(org6._id, newOrg._id, 'testy', customer._id, function(err, result) {
700
+ should.exist(err);
701
+ should.not.exist(result);
702
+ return done();
703
+ });
704
+
705
+ });
706
+
707
+ });
708
+
709
+ });
710
+
711
+ });
712
+
713
+ describe('People', function() {
714
+
715
+ it('Flags person to be merged', function (done) {
716
+
717
+ config.flag = true;
718
+ merge = new Merge(mongoose, config);
719
+
720
+ merge.people(person2, person1, 'testy', customer._id, function(err, result) {
721
+ should.not.exist(err);
722
+ should.exist(result);
723
+ result.addedTo.flags.length.should.equal(1);
724
+ return done();
725
+ });
726
+
727
+ });
728
+
729
+ it('Merges people', function (done) {
730
+
731
+ // reset to not flag
732
+ config.flag = false;
733
+ merge = new Merge(mongoose, config);
734
+
735
+ merge.peopleById(person2.id, person1.id, 'testy', customer._id, function(err, result) {
736
+
737
+ should.not.exist(err);
738
+ should.exist(result);
739
+ result.slug.should.equal('braxton-miller');
740
+ result.id.should.equal(person2.id);
741
+ should.equal(result.bio, 'Hello World!');
742
+ result.aliases.should.containEql(person1.name.full);
743
+ result.sources.length.should.equal(1);
744
+
745
+ return done();
746
+
747
+ });
748
+
749
+ });
750
+
751
+ it('Merges people with different phone numbers', function (done) {
752
+
753
+ // reset to not flag
754
+ config.flag = false;
755
+ merge = new Merge(mongoose, config);
756
+
757
+ Person.getById(person2._id, { CUSTOMER_ID: customer._id }, function(err, result) {
758
+
759
+ should.not.exist(err);
760
+ should.exist(result);
761
+
762
+ result.contact.phone = [];
763
+ result.contact.phone.push({
764
+ number: '207-555-1212',
765
+ type: 'other',
766
+ primary: true
767
+ });
768
+
769
+ Person.upsert(result, 'test', function(err, result) {
770
+
771
+ should.not.exist(err);
772
+ should.exist(result);
773
+ person2 = result;
774
+
775
+ var newPerson = new Person();
776
+ newPerson.name.first = 'New';
777
+ newPerson.name.last = 'Person';
778
+ newPerson.slug = 'new-person';
779
+ newPerson.contact.phone = [];
780
+ newPerson.contact.phone.push({
781
+ number: '207-939-6565',
782
+ type: 'other',
783
+ primary: true
784
+ });
785
+
786
+ Person.upsert(newPerson, 'test', function(err, result) {
787
+
788
+ should.not.exist(err);
789
+ should.exist(result);
790
+ newPerson = result;
791
+
792
+ merge.peopleById(person2.id, newPerson.id, 'testy', customer._id, function(err, result) {
793
+ should.not.exist(err);
794
+ should.exist(result);
795
+ result.contact.phone.length.should.equal(2);
796
+ return done();
797
+ });
798
+
799
+ });
800
+
801
+ });
802
+
803
+ });
804
+
805
+ });
806
+
807
+ it('Merges people with same phone number', function (done) {
808
+
809
+ Person.getById(person2._id, { CUSTOMER_ID: customer._id }, function(err, result) {
810
+
811
+ should.not.exist(err);
812
+ should.exist(result);
813
+
814
+ result.contact.phone = [];
815
+ result.contact.phone.push({
816
+ number: '207-939-6565',
817
+ type: 'other',
818
+ primary: true
819
+ });
820
+
821
+ Person.upsert(result, 'test', function(err, result) {
822
+
823
+ should.not.exist(err);
824
+ should.exist(result);
825
+ person2 = result;
826
+
827
+ var newPerson = new Person();
828
+ newPerson.name.first = 'New';
829
+ newPerson.name.last = 'Person';
830
+ newPerson.slug = 'new-person';
831
+ newPerson.contact.phone = [];
832
+ newPerson.contact.phone.push({
833
+ number: '207-939-6565',
834
+ type: 'other',
835
+ primary: true
836
+ });
837
+
838
+ Person.upsert(newPerson, 'test', function(err, result) {
839
+
840
+ should.not.exist(err);
841
+ should.exist(result);
842
+ newPerson = result;
843
+
844
+ merge.peopleById(person2.id, newPerson.id, 'testy', customer._id, function(err, result) {
845
+ should.not.exist(err);
846
+ should.exist(result);
847
+ result.contact.phone.length.should.equal(1);
848
+ return done();
849
+ });
850
+
851
+ });
852
+
853
+ });
854
+
855
+ });
856
+
857
+ });
858
+
859
+ it('Merges two people that are referenced by same org', function (done) {
860
+
861
+ // reset to not flag
862
+ config.flag = false;
863
+ merge = new Merge(mongoose, config);
864
+
865
+ var myNewPerson1 = new Person();
866
+ myNewPerson1.name = { first:'jason', last:'reynolds'};
867
+ myNewPerson1.slug = 'jason-reynolds';
868
+
869
+ var myNewPerson2 = new Person();
870
+ myNewPerson2.name = { first:'jim', last:'jenkins'};
871
+ myNewPerson2.slug = 'jim-jenkins';
872
+
873
+ Person.upsert(myNewPerson1, 'test', function(err, newPerson1) {
874
+
875
+ should.not.exist(err);
876
+ should.exist(newPerson1);
877
+
878
+ Person.upsert(myNewPerson2, 'test', function (err, newPerson2) {
879
+
880
+ should.not.exist(err);
881
+ should.exist(newPerson2);
882
+
883
+ var myNewOrg = new Organization();
884
+ myNewOrg.name = 'test';
885
+ myNewOrg.slug = 'test';
886
+ myNewOrg.people = [];
887
+ myNewOrg.addPerson({ person: newPerson1._id, title: 'CEO' });
888
+ myNewOrg.addPerson({ person: newPerson2._id, title: 'CTO' });
889
+
890
+ Organization.upsert(myNewOrg, 'testy', function(err, result) {
891
+
892
+ should.not.exist(err);
893
+ should.exist(result);
894
+
895
+ merge.peopleById(newPerson2.id, newPerson1.id, 'testy', customer._id, function(err, merged) {
896
+
897
+ should.not.exist(err);
898
+ should.exist(merged);
899
+
900
+ Organization.getById(result.id, { CUSTOMER_ID: customer._id, role: 'none' }, function(err, finalResult) {
901
+
902
+ should.not.exist(err);
903
+ should.exist(finalResult);
904
+ should.exist(finalResult.people);
905
+ finalResult.people.length.should.equal(1);
906
+ finalResult.people[0].person._id.toString().should.equal(newPerson2._id.toString());
907
+
908
+ return done();
909
+
910
+ });
911
+
912
+ });
913
+
914
+ });
915
+
916
+ });
917
+
918
+ });
919
+
920
+ });
921
+
922
+ it('Merges two people and only source is referenced by org', function (done) {
923
+
924
+ var myNewPerson1 = new Person();
925
+ myNewPerson1.name = { first:'jason', last:'reynolds'};
926
+ myNewPerson1.slug = 'jason-reynolds';
927
+
928
+ var myNewPerson2 = new Person();
929
+ myNewPerson2.name = { first:'jim', last:'jenkins'};
930
+ myNewPerson2.slug = 'jim-jenkins';
931
+
932
+ Person.upsert(myNewPerson1, 'test', function(err, newPerson1) {
933
+
934
+ should.not.exist(err);
935
+ should.exist(newPerson1);
936
+
937
+ Person.upsert(myNewPerson2, 'test', function (err, newPerson2) {
938
+
939
+ should.not.exist(err);
940
+ should.exist(newPerson2);
941
+
942
+ var myNewOrg = new Organization();
943
+ myNewOrg.name = 'test';
944
+ myNewOrg.slug = 'test';
945
+ myNewOrg.people = [];
946
+ myNewOrg.addPerson({ person: newPerson1._id, title: 'CEO' });
947
+
948
+ Organization.upsert(myNewOrg, 'testy', function(err, result) {
949
+
950
+ should.not.exist(err);
951
+ should.exist(result);
952
+ should.exist(result.people);
953
+ result.people.length.should.equal(1);
954
+ result.people[0].person.toString().should.equal(newPerson1._id.toString()); // source
955
+
956
+ merge.peopleById(newPerson2.id, newPerson1.id, 'testy', customer._id, function(err, merged) {
957
+
958
+ should.not.exist(err);
959
+ should.exist(merged);
960
+
961
+ Organization.getById(result.id, { CUSTOMER_ID: customer._id, role: 'none' }, function(err, finalResult) {
962
+
963
+ should.not.exist(err);
964
+ should.exist(finalResult);
965
+ should.exist(finalResult.people);
966
+ finalResult.people.length.should.equal(1);
967
+ finalResult.people[0].person._id.toString().should.equal(newPerson2._id.toString()); // target
968
+
969
+ return done();
970
+
971
+ });
972
+
973
+ });
974
+
975
+ });
976
+
977
+ });
978
+
979
+ });
980
+
981
+ });
982
+
983
+ it('Merges two sources for same customer', function(done) {
984
+
985
+ // reset to not flag
986
+ config.flag = false;
987
+ merge = new Merge(mongoose, config);
988
+
989
+ var newPerson1 = new Person();
990
+ newPerson1.name.first = 'New';
991
+ newPerson1.name.last = 'Person';
992
+ newPerson1.slug = 'new-person-1';
993
+ newPerson1.sources = [{
994
+ person: person3,
995
+ customer: customer
996
+ }];
997
+
998
+ var newPerson2 = new Person();
999
+ newPerson2.name.first = 'Newer';
1000
+ newPerson2.name.last = 'Person';
1001
+ newPerson2.slug = 'new-person-2';
1002
+ newPerson2.sources = [{
1003
+ person: person2,
1004
+ customer: customer
1005
+ }];
1006
+
1007
+ Person.upsert(newPerson1, 'test', function(err, result) {
1008
+
1009
+ should.not.exist(err);
1010
+ should.exist(result);
1011
+ newPerson1 = result;
1012
+
1013
+ Person.upsert(newPerson2, 'test', function(err, result) {
1014
+
1015
+ should.not.exist(err);
1016
+ should.exist(result);
1017
+ newPerson2 = result;
1018
+
1019
+ merge.peopleById(newPerson2.id, newPerson1.id, 'testy', customer._id, function(err, result) {
1020
+
1021
+ should.not.exist(err);
1022
+ should.exist(result);
1023
+
1024
+ result.sources.length.should.equal(2);
1025
+ result.sources[0].person._id.toString().should.equal(person2._id.toString());
1026
+ result.sources[0].customer.toString().should.equal(customer._id.toString());
1027
+ result.sources[1].person._id.toString().should.equal(person3._id.toString());
1028
+ result.sources[1].customer.toString().should.equal(customer._id.toString());
1029
+
1030
+
1031
+ return done();
1032
+
1033
+ });
1034
+
1035
+ });
1036
+
1037
+ });
1038
+
1039
+ });
1040
+
1041
+ it('Merges same source for different customers', function(done) {
1042
+
1043
+ // reset to not flag
1044
+ config.flag = false;
1045
+ merge = new Merge(mongoose, config);
1046
+
1047
+ var newPerson1 = new Person();
1048
+ newPerson1.name.first = 'New';
1049
+ newPerson1.name.last = 'Person';
1050
+ newPerson1.slug = 'new-person-1';
1051
+ newPerson1.sources = [{
1052
+ person: person3._id,
1053
+ customer: org2._id
1054
+ }];
1055
+
1056
+ var newPerson2 = new Person();
1057
+ newPerson2.name.first = 'Newer';
1058
+ newPerson2.name.last = 'Person';
1059
+ newPerson2.slug = 'new-person-2';
1060
+ newPerson2.sources = [{
1061
+ person: person3._id,
1062
+ customer: org3._id
1063
+ }];
1064
+
1065
+ Person.upsert(newPerson1, 'test', function(err, result) {
1066
+
1067
+ should.not.exist(err);
1068
+ should.exist(result);
1069
+ newPerson1 = result;
1070
+
1071
+ Person.upsert(newPerson2, 'test', function(err, result) {
1072
+
1073
+ should.not.exist(err);
1074
+ should.exist(result);
1075
+ newPerson2 = result;
1076
+
1077
+ merge.peopleById(newPerson2.id, newPerson1.id, 'testy', customer._id, function(err, result) {
1078
+
1079
+ should.not.exist(err);
1080
+ should.exist(result);
1081
+
1082
+ result.sources.length.should.equal(2);
1083
+ result.sources[0].person._id.toString().should.equal(person3._id.toString());
1084
+ result.sources[0].customer.toString().should.equal(org3._id.toString());
1085
+ result.sources[1].person._id.toString().should.equal(person3._id.toString());
1086
+ result.sources[1].customer.toString().should.equal(org2._id.toString());
1087
+
1088
+
1089
+ return done();
1090
+
1091
+ });
1092
+
1093
+ });
1094
+
1095
+ });
1096
+
1097
+ });
1098
+
1099
+ it('Merges two sources for two customers', function(done) {
1100
+
1101
+ // reset to not flag
1102
+ config.flag = false;
1103
+ merge = new Merge(mongoose, config);
1104
+
1105
+ var newPerson1 = new Person();
1106
+ newPerson1.name.first = 'New';
1107
+ newPerson1.name.last = 'Person';
1108
+ newPerson1.slug = 'new-person-1';
1109
+ newPerson1.sources = [{
1110
+ person: person3._id,
1111
+ customer: org3._id
1112
+ }];
1113
+
1114
+ var newPerson2 = new Person();
1115
+ newPerson2.name.first = 'Newer';
1116
+ newPerson2.name.last = 'Person';
1117
+ newPerson2.slug = 'new-person-2';
1118
+ newPerson2.sources = [{
1119
+ person: person2._id,
1120
+ customer: org2._id
1121
+ }];
1122
+
1123
+ Person.upsert(newPerson1, 'test', function(err, result) {
1124
+
1125
+ should.not.exist(err);
1126
+ should.exist(result);
1127
+ newPerson1 = result;
1128
+
1129
+ Person.upsert(newPerson2, 'test', function(err, result) {
1130
+
1131
+ should.not.exist(err);
1132
+ should.exist(result);
1133
+ newPerson2 = result;
1134
+
1135
+ merge.peopleById(newPerson2.id, newPerson1.id, 'testy', customer._id, function(err, result) {
1136
+
1137
+ should.not.exist(err);
1138
+ should.exist(result);
1139
+
1140
+ result.sources.length.should.equal(2);
1141
+ result.sources[0].person._id.toString().should.equal(person2._id.toString());
1142
+ result.sources[0].customer.toString().should.equal(org2._id.toString());
1143
+ result.sources[1].person._id.toString().should.equal(person3._id.toString());
1144
+ result.sources[1].customer.toString().should.equal(org3._id.toString());
1145
+
1146
+
1147
+ return done();
1148
+
1149
+ });
1150
+
1151
+ });
1152
+
1153
+ });
1154
+
1155
+ });
1156
+
1157
+ });
1158
+
1159
+ });