@dhyasama/totem-merge 6.0.2 → 6.3.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
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
 
3
+ var _ = require('underscore');
3
4
  var organizations = require('./lib/organizations');
4
5
  var people = require('./lib/people');
5
6
  var Logger = require('@dhyasama/totem-logger');
@@ -38,24 +39,24 @@ var Merge = function(mongoose, config) {
38
39
 
39
40
  //////////////////////////////////////////////
40
41
 
41
- Merge.prototype.organizations = function(target, source, user, customerId, cb) {
42
- organizations.merge(this, target, source, user, customerId, cb);
42
+ Merge.prototype.organizations = function(target, source, user, cb) {
43
+ organizations.merge(this, target, source, user, cb);
43
44
  };
44
45
 
45
- Merge.prototype.organizationsById = function(targetId, sourceId, user, customerId, cb) {
46
+ Merge.prototype.organizationsById = function(targetId, sourceId, user, cb) {
46
47
 
47
48
  var self = this;
48
49
 
49
50
  self.logger.writeToInfoLog('Merge org ' + sourceId + ' into ' + targetId);
50
51
 
51
- self.Organization.getById(targetId, { CUSTOMER_ID: customerId, isWorkerProcess: true }, function(err, target) {
52
+ self.Organization.getByIdAsGlobal(targetId, {}, function(err, target) {
52
53
 
53
54
  if (err) { return cb(err, null); }
54
55
  else if (!target) { return cb(new Error('target not found'), null); }
55
56
 
56
57
  self.logger.writeToInfoLog(' Found target ' + target.name);
57
58
 
58
- self.Organization.getById(sourceId, { CUSTOMER_ID: customerId, isWorkerProcess: true }, function(err, source) {
59
+ self.Organization.getByIdAsGlobal(sourceId, {}, function(err, source) {
59
60
 
60
61
  if (err) { return cb(err, null); }
61
62
  else if (!source) { return cb(new Error('source not found'), null); }
@@ -63,7 +64,7 @@ Merge.prototype.organizationsById = function(targetId, sourceId, user, customerI
63
64
  self.logger.writeToInfoLog(' Found source ' + source.name);
64
65
  self.logger.writeToInfoLog(' Merging...');
65
66
 
66
- organizations.merge(self, target, source, user, customerId, cb);
67
+ organizations.merge(self, target, source, user, cb);
67
68
 
68
69
  });
69
70
 
@@ -71,34 +72,34 @@ Merge.prototype.organizationsById = function(targetId, sourceId, user, customerI
71
72
 
72
73
  };
73
74
 
74
- Merge.prototype.people = function(target, source, user, customerId, cb) {
75
- people.merge(this, target, source, user, customerId, cb);
75
+ Merge.prototype.people = function(target, source, user, cb) {
76
+ people.merge(this, target, source, user, cb);
76
77
  };
77
78
 
78
- Merge.prototype.peopleById = function(targetId, sourceId, user, customerId, cb) {
79
+ Merge.prototype.peopleById = function(targetId, sourceId, user, cb) {
79
80
 
80
81
  var self = this;
81
82
 
82
83
  self.logger.writeToInfoLog('Merge person ' + sourceId + ' into ' + targetId);
83
84
 
84
- self.Person.getById(targetId, { CUSTOMER_ID: customerId, isWorkerProcess: true }, function(err, target) {
85
+ self.Person.getByIdAsGlobal(targetId, {}, function(err, target) {
85
86
 
86
87
  if (err) { return cb(err, null); }
87
88
  else if (!target) { return cb(new Error('target not found'), null); }
88
89
 
89
90
  console.log(' Found target', target.name.full);
90
- console.log(' Target sources:', target.sources.length);
91
+ console.log(' Target sources:', target.sources);
91
92
 
92
- self.Person.getById(sourceId, { CUSTOMER_ID: customerId, isWorkerProcess: true }, function(err, source) {
93
+ self.Person.getByIdAsGlobal(sourceId, {}, function(err, source) {
93
94
 
94
95
  if (err) { return cb(err, null); }
95
96
  else if (!source) { return cb(new Error('source not found'), null); }
96
97
 
97
98
  self.logger.writeToInfoLog(' Found source ' + source.name.full);
98
- self.logger.writeToInfoLog(' Source sources ' + source.sources.length);
99
+ self.logger.writeToInfoLog(' Source sources ' + source.sources);
99
100
  self.logger.writeToInfoLog(' Merging...');
100
101
 
101
- people.merge(self, target, source, user, customerId, cb);
102
+ people.merge(self, target, source, user, cb);
102
103
 
103
104
  });
104
105
 
@@ -7,7 +7,7 @@ let mongoose = require('mongoose');
7
7
  let ObjectId = mongoose.Types.ObjectId;
8
8
  let logger;
9
9
 
10
- var merge = function merge(context, target, source, mergedBy, customerId, cb) {
10
+ var merge = function merge(context, target, source, mergedBy, cb) {
11
11
 
12
12
  logger = context.logger;
13
13
 
@@ -37,7 +37,7 @@ var merge = function merge(context, target, source, mergedBy, customerId, cb) {
37
37
  var url = 'https://admin.ffvc.com/orgs/' + source.id;
38
38
  var text = 'This org should be merged with ' + url;
39
39
 
40
- context.Organization.addFlag(target._id, target._id, text, customerId, function(err, result) {
40
+ context.Organization.addFlag(target._id, target._id, text, function(err, result) {
41
41
  if (err) return cb(err, null);
42
42
  return cb(null, result);
43
43
  });
@@ -70,7 +70,7 @@ var merge = function merge(context, target, source, mergedBy, customerId, cb) {
70
70
  saveOrganization.bind(null, target, username),
71
71
 
72
72
  // Change source references to target
73
- updateReferences.bind(null, context, target, source, mergedBy, customerId)
73
+ updateReferences.bind(null, context, target, source, mergedBy)
74
74
 
75
75
  ], function(err, results) {
76
76
 
@@ -148,7 +148,7 @@ var verifyOkToMerge = function verifyOkToMerge(target, source, cb) {
148
148
 
149
149
  };
150
150
 
151
- var updateReferences = function updateReferences(context, target, source, mergedBy, customerId, cb) {
151
+ var updateReferences = function updateReferences(context, target, source, mergedBy, cb) {
152
152
 
153
153
  logger.writeToInfoLog('Update references...');
154
154
 
@@ -161,8 +161,8 @@ var updateReferences = function updateReferences(context, target, source, merged
161
161
  var oldOrgId = ObjectId(source.id);
162
162
  var newOrgId = ObjectId(target.id);
163
163
 
164
- logger.writeToInfoLog(' Target: ' + newOrgId.toString());
165
- logger.writeToInfoLog(' Source: ' + oldOrgId.toString());
164
+ logger.writeToInfoLog(' Target:', newOrgId);
165
+ logger.writeToInfoLog(' Source:', oldOrgId);
166
166
 
167
167
  // mergedBy can be either an account or a username
168
168
  var username = mergedBy.username ? mergedBy.username : mergedBy;
@@ -171,7 +171,7 @@ var updateReferences = function updateReferences(context, target, source, merged
171
171
  // and get cleaned and updated on each pull anyway
172
172
 
173
173
  async.parallel([
174
- updateDeals.bind(context, newOrgId, oldOrgId, mergedBy, customerId),
174
+ updateDeals.bind(context, newOrgId, oldOrgId, mergedBy),
175
175
  updateLists.bind(context, newOrgId, oldOrgId, username),
176
176
  updateMessages.bind(context, newOrgId, oldOrgId, username),
177
177
  updateNews.bind(context, newOrgId, oldOrgId, username),
@@ -180,7 +180,7 @@ var updateReferences = function updateReferences(context, target, source, merged
180
180
 
181
181
  };
182
182
 
183
- var updateDeals = function updateDeals(newOrgId, oldOrgId, user, customerId, cb) {
183
+ var updateDeals = function updateDeals(newOrgId, oldOrgId, user, cb) {
184
184
 
185
185
  logger.writeToInfoLog(' Updating deals...');
186
186
 
@@ -205,7 +205,7 @@ var updateDeals = function updateDeals(newOrgId, oldOrgId, user, customerId, cb)
205
205
  logger.writeToInfoLog(' Found ' + tasks.length + ' deal references to update');
206
206
 
207
207
  var queue = async.queue(function(task, callback) {
208
- self.Deal.modifyById(task.id, task.update, { CUSTOMER_ID: customerId }, function(err, result) {
208
+ self.Deal.modifyById(task.id, task.update, {}, function(err, result) {
209
209
  if (err) { return callback(err, null); }
210
210
  else if (result) { updated.push(result); }
211
211
  return callback(null, result);
package/lib/people.js CHANGED
@@ -6,7 +6,7 @@ let helpers = require('./helpers');
6
6
  let mongoose = require('mongoose');
7
7
  let ObjectId = mongoose.Types.ObjectId;
8
8
 
9
- var merge = function merge(context, target, source, mergedBy, customerId, cb) {
9
+ var merge = function merge(context, target, source, mergedBy, cb) {
10
10
 
11
11
  console.log('Merging people...');
12
12
 
@@ -21,6 +21,9 @@ var merge = function merge(context, target, source, mergedBy, customerId, cb) {
21
21
 
22
22
  console.log('Saving target person...');
23
23
  console.log(' ID:', person._id.toString());
24
+ console.log(' LinkedIn:', person.social.linkedin);
25
+ console.log(' Twitter:', person.social.twitter);
26
+ console.log(' Email:', person.contact.email);
24
27
 
25
28
  context.Person.upsert(person, username, function(err, result) {
26
29
  return callback(err, result);
@@ -33,7 +36,7 @@ var merge = function merge(context, target, source, mergedBy, customerId, cb) {
33
36
  var url = 'https://admin.totemvc.com/people/' + source.id;
34
37
  var text = 'This person should be merged with ' + url;
35
38
 
36
- context.Person.addFlag(target._id, target._id, text, customerId, function(err, result) {
39
+ context.Person.addFlag(target._id, target._id, text, function(err, result) {
37
40
  if (err) return cb(err, null);
38
41
  return cb(null, result);
39
42
  });
@@ -107,7 +110,7 @@ var mergeSourceIntoTarget = function mergeEntities(target, source, cb) {
107
110
  _.each(sourceSources, function(sourceSource) {
108
111
 
109
112
  var match = _.find(targetSources, function(targetSource) {
110
- return targetSource.customer.toString() === sourceSource.customer.toString() && targetSource.person.toString() === sourceSource.person.toString();
113
+ return sourceSource.customer && sourceSource.person && targetSource.customer && targetSource.person && targetSource.customer.toString() === sourceSource.customer.toString() && targetSource.person.toString() === sourceSource.person.toString();
111
114
  });
112
115
 
113
116
  if (!match) { targetSources.push(sourceSource); }
@@ -147,6 +150,7 @@ var updateReferences = function updateReferences(context, target, source, merged
147
150
 
148
151
  console.log('Updating references...');
149
152
 
153
+
150
154
  // validate parameters
151
155
  if (!context) return cb(new Error('context is required'), null);
152
156
  if (!target) return cb(new Error('target is required'), null);
@@ -156,8 +160,8 @@ var updateReferences = function updateReferences(context, target, source, merged
156
160
  var oldPersonId = ObjectId(source.id);
157
161
  var newPersonId = ObjectId(target.id);
158
162
 
159
- console.log(' Target:', newPersonId.toString());
160
- console.log(' Source:', oldPersonId.toString());
163
+ console.log(' Target:', newPersonId);
164
+ console.log(' Source:', oldPersonId);
161
165
 
162
166
  // mergedBy can be either an account or a username
163
167
  var username = mergedBy.username ? mergedBy.username : mergedBy;
@@ -223,7 +227,7 @@ var updateDeals = function updateDeals(newPersonId, oldPersonId, user, cb) {
223
227
  console.log(' Found ' + tasks.length + ' deal references to update');
224
228
 
225
229
  var queue = async.queue(function(task, callback) {
226
- self.Deal.modifyById(task.id, task.update, function(err, result) {
230
+ self.Deal.modifyById(task.id, task.update, {}, function(err, result) {
227
231
  if (err) { return callback(err, null); }
228
232
  else if (result) { updated.push(result); }
229
233
  return callback(null, result);
@@ -270,7 +274,7 @@ var updateDocuments = function updateDocuments(newPersonId, oldPersonId, user, c
270
274
  console.log(' Found ' + tasks.length + ' doc references to update');
271
275
 
272
276
  var queue = async.queue(function(task, callback) {
273
- self.Document.modifyById(task.id, task.update, function(err, result) {
277
+ self.Document.modifyById(task.id, task.update, {}, function(err, result) {
274
278
  if (err) { return callback(err, null); }
275
279
  else if (result) { updated.push(result); }
276
280
  return callback(null, result);
@@ -575,7 +579,7 @@ var updateNotes = function updateNotes(newPersonId, oldPersonId, user, cb) {
575
579
  console.log(' Found ' + tasks.length + ' note references to update');
576
580
 
577
581
  var queue = async.queue(function(task, callback) {
578
- self.Note.modifyById(task.id, task.update, function(err, result) {
582
+ self.Note.modifyById(task.id, task.update, {}, function(err, result) {
579
583
  if (err) { return callback(err, null); }
580
584
  else if (result) { updated.push(result); }
581
585
  return callback(null, result);
package/package.json CHANGED
@@ -1,12 +1,8 @@
1
1
  {
2
2
  "name": "@dhyasama/totem-merge",
3
- "version": "6.0.2",
4
- "license": "UNLICENSED",
3
+ "version": "6.3.0",
5
4
  "description": "Org and person merging package",
6
5
  "main": "index.js",
7
- "engines": {
8
- "node": "12.13.1"
9
- },
10
6
  "scripts": {
11
7
  "prestart": "node ./lib/changeBSON",
12
8
  "start": "NODE_ENV=test node ./cli",
@@ -17,16 +13,16 @@
17
13
  },
18
14
  "author": "Jason Reynolds",
19
15
  "dependencies": {
20
- "@dhyasama/totem-logger": "^2.0.1",
21
- "@dhyasama/totem-utilities": "^3.0.2",
22
- "async": "^2.6.3",
23
- "commander": "^2.20.3",
16
+ "@dhyasama/totem-utilities": "^3.0.0",
17
+ "@dhyasama/totem-logger": "^2.0.0",
18
+ "async": "^2.0.0-rc.3",
19
+ "commander": "^2.14.1",
24
20
  "mongoose": "~4.5.7",
25
21
  "phone-formatter": "0.0.2",
26
22
  "underscore": "^1.8.3"
27
23
  },
28
24
  "devDependencies": {
29
- "@dhyasama/totem-models": "^8.0.4",
25
+ "@dhyasama/totem-models": "^8.0.0",
30
26
  "mocha": "^5.2.0",
31
27
  "mocha-mongoose": "^1.1.1",
32
28
  "should": "^8.2.2"
package/test/config.js CHANGED
@@ -7,7 +7,7 @@ module.exports = {
7
7
  LPS_ENABLED: false,
8
8
 
9
9
  db: {
10
- uri: 'mongodb://gZvwpF5aLFvdc7KFa7qLLiZYv6CTLdfs:TKfdi3LD4583f8EL11k5Y0uukZ151d9n@test-shard-00-00-gv0f3.mongodb.net:27017,test-shard-00-01-gv0f3.mongodb.net:27017,test-shard-00-02-gv0f3.mongodb.net:27017/test?ssl=true&replicaSet=Test-shard-0&authSource=admin'
10
+ uri: 'mongodb://ffvc:ytnn5m7A9snIJSOLRqwo7Bpo2QkyYx6q@ds043952.mongolab.com:43952/ff-platform-test'
11
11
  },
12
12
 
13
13
  crypto: {
package/test/index.js CHANGED
@@ -1,15 +1,8 @@
1
1
  "use strict";
2
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
3
  var should = require("should");
12
4
  var mongoose = require('mongoose');
5
+ var config = require('./config');
13
6
  var models = require('@dhyasama/totem-models')(mongoose, config);
14
7
  var clearDB = require('mocha-mongoose')(config.db.uri, {noClear: true});
15
8
  var Merge = require('../index.js');
@@ -39,34 +32,9 @@ describe('Merge', function() {
39
32
  clearDB(done);
40
33
  });
41
34
 
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
35
  // People prep
63
36
  before(function(done) {
64
37
 
65
- var p3 = new Person();
66
- p3.name.first = 'Toni';
67
- p3.name.last = 'Braxton';
68
- p3.slug = 'toni-braxton';
69
-
70
38
  var p1 = new Person();
71
39
  p1.name.first = 'George';
72
40
  p1.name.last = 'Miller';
@@ -74,8 +42,8 @@ describe('Merge', function() {
74
42
  p1.bio = 'Hello World!';
75
43
  p1.aliases.push('G-Money');
76
44
  p1.sources.push({
77
- person: p3._id,
78
- customer: customer._id
45
+ person: new mongoose.Types.ObjectId(),
46
+ customer: config.CUSTOMER_ID
79
47
  });
80
48
 
81
49
  var p2 = new Person();
@@ -84,6 +52,11 @@ describe('Merge', function() {
84
52
  p2.slug = 'braxton-miller';
85
53
  p2.sources.push(p1.sources[0]);
86
54
 
55
+ var p3 = new Person();
56
+ p3.name.first = 'Toni';
57
+ p3.name.last = 'Braxton';
58
+ p3.slug = 'toni-braxton';
59
+
87
60
  Person.upsert(p1, 'test', function(err, newPerson1) {
88
61
 
89
62
  should.not.exist(err);
@@ -180,7 +153,7 @@ describe('Merge', function() {
180
153
  should.exist(newOrg1);
181
154
 
182
155
  // getbyid to populate investedIn
183
- Organization.getById(newOrg1.id, { CUSTOMER_ID: customer._id, role: 'none' }, function(err, result1) {
156
+ Organization.getById(newOrg1.id, { role: 'none' }, function(err, result1) {
184
157
 
185
158
  should.not.exist(err);
186
159
  should.exist(result1);
@@ -192,7 +165,7 @@ describe('Merge', function() {
192
165
  should.not.exist(err);
193
166
  should.exist(newOrg2);
194
167
 
195
- Organization.getById(newOrg2.id, { CUSTOMER_ID: customer._id, role: 'none' }, function(err, result2) {
168
+ Organization.getById(newOrg2.id, { role: 'none' }, function(err, result2) {
196
169
 
197
170
  should.not.exist(err);
198
171
  should.exist(result2);
@@ -204,7 +177,7 @@ describe('Merge', function() {
204
177
  should.not.exist(err);
205
178
  should.exist(newOrg3);
206
179
 
207
- Organization.getById(newOrg3.id, { CUSTOMER_ID: customer._id, role: 'none' }, function(err, result3) {
180
+ Organization.getById(newOrg3.id, { role: 'none' }, function(err, result3) {
208
181
 
209
182
  should.not.exist(err);
210
183
  should.exist(result3);
@@ -216,7 +189,7 @@ describe('Merge', function() {
216
189
  should.not.exist(err);
217
190
  should.exist(newOrg4);
218
191
 
219
- Organization.getById(newOrg4.id, { CUSTOMER_ID: customer._id, role: 'none' }, function(err, result4) {
192
+ Organization.getById(newOrg4.id, { role: 'none' }, function(err, result4) {
220
193
 
221
194
  should.not.exist(err);
222
195
  should.exist(result4);
@@ -228,7 +201,7 @@ describe('Merge', function() {
228
201
  should.not.exist(err);
229
202
  should.exist(newOrg5);
230
203
 
231
- Organization.getById(newOrg5.id, { CUSTOMER_ID: customer._id, role: 'none' }, function(err, result5) {
204
+ Organization.getById(newOrg5.id, { role: 'none' }, function(err, result5) {
232
205
 
233
206
  should.not.exist(err);
234
207
  should.exist(result5);
@@ -240,7 +213,7 @@ describe('Merge', function() {
240
213
  should.not.exist(err);
241
214
  should.exist(newOrg6);
242
215
 
243
- Organization.getById(newOrg6.id, { CUSTOMER_ID: customer._id, role: 'none' }, function(err, result6) {
216
+ Organization.getById(newOrg6.id, { role: 'none' }, function(err, result6) {
244
217
 
245
218
  should.not.exist(err);
246
219
  should.exist(result6);
@@ -278,7 +251,7 @@ describe('Merge', function() {
278
251
  // List prep
279
252
  before(function(done) {
280
253
 
281
- List.createList('Test List', { organizations:[org1], people: [person1] }, person1, { CUSTOMER_ID: customer._id }, function(err, list) {
254
+ List.createList('Test List', { organizations:[org1], people: [person1] }, person1, function(err, list) {
282
255
  should.not.exist(err);
283
256
  should.exist(list);
284
257
  return done();
@@ -311,7 +284,7 @@ describe('Merge', function() {
311
284
  var interaction = new Interaction({
312
285
  calendarEventId: new mongoose.Types.ObjectId(),
313
286
  providerEventId: 'ABC123',
314
- totemCustomerId: customer._id,
287
+ totemCustomerId: config.CUSTOMER_ID,
315
288
  summary: 'Test',
316
289
  startTime: new Date(),
317
290
  recurring: false,
@@ -329,6 +302,28 @@ describe('Merge', function() {
329
302
 
330
303
  });
331
304
 
305
+ // Customer prep
306
+ before(function(done) {
307
+
308
+ customer = new Organization();
309
+ customer.name = 'HUGE VC';
310
+ customer.slug = 'huge-vc';
311
+
312
+ Organization.upsert(customer, 'test', function(err, result) {
313
+
314
+ should.not.exist(err);
315
+ should.exist(result);
316
+
317
+ customer = result;
318
+
319
+ config.CUSTOMER_ID = customer._id.toString();
320
+
321
+ return done();
322
+
323
+ });
324
+
325
+ });
326
+
332
327
  describe('Orgs', function() {
333
328
 
334
329
  it('Flags organization to be merged', function (done) {
@@ -336,7 +331,7 @@ describe('Merge', function() {
336
331
  config.flag = true;
337
332
  merge = new Merge(mongoose, config);
338
333
 
339
- merge.organizations(org2, org1, 'testy', customer._id, function(err, result) {
334
+ merge.organizations(org2, org1, 'testy', function(err, result) {
340
335
  should.not.exist(err);
341
336
  should.exist(result);
342
337
  result.addedTo.flags.length.should.equal(1);
@@ -358,7 +353,7 @@ describe('Merge', function() {
358
353
  should.not.exist(err);
359
354
  should.exist(result);
360
355
 
361
- merge.organizationsById(org2._id, org1._id, 'testy', customer._id, function(err, result) {
356
+ merge.organizationsById(org2._id, org1._id, 'testy', function(err, result) {
362
357
 
363
358
  should.not.exist(err);
364
359
  should.exist(result);
@@ -417,7 +412,7 @@ describe('Merge', function() {
417
412
  should.not.exist(err);
418
413
  should.exist(result);
419
414
 
420
- merge.organizationsById(org5._id, org4._id, 'testy', customer._id, function(err, result) {
415
+ merge.organizationsById(org5._id, org4._id, 'testy', function(err, result) {
421
416
  should.not.exist(err);
422
417
  should.exist(result);
423
418
  result.people.length.should.equal(2);
@@ -436,9 +431,11 @@ describe('Merge', function() {
436
431
  config.flag = false;
437
432
  merge = new Merge(mongoose, config);
438
433
 
434
+ var pid = new mongoose.Types.ObjectId();
435
+
439
436
  org2.people = [];
440
437
  org2.people.push({
441
- person: person2,
438
+ person: pid,
442
439
  roles: [{
443
440
  role: 'test 1'
444
441
  }]
@@ -446,7 +443,7 @@ describe('Merge', function() {
446
443
 
447
444
  org5.people = [];
448
445
  org5.people.push({
449
- person: person2,
446
+ person: pid,
450
447
  roles: [{
451
448
  role: 'test 2'
452
449
  }]
@@ -466,7 +463,7 @@ describe('Merge', function() {
466
463
 
467
464
  org5 = result;
468
465
 
469
- merge.organizationsById(org5._id, org2._id, 'testy', customer._id, function(err, result) {
466
+ merge.organizationsById(org5._id, org2._id, 'testy', function(err, result) {
470
467
  should.not.exist(err);
471
468
  should.exist(result);
472
469
  result.people.length.should.equal(1);
@@ -496,14 +493,14 @@ describe('Merge', function() {
496
493
  should.not.exist(err);
497
494
  should.exist(result1);
498
495
 
499
- merge.organizationsById(org6._id, org5._id, new mongoose.Types.ObjectId(), customer._id, function(err, result2) {
496
+ merge.organizationsById(org6._id, org5._id, new mongoose.Types.ObjectId(), function(err, result2) {
500
497
 
501
498
  should.not.exist(err);
502
499
  should.exist(result2);
503
500
 
504
501
  org6 = result2;
505
502
 
506
- Deal.getById(deal.id, { CUSTOMER_ID: customer._id }, function(err, result3) {
503
+ Deal.getById(deal.id, function(err, result3) {
507
504
 
508
505
  should.not.exist(err);
509
506
  should.exist(result3);
@@ -539,14 +536,14 @@ describe('Merge', function() {
539
536
  should.not.exist(err);
540
537
  should.exist(result1);
541
538
 
542
- merge.organizationsById(org6._id, org3._id, new mongoose.Types.ObjectId(), customer._id, function(err, result2) {
539
+ merge.organizationsById(org6._id, org3._id, new mongoose.Types.ObjectId(), function(err, result2) {
543
540
 
544
541
  should.not.exist(err);
545
542
  should.exist(result2);
546
543
 
547
544
  org6 = result2;
548
545
 
549
- List.getById(list.id, { CUSTOMER_ID: customer._id }, function(err, result3) {
546
+ List.getById(list.id, function(err, result3) {
550
547
 
551
548
  should.not.exist(err);
552
549
  should.exist(result3);
@@ -595,14 +592,14 @@ describe('Merge', function() {
595
592
  should.not.exist(err);
596
593
  should.exist(result1);
597
594
 
598
- merge.organizationsById(org6._id, newOrg._id, new mongoose.Types.ObjectId(), customer._id, function(err, result2) {
595
+ merge.organizationsById(org6._id, newOrg._id, new mongoose.Types.ObjectId(), function(err, result2) {
599
596
 
600
597
  should.not.exist(err);
601
598
  should.exist(result2);
602
599
 
603
600
  org6 = result2;
604
601
 
605
- Message.getById(message.id, { CUSTOMER_ID: customer._id }, function(err, result3) {
602
+ Message.getById(message.id, function(err, result3) {
606
603
 
607
604
  should.not.exist(err);
608
605
  should.exist(result3);
@@ -649,19 +646,19 @@ describe('Merge', function() {
649
646
  should.not.exist(err);
650
647
  should.exist(result1);
651
648
 
652
- merge.organizationsById(org6._id, newOrg._id, new mongoose.Types.ObjectId(), customer._id, function(err, result2) {
649
+ merge.organizationsById(org6._id, newOrg._id, new mongoose.Types.ObjectId(), function(err, result2) {
653
650
 
654
651
  should.not.exist(err);
655
652
  should.exist(result2);
656
653
 
657
654
  org6 = result2;
658
655
 
659
- News.findById(news.id, function(err, result3) {
656
+ News.getById(news.id, function(err, result3) {
660
657
 
661
658
  should.not.exist(err);
662
659
  should.exist(result3);
663
660
 
664
- result3.org.toString().should.equal(org6._id.toString());
661
+ result3.org._id.toString().should.equal(org6._id.toString());
665
662
 
666
663
  return done();
667
664
 
@@ -696,7 +693,7 @@ describe('Merge', function() {
696
693
  should.not.exist(err);
697
694
  should.exist(result);
698
695
 
699
- merge.organizationsById(org6._id, newOrg._id, 'testy', customer._id, function(err, result) {
696
+ merge.organizationsById(org6._id, newOrg._id, 'testy', function(err, result) {
700
697
  should.exist(err);
701
698
  should.not.exist(result);
702
699
  return done();
@@ -717,7 +714,7 @@ describe('Merge', function() {
717
714
  config.flag = true;
718
715
  merge = new Merge(mongoose, config);
719
716
 
720
- merge.people(person2, person1, 'testy', customer._id, function(err, result) {
717
+ merge.people(person2, person1, 'testy', function(err, result) {
721
718
  should.not.exist(err);
722
719
  should.exist(result);
723
720
  result.addedTo.flags.length.should.equal(1);
@@ -732,7 +729,9 @@ describe('Merge', function() {
732
729
  config.flag = false;
733
730
  merge = new Merge(mongoose, config);
734
731
 
735
- merge.peopleById(person2.id, person1.id, 'testy', customer._id, function(err, result) {
732
+ config.CUSTOMER_ID = 'GLOBAL_PROCESS';
733
+
734
+ merge.peopleById(person2.id, person1.id, 'testy', function(err, result) {
736
735
 
737
736
  should.not.exist(err);
738
737
  should.exist(result);
@@ -742,7 +741,12 @@ describe('Merge', function() {
742
741
  result.aliases.should.containEql(person1.name.full);
743
742
  result.sources.length.should.equal(1);
744
743
 
745
- return done();
744
+ List.getByPerson(person1, function(err, result) {
745
+ should.not.exist(err);
746
+ should.exist(result);
747
+ result.length.should.equal(0);
748
+ return done();
749
+ });
746
750
 
747
751
  });
748
752
 
@@ -754,7 +758,7 @@ describe('Merge', function() {
754
758
  config.flag = false;
755
759
  merge = new Merge(mongoose, config);
756
760
 
757
- Person.getById(person2._id, { CUSTOMER_ID: customer._id }, function(err, result) {
761
+ Person.getById(person2._id, function(err, result) {
758
762
 
759
763
  should.not.exist(err);
760
764
  should.exist(result);
@@ -789,7 +793,7 @@ describe('Merge', function() {
789
793
  should.exist(result);
790
794
  newPerson = result;
791
795
 
792
- merge.peopleById(person2.id, newPerson.id, 'testy', customer._id, function(err, result) {
796
+ merge.peopleById(person2.id, newPerson.id, 'testy', function(err, result) {
793
797
  should.not.exist(err);
794
798
  should.exist(result);
795
799
  result.contact.phone.length.should.equal(2);
@@ -806,7 +810,7 @@ describe('Merge', function() {
806
810
 
807
811
  it('Merges people with same phone number', function (done) {
808
812
 
809
- Person.getById(person2._id, { CUSTOMER_ID: customer._id }, function(err, result) {
813
+ Person.getById(person2._id, function(err, result) {
810
814
 
811
815
  should.not.exist(err);
812
816
  should.exist(result);
@@ -841,7 +845,7 @@ describe('Merge', function() {
841
845
  should.exist(result);
842
846
  newPerson = result;
843
847
 
844
- merge.peopleById(person2.id, newPerson.id, 'testy', customer._id, function(err, result) {
848
+ merge.peopleById(person2.id, newPerson.id, 'testy', function(err, result) {
845
849
  should.not.exist(err);
846
850
  should.exist(result);
847
851
  result.contact.phone.length.should.equal(1);
@@ -862,6 +866,8 @@ describe('Merge', function() {
862
866
  config.flag = false;
863
867
  merge = new Merge(mongoose, config);
864
868
 
869
+ config.CUSTOMER_ID = 'GLOBAL_PROCESS';
870
+
865
871
  var myNewPerson1 = new Person();
866
872
  myNewPerson1.name = { first:'jason', last:'reynolds'};
867
873
  myNewPerson1.slug = 'jason-reynolds';
@@ -892,12 +898,12 @@ describe('Merge', function() {
892
898
  should.not.exist(err);
893
899
  should.exist(result);
894
900
 
895
- merge.peopleById(newPerson2.id, newPerson1.id, 'testy', customer._id, function(err, merged) {
901
+ merge.peopleById(newPerson2.id, newPerson1.id, 'testy', function(err, merged) {
896
902
 
897
903
  should.not.exist(err);
898
904
  should.exist(merged);
899
905
 
900
- Organization.getById(result.id, { CUSTOMER_ID: customer._id, role: 'none' }, function(err, finalResult) {
906
+ Organization.getById(result.id, { role: 'none' }, function(err, finalResult) {
901
907
 
902
908
  should.not.exist(err);
903
909
  should.exist(finalResult);
@@ -953,12 +959,12 @@ describe('Merge', function() {
953
959
  result.people.length.should.equal(1);
954
960
  result.people[0].person.toString().should.equal(newPerson1._id.toString()); // source
955
961
 
956
- merge.peopleById(newPerson2.id, newPerson1.id, 'testy', customer._id, function(err, merged) {
962
+ merge.peopleById(newPerson2.id, newPerson1.id, 'testy', function(err, merged) {
957
963
 
958
964
  should.not.exist(err);
959
965
  should.exist(merged);
960
966
 
961
- Organization.getById(result.id, { CUSTOMER_ID: customer._id, role: 'none' }, function(err, finalResult) {
967
+ Organization.getById(result.id, { role: 'none' }, function(err, finalResult) {
962
968
 
963
969
  should.not.exist(err);
964
970
  should.exist(finalResult);
@@ -986,13 +992,17 @@ describe('Merge', function() {
986
992
  config.flag = false;
987
993
  merge = new Merge(mongoose, config);
988
994
 
995
+ var source1 = new mongoose.Types.ObjectId();
996
+ var source2 = new mongoose.Types.ObjectId();
997
+ var customer1 = new mongoose.Types.ObjectId();
998
+
989
999
  var newPerson1 = new Person();
990
1000
  newPerson1.name.first = 'New';
991
1001
  newPerson1.name.last = 'Person';
992
1002
  newPerson1.slug = 'new-person-1';
993
1003
  newPerson1.sources = [{
994
- person: person3,
995
- customer: customer
1004
+ person: source2,
1005
+ customer: customer1
996
1006
  }];
997
1007
 
998
1008
  var newPerson2 = new Person();
@@ -1000,8 +1010,8 @@ describe('Merge', function() {
1000
1010
  newPerson2.name.last = 'Person';
1001
1011
  newPerson2.slug = 'new-person-2';
1002
1012
  newPerson2.sources = [{
1003
- person: person2,
1004
- customer: customer
1013
+ person: source1,
1014
+ customer: customer1
1005
1015
  }];
1006
1016
 
1007
1017
  Person.upsert(newPerson1, 'test', function(err, result) {
@@ -1016,16 +1026,16 @@ describe('Merge', function() {
1016
1026
  should.exist(result);
1017
1027
  newPerson2 = result;
1018
1028
 
1019
- merge.peopleById(newPerson2.id, newPerson1.id, 'testy', customer._id, function(err, result) {
1029
+ merge.peopleById(newPerson2.id, newPerson1.id, 'testy', function(err, result) {
1020
1030
 
1021
1031
  should.not.exist(err);
1022
1032
  should.exist(result);
1023
1033
 
1024
1034
  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());
1035
+ result.sources[0].person.toString().should.equal(source1.toString());
1036
+ result.sources[0].customer.toString().should.equal(customer1.toString());
1037
+ result.sources[1].person.toString().should.equal(source2.toString());
1038
+ result.sources[1].customer.toString().should.equal(customer1.toString());
1029
1039
 
1030
1040
 
1031
1041
  return done();
@@ -1044,13 +1054,17 @@ describe('Merge', function() {
1044
1054
  config.flag = false;
1045
1055
  merge = new Merge(mongoose, config);
1046
1056
 
1057
+ var source1 = new mongoose.Types.ObjectId();
1058
+ var customer1 = new mongoose.Types.ObjectId();
1059
+ var customer2 = new mongoose.Types.ObjectId();
1060
+
1047
1061
  var newPerson1 = new Person();
1048
1062
  newPerson1.name.first = 'New';
1049
1063
  newPerson1.name.last = 'Person';
1050
1064
  newPerson1.slug = 'new-person-1';
1051
1065
  newPerson1.sources = [{
1052
- person: person3._id,
1053
- customer: org2._id
1066
+ person: source1,
1067
+ customer: customer1
1054
1068
  }];
1055
1069
 
1056
1070
  var newPerson2 = new Person();
@@ -1058,8 +1072,8 @@ describe('Merge', function() {
1058
1072
  newPerson2.name.last = 'Person';
1059
1073
  newPerson2.slug = 'new-person-2';
1060
1074
  newPerson2.sources = [{
1061
- person: person3._id,
1062
- customer: org3._id
1075
+ person: source1,
1076
+ customer: customer2
1063
1077
  }];
1064
1078
 
1065
1079
  Person.upsert(newPerson1, 'test', function(err, result) {
@@ -1074,16 +1088,16 @@ describe('Merge', function() {
1074
1088
  should.exist(result);
1075
1089
  newPerson2 = result;
1076
1090
 
1077
- merge.peopleById(newPerson2.id, newPerson1.id, 'testy', customer._id, function(err, result) {
1091
+ merge.peopleById(newPerson2.id, newPerson1.id, 'testy', function(err, result) {
1078
1092
 
1079
1093
  should.not.exist(err);
1080
1094
  should.exist(result);
1081
1095
 
1082
1096
  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());
1097
+ result.sources[0].person.toString().should.equal(source1.toString());
1098
+ result.sources[0].customer.toString().should.equal(customer2.toString());
1099
+ result.sources[1].person.toString().should.equal(source1.toString());
1100
+ result.sources[1].customer.toString().should.equal(customer1.toString());
1087
1101
 
1088
1102
 
1089
1103
  return done();
@@ -1102,13 +1116,18 @@ describe('Merge', function() {
1102
1116
  config.flag = false;
1103
1117
  merge = new Merge(mongoose, config);
1104
1118
 
1119
+ var source1 = new mongoose.Types.ObjectId();
1120
+ var source2 = new mongoose.Types.ObjectId();
1121
+ var customer1 = new mongoose.Types.ObjectId();
1122
+ var customer2 = new mongoose.Types.ObjectId();
1123
+
1105
1124
  var newPerson1 = new Person();
1106
1125
  newPerson1.name.first = 'New';
1107
1126
  newPerson1.name.last = 'Person';
1108
1127
  newPerson1.slug = 'new-person-1';
1109
1128
  newPerson1.sources = [{
1110
- person: person3._id,
1111
- customer: org3._id
1129
+ person: source1,
1130
+ customer: customer1
1112
1131
  }];
1113
1132
 
1114
1133
  var newPerson2 = new Person();
@@ -1116,8 +1135,8 @@ describe('Merge', function() {
1116
1135
  newPerson2.name.last = 'Person';
1117
1136
  newPerson2.slug = 'new-person-2';
1118
1137
  newPerson2.sources = [{
1119
- person: person2._id,
1120
- customer: org2._id
1138
+ person: source2,
1139
+ customer: customer2
1121
1140
  }];
1122
1141
 
1123
1142
  Person.upsert(newPerson1, 'test', function(err, result) {
@@ -1132,17 +1151,89 @@ describe('Merge', function() {
1132
1151
  should.exist(result);
1133
1152
  newPerson2 = result;
1134
1153
 
1135
- merge.peopleById(newPerson2.id, newPerson1.id, 'testy', customer._id, function(err, result) {
1154
+ merge.peopleById(newPerson2.id, newPerson1.id, 'testy', function(err, result) {
1136
1155
 
1137
1156
  should.not.exist(err);
1138
1157
  should.exist(result);
1139
1158
 
1140
1159
  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());
1160
+ result.sources[0].person.toString().should.equal(source2.toString());
1161
+ result.sources[0].customer.toString().should.equal(customer2.toString());
1162
+ result.sources[1].person.toString().should.equal(source1.toString());
1163
+ result.sources[1].customer.toString().should.equal(customer1.toString());
1164
+
1165
+
1166
+ return done();
1167
+
1168
+ });
1169
+
1170
+ });
1171
+
1172
+ });
1173
+
1174
+ });
1175
+
1176
+ it('Merges four sources for two customers', function(done) {
1177
+
1178
+ // reset to not flag
1179
+ config.flag = false;
1180
+ merge = new Merge(mongoose, config);
1181
+
1182
+ var source1 = new mongoose.Types.ObjectId();
1183
+ var source2 = new mongoose.Types.ObjectId();
1184
+ var source3 = new mongoose.Types.ObjectId();
1185
+ var source4 = new mongoose.Types.ObjectId();
1186
+ var customer1 = new mongoose.Types.ObjectId();
1187
+ var customer2 = new mongoose.Types.ObjectId();
1188
+
1189
+ var newPerson1 = new Person();
1190
+ newPerson1.name.first = 'New';
1191
+ newPerson1.name.last = 'Person';
1192
+ newPerson1.slug = 'new-person-1';
1193
+ newPerson1.sources = [{
1194
+ person: source1,
1195
+ customer: customer1
1196
+ }, {
1197
+ person: source2,
1198
+ customer: customer1
1199
+ }, {
1200
+ person: source2,
1201
+ customer: customer2
1202
+ }];
1203
+
1204
+ var newPerson2 = new Person();
1205
+ newPerson2.name.first = 'Newer';
1206
+ newPerson2.name.last = 'Person';
1207
+ newPerson2.slug = 'new-person-2';
1208
+ newPerson2.sources = [{
1209
+ person: source2,
1210
+ customer: customer2
1211
+ },{
1212
+ person: source3,
1213
+ customer: customer1
1214
+ },{
1215
+ person: source4,
1216
+ customer: customer2
1217
+ }];
1218
+
1219
+ Person.upsert(newPerson1, 'test', function(err, result) {
1220
+
1221
+ should.not.exist(err);
1222
+ should.exist(result);
1223
+ newPerson1 = result;
1224
+
1225
+ Person.upsert(newPerson2, 'test', function(err, result) {
1226
+
1227
+ should.not.exist(err);
1228
+ should.exist(result);
1229
+ newPerson2 = result;
1230
+
1231
+ merge.peopleById(newPerson2.id, newPerson1.id, 'testy', function(err, result) {
1232
+
1233
+ should.not.exist(err);
1234
+ should.exist(result);
1145
1235
 
1236
+ result.sources.length.should.equal(5);
1146
1237
 
1147
1238
  return done();
1148
1239