@dhyasama/totem-merge 6.13.0 → 6.15.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/index.js CHANGED
@@ -13,17 +13,14 @@ var Merge = function(mongoose, config) {
13
13
  this.Deal = mongoose.model('Deal');
14
14
  this.Document = mongoose.model('Document');
15
15
  this.Financials = mongoose.model('Financials');
16
- this.Flag = mongoose.model('Flag');
17
- this.Interaction = mongoose.model('Interaction');
18
16
  this.LimitedPartner = mongoose.model('LimitedPartner');
19
- this.List = mongoose.model('List');
20
17
  this.Message = mongoose.model('Message');
18
+ this.Meeting = mongoose.model('Meeting');
21
19
  this.News = mongoose.model('News');
22
20
  this.Note = mongoose.model('Note');
23
21
  this.Organization = mongoose.model('Organization');
24
22
  this.Person = mongoose.model('Person');
25
23
  this.Round = mongoose.model('Round');
26
- this.Snapshot = mongoose.model('Snapshot');
27
24
 
28
25
  // can choose to flag an entity as needing to be merged rather than actually merging it
29
26
  // particularly useful for automated processes, e.g., a batch import that detects dupes
@@ -175,7 +175,6 @@ var updateReferences = function updateReferences(context, target, source, merged
175
175
  updateCapTables.bind(context, newOrgId, oldOrgId),
176
176
  updateDeals.bind(context, newOrgId, oldOrgId, customerId),
177
177
  updateFinancials.bind(context, newOrgId, oldOrgId),
178
- updateLists.bind(context, newOrgId, oldOrgId),
179
178
  updateMessages.bind(context, newOrgId, oldOrgId),
180
179
  updateNews.bind(context, newOrgId, oldOrgId),
181
180
  updateOrganizations.bind(context, newOrgId, oldOrgId),
@@ -307,52 +306,6 @@ var updateFinancials = function updateFinancials(newOrgId, oldOrgId, cb) {
307
306
 
308
307
  };
309
308
 
310
- var updateLists = function updateLists(newOrgId, oldOrgId, cb) {
311
-
312
- logger.writeToInfoLog(' Updating lists...');
313
-
314
- var self = this;
315
-
316
- self.List.collection.find({ 'organizations': oldOrgId }).toArray(function(err, items) {
317
-
318
- if (err) return cb(err, null);
319
- else if (!items || items.length === 0) return cb(null, []);
320
-
321
- var tasks = [];
322
- var updated = [];
323
-
324
- _.each(items, function(item) {
325
-
326
- var orgs = _.map(item.organizations, function(o) {
327
- if (o.toString() === oldOrgId.toString()) { o = newOrgId; }
328
- return o;
329
- });
330
-
331
- tasks.push({
332
- id: item._id,
333
- update: { $set: { organizations: orgs } }
334
- });
335
-
336
- });
337
-
338
- logger.writeToInfoLog(' Found ' + tasks.length + ' list references to update');
339
-
340
- var queue = async.queue(function(task, callback) {
341
- self.List.modifyById(task.id, task.update, function(err, result) {
342
- if (err) { return callback(err, null); }
343
- else if (result) { updated.push(result); }
344
- return callback(null, result);
345
- });
346
- }, 8);
347
-
348
- queue.drain = function() { return cb(null, updated); };
349
-
350
- queue.push(tasks);
351
-
352
- });
353
-
354
- };
355
-
356
309
  var updateMessages = function updateMessages(newOrgId, oldOrgId, cb) {
357
310
 
358
311
  logger.writeToInfoLog(' Updating messages...');
package/lib/people.js CHANGED
@@ -90,8 +90,6 @@ var mergeSourceIntoTarget = function mergeEntities(target, source, cb) {
90
90
  'deleted',
91
91
  'entered',
92
92
  '__v',
93
- 'status',
94
- 'lps',
95
93
  'subscriptions',
96
94
  'account',
97
95
  'sources'
@@ -99,7 +97,7 @@ var mergeSourceIntoTarget = function mergeEntities(target, source, cb) {
99
97
 
100
98
  var mergeSources = function mergeSources(target, source) {
101
99
 
102
- // Note interaction counts are not combined here but will be updated on next calendar pull
100
+ // Note: source interaction counts are not merged; they refresh via downstream sync
103
101
 
104
102
  var targetSources = target.sources || [];
105
103
  var sourceSources = source.sources || [];
@@ -171,14 +169,10 @@ var updateReferences = function updateReferences(context, target, source, merged
171
169
 
172
170
  updateDocuments.bind(context, newPersonId, oldPersonId, null),
173
171
 
174
- updateFlags.bind(context, newPersonId, oldPersonId, username),
175
-
176
- updateInteractions.bind(context, newPersonId, oldPersonId, username),
172
+ updateMeetings.bind(context, newPersonId, oldPersonId, username),
177
173
 
178
174
  updateLimitedPartners.bind(context, newPersonId, oldPersonId, mergedBy),
179
175
 
180
- updateLists.bind(context, newPersonId, oldPersonId, null),
181
-
182
176
  updateMessages.bind(context, newPersonId, oldPersonId, null),
183
177
 
184
178
  updateNotes.bind(context, newPersonId, oldPersonId, username),
@@ -244,8 +238,10 @@ var updateDocuments = function updateDocuments(newPersonId, oldPersonId, user, c
244
238
 
245
239
  var self = this;
246
240
 
247
- // Get all documents for this person
248
- self.Document.collection.find({ $or: [{ 'source.person': oldPersonId }, { 'referrer.person': oldPersonId }]}).toArray(function(err, items) {
241
+ if (!self.Document || !self.Document.collection) { return cb(null, []); }
242
+
243
+ // Get all documents created by this person
244
+ self.Document.collection.find({ 'createdBy': oldPersonId }).toArray(function(err, items) {
249
245
 
250
246
  if (err) return cb(err, null);
251
247
  else if (!items || items.length === 0) return cb(null, []);
@@ -257,13 +253,14 @@ var updateDocuments = function updateDocuments(newPersonId, oldPersonId, user, c
257
253
 
258
254
  var setObj = {};
259
255
 
260
- if (item.source && item.source.person && item.source.person.toString() === oldPersonId.toString()) { setObj.source = { person: newPersonId }; }
261
- if (item.referrer && item.referrer.person && item.referrer.person.toString() === oldPersonId.toString()) { setObj.referrer = { person: newPersonId }; }
256
+ if (item.createdBy && item.createdBy.toString && item.createdBy.toString() === oldPersonId.toString()) { setObj.createdBy = newPersonId; }
262
257
 
263
- tasks.push({
264
- id: item._id,
265
- update: { $set: setObj }
266
- });
258
+ if (Object.keys(setObj).length > 0) {
259
+ tasks.push({
260
+ id: item._id,
261
+ update: { $set: setObj }
262
+ });
263
+ }
267
264
 
268
265
  });
269
266
 
@@ -285,60 +282,18 @@ var updateDocuments = function updateDocuments(newPersonId, oldPersonId, user, c
285
282
 
286
283
  };
287
284
 
288
- var updateFlags = function updateFlags(newPersonId, oldPersonId, user, cb) {
285
+ var updateMeetings = function updateMeetings(newPersonId, oldPersonId, user, cb) {
289
286
 
290
- console.log(' Updating flags...');
287
+ console.log(' Updating meetings...');
291
288
 
292
289
  var self = this;
293
290
 
294
- // Get all flags for this person
295
- self.Flag.collection.find({ 'createdBy': oldPersonId }).toArray(function(err, items) {
296
-
297
- if (err) return cb(err, null);
298
- else if (!items || items.length === 0) return cb(null, []);
299
-
300
- var tasks = [];
301
- var updated = [];
302
-
303
- _.each(items, function(item) {
304
-
305
- var setObj = {};
306
-
307
- if (item.createdBy.toString() === oldPersonId.toString()) { setObj.createdBy = newPersonId; }
308
-
309
- tasks.push({
310
- id: item._id,
311
- update: { $set: setObj }
312
- });
313
-
314
- });
315
-
316
- console.log(' Found ' + tasks.length + ' flag references to update');
317
-
318
- var queue = async.queue(function(task, callback) {
319
- self.Flag.modifyById(task.id, task.update, function(err, result) {
320
- if (err) { return callback(err, null); }
321
- else if (result) { updated.push(result); }
322
- return callback(null, result);
323
- });
324
- }, 8);
325
-
326
- queue.drain = function() { return cb(null, updated); };
327
-
328
- queue.push(tasks);
329
-
330
- });
331
-
332
- };
333
-
334
- var updateInteractions = function updateInteractions(newPersonId, oldPersonId, user, cb) {
335
-
336
- console.log(' Updating interactions...');
337
-
338
- var self = this;
291
+ if (!self.Meeting || !self.Meeting.collection) {
292
+ return cb(null, []);
293
+ }
339
294
 
340
- // Get all interactions for this person
341
- self.Interaction.collection.find({ $or: [{ 'attendees.external': oldPersonId }, { 'attendees.internal': oldPersonId }] }).toArray(function(err, items) {
295
+ // Get all meetings containing the old person id in people array
296
+ self.Meeting.collection.find({ 'people': oldPersonId }).toArray(function(err, items) {
342
297
 
343
298
  if (err) return cb(err, null);
344
299
  else if (!items || items.length === 0) return cb(null, []);
@@ -348,31 +303,22 @@ var updateInteractions = function updateInteractions(newPersonId, oldPersonId, u
348
303
 
349
304
  _.each(items, function(item) {
350
305
 
351
- item.attendees.internal = _.map(item.attendees.internal, function(attendee) {
352
- if (attendee.toString() === oldPersonId.toString()) { attendee = newPersonId; }
353
- return attendee;
354
- });
355
-
356
- item.attendees.external = _.map(item.attendees.external, function(attendee) {
357
- if (attendee.toString() === oldPersonId.toString()) { attendee = newPersonId; }
358
- return attendee;
306
+ item.people = _.map(item.people, function(person) {
307
+ if (person.toString() === oldPersonId.toString()) { person = newPersonId; }
308
+ return person;
359
309
  });
360
310
 
361
311
  tasks.push({
362
312
  id: item._id,
363
- update: { $set: {
364
- 'attendees.internal': item.attendees.internal,
365
- 'attendees.external': item.attendees.external
366
- }
367
- }
313
+ update: { $set: { 'people': item.people } }
368
314
  });
369
315
 
370
316
  });
371
317
 
372
- console.log(' Found ' + tasks.length + ' interaction references to update');
318
+ console.log(' Found ' + tasks.length + ' meeting references to update');
373
319
 
374
320
  var queue = async.queue(function(task, callback) {
375
- self.Interaction.modifyById(task.id, task.update, function(err, result) {
321
+ self.Meeting.findByIdAndUpdate(task.id, task.update, { upsert: false, new: true }, function(err, result) {
376
322
  if (err) { return callback(err, null); }
377
323
  else if (result) { updated.push(result); }
378
324
  return callback(null, result);
@@ -437,57 +383,6 @@ var updateLimitedPartners = function updateLimitedPartners(newPersonId, oldPerso
437
383
 
438
384
  };
439
385
 
440
- var updateLists = function updateLists(newPersonId, oldPersonId, user, cb) {
441
-
442
- console.log(' Updating lists...');
443
-
444
- var self = this;
445
-
446
- // Get all lists for this person
447
- self.List.collection.find({ $or: [{ 'people': oldPersonId }, { 'createdBy': oldPersonId }] }).toArray(function(err, items) {
448
-
449
- if (err) return cb(err, null);
450
- else if (!items || items.length === 0) return cb(null, []);
451
-
452
- var tasks = [];
453
- var updated = [];
454
-
455
- _.each(items, function(item) {
456
-
457
- var setObj = {};
458
-
459
- if (item.createdBy.toString() === oldPersonId.toString()) { setObj.createdBy = newPersonId; }
460
-
461
- setObj.people = _.map(item.people, function(person) {
462
- if (person.toString() === oldPersonId.toString()) { person = newPersonId; }
463
- return person;
464
- });
465
-
466
- tasks.push({
467
- id: item._id,
468
- update: { $set: setObj }
469
- });
470
-
471
- });
472
-
473
- console.log(' Found ' + tasks.length + ' list references to update');
474
-
475
- var queue = async.queue(function(task, callback) {
476
- self.List.modifyById(task.id, task.update, function(err, result) {
477
- if (err) { return callback(err, null); }
478
- else if (result) { updated.push(result); }
479
- return callback(null, result);
480
- });
481
- }, 8);
482
-
483
- queue.drain = function() { return cb(null, updated); };
484
-
485
- queue.push(tasks);
486
-
487
- });
488
-
489
- };
490
-
491
386
  var updateMessages = function updateMessages(newPersonId, oldPersonId, user, cb) {
492
387
 
493
388
  console.log(' Updating messages...');
@@ -597,7 +492,7 @@ var updateOrganizations = function updateOrganizations(newPersonId, oldPersonId,
597
492
  var self = this;
598
493
 
599
494
  // Get all orgs for this person
600
- self.Organization.collection.find({ $or: [{ 'people.person': oldPersonId }, { 'chair.first': oldPersonId }, { 'chair.second': oldPersonId }] }).toArray(function(err, orgs) {
495
+ self.Organization.collection.find({ $or: [{ 'people.person': oldPersonId }, { 'chairs.first': oldPersonId }, { 'chairs.second': oldPersonId }] }).toArray(function(err, orgs) {
601
496
 
602
497
  if (err) return cb(err, null);
603
498
  else if (!orgs || orgs.length === 0) return cb(null, []);
@@ -631,13 +526,14 @@ var updateOrganizations = function updateOrganizations(newPersonId, oldPersonId,
631
526
  // and be unable to determine which data to keep. We still can't know for sure, but let's default to keeping the
632
527
  // target/new person's data. We do that by removing the source/old person here and then map the target/new person id.
633
528
 
634
- var oldPerson = _.find(org.people, function(person) { return person.person.toString() === oldPersonId.toString(); });
635
- var newPerson = _.find(org.people, function(person) { return person.person.toString() === newPersonId.toString(); });
529
+ var peopleArr = org.people || [];
530
+ var oldPerson = _.find(peopleArr, function(person) { return person.person && person.person.toString() === oldPersonId.toString(); });
531
+ var newPerson = _.find(peopleArr, function(person) { return person.person && person.person.toString() === newPersonId.toString(); });
636
532
 
637
533
  // Both people are at this org
638
534
  if (oldPerson && newPerson) {
639
- org.people = _.reject(org.people, function(person) {
640
- return person.person.toString() === oldPersonId.toString();
535
+ org.people = _.reject(peopleArr, function(person) {
536
+ return person.person && person.person.toString() === oldPersonId.toString();
641
537
  });
642
538
  }
643
539
 
@@ -651,14 +547,14 @@ var updateOrganizations = function updateOrganizations(newPersonId, oldPersonId,
651
547
  // note this must be done before mapping the old id to the new id
652
548
  checkIfBothPeopleAtOrg(org);
653
549
 
654
- setObj.people = _.map(org.people, function(person) {
655
- if (person.person.toString() === oldPersonId.toString()) { person.person = newPersonId; }
550
+ setObj.people = _.map(org.people || [], function(person) {
551
+ if (person && person.person && person.person.toString() === oldPersonId.toString()) { person.person = newPersonId; }
656
552
  return person;
657
553
  });
658
554
 
659
- setObj.chairs = _.map(org.chairs, function(chair) {
660
- if (chair.first && chair.first.toString() === oldPersonId.toString()) { chair.first = newPersonId; }
661
- if (chair.second && chair.second.toString() === oldPersonId.toString()) { chair.second = newPersonId; }
555
+ setObj.chairs = _.map(org.chairs || [], function(chair) {
556
+ if (chair && chair.first && chair.first.toString() === oldPersonId.toString()) { chair.first = newPersonId; }
557
+ if (chair && chair.second && chair.second.toString() === oldPersonId.toString()) { chair.second = newPersonId; }
662
558
  return chair;
663
559
  });
664
560
 
@@ -693,26 +589,26 @@ var updatePeople = function updatePeople(newPersonId, oldPersonId, user, cb) {
693
589
 
694
590
  var self = this;
695
591
 
696
- // Get all orgs for this person
697
- self.Organization.collection.find({ 'related': oldPersonId }).toArray(function(err, items) {
592
+ // Update Person.related where oldPersonId appears
593
+ self.Person.collection.find({ 'related': oldPersonId }).toArray(function(err, people) {
698
594
 
699
595
  if (err) return cb(err, null);
700
- else if (!items || items.length === 0) return cb(null, []);
596
+ else if (!people || people.length === 0) return cb(null, []);
701
597
 
702
598
  var tasks = [];
703
599
  var updated = [];
704
600
 
705
- _.each(items, function(item) {
601
+ _.each(people, function(personDoc) {
706
602
 
707
603
  var setObj = {};
708
604
 
709
- setObj.related = _.map(item.related, function(person) {
710
- if (person.toString() === oldPersonId.toString()) { person = newPersonId; }
605
+ setObj.related = _.map(personDoc.related || [], function(person) {
606
+ if (person && person.toString() === oldPersonId.toString()) { person = newPersonId; }
711
607
  return person;
712
608
  });
713
609
 
714
610
  tasks.push({
715
- id: item._id,
611
+ id: personDoc._id,
716
612
  update: { $set: setObj }
717
613
  });
718
614
 
@@ -721,7 +617,7 @@ var updatePeople = function updatePeople(newPersonId, oldPersonId, user, cb) {
721
617
  console.log(' Found ' + tasks.length + ' person references to update');
722
618
 
723
619
  var queue = async.queue(function(task, callback) {
724
- self.Organization.modifyById(task.id, task.update, function(err, result) {
620
+ self.Person.modifyById(task.id, task.update, function(err, result) {
725
621
  if (err) { return callback(err, null); }
726
622
  else if (result) { updated.push(result); }
727
623
  return callback(null, result);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dhyasama/totem-merge",
3
- "version": "6.13.0",
3
+ "version": "6.15.0",
4
4
  "description": "Org and person merging package",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -22,7 +22,7 @@
22
22
  "underscore": "^1.13.6"
23
23
  },
24
24
  "devDependencies": {
25
- "@dhyasama/totem-models": "^9.128.1",
25
+ "@dhyasama/totem-models": "^11.110.0",
26
26
  "mocha": "^5.2.0",
27
27
  "mocha-mongoose": "^1.1.1",
28
28
  "should": "^8.2.2"
package/test/index.js CHANGED
@@ -15,7 +15,7 @@ var clearDB = require('mocha-mongoose')(config.db.uri, {noClear: true});
15
15
  var Merge = require('../index.js');
16
16
  var merge;
17
17
  var Deal = mongoose.model('Deal');
18
- var Interaction = mongoose.model('Interaction');
18
+ // var Interaction = mongoose.model('Interaction'); // deprecated
19
19
  var List = mongoose.model('List');
20
20
  var Message = mongoose.model('Message');
21
21
  var News = mongoose.model('News');
@@ -305,29 +305,7 @@ describe('Merge', function() {
305
305
 
306
306
  });
307
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
- });
308
+ // Interaction prep removed (replaced by meetings schema)
331
309
 
332
310
  describe('Orgs', function() {
333
311