@dhyasama/totem-models 8.29.0 → 8.30.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/Financials.js CHANGED
@@ -10,6 +10,8 @@
10
10
  module.exports = function(mongoose, config) {
11
11
 
12
12
  var Schema = mongoose.Schema;
13
+ var Account = mongoose.model('Account');
14
+ var async = require('async');
13
15
  var _ = require('underscore');
14
16
  var moment = require('moment');
15
17
 
@@ -276,7 +278,7 @@ module.exports = function(mongoose, config) {
276
278
 
277
279
  };
278
280
 
279
- Financials.statics.getCompanyInitialUUIDsToSend = function getCompanyInitialUUIDsToSend(cb) {
281
+ Financials.statics.getCompanyInitialToSend = function getCompanyInitialToSend(cb) {
280
282
 
281
283
  var self = this;
282
284
  var now = new Date();
@@ -295,26 +297,38 @@ module.exports = function(mongoose, config) {
295
297
 
296
298
  if (err) { return cb(err, null); }
297
299
 
298
- var snapshots = _.pluck(result || [], 'snapshots');
300
+ var results = [];
299
301
 
302
+ var snapshots = _.pluck(result || [], 'snapshots');
300
303
  snapshots = _.flatten(snapshots);
301
304
 
302
- snapshots = _.filter(snapshots, function(snapshot) {
305
+ _.each(snapshots, function(snapshot) {
303
306
 
304
307
  var hasRecipients = snapshot.notifications.company.sendTo.length > 0;
305
308
  var hasUnsentSendOnLessThanNow = snapshot.notifications.company.initial.sendOn < now && !snapshot.notifications.company.initial.sentOn;
306
309
 
307
- return hasRecipients && hasUnsentSendOnLessThanNow;
310
+ if(hasRecipients && hasUnsentSendOnLessThanNow) {
311
+
312
+ var recipients = snapshot.notifications.company.sendTo.map(function(sendTo) {
313
+ return sendTo.email;
314
+ }).join(',');
315
+
316
+ results.push({
317
+ 'uuid': snapshot.uuid,
318
+ 'recipients': recipients
319
+ });
320
+
321
+ }
308
322
 
309
323
  });
310
324
 
311
- return cb(null, _.pluck(snapshots, 'uuid'));
325
+ return cb(null, results);
312
326
 
313
327
  });
314
328
 
315
329
  };
316
330
 
317
- Financials.statics.getCompanyReminderUUIDsToSend = function getCompanyReminderUUIDsToSend(cb) {
331
+ Financials.statics.getCompanyReminderToSend = function getCompanyReminderToSend(cb) {
318
332
 
319
333
  var self = this;
320
334
  var now = new Date();
@@ -325,13 +339,13 @@ module.exports = function(mongoose, config) {
325
339
  'notifications.company.sendTo': { $gt: [] },
326
340
  'notifications.company.reminders.sendOn': { $lte: now },
327
341
  'notifications.company.reminders.sentOn': null,
328
- "$or": [
342
+ '$or': [
329
343
  {
330
- "submittedOn": null,
344
+ 'submittedOn': null,
331
345
  },
332
346
  {
333
- "submittedOn": { $ne: null },
334
- "rejection": { $ne: null }
347
+ 'submittedOn': { $ne: null },
348
+ 'rejection': { $ne: null }
335
349
  }
336
350
  ]
337
351
  }
@@ -342,11 +356,12 @@ module.exports = function(mongoose, config) {
342
356
 
343
357
  if (err) { return cb(err, null); }
344
358
 
345
- var snapshots = _.pluck(result || [], 'snapshots');
359
+ var results = [];
346
360
 
361
+ var snapshots = _.pluck(result || [], 'snapshots');
347
362
  snapshots = _.flatten(snapshots);
348
363
 
349
- snapshots = _.filter(snapshots, function(snapshot) {
364
+ _.each(snapshots, function(snapshot) {
350
365
 
351
366
  var hasRecipients = snapshot.notifications.company.sendTo.length > 0;
352
367
  var hasUnsentSendOnLessThanNow = _.find(snapshot.notifications.company.reminders, function(reminder) {
@@ -355,17 +370,28 @@ module.exports = function(mongoose, config) {
355
370
  var hasBeenSubmitted = snapshot.submittedOn;
356
371
  var hasBeenRejected = snapshot.rejection;
357
372
 
358
- return hasRecipients && hasUnsentSendOnLessThanNow && ((!hasBeenSubmitted) || (hasBeenSubmitted && hasBeenRejected));
373
+ if(hasRecipients && hasUnsentSendOnLessThanNow && ((!hasBeenSubmitted) || (hasBeenSubmitted && hasBeenRejected))) {
374
+
375
+ var recipients = snapshot.notifications.company.sendTo.map(function(sendTo) {
376
+ return sendTo.email;
377
+ }).join(',');
378
+
379
+ results.push({
380
+ 'uuid': snapshot.uuid,
381
+ 'recipients': recipients
382
+ });
383
+
384
+ }
359
385
 
360
386
  });
361
387
 
362
- return cb(null, _.pluck(snapshots, 'uuid'));
388
+ return cb(null, results);
363
389
 
364
390
  });
365
391
 
366
392
  };
367
393
 
368
- Financials.statics.getCompanyRejectionUUIDsToSend = function getCompanyRejectionUUIDsToSend(cb) {
394
+ Financials.statics.getCompanyRejectionToSend = function getCompanyRejectionToSend(cb) {
369
395
 
370
396
  var self = this;
371
397
  var now = new Date();
@@ -376,8 +402,8 @@ module.exports = function(mongoose, config) {
376
402
  'notifications.company.sendTo': { $gt: [] },
377
403
  'notifications.company.rejections.sendOn': { $lte: now },
378
404
  'notifications.company.rejections.sentOn': null,
379
- "submittedOn": { $ne: null },
380
- "rejection": { $ne: null }
405
+ 'submittedOn': { $ne: null },
406
+ 'rejection': { $ne: null }
381
407
  }
382
408
  }
383
409
  });
@@ -386,11 +412,12 @@ module.exports = function(mongoose, config) {
386
412
 
387
413
  if (err) { return cb(err, null); }
388
414
 
389
- var snapshots = _.pluck(result || [], 'snapshots');
415
+ var results = [];
390
416
 
417
+ var snapshots = _.pluck(result || [], 'snapshots');
391
418
  snapshots = _.flatten(snapshots);
392
419
 
393
- snapshots = _.filter(snapshots, function(snapshot) {
420
+ _.each(snapshots, function(snapshot) {
394
421
 
395
422
  var hasRecipients = snapshot.notifications.company.sendTo.length > 0;
396
423
  var hasUnsentSendOnLessThanNow = _.find(snapshot.notifications.company.rejections, function(rejection) {
@@ -399,17 +426,28 @@ module.exports = function(mongoose, config) {
399
426
  var hasBeenSubmitted = snapshot.submittedOn;
400
427
  var hasBeenRejected = snapshot.rejection;
401
428
 
402
- return hasRecipients && hasUnsentSendOnLessThanNow && hasBeenSubmitted && hasBeenRejected;
429
+ if(hasRecipients && hasUnsentSendOnLessThanNow && hasBeenSubmitted && hasBeenRejected) {
430
+
431
+ var recipients = snapshot.notifications.company.sendTo.map(function(sendTo) {
432
+ return sendTo.email;
433
+ }).join(',');
434
+
435
+ results.push({
436
+ 'uuid': snapshot.uuid,
437
+ 'recipients': recipients
438
+ });
439
+
440
+ }
403
441
 
404
442
  });
405
443
 
406
- return cb(null, _.pluck(snapshots, 'uuid'));
444
+ return cb(null, results);
407
445
 
408
446
  });
409
447
 
410
448
  };
411
449
 
412
- Financials.statics.getStakeholderInitialUUIDsToSend = function getStakeholderInitialUUIDsToSend(cb) {
450
+ Financials.statics.getStakeholderInitialToSend = function getStakeholderInitialToSend(cb) {
413
451
 
414
452
  var self = this;
415
453
  var now = new Date();
@@ -429,31 +467,50 @@ module.exports = function(mongoose, config) {
429
467
 
430
468
  if (err) { return cb(err, null); }
431
469
 
432
- var snapshots = _.pluck(result || [], 'snapshots');
470
+ var results = [];
433
471
 
472
+ var snapshots = _.pluck(result || [], 'snapshots');
434
473
  snapshots = _.flatten(snapshots);
435
474
 
436
- snapshots = _.filter(snapshots, function(snapshot) {
475
+ async.each(snapshots, function(snapshot, callback1) {
437
476
 
438
- return _.some(snapshot.notifications.stakeholders, function(stakeholder) {
477
+ async.each(snapshot.notifications.stakeholders, function(stakeholder, callback2) {
439
478
 
440
479
  var hasEmail = stakeholder.sendTo && stakeholder.sendTo.email;
441
480
  var hasOrganization = stakeholder.sendTo && stakeholder.sendTo.organization;
442
481
  var hasUnsentSendOnLessThanNow = stakeholder.initial && stakeholder.initial.sendOn < now && !stakeholder.initial.sentOn;
443
482
 
444
- return hasEmail && hasOrganization && hasUnsentSendOnLessThanNow;
483
+ if (!hasEmail || !hasOrganization || !hasUnsentSendOnLessThanNow) return callback2();
445
484
 
485
+ Account.findByEmail(stakeholder.sendTo.email, function(err, account) {
486
+
487
+ var hasAccount = !_.isEmpty(account);
488
+ var hasAccessToken = !_.isEmpty(account) && account.auth && (account.auth.google && account.auth.google.accessToken) || (account.auth.microsoft && account.auth.microsoft.accessToken);
489
+
490
+ if (!hasAccount || hasAccessToken) return callback2();
491
+
492
+ results.push({
493
+ 'uuid': snapshot.uuid,
494
+ 'recipients': stakeholder.sendTo.email
495
+ });
496
+
497
+ return callback2();
498
+
499
+ });
500
+
501
+ }, function (err) {
502
+ return callback1(err, results);
446
503
  });
447
504
 
505
+ }, function (err) {
506
+ return cb(err, results);
448
507
  });
449
508
 
450
- return cb(null, _.pluck(snapshots, 'uuid'));
451
-
452
509
  });
453
510
 
454
511
  };
455
512
 
456
- Financials.statics.getStakeholderReminderUUIDsToSend = function getStakeholderReminderUUIDsToSend(cb) {
513
+ Financials.statics.getStakeholderReminderToSend = function getStakeholderReminderToSend(cb) {
457
514
 
458
515
  var self = this;
459
516
  var now = new Date();
@@ -461,8 +518,8 @@ module.exports = function(mongoose, config) {
461
518
  var query = self.find({
462
519
  'snapshots': {
463
520
  $elemMatch: {
464
- 'notifications.stakeholders.sendTo.email': { $ne: null },
465
- 'notifications.stakeholders.sendTo.organization': { $ne: null },
521
+ 'notifications.stakeholders.sendTo': { $gt: [] },
522
+ 'notifications.stakeholders.organization': { $ne: null },
466
523
  'notifications.stakeholders.reminders.sendOn': { $lte: now },
467
524
  'notifications.stakeholders.reminders.sentOn': null
468
525
  }
@@ -473,28 +530,47 @@ module.exports = function(mongoose, config) {
473
530
 
474
531
  if (err) { return cb(err, null); }
475
532
 
476
- var snapshots = _.pluck(result || [], 'snapshots');
533
+ var results = [];
477
534
 
535
+ var snapshots = _.pluck(result || [], 'snapshots');
478
536
  snapshots = _.flatten(snapshots);
479
537
 
480
- snapshots = _.filter(snapshots, function(snapshot) {
538
+ async.each(snapshots, function(snapshot, callback1) {
481
539
 
482
- return _.some(snapshot.notifications.stakeholders, function(stakeholder) {
540
+ async.each(snapshot.notifications.stakeholders, function(stakeholder, callback2) {
483
541
 
484
- var hasEmail = stakeholder.sendTo && stakeholder.sendTo.email;
485
- var hasOrganization = stakeholder.sendTo && stakeholder.sendTo.organization;
542
+ var hasRecipients = stakeholder.sendTo && stakeholder.sendTo.length > 0;
543
+ var hasOrganization = stakeholder.organization;
486
544
  var hasUnsentSendOnLessThanNow = _.find(stakeholder.reminders, function(reminder) {
487
545
  return reminder.sendOn < now && !reminder.sentOn;
488
546
  });
489
547
 
490
- return hasEmail && hasOrganization && hasUnsentSendOnLessThanNow;
548
+ if (!hasRecipients || !hasOrganization || !hasUnsentSendOnLessThanNow) return callback1();
549
+
550
+ Account.findByEmail(stakeholder.sendTo.email, function(err, account) {
551
+
552
+ var hasAccount = !_.isEmpty(account);
553
+ var hasAccessToken = !_.isEmpty(account) && account.auth && (account.auth.google && account.auth.google.accessToken) || (account.auth.microsoft && account.auth.microsoft.accessToken);
554
+
555
+ if (!hasAccount || hasAccessToken) return callback2();
491
556
 
557
+ results.push({
558
+ 'uuid': snapshot.uuid,
559
+ 'recipients': stakeholder.sendTo.email
560
+ });
561
+
562
+ return callback2();
563
+
564
+ });
565
+
566
+ }, function (err) {
567
+ return callback1(err, results);
492
568
  });
493
569
 
570
+ }, function (err) {
571
+ return cb(err, results);
494
572
  });
495
573
 
496
- return cb(null, _.pluck(snapshots, 'uuid'));
497
-
498
574
  });
499
575
 
500
576
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dhyasama/totem-models",
3
- "version": "8.29.0",
3
+ "version": "8.30.0",
4
4
  "author": "Jason Reynolds",
5
5
  "license": "UNLICENSED",
6
6
  "description": "Models for Totem platform",
@@ -92,8 +92,11 @@ describe('Financials', function() {
92
92
  // getByUUID
93
93
  // getForCampaign
94
94
  // getForCustomer
95
- // getUUIDsToSendInitial
96
- // getUUIDsToSendReminder
95
+ // getCompanyInitialToSend
96
+ // getCompanyReminderToSend
97
+ // getCompanyRejectionToSend
98
+ // getStakeholderInitialToSend
99
+ // getStakeholderReminderToSend
97
100
  // removeCustomerFinancials
98
101
  // upsert
99
102