@dhyasama/totem-models 5.0.1 → 5.0.2
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/Organization.js +5 -27
- package/package.json +1 -1
package/lib/Organization.js
CHANGED
|
@@ -570,45 +570,23 @@ module.exports = function(mongoose, config) {
|
|
|
570
570
|
// Note that running a method on a document does not save it
|
|
571
571
|
///////////////////////////////////////////////////////////////////////////////////////
|
|
572
572
|
|
|
573
|
-
Organization.methods.addDeal = function(
|
|
573
|
+
Organization.methods.addDeal = function(deal) {
|
|
574
574
|
|
|
575
575
|
var self = this;
|
|
576
576
|
|
|
577
577
|
// Validate inputs
|
|
578
|
-
if (!
|
|
579
|
-
if (!status) throw new Error('Status is required');
|
|
580
|
-
if (!account) throw new Error('Account is required');
|
|
581
|
-
|
|
582
|
-
// Check if status exists and is active
|
|
583
|
-
var statusMatch = _.find(customer.customer.deals.statuses, function(s) { return s.name.toLowerCase() == status.toLowerCase() && s.active; });
|
|
584
|
-
if (!statusMatch) throw new Error('This deal status is not valid for this customer');
|
|
585
|
-
|
|
586
|
-
// Prepare an entry to track history of deal
|
|
587
|
-
var historyEntry = {
|
|
588
|
-
timestamp: new Date(),
|
|
589
|
-
account: account,
|
|
590
|
-
status: status
|
|
591
|
-
};
|
|
578
|
+
if (!deal) throw new Error('deal is required');
|
|
592
579
|
|
|
593
580
|
// Check if this deal already exists
|
|
594
581
|
var dealMatch = _.find(self.deals, function(d) {
|
|
595
|
-
return
|
|
582
|
+
return d._id.toString() === deal._id.toString();
|
|
596
583
|
});
|
|
597
584
|
|
|
598
585
|
// Update the deal
|
|
599
|
-
if (dealMatch) {
|
|
600
|
-
dealMatch.status = status;
|
|
601
|
-
dealMatch.history.push(historyEntry);
|
|
602
|
-
}
|
|
586
|
+
if (dealMatch) { dealMatch = deal; }
|
|
603
587
|
|
|
604
588
|
// Add the deal
|
|
605
|
-
else {
|
|
606
|
-
self.deals.push({
|
|
607
|
-
customer: customer,
|
|
608
|
-
status: status,
|
|
609
|
-
history: [historyEntry]
|
|
610
|
-
});
|
|
611
|
-
}
|
|
589
|
+
else { self.deals.push(deal); }
|
|
612
590
|
|
|
613
591
|
};
|
|
614
592
|
|