@dhyasama/totem-merge 6.14.0 → 6.16.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
@@ -21,6 +21,7 @@ var Merge = function(mongoose, config) {
21
21
  this.Organization = mongoose.model('Organization');
22
22
  this.Person = mongoose.model('Person');
23
23
  this.Round = mongoose.model('Round');
24
+ this.Snapshot = mongoose.model('Snapshot');
24
25
 
25
26
  // can choose to flag an entity as needing to be merged rather than actually merging it
26
27
  // particularly useful for automated processes, e.g., a batch import that detects dupes
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 || [];
@@ -240,8 +238,10 @@ var updateDocuments = function updateDocuments(newPersonId, oldPersonId, user, c
240
238
 
241
239
  var self = this;
242
240
 
243
- // Get all documents for this person
244
- 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) {
245
245
 
246
246
  if (err) return cb(err, null);
247
247
  else if (!items || items.length === 0) return cb(null, []);
@@ -253,13 +253,14 @@ var updateDocuments = function updateDocuments(newPersonId, oldPersonId, user, c
253
253
 
254
254
  var setObj = {};
255
255
 
256
- if (item.source && item.source.person && item.source.person.toString() === oldPersonId.toString()) { setObj.source = { person: newPersonId }; }
257
- 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; }
258
257
 
259
- tasks.push({
260
- id: item._id,
261
- update: { $set: setObj }
262
- });
258
+ if (Object.keys(setObj).length > 0) {
259
+ tasks.push({
260
+ id: item._id,
261
+ update: { $set: setObj }
262
+ });
263
+ }
263
264
 
264
265
  });
265
266
 
@@ -317,7 +318,7 @@ var updateMeetings = function updateMeetings(newPersonId, oldPersonId, user, cb)
317
318
  console.log(' Found ' + tasks.length + ' meeting references to update');
318
319
 
319
320
  var queue = async.queue(function(task, callback) {
320
- self.Meeting.modifyById(task.id, task.update, function(err, result) {
321
+ self.Meeting.findByIdAndUpdate(task.id, task.update, { upsert: false, new: true }, function(err, result) {
321
322
  if (err) { return callback(err, null); }
322
323
  else if (result) { updated.push(result); }
323
324
  return callback(null, result);
@@ -491,7 +492,7 @@ var updateOrganizations = function updateOrganizations(newPersonId, oldPersonId,
491
492
  var self = this;
492
493
 
493
494
  // Get all orgs for this person
494
- 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) {
495
496
 
496
497
  if (err) return cb(err, null);
497
498
  else if (!orgs || orgs.length === 0) return cb(null, []);
@@ -525,13 +526,14 @@ var updateOrganizations = function updateOrganizations(newPersonId, oldPersonId,
525
526
  // and be unable to determine which data to keep. We still can't know for sure, but let's default to keeping the
526
527
  // target/new person's data. We do that by removing the source/old person here and then map the target/new person id.
527
528
 
528
- var oldPerson = _.find(org.people, function(person) { return person.person.toString() === oldPersonId.toString(); });
529
- 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(); });
530
532
 
531
533
  // Both people are at this org
532
534
  if (oldPerson && newPerson) {
533
- org.people = _.reject(org.people, function(person) {
534
- return person.person.toString() === oldPersonId.toString();
535
+ org.people = _.reject(peopleArr, function(person) {
536
+ return person.person && person.person.toString() === oldPersonId.toString();
535
537
  });
536
538
  }
537
539
 
@@ -545,14 +547,14 @@ var updateOrganizations = function updateOrganizations(newPersonId, oldPersonId,
545
547
  // note this must be done before mapping the old id to the new id
546
548
  checkIfBothPeopleAtOrg(org);
547
549
 
548
- setObj.people = _.map(org.people, function(person) {
549
- 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; }
550
552
  return person;
551
553
  });
552
554
 
553
- setObj.chairs = _.map(org.chairs, function(chair) {
554
- if (chair.first && chair.first.toString() === oldPersonId.toString()) { chair.first = newPersonId; }
555
- 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; }
556
558
  return chair;
557
559
  });
558
560
 
@@ -587,26 +589,26 @@ var updatePeople = function updatePeople(newPersonId, oldPersonId, user, cb) {
587
589
 
588
590
  var self = this;
589
591
 
590
- // Get all orgs for this person
591
- 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) {
592
594
 
593
595
  if (err) return cb(err, null);
594
- else if (!items || items.length === 0) return cb(null, []);
596
+ else if (!people || people.length === 0) return cb(null, []);
595
597
 
596
598
  var tasks = [];
597
599
  var updated = [];
598
600
 
599
- _.each(items, function(item) {
601
+ _.each(people, function(personDoc) {
600
602
 
601
603
  var setObj = {};
602
604
 
603
- setObj.related = _.map(item.related, function(person) {
604
- if (person.toString() === oldPersonId.toString()) { person = newPersonId; }
605
+ setObj.related = _.map(personDoc.related || [], function(person) {
606
+ if (person && person.toString() === oldPersonId.toString()) { person = newPersonId; }
605
607
  return person;
606
608
  });
607
609
 
608
610
  tasks.push({
609
- id: item._id,
611
+ id: personDoc._id,
610
612
  update: { $set: setObj }
611
613
  });
612
614
 
@@ -615,7 +617,7 @@ var updatePeople = function updatePeople(newPersonId, oldPersonId, user, cb) {
615
617
  console.log(' Found ' + tasks.length + ' person references to update');
616
618
 
617
619
  var queue = async.queue(function(task, callback) {
618
- self.Organization.modifyById(task.id, task.update, function(err, result) {
620
+ self.Person.modifyById(task.id, task.update, function(err, result) {
619
621
  if (err) { return callback(err, null); }
620
622
  else if (result) { updated.push(result); }
621
623
  return callback(null, result);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dhyasama/totem-merge",
3
- "version": "6.14.0",
3
+ "version": "6.16.0",
4
4
  "description": "Org and person merging package",
5
5
  "main": "index.js",
6
6
  "scripts": {
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