@dhyasama/totem-models 4.5.0 → 4.5.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.
@@ -389,7 +389,7 @@ module.exports = function(mongoose, config) {
389
389
  // Don't return statuses for a different customer
390
390
  if (self._id.toString() != config.CUSTOMER_ID) return statuses;
391
391
 
392
- // Backwards schema compatability
392
+ // Backwards schema compatibility
393
393
  else if (!self.customer.deals) return statuses;
394
394
 
395
395
  // Deals aren't on for customer
@@ -581,32 +581,41 @@ module.exports = function(mongoose, config) {
581
581
  if (!status) throw new Error('Status is required');
582
582
  if (!account) throw new Error('Account is required');
583
583
 
584
- // Make sure customer and deals are active
585
- if (!customer.customer.active) throw new Error('Customer is not active');
586
- if (!customer.customer.deals.active) throw new Error('Deals are not active for this customer');
584
+ // Check if status exists and is active
585
+ var statusMatch = _.find(customer.customer.deals.statuses, function(s) { return s.name.toLowerCase() == status.toLowerCase() && s.active; });
586
+ if (!statusMatch) throw new Error('This deal status is not valid for this customer');
587
+
588
+ // Prepare an entry to track history of deal
589
+ var historyEntry = {
590
+ timestamp: new Date(),
591
+ account: account,
592
+ status: status
593
+ };
587
594
 
588
595
  // Check if this deal already exists
589
596
  var dealMatch = _.find(self.deals, function(d) {
590
597
  var cid = d.customer._id ? d.customer._id.toString() : d.customer.toString();
591
- return cid == customer._id.toString() && d.status.toLowerCase() == status.toLowerCase();
598
+ return cid == customer._id.toString();
592
599
  });
593
600
 
594
- if (dealMatch) throw new Error('This deal status already exists for this customer');
601
+ // Update the deal
602
+ if (dealMatch) {
595
603
 
596
- // Check if status exists and is active
597
- var statusMatch = _.find(customer.customer.deals.statuses, function(s) { return s.name.toLowerCase() == status.toLowerCase() && s.active; });
598
- if (!statusMatch) throw new Error('This deal status is not valid for this customer');
604
+ dealMatch.status = status;
605
+ dealMatch.history.push(historyEntry);
606
+
607
+ }
599
608
 
600
609
  // Add the deal
601
- self.deals.push({
602
- customer: customer,
603
- status: status,
604
- history: [{
605
- timestamp: new Date(),
606
- account: account,
607
- status: status
608
- }]
609
- });
610
+ else {
611
+
612
+ self.deals.push({
613
+ customer: customer,
614
+ status: status,
615
+ history: [historyEntry]
616
+ });
617
+
618
+ }
610
619
 
611
620
  };
612
621
 
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@dhyasama/totem-models",
3
- "version": "4.5.0",
3
+ "version": "4.5.2",
4
4
  "author": "Jason Reynolds",
5
5
  "license": "UNLICENSED",
6
6
  "description": "Models for Totem platform",
7
7
  "main": "index.js",
8
8
  "scripts": {
9
- "test": "NODE_ENV=test node ./changeBSON && mocha -R spec -t 15000"
9
+ "test": "NODE_ENV=test node ./changeBSON && mocha -R spec -t 15000 --exit"
10
10
  },
11
11
  "publishConfig": {
12
12
  "registry": "https://registry.npmjs.org/"
@@ -1175,13 +1175,15 @@ describe('Organization', function() {
1175
1175
  var threwError = false;
1176
1176
 
1177
1177
  try {
1178
- org.addDeal('New', customer1, account);
1178
+ org.addDeal('Pass', customer1, account);
1179
1179
  }
1180
1180
  catch (err) {
1181
1181
  threwError = true;
1182
1182
  }
1183
1183
 
1184
- threwError.should.equal(true);
1184
+ threwError.should.equal(false);
1185
+ org.deals.length.should.equal(1);
1186
+ org.deals[0].status.should.equal('Pass');
1185
1187
 
1186
1188
  return done();
1187
1189