@dhyasama/totem-models 8.28.2 → 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
 
@@ -30,7 +32,6 @@ module.exports = function(mongoose, config) {
30
32
  managedServices: { type: Boolean, default: false, required: true },
31
33
 
32
34
  status: { type: String },
33
-
34
35
  action: { type: String },
35
36
 
36
37
  year: { type: Number, default: 0 },
@@ -147,12 +148,11 @@ module.exports = function(mongoose, config) {
147
148
 
148
149
  stakeholders: [{
149
150
 
150
- organization: { type: Schema.ObjectId, ref: 'Organization' },
151
-
152
- sendTo: [{
151
+ sendTo: {
152
+ organization: { type: Schema.ObjectId, ref: 'Organization' },
153
153
  email: { type: String, trim: true },
154
154
  name: { type: String, trim: true }
155
- }],
155
+ },
156
156
 
157
157
  initial: {
158
158
  sendOn: { type: Date, required: false },
@@ -278,7 +278,7 @@ module.exports = function(mongoose, config) {
278
278
 
279
279
  };
280
280
 
281
- Financials.statics.getCompanyInitialUUIDsToSend = function getCompanyInitialUUIDsToSend(cb) {
281
+ Financials.statics.getCompanyInitialToSend = function getCompanyInitialToSend(cb) {
282
282
 
283
283
  var self = this;
284
284
  var now = new Date();
@@ -297,26 +297,38 @@ module.exports = function(mongoose, config) {
297
297
 
298
298
  if (err) { return cb(err, null); }
299
299
 
300
- var snapshots = _.pluck(result || [], 'snapshots');
300
+ var results = [];
301
301
 
302
+ var snapshots = _.pluck(result || [], 'snapshots');
302
303
  snapshots = _.flatten(snapshots);
303
304
 
304
- snapshots = _.filter(snapshots, function(snapshot) {
305
+ _.each(snapshots, function(snapshot) {
305
306
 
306
307
  var hasRecipients = snapshot.notifications.company.sendTo.length > 0;
307
308
  var hasUnsentSendOnLessThanNow = snapshot.notifications.company.initial.sendOn < now && !snapshot.notifications.company.initial.sentOn;
308
309
 
309
- 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
+ }
310
322
 
311
323
  });
312
324
 
313
- return cb(null, _.pluck(snapshots, 'uuid'));
325
+ return cb(null, results);
314
326
 
315
327
  });
316
328
 
317
329
  };
318
330
 
319
- Financials.statics.getCompanyReminderUUIDsToSend = function getCompanyReminderUUIDsToSend(cb) {
331
+ Financials.statics.getCompanyReminderToSend = function getCompanyReminderToSend(cb) {
320
332
 
321
333
  var self = this;
322
334
  var now = new Date();
@@ -327,13 +339,13 @@ module.exports = function(mongoose, config) {
327
339
  'notifications.company.sendTo': { $gt: [] },
328
340
  'notifications.company.reminders.sendOn': { $lte: now },
329
341
  'notifications.company.reminders.sentOn': null,
330
- "$or": [
342
+ '$or': [
331
343
  {
332
- "submittedOn": null,
344
+ 'submittedOn': null,
333
345
  },
334
346
  {
335
- "submittedOn": { $ne: null },
336
- "rejection": { $ne: null }
347
+ 'submittedOn': { $ne: null },
348
+ 'rejection': { $ne: null }
337
349
  }
338
350
  ]
339
351
  }
@@ -344,11 +356,12 @@ module.exports = function(mongoose, config) {
344
356
 
345
357
  if (err) { return cb(err, null); }
346
358
 
347
- var snapshots = _.pluck(result || [], 'snapshots');
359
+ var results = [];
348
360
 
361
+ var snapshots = _.pluck(result || [], 'snapshots');
349
362
  snapshots = _.flatten(snapshots);
350
363
 
351
- snapshots = _.filter(snapshots, function(snapshot) {
364
+ _.each(snapshots, function(snapshot) {
352
365
 
353
366
  var hasRecipients = snapshot.notifications.company.sendTo.length > 0;
354
367
  var hasUnsentSendOnLessThanNow = _.find(snapshot.notifications.company.reminders, function(reminder) {
@@ -357,17 +370,28 @@ module.exports = function(mongoose, config) {
357
370
  var hasBeenSubmitted = snapshot.submittedOn;
358
371
  var hasBeenRejected = snapshot.rejection;
359
372
 
360
- 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
+ }
361
385
 
362
386
  });
363
387
 
364
- return cb(null, _.pluck(snapshots, 'uuid'));
388
+ return cb(null, results);
365
389
 
366
390
  });
367
391
 
368
392
  };
369
393
 
370
- Financials.statics.getCompanyRejectionUUIDsToSend = function getCompanyRejectionUUIDsToSend(cb) {
394
+ Financials.statics.getCompanyRejectionToSend = function getCompanyRejectionToSend(cb) {
371
395
 
372
396
  var self = this;
373
397
  var now = new Date();
@@ -378,8 +402,8 @@ module.exports = function(mongoose, config) {
378
402
  'notifications.company.sendTo': { $gt: [] },
379
403
  'notifications.company.rejections.sendOn': { $lte: now },
380
404
  'notifications.company.rejections.sentOn': null,
381
- "submittedOn": { $ne: null },
382
- "rejection": { $ne: null }
405
+ 'submittedOn': { $ne: null },
406
+ 'rejection': { $ne: null }
383
407
  }
384
408
  }
385
409
  });
@@ -388,11 +412,12 @@ module.exports = function(mongoose, config) {
388
412
 
389
413
  if (err) { return cb(err, null); }
390
414
 
391
- var snapshots = _.pluck(result || [], 'snapshots');
415
+ var results = [];
392
416
 
417
+ var snapshots = _.pluck(result || [], 'snapshots');
393
418
  snapshots = _.flatten(snapshots);
394
419
 
395
- snapshots = _.filter(snapshots, function(snapshot) {
420
+ _.each(snapshots, function(snapshot) {
396
421
 
397
422
  var hasRecipients = snapshot.notifications.company.sendTo.length > 0;
398
423
  var hasUnsentSendOnLessThanNow = _.find(snapshot.notifications.company.rejections, function(rejection) {
@@ -401,17 +426,28 @@ module.exports = function(mongoose, config) {
401
426
  var hasBeenSubmitted = snapshot.submittedOn;
402
427
  var hasBeenRejected = snapshot.rejection;
403
428
 
404
- 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
+ }
405
441
 
406
442
  });
407
443
 
408
- return cb(null, _.pluck(snapshots, 'uuid'));
444
+ return cb(null, results);
409
445
 
410
446
  });
411
447
 
412
448
  };
413
449
 
414
- Financials.statics.getStakeholderInitialUUIDsToSend = function getStakeholderInitialUUIDsToSend(cb) {
450
+ Financials.statics.getStakeholderInitialToSend = function getStakeholderInitialToSend(cb) {
415
451
 
416
452
  var self = this;
417
453
  var now = new Date();
@@ -419,8 +455,8 @@ module.exports = function(mongoose, config) {
419
455
  var query = self.find({
420
456
  'snapshots': {
421
457
  $elemMatch: {
422
- 'notifications.stakeholders.sendTo': { $gt: [] },
423
- 'notifications.stakeholders.organization': { $ne: null },
458
+ 'notifications.stakeholders.sendTo.email': { $ne: null },
459
+ 'notifications.stakeholders.sendTo.organization': { $ne: null },
424
460
  'notifications.stakeholders.initial.sendOn': { $lte: now },
425
461
  'notifications.stakeholders.initial.sentOn': null
426
462
  }
@@ -431,33 +467,50 @@ module.exports = function(mongoose, config) {
431
467
 
432
468
  if (err) { return cb(err, null); }
433
469
 
434
- var snapshots = _.pluck(result || [], 'snapshots');
470
+ var results = [];
435
471
 
472
+ var snapshots = _.pluck(result || [], 'snapshots');
436
473
  snapshots = _.flatten(snapshots);
437
474
 
438
- snapshots = _.filter(snapshots, function(snapshot) {
475
+ async.each(snapshots, function(snapshot, callback1) {
439
476
 
440
- snapshot = snapshot.toObject();
477
+ async.each(snapshot.notifications.stakeholders, function(stakeholder, callback2) {
441
478
 
442
- return _.some(snapshot.notifications.stakeholders, function(stakeholder) {
443
-
444
- var hasRecipients = stakeholder.sendTo && stakeholder.sendTo.length > 0;
445
- var hasOrganization = stakeholder.organization;
479
+ var hasEmail = stakeholder.sendTo && stakeholder.sendTo.email;
480
+ var hasOrganization = stakeholder.sendTo && stakeholder.sendTo.organization;
446
481
  var hasUnsentSendOnLessThanNow = stakeholder.initial && stakeholder.initial.sendOn < now && !stakeholder.initial.sentOn;
447
482
 
448
- return hasRecipients && hasOrganization && hasUnsentSendOnLessThanNow;
483
+ if (!hasEmail || !hasOrganization || !hasUnsentSendOnLessThanNow) return callback2();
449
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);
450
503
  });
451
504
 
505
+ }, function (err) {
506
+ return cb(err, results);
452
507
  });
453
508
 
454
- return cb(null, _.pluck(snapshots, 'uuid'));
455
-
456
509
  });
457
510
 
458
511
  };
459
512
 
460
- Financials.statics.getStakeholderReminderUUIDsToSend = function getStakeholderReminderUUIDsToSend(cb) {
513
+ Financials.statics.getStakeholderReminderToSend = function getStakeholderReminderToSend(cb) {
461
514
 
462
515
  var self = this;
463
516
  var now = new Date();
@@ -477,13 +530,14 @@ module.exports = function(mongoose, config) {
477
530
 
478
531
  if (err) { return cb(err, null); }
479
532
 
480
- var snapshots = _.pluck(result || [], 'snapshots');
533
+ var results = [];
481
534
 
535
+ var snapshots = _.pluck(result || [], 'snapshots');
482
536
  snapshots = _.flatten(snapshots);
483
537
 
484
- snapshots = _.filter(snapshots, function(snapshot) {
538
+ async.each(snapshots, function(snapshot, callback1) {
485
539
 
486
- return _.some(snapshot.notifications.stakeholders, function(stakeholder) {
540
+ async.each(snapshot.notifications.stakeholders, function(stakeholder, callback2) {
487
541
 
488
542
  var hasRecipients = stakeholder.sendTo && stakeholder.sendTo.length > 0;
489
543
  var hasOrganization = stakeholder.organization;
@@ -491,14 +545,32 @@ module.exports = function(mongoose, config) {
491
545
  return reminder.sendOn < now && !reminder.sentOn;
492
546
  });
493
547
 
494
- return hasRecipients && 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);
495
554
 
555
+ if (!hasAccount || hasAccessToken) return callback2();
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);
496
568
  });
497
569
 
570
+ }, function (err) {
571
+ return cb(err, results);
498
572
  });
499
573
 
500
- return cb(null, _.pluck(snapshots, 'uuid'));
501
-
502
574
  });
503
575
 
504
576
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dhyasama/totem-models",
3
- "version": "8.28.2",
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