@dhyasama/totem-merge 6.4.0 → 6.6.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/lib/helpers.js CHANGED
@@ -97,10 +97,10 @@ var unique = function unique(array, key) {
97
97
  val = item;
98
98
  }
99
99
  else if (typeOf(item) == 'object') {
100
- val = key == 'number' ? phoneFormatter.normalize(item[key]) : item[key];
100
+ val = key == 'number' && item[key] ? phoneFormatter.normalize(item[key]) : item[key];
101
101
  }
102
102
  else {
103
- val = key == 'number' ? phoneFormatter.normalize(item) : item;
103
+ val = key == 'number' && item[key] ? phoneFormatter.normalize(item) : item;
104
104
  }
105
105
 
106
106
  // take one last shot
@@ -1,4 +1,4 @@
1
- "use strict";
1
+ 'use strict';
2
2
 
3
3
  let async = require('async');
4
4
  let _ = require('underscore');
@@ -43,6 +43,7 @@ var merge = function merge(context, target, source, mergedBy, customerId, cb) {
43
43
  });
44
44
 
45
45
  }
46
+
46
47
  else {
47
48
 
48
49
  // mergedBy can be either an account or a username
@@ -70,7 +71,7 @@ var merge = function merge(context, target, source, mergedBy, customerId, cb) {
70
71
  saveOrganization.bind(null, target, username),
71
72
 
72
73
  // Change source references to target
73
- updateReferences.bind(null, context, target, source, mergedBy, customerId)
74
+ updateReferences.bind(null, context, target, source, mergedBy)
74
75
 
75
76
  ], function(err, results) {
76
77
 
@@ -125,7 +126,7 @@ var mergeSourceIntoTarget = function mergeEntities(target, source, cb) {
125
126
 
126
127
  // mark as entered by system
127
128
  target.entered = {
128
- by: 'ff-merge',
129
+ by: 'merge-package',
129
130
  on: new Date()
130
131
  };
131
132
 
@@ -148,7 +149,7 @@ var verifyOkToMerge = function verifyOkToMerge(target, source, cb) {
148
149
 
149
150
  };
150
151
 
151
- var updateReferences = function updateReferences(context, target, source, mergedBy, customerId, cb) {
152
+ var updateReferences = function updateReferences(context, target, source, mergedBy, cb) {
152
153
 
153
154
  logger.writeToInfoLog('Update references...');
154
155
 
@@ -171,16 +172,60 @@ var updateReferences = function updateReferences(context, target, source, merged
171
172
  // and get cleaned and updated on each pull anyway
172
173
 
173
174
  async.parallel([
174
- updateDeals.bind(context, newOrgId, oldOrgId, mergedBy, customerId),
175
- updateLists.bind(context, newOrgId, oldOrgId, username),
176
- updateMessages.bind(context, newOrgId, oldOrgId, username),
177
- updateNews.bind(context, newOrgId, oldOrgId, username),
178
- updateOrganizations.bind(context, newOrgId, oldOrgId, username)
179
- ], cb)
175
+ updateCapTables.bind(context, newOrgId, oldOrgId),
176
+ updateDeals.bind(context, newOrgId, oldOrgId),
177
+ updateFinancials.bind(context, newOrgId, oldOrgId),
178
+ updateLists.bind(context, newOrgId, oldOrgId),
179
+ updateMessages.bind(context, newOrgId, oldOrgId),
180
+ updateNews.bind(context, newOrgId, oldOrgId),
181
+ updateOrganizations.bind(context, newOrgId, oldOrgId),
182
+ updateRounds.bind(context, newOrgId, oldOrgId)
183
+ ], cb);
180
184
 
181
185
  };
182
186
 
183
- var updateDeals = function updateDeals(newOrgId, oldOrgId, user, customerId, cb) {
187
+ var updateCapTables = function updateCapTables(newOrgId, oldOrgId, cb) {
188
+
189
+ logger.writeToInfoLog(' Updating cap tables...');
190
+
191
+ var self = this;
192
+
193
+ // Get all cap tables for this org
194
+ self.CapTable.collection.find({ 'organization': oldOrgId }).toArray(function(err, items) {
195
+
196
+ if (err) return cb(err, null);
197
+ else if (!items || items.length === 0) return cb(null, []);
198
+
199
+ var tasks = [];
200
+ var updated = [];
201
+
202
+ _.each(items, function(item) {
203
+ tasks.push({
204
+ id: item._id,
205
+ update: { $set: { organization: newOrgId } }
206
+ });
207
+ });
208
+
209
+ logger.writeToInfoLog(' Found ' + tasks.length + ' cap table references to update');
210
+
211
+ var queue = async.queue(function(task, callback) {
212
+
213
+ self.CapTable.modifyById(task.id, task.update, function(err, result) {
214
+ if (err) { return callback(err, null); }
215
+ else if (result) { updated.push(result); }
216
+ return callback(null, result);
217
+ });
218
+ }, 8);
219
+
220
+ queue.drain = function() { return cb(null, updated); };
221
+
222
+ queue.push(tasks);
223
+
224
+ });
225
+
226
+ };
227
+
228
+ var updateDeals = function updateDeals(newOrgId, oldOrgId, cb) {
184
229
 
185
230
  logger.writeToInfoLog(' Updating deals...');
186
231
 
@@ -206,7 +251,48 @@ var updateDeals = function updateDeals(newOrgId, oldOrgId, user, customerId, cb)
206
251
 
207
252
  var queue = async.queue(function(task, callback) {
208
253
 
209
- self.Deal.modifyById(task.id, task.update, { CUSTOMER_ID: customerId }, function(err, result) {
254
+ self.Deal.modifyById(task.id, task.update, function(err, result) {
255
+ if (err) { return callback(err, null); }
256
+ else if (result) { updated.push(result); }
257
+ return callback(null, result);
258
+ });
259
+ }, 8);
260
+
261
+ queue.drain = function() { return cb(null, updated); };
262
+
263
+ queue.push(tasks);
264
+
265
+ });
266
+
267
+ };
268
+
269
+ var updateFinancials = function updateFinancials(newOrgId, oldOrgId, cb) {
270
+
271
+ logger.writeToInfoLog(' Updating financials...');
272
+
273
+ var self = this;
274
+
275
+ // Get all financials for this org
276
+ self.Financials.collection.find({ 'organization': oldOrgId }).toArray(function(err, items) {
277
+
278
+ if (err) return cb(err, null);
279
+ else if (!items || items.length === 0) return cb(null, []);
280
+
281
+ var tasks = [];
282
+ var updated = [];
283
+
284
+ _.each(items, function(item) {
285
+ tasks.push({
286
+ id: item._id,
287
+ update: { $set: { organization: newOrgId } }
288
+ });
289
+ });
290
+
291
+ logger.writeToInfoLog(' Found ' + tasks.length + ' financials references to update');
292
+
293
+ var queue = async.queue(function(task, callback) {
294
+
295
+ self.Financials.modifyById(task.id, task.update, function(err, result) {
210
296
  if (err) { return callback(err, null); }
211
297
  else if (result) { updated.push(result); }
212
298
  return callback(null, result);
@@ -221,7 +307,7 @@ var updateDeals = function updateDeals(newOrgId, oldOrgId, user, customerId, cb)
221
307
 
222
308
  };
223
309
 
224
- var updateLists = function updateLists(newOrgId, oldOrgId, user, cb) {
310
+ var updateLists = function updateLists(newOrgId, oldOrgId, cb) {
225
311
 
226
312
  logger.writeToInfoLog(' Updating lists...');
227
313
 
@@ -267,7 +353,7 @@ var updateLists = function updateLists(newOrgId, oldOrgId, user, cb) {
267
353
 
268
354
  };
269
355
 
270
- var updateMessages = function updateMessages(newOrgId, oldOrgId, user, cb) {
356
+ var updateMessages = function updateMessages(newOrgId, oldOrgId, cb) {
271
357
 
272
358
  logger.writeToInfoLog(' Updating messages...');
273
359
 
@@ -306,7 +392,7 @@ var updateMessages = function updateMessages(newOrgId, oldOrgId, user, cb) {
306
392
 
307
393
  };
308
394
 
309
- var updateNews = function updateNews(newOrgId, oldOrgId, user, cb) {
395
+ var updateNews = function updateNews(newOrgId, oldOrgId, cb) {
310
396
 
311
397
  logger.writeToInfoLog(' Updating news...');
312
398
 
@@ -345,7 +431,7 @@ var updateNews = function updateNews(newOrgId, oldOrgId, user, cb) {
345
431
 
346
432
  };
347
433
 
348
- var updateOrganizations = function updateOrganizations(newOrgId, oldOrgId, user, cb) {
434
+ var updateOrganizations = function updateOrganizations(newOrgId, oldOrgId, cb) {
349
435
 
350
436
  logger.writeToInfoLog(' Updating orgs...');
351
437
 
@@ -373,11 +459,11 @@ var updateOrganizations = function updateOrganizations(newOrgId, oldOrgId, user,
373
459
  return o;
374
460
  });
375
461
 
376
- var publicBy = item["operating.acquired.public.by"];
377
- var privateBy = item["operating.acquired.private.by"];
462
+ var publicBy = item['operating.acquired.public.by'];
463
+ var privateBy = item['operating.acquired.private.by'];
378
464
 
379
- if (publicBy && publicBy.toString() === oldOrgId) { setObj["operating.acquired.public.by"] = newOrgId; }
380
- if (privateBy && privateBy.toString() === oldOrgId) { setObj["operating.acquired.private.by"] = newOrgId; }
465
+ if (publicBy && publicBy.toString() === oldOrgId) { setObj['operating.acquired.public.by'] = newOrgId; }
466
+ if (privateBy && privateBy.toString() === oldOrgId) { setObj['operating.acquired.private.by'] = newOrgId; }
381
467
 
382
468
  tasks.push({
383
469
  id: item._id,
@@ -406,4 +492,45 @@ var updateOrganizations = function updateOrganizations(newOrgId, oldOrgId, user,
406
492
 
407
493
  };
408
494
 
495
+ var updateRounds = function updateRounds(newOrgId, oldOrgId, cb) {
496
+
497
+ logger.writeToInfoLog(' Updating rounds...');
498
+
499
+ var self = this;
500
+
501
+ // Get all rounds for this org
502
+ self.Round.collection.find({ 'organization': oldOrgId }).toArray(function(err, items) {
503
+
504
+ if (err) return cb(err, null);
505
+ else if (!items || items.length === 0) return cb(null, []);
506
+
507
+ var tasks = [];
508
+ var updated = [];
509
+
510
+ _.each(items, function(item) {
511
+ tasks.push({
512
+ id: item._id,
513
+ update: { $set: { organization: newOrgId } }
514
+ });
515
+ });
516
+
517
+ logger.writeToInfoLog(' Found ' + tasks.length + ' round references to update');
518
+
519
+ var queue = async.queue(function(task, callback) {
520
+
521
+ self.Round.modifyById(task.id, task.update, function(err, result) {
522
+ if (err) { return callback(err, null); }
523
+ else if (result) { updated.push(result); }
524
+ return callback(null, result);
525
+ });
526
+ }, 8);
527
+
528
+ queue.drain = function() { return cb(null, updated); };
529
+
530
+ queue.push(tasks);
531
+
532
+ });
533
+
534
+ };
535
+
409
536
  module.exports.merge = merge;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dhyasama/totem-merge",
3
- "version": "6.4.0",
3
+ "version": "6.6.0",
4
4
  "description": "Org and person merging package",
5
5
  "main": "index.js",
6
6
  "engines": {
@@ -22,10 +22,10 @@
22
22
  "commander": "^2.20.3",
23
23
  "mongoose": "~4.5.7",
24
24
  "phone-formatter": "0.0.2",
25
- "underscore": "^1.8.3"
25
+ "underscore": "^1.13.2"
26
26
  },
27
27
  "devDependencies": {
28
- "@dhyasama/totem-models": "^8.0.4",
28
+ "@dhyasama/totem-models": "^9.51.0",
29
29
  "mocha": "^5.2.0",
30
30
  "mocha-mongoose": "^1.1.1",
31
31
  "should": "^8.2.2"