@dhyasama/totem-models 8.29.0 → 8.31.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();
@@ -291,30 +293,47 @@ module.exports = function(mongoose, config) {
291
293
  }
292
294
  });
293
295
 
296
+ query.populate('organization', 'name logoUrl');
297
+ query.populate('customer', 'name logoUrl customer.totemUrl');
298
+
294
299
  query.exec(function(err, result) {
295
300
 
296
301
  if (err) { return cb(err, null); }
297
302
 
298
- var snapshots = _.pluck(result || [], 'snapshots');
303
+ var results = [];
304
+
305
+ _.each(result, function(financial) {
306
+
307
+ _.each(financial.snapshots, function(snapshot) {
299
308
 
300
- snapshots = _.flatten(snapshots);
309
+ var hasRecipients = snapshot.notifications.company.sendTo.length > 0;
310
+ var hasUnsentSendOnLessThanNow = snapshot.notifications.company.initial.sendOn < now && !snapshot.notifications.company.initial.sentOn;
301
311
 
302
- snapshots = _.filter(snapshots, function(snapshot) {
312
+ if(hasRecipients && hasUnsentSendOnLessThanNow) {
303
313
 
304
- var hasRecipients = snapshot.notifications.company.sendTo.length > 0;
305
- var hasUnsentSendOnLessThanNow = snapshot.notifications.company.initial.sendOn < now && !snapshot.notifications.company.initial.sentOn;
314
+ var recipients = snapshot.notifications.company.sendTo.map(function(sendTo) {
315
+ return sendTo.email;
316
+ }).join(',');
306
317
 
307
- return hasRecipients && hasUnsentSendOnLessThanNow;
318
+ results.push({
319
+ 'snapshot': snapshot,
320
+ 'customer': snapsot.customer,
321
+ 'organization': snapshot.organization
322
+ });
323
+
324
+ }
325
+
326
+ });
308
327
 
309
328
  });
310
329
 
311
- return cb(null, _.pluck(snapshots, 'uuid'));
330
+ return cb(null, results);
312
331
 
313
332
  });
314
333
 
315
334
  };
316
335
 
317
- Financials.statics.getCompanyReminderUUIDsToSend = function getCompanyReminderUUIDsToSend(cb) {
336
+ Financials.statics.getCompanyReminderToSend = function getCompanyReminderToSend(cb) {
318
337
 
319
338
  var self = this;
320
339
  var now = new Date();
@@ -325,47 +344,64 @@ module.exports = function(mongoose, config) {
325
344
  'notifications.company.sendTo': { $gt: [] },
326
345
  'notifications.company.reminders.sendOn': { $lte: now },
327
346
  'notifications.company.reminders.sentOn': null,
328
- "$or": [
347
+ '$or': [
329
348
  {
330
- "submittedOn": null,
349
+ 'submittedOn': null,
331
350
  },
332
351
  {
333
- "submittedOn": { $ne: null },
334
- "rejection": { $ne: null }
352
+ 'submittedOn': { $ne: null },
353
+ 'rejection': { $ne: null }
335
354
  }
336
355
  ]
337
356
  }
338
357
  }
339
358
  });
340
359
 
360
+ query.populate('organization', 'name logoUrl');
361
+ query.populate('customer', 'name logoUrl customer.totemUrl');
362
+
341
363
  query.exec(function(err, result) {
342
364
 
343
365
  if (err) { return cb(err, null); }
344
366
 
345
- var snapshots = _.pluck(result || [], 'snapshots');
367
+ var results = [];
346
368
 
347
- snapshots = _.flatten(snapshots);
369
+ _.each(result, function(financial) {
348
370
 
349
- snapshots = _.filter(snapshots, function(snapshot) {
371
+ _.each(financial.snapshots, function(snapshot) {
350
372
 
351
- var hasRecipients = snapshot.notifications.company.sendTo.length > 0;
352
- var hasUnsentSendOnLessThanNow = _.find(snapshot.notifications.company.reminders, function(reminder) {
353
- return reminder.sendOn < now && !reminder.sentOn;
354
- });
355
- var hasBeenSubmitted = snapshot.submittedOn;
356
- var hasBeenRejected = snapshot.rejection;
373
+ var hasRecipients = snapshot.notifications.company.sendTo.length > 0;
374
+ var hasUnsentSendOnLessThanNow = _.find(snapshot.notifications.company.reminders, function(reminder) {
375
+ return reminder.sendOn < now && !reminder.sentOn;
376
+ });
377
+ var hasBeenSubmitted = snapshot.submittedOn;
378
+ var hasBeenRejected = snapshot.rejection;
379
+
380
+ if(hasRecipients && hasUnsentSendOnLessThanNow && ((!hasBeenSubmitted) || (hasBeenSubmitted && hasBeenRejected))) {
381
+
382
+ var recipients = snapshot.notifications.company.sendTo.map(function(sendTo) {
383
+ return sendTo.email;
384
+ }).join(',');
385
+
386
+ results.push({
387
+ 'snapshot': snapshot,
388
+ 'customer': snapsot.customer,
389
+ 'organization': snapshot.organization
390
+ });
391
+
392
+ }
357
393
 
358
- return hasRecipients && hasUnsentSendOnLessThanNow && ((!hasBeenSubmitted) || (hasBeenSubmitted && hasBeenRejected));
394
+ });
359
395
 
360
396
  });
361
397
 
362
- return cb(null, _.pluck(snapshots, 'uuid'));
398
+ return cb(null, results);
363
399
 
364
400
  });
365
401
 
366
402
  };
367
403
 
368
- Financials.statics.getCompanyRejectionUUIDsToSend = function getCompanyRejectionUUIDsToSend(cb) {
404
+ Financials.statics.getCompanyRejectionToSend = function getCompanyRejectionToSend(cb) {
369
405
 
370
406
  var self = this;
371
407
  var now = new Date();
@@ -376,40 +412,57 @@ module.exports = function(mongoose, config) {
376
412
  'notifications.company.sendTo': { $gt: [] },
377
413
  'notifications.company.rejections.sendOn': { $lte: now },
378
414
  'notifications.company.rejections.sentOn': null,
379
- "submittedOn": { $ne: null },
380
- "rejection": { $ne: null }
415
+ 'submittedOn': { $ne: null },
416
+ 'rejection': { $ne: null }
381
417
  }
382
418
  }
383
419
  });
384
420
 
421
+ query.populate('organization', 'name logoUrl');
422
+ query.populate('customer', 'name logoUrl customer.totemUrl');
423
+
385
424
  query.exec(function(err, result) {
386
425
 
387
426
  if (err) { return cb(err, null); }
388
427
 
389
- var snapshots = _.pluck(result || [], 'snapshots');
428
+ var results = [];
390
429
 
391
- snapshots = _.flatten(snapshots);
430
+ _.each(result, function(financial) {
392
431
 
393
- snapshots = _.filter(snapshots, function(snapshot) {
432
+ _.each(financial.snapshots, function(snapshot) {
394
433
 
395
- var hasRecipients = snapshot.notifications.company.sendTo.length > 0;
396
- var hasUnsentSendOnLessThanNow = _.find(snapshot.notifications.company.rejections, function(rejection) {
397
- return rejection.sendOn < now && !rejection.sentOn;
398
- });
399
- var hasBeenSubmitted = snapshot.submittedOn;
400
- var hasBeenRejected = snapshot.rejection;
434
+ var hasRecipients = snapshot.notifications.company.sendTo.length > 0;
435
+ var hasUnsentSendOnLessThanNow = _.find(snapshot.notifications.company.rejections, function(rejection) {
436
+ return rejection.sendOn < now && !rejection.sentOn;
437
+ });
438
+ var hasBeenSubmitted = snapshot.submittedOn;
439
+ var hasBeenRejected = snapshot.rejection;
440
+
441
+ if(hasRecipients && hasUnsentSendOnLessThanNow && hasBeenSubmitted && hasBeenRejected) {
442
+
443
+ var recipients = snapshot.notifications.company.sendTo.map(function(sendTo) {
444
+ return sendTo.email;
445
+ }).join(',');
446
+
447
+ results.push({
448
+ 'snapshot': snapshot,
449
+ 'customer': snapsot.customer,
450
+ 'organization': snapshot.organization
451
+ });
401
452
 
402
- return hasRecipients && hasUnsentSendOnLessThanNow && hasBeenSubmitted && hasBeenRejected;
453
+ }
454
+
455
+ });
403
456
 
404
457
  });
405
458
 
406
- return cb(null, _.pluck(snapshots, 'uuid'));
459
+ return cb(null, results);
407
460
 
408
461
  });
409
462
 
410
463
  };
411
464
 
412
- Financials.statics.getStakeholderInitialUUIDsToSend = function getStakeholderInitialUUIDsToSend(cb) {
465
+ Financials.statics.getStakeholderInitialToSend = function getStakeholderInitialToSend(cb) {
413
466
 
414
467
  var self = this;
415
468
  var now = new Date();
@@ -425,35 +478,62 @@ module.exports = function(mongoose, config) {
425
478
  }
426
479
  });
427
480
 
481
+ query.populate('organization', 'name logoUrl');
482
+ query.populate('customer', 'name logoUrl customer.totemUrl');
483
+ query.populate('snapshots.notifications.stakeholders.sendTo.organization', 'name logoUrl customer.totemUrl');
484
+
428
485
  query.exec(function(err, result) {
429
486
 
430
487
  if (err) { return cb(err, null); }
431
488
 
432
- var snapshots = _.pluck(result || [], 'snapshots');
489
+ var results = [];
490
+
491
+ async.each(result, function(financial, callback1) {
492
+
493
+ async.each(financial.snapshots, function(snapshot, callback2) {
494
+
495
+ async.each(snapshot.notifications.stakeholders, function(stakeholder, callback3) {
496
+
497
+ var hasEmail = stakeholder.sendTo && stakeholder.sendTo.email;
498
+ var hasOrganization = stakeholder.sendTo && stakeholder.sendTo.organization;
499
+ var hasUnsentSendOnLessThanNow = stakeholder.initial && stakeholder.initial.sendOn < now && !stakeholder.initial.sentOn;
433
500
 
434
- snapshots = _.flatten(snapshots);
501
+ if (!hasEmail || !hasOrganization || !hasUnsentSendOnLessThanNow) return callback3();
435
502
 
436
- snapshots = _.filter(snapshots, function(snapshot) {
503
+ Account.findByEmail(stakeholder.sendTo.email, function(err, account) {
437
504
 
438
- return _.some(snapshot.notifications.stakeholders, function(stakeholder) {
505
+ var hasAccount = !_.isEmpty(account);
506
+ var hasAccessToken = !_.isEmpty(account) && account.auth && (account.auth.google && account.auth.google.accessToken) || (account.auth.microsoft && account.auth.microsoft.accessToken);
439
507
 
440
- var hasEmail = stakeholder.sendTo && stakeholder.sendTo.email;
441
- var hasOrganization = stakeholder.sendTo && stakeholder.sendTo.organization;
442
- var hasUnsentSendOnLessThanNow = stakeholder.initial && stakeholder.initial.sendOn < now && !stakeholder.initial.sentOn;
508
+ if (!hasAccount || hasAccessToken) return callback3();
443
509
 
444
- return hasEmail && hasOrganization && hasUnsentSendOnLessThanNow;
510
+ results.push({
511
+ 'snapshot': snapshot,
512
+ 'stakeholder': stakeholder,
513
+ 'organization': snapshot.organization
514
+ });
445
515
 
516
+ return callback3();
517
+
518
+ });
519
+
520
+ }, function (err) {
521
+ return callback2(err, results);
522
+ });
523
+
524
+ }, function (err) {
525
+ return callback1(err, results);
446
526
  });
447
527
 
528
+ }, function (err) {
529
+ return cb(err, results);
448
530
  });
449
531
 
450
- return cb(null, _.pluck(snapshots, 'uuid'));
451
-
452
532
  });
453
533
 
454
534
  };
455
535
 
456
- Financials.statics.getStakeholderReminderUUIDsToSend = function getStakeholderReminderUUIDsToSend(cb) {
536
+ Financials.statics.getStakeholderReminderToSend = function getStakeholderReminderToSend(cb) {
457
537
 
458
538
  var self = this;
459
539
  var now = new Date();
@@ -461,67 +541,79 @@ module.exports = function(mongoose, config) {
461
541
  var query = self.find({
462
542
  'snapshots': {
463
543
  $elemMatch: {
464
- 'notifications.stakeholders.sendTo.email': { $ne: null },
465
- 'notifications.stakeholders.sendTo.organization': { $ne: null },
544
+ 'notifications.stakeholders.sendTo': { $gt: [] },
545
+ 'notifications.stakeholders.organization': { $ne: null },
466
546
  'notifications.stakeholders.reminders.sendOn': { $lte: now },
467
547
  'notifications.stakeholders.reminders.sentOn': null
468
548
  }
469
549
  }
470
550
  });
471
551
 
552
+ query.populate('organization', 'name logoUrl');
553
+ query.populate('customer', 'name logoUrl customer.totemUrl');
554
+ query.populate('snapshots.notifications.stakeholders.sendTo.organization', 'name logoUrl customer.totemUrl');
555
+
472
556
  query.exec(function(err, result) {
473
557
 
474
558
  if (err) { return cb(err, null); }
475
559
 
476
- var snapshots = _.pluck(result || [], 'snapshots');
560
+ var results = [];
477
561
 
478
- snapshots = _.flatten(snapshots);
562
+ async.each(result, function(financial, callback1) {
479
563
 
480
- snapshots = _.filter(snapshots, function(snapshot) {
564
+ async.each(financial.snapshots, function(snapshot, callback2) {
481
565
 
482
- return _.some(snapshot.notifications.stakeholders, function(stakeholder) {
566
+ async.each(snapshot.notifications.stakeholders, function(stakeholder, callback3) {
483
567
 
484
- var hasEmail = stakeholder.sendTo && stakeholder.sendTo.email;
485
- var hasOrganization = stakeholder.sendTo && stakeholder.sendTo.organization;
486
- var hasUnsentSendOnLessThanNow = _.find(stakeholder.reminders, function(reminder) {
487
- return reminder.sendOn < now && !reminder.sentOn;
488
- });
568
+ var hasRecipients = stakeholder.sendTo && stakeholder.sendTo.length > 0;
569
+ var hasOrganization = stakeholder.organization;
570
+ var hasUnsentSendOnLessThanNow = _.find(stakeholder.reminders, function(reminder) {
571
+ return reminder.sendOn < now && !reminder.sentOn;
572
+ });
489
573
 
490
- return hasEmail && hasOrganization && hasUnsentSendOnLessThanNow;
574
+ if (!hasRecipients || !hasOrganization || !hasUnsentSendOnLessThanNow) return callback3();
491
575
 
492
- });
576
+ Account.findByEmail(stakeholder.sendTo.email, function(err, account) {
493
577
 
494
- });
578
+ var hasAccount = !_.isEmpty(account);
579
+ var hasAccessToken = !_.isEmpty(account) && account.auth && (account.auth.google && account.auth.google.accessToken) || (account.auth.microsoft && account.auth.microsoft.accessToken);
495
580
 
496
- return cb(null, _.pluck(snapshots, 'uuid'));
581
+ if (!hasAccount || hasAccessToken) return callback3();
497
582
 
498
- });
583
+ results.push({
584
+ 'snapshot': snapshot,
585
+ 'stakeholder': stakeholder,
586
+ 'organization': snapshot.organization
587
+ });
499
588
 
500
- };
589
+ return callback3();
501
590
 
502
- Financials.statics.getForCustomer = function getForCustomer(customerId, cb) {
591
+ });
503
592
 
504
- var self = this;
593
+ }, function (err) {
594
+ return callback2(err, results);
595
+ });
505
596
 
506
- var query = self.find({
507
- 'customer': customerId
508
- });
597
+ }, function (err) {
598
+ return callback1(err, results);
599
+ });
509
600
 
510
- query.exec(cb);
601
+ }, function (err) {
602
+ return cb(err, results);
603
+ });
604
+
605
+ });
511
606
 
512
607
  };
513
608
 
514
- Financials.statics.getToSend = function getToSend(uuid, cb) {
609
+ Financials.statics.getForCustomer = function getForCustomer(customerId, cb) {
515
610
 
516
611
  var self = this;
517
612
 
518
- var query = self.findOne({
519
- 'snapshots.uuid': uuid
613
+ var query = self.find({
614
+ 'customer': customerId
520
615
  });
521
616
 
522
- query.populate('organization', 'name logoUrl');
523
- query.populate('customer', 'name logoUrl customer.totemUrl');
524
-
525
617
  query.exec(cb);
526
618
 
527
619
  };
@@ -564,7 +656,6 @@ module.exports = function(mongoose, config) {
564
656
 
565
657
  };
566
658
 
567
-
568
659
  ///////////////////////////////////////////////////////////////////////////////////////
569
660
  // HOOKS
570
661
  // Pre-save: fired on every document before it is saved
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dhyasama/totem-models",
3
- "version": "8.29.0",
3
+ "version": "8.31.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