@dhyasama/totem-models 8.30.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 +117 -102
- package/package.json +1 -1
package/lib/Financials.js
CHANGED
|
@@ -293,32 +293,37 @@ module.exports = function(mongoose, config) {
|
|
|
293
293
|
}
|
|
294
294
|
});
|
|
295
295
|
|
|
296
|
+
query.populate('organization', 'name logoUrl');
|
|
297
|
+
query.populate('customer', 'name logoUrl customer.totemUrl');
|
|
298
|
+
|
|
296
299
|
query.exec(function(err, result) {
|
|
297
300
|
|
|
298
301
|
if (err) { return cb(err, null); }
|
|
299
302
|
|
|
300
303
|
var results = [];
|
|
301
304
|
|
|
302
|
-
|
|
303
|
-
snapshots = _.flatten(snapshots);
|
|
305
|
+
_.each(result, function(financial) {
|
|
304
306
|
|
|
305
|
-
|
|
307
|
+
_.each(financial.snapshots, function(snapshot) {
|
|
306
308
|
|
|
307
|
-
|
|
308
|
-
|
|
309
|
+
var hasRecipients = snapshot.notifications.company.sendTo.length > 0;
|
|
310
|
+
var hasUnsentSendOnLessThanNow = snapshot.notifications.company.initial.sendOn < now && !snapshot.notifications.company.initial.sentOn;
|
|
309
311
|
|
|
310
|
-
|
|
312
|
+
if(hasRecipients && hasUnsentSendOnLessThanNow) {
|
|
311
313
|
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
314
|
+
var recipients = snapshot.notifications.company.sendTo.map(function(sendTo) {
|
|
315
|
+
return sendTo.email;
|
|
316
|
+
}).join(',');
|
|
315
317
|
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
318
|
+
results.push({
|
|
319
|
+
'snapshot': snapshot,
|
|
320
|
+
'customer': snapsot.customer,
|
|
321
|
+
'organization': snapshot.organization
|
|
322
|
+
});
|
|
320
323
|
|
|
321
|
-
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
});
|
|
322
327
|
|
|
323
328
|
});
|
|
324
329
|
|
|
@@ -352,36 +357,41 @@ module.exports = function(mongoose, config) {
|
|
|
352
357
|
}
|
|
353
358
|
});
|
|
354
359
|
|
|
360
|
+
query.populate('organization', 'name logoUrl');
|
|
361
|
+
query.populate('customer', 'name logoUrl customer.totemUrl');
|
|
362
|
+
|
|
355
363
|
query.exec(function(err, result) {
|
|
356
364
|
|
|
357
365
|
if (err) { return cb(err, null); }
|
|
358
366
|
|
|
359
367
|
var results = [];
|
|
360
368
|
|
|
361
|
-
|
|
362
|
-
snapshots = _.flatten(snapshots);
|
|
369
|
+
_.each(result, function(financial) {
|
|
363
370
|
|
|
364
|
-
|
|
371
|
+
_.each(financial.snapshots, function(snapshot) {
|
|
365
372
|
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
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;
|
|
372
379
|
|
|
373
|
-
|
|
380
|
+
if(hasRecipients && hasUnsentSendOnLessThanNow && ((!hasBeenSubmitted) || (hasBeenSubmitted && hasBeenRejected))) {
|
|
374
381
|
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
382
|
+
var recipients = snapshot.notifications.company.sendTo.map(function(sendTo) {
|
|
383
|
+
return sendTo.email;
|
|
384
|
+
}).join(',');
|
|
378
385
|
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
386
|
+
results.push({
|
|
387
|
+
'snapshot': snapshot,
|
|
388
|
+
'customer': snapsot.customer,
|
|
389
|
+
'organization': snapshot.organization
|
|
390
|
+
});
|
|
383
391
|
|
|
384
|
-
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
});
|
|
385
395
|
|
|
386
396
|
});
|
|
387
397
|
|
|
@@ -408,36 +418,41 @@ module.exports = function(mongoose, config) {
|
|
|
408
418
|
}
|
|
409
419
|
});
|
|
410
420
|
|
|
421
|
+
query.populate('organization', 'name logoUrl');
|
|
422
|
+
query.populate('customer', 'name logoUrl customer.totemUrl');
|
|
423
|
+
|
|
411
424
|
query.exec(function(err, result) {
|
|
412
425
|
|
|
413
426
|
if (err) { return cb(err, null); }
|
|
414
427
|
|
|
415
428
|
var results = [];
|
|
416
429
|
|
|
417
|
-
|
|
418
|
-
snapshots = _.flatten(snapshots);
|
|
430
|
+
_.each(result, function(financial) {
|
|
419
431
|
|
|
420
|
-
|
|
432
|
+
_.each(financial.snapshots, function(snapshot) {
|
|
421
433
|
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
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;
|
|
428
440
|
|
|
429
|
-
|
|
441
|
+
if(hasRecipients && hasUnsentSendOnLessThanNow && hasBeenSubmitted && hasBeenRejected) {
|
|
430
442
|
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
443
|
+
var recipients = snapshot.notifications.company.sendTo.map(function(sendTo) {
|
|
444
|
+
return sendTo.email;
|
|
445
|
+
}).join(',');
|
|
434
446
|
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
447
|
+
results.push({
|
|
448
|
+
'snapshot': snapshot,
|
|
449
|
+
'customer': snapsot.customer,
|
|
450
|
+
'organization': snapshot.organization
|
|
451
|
+
});
|
|
439
452
|
|
|
440
|
-
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
});
|
|
441
456
|
|
|
442
457
|
});
|
|
443
458
|
|
|
@@ -463,39 +478,47 @@ module.exports = function(mongoose, config) {
|
|
|
463
478
|
}
|
|
464
479
|
});
|
|
465
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
|
+
|
|
466
485
|
query.exec(function(err, result) {
|
|
467
486
|
|
|
468
487
|
if (err) { return cb(err, null); }
|
|
469
488
|
|
|
470
489
|
var results = [];
|
|
471
490
|
|
|
472
|
-
|
|
473
|
-
snapshots = _.flatten(snapshots);
|
|
491
|
+
async.each(result, function(financial, callback1) {
|
|
474
492
|
|
|
475
|
-
|
|
493
|
+
async.each(financial.snapshots, function(snapshot, callback2) {
|
|
476
494
|
|
|
477
|
-
|
|
495
|
+
async.each(snapshot.notifications.stakeholders, function(stakeholder, callback3) {
|
|
478
496
|
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
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;
|
|
482
500
|
|
|
483
|
-
|
|
501
|
+
if (!hasEmail || !hasOrganization || !hasUnsentSendOnLessThanNow) return callback3();
|
|
484
502
|
|
|
485
|
-
|
|
503
|
+
Account.findByEmail(stakeholder.sendTo.email, function(err, account) {
|
|
486
504
|
|
|
487
|
-
|
|
488
|
-
|
|
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);
|
|
489
507
|
|
|
490
|
-
|
|
508
|
+
if (!hasAccount || hasAccessToken) return callback3();
|
|
491
509
|
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
510
|
+
results.push({
|
|
511
|
+
'snapshot': snapshot,
|
|
512
|
+
'stakeholder': stakeholder,
|
|
513
|
+
'organization': snapshot.organization
|
|
514
|
+
});
|
|
515
|
+
|
|
516
|
+
return callback3();
|
|
496
517
|
|
|
497
|
-
|
|
518
|
+
});
|
|
498
519
|
|
|
520
|
+
}, function (err) {
|
|
521
|
+
return callback2(err, results);
|
|
499
522
|
});
|
|
500
523
|
|
|
501
524
|
}, function (err) {
|
|
@@ -526,41 +549,49 @@ module.exports = function(mongoose, config) {
|
|
|
526
549
|
}
|
|
527
550
|
});
|
|
528
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
|
+
|
|
529
556
|
query.exec(function(err, result) {
|
|
530
557
|
|
|
531
558
|
if (err) { return cb(err, null); }
|
|
532
559
|
|
|
533
560
|
var results = [];
|
|
534
561
|
|
|
535
|
-
|
|
536
|
-
snapshots = _.flatten(snapshots);
|
|
562
|
+
async.each(result, function(financial, callback1) {
|
|
537
563
|
|
|
538
|
-
|
|
564
|
+
async.each(financial.snapshots, function(snapshot, callback2) {
|
|
539
565
|
|
|
540
|
-
|
|
566
|
+
async.each(snapshot.notifications.stakeholders, function(stakeholder, callback3) {
|
|
541
567
|
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
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
|
+
});
|
|
547
573
|
|
|
548
|
-
|
|
574
|
+
if (!hasRecipients || !hasOrganization || !hasUnsentSendOnLessThanNow) return callback3();
|
|
549
575
|
|
|
550
|
-
|
|
576
|
+
Account.findByEmail(stakeholder.sendTo.email, function(err, account) {
|
|
551
577
|
|
|
552
|
-
|
|
553
|
-
|
|
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);
|
|
554
580
|
|
|
555
|
-
|
|
581
|
+
if (!hasAccount || hasAccessToken) return callback3();
|
|
556
582
|
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
583
|
+
results.push({
|
|
584
|
+
'snapshot': snapshot,
|
|
585
|
+
'stakeholder': stakeholder,
|
|
586
|
+
'organization': snapshot.organization
|
|
587
|
+
});
|
|
588
|
+
|
|
589
|
+
return callback3();
|
|
561
590
|
|
|
562
|
-
|
|
591
|
+
});
|
|
563
592
|
|
|
593
|
+
}, function (err) {
|
|
594
|
+
return callback2(err, results);
|
|
564
595
|
});
|
|
565
596
|
|
|
566
597
|
}, function (err) {
|
|
@@ -587,21 +618,6 @@ module.exports = function(mongoose, config) {
|
|
|
587
618
|
|
|
588
619
|
};
|
|
589
620
|
|
|
590
|
-
Financials.statics.getToSend = function getToSend(uuid, cb) {
|
|
591
|
-
|
|
592
|
-
var self = this;
|
|
593
|
-
|
|
594
|
-
var query = self.findOne({
|
|
595
|
-
'snapshots.uuid': uuid
|
|
596
|
-
});
|
|
597
|
-
|
|
598
|
-
query.populate('organization', 'name logoUrl');
|
|
599
|
-
query.populate('customer', 'name logoUrl customer.totemUrl');
|
|
600
|
-
|
|
601
|
-
query.exec(cb);
|
|
602
|
-
|
|
603
|
-
};
|
|
604
|
-
|
|
605
621
|
Financials.statics.removeCustomerFinancials = function removeCustomerFinancials(customerId, cb) {
|
|
606
622
|
|
|
607
623
|
// this is only used to wipe out financials created by the google sheet import process
|
|
@@ -640,7 +656,6 @@ module.exports = function(mongoose, config) {
|
|
|
640
656
|
|
|
641
657
|
};
|
|
642
658
|
|
|
643
|
-
|
|
644
659
|
///////////////////////////////////////////////////////////////////////////////////////
|
|
645
660
|
// HOOKS
|
|
646
661
|
// Pre-save: fired on every document before it is saved
|