@dhyasama/totem-models 4.4.0 → 4.5.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/.npmignore ADDED
@@ -0,0 +1,14 @@
1
+ lib-cov
2
+ *.seed
3
+ *.log
4
+ *.csv
5
+ *.dat
6
+ *.out
7
+ *.pid
8
+ *.gz
9
+
10
+ npm-debug.log
11
+ node_modules
12
+
13
+ .DS_Store
14
+ .idea
@@ -33,6 +33,16 @@ module.exports = function(mongoose, config) {
33
33
  linkedin: { type: String, trim: true, unique: true, partialFilterExpression : { type :"string" } }
34
34
  },
35
35
 
36
+ deals: [{
37
+ customer: { type: Schema.ObjectId, ref: 'Organization' },
38
+ status: { type: String, trim: true },
39
+ history: [{
40
+ timestamp: { type: String },
41
+ account: { type: Schema.ObjectId, ref: 'Account' },
42
+ status: { type: String }
43
+ }]
44
+ }],
45
+
36
46
  contact: {
37
47
 
38
48
  phone: [{
@@ -139,6 +149,10 @@ module.exports = function(mongoose, config) {
139
149
  url: { type: String, unique: true, partialFilterExpression : { type :"string" }, trim: true }
140
150
  },
141
151
 
152
+ carta: {
153
+ corporationid: { type: String, unique: true, partialFilterExpression : { type :"string" }, trim: true }
154
+ },
155
+
142
156
  related: [{ type: Schema.ObjectId, ref: 'Organization' }],
143
157
 
144
158
  // customer specific (customer id is stored on note)
@@ -174,7 +188,7 @@ module.exports = function(mongoose, config) {
174
188
  }
175
189
  },
176
190
 
177
- eshares: {
191
+ carta: {
178
192
  firmid: { type: String, unique: true, partialFilterExpression : { type :"string" }, trim: true }
179
193
  },
180
194
 
@@ -184,6 +198,15 @@ module.exports = function(mongoose, config) {
184
198
 
185
199
  inbound: {
186
200
  active: { type: Boolean, default: false }
201
+ },
202
+
203
+ deals: {
204
+ active: { type: Boolean, default: false },
205
+ statuses: [{
206
+ name: { type: String, trim: true },
207
+ order: { type: Number },
208
+ active: { type: Boolean, default: true }
209
+ }]
187
210
  }
188
211
 
189
212
  },
@@ -358,6 +381,29 @@ module.exports = function(mongoose, config) {
358
381
 
359
382
  });
360
383
 
384
+ Organization.virtual('customer.deals.activeStatuses').get(function () {
385
+
386
+ var self = this;
387
+ var statuses = [];
388
+
389
+ // Don't return statuses for a different customer
390
+ if (self._id.toString() != config.CUSTOMER_ID) return statuses;
391
+
392
+ // Backwards schema compatability
393
+ else if (!self.customer.deals) return statuses;
394
+
395
+ // Deals aren't on for customer
396
+ else if (!self.customer.deals.active) return statuses;
397
+
398
+ // Get active status names and sort them
399
+ statuses = _.filter(self.customer.deals.statuses, function(s) { return s.active; });
400
+ statuses = _.sortBy(statuses, 'order');
401
+ statuses = _.pluck(statuses, 'name');
402
+
403
+ return statuses;
404
+
405
+ });
406
+
361
407
  Organization.virtual('employees.all').get(function () {
362
408
 
363
409
  var self = this;
@@ -526,6 +572,44 @@ module.exports = function(mongoose, config) {
526
572
  // Note that running a method on a document does not save it
527
573
  ///////////////////////////////////////////////////////////////////////////////////////
528
574
 
575
+ Organization.methods.addDeal = function(status, customer, account) {
576
+
577
+ var self = this;
578
+
579
+ // Validate inputs
580
+ if (!customer) throw new Error('Customer is required');
581
+ if (!status) throw new Error('Status is required');
582
+ if (!account) throw new Error('Account is required');
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');
587
+
588
+ // Check if this deal already exists
589
+ var dealMatch = _.find(self.deals, function(d) {
590
+ var cid = d.customer._id ? d.customer._id.toString() : d.customer.toString();
591
+ return cid == customer._id.toString() && d.status.toLowerCase() == status.toLowerCase();
592
+ });
593
+
594
+ if (dealMatch) throw new Error('This deal status already exists for this customer');
595
+
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');
599
+
600
+ // 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
+
611
+ };
612
+
529
613
  Organization.methods.addPerson = function(personToAdd) {
530
614
 
531
615
  var self = this;
@@ -957,6 +1041,32 @@ module.exports = function(mongoose, config) {
957
1041
 
958
1042
  };
959
1043
 
1044
+ Organization.statics.getDeals = function getDeals(cb) {
1045
+
1046
+ var self = this;
1047
+
1048
+ self
1049
+ .find({ 'deals.customer': config.CUSTOMER_ID, 'deleted': { $ne: true } })
1050
+ .select('name website websiteAliases deals')
1051
+ .exec(function(err, result) {
1052
+
1053
+ if (err) return cb(err, null);
1054
+
1055
+ result = _.map(result, function(item) {
1056
+ return {
1057
+ name: item.name,
1058
+ website: item.website,
1059
+ websiteAliases: item.websiteAliases,
1060
+ deals: item.deals
1061
+ }
1062
+ });
1063
+
1064
+ return cb(null, result);
1065
+
1066
+ });
1067
+
1068
+ };
1069
+
960
1070
  Organization.statics.getFlags = function getFlags(orgid, cb) {
961
1071
 
962
1072
  var self = this;
@@ -1513,6 +1623,7 @@ module.exports = function(mongoose, config) {
1513
1623
  doc.chairs = _.reject(doc.chairs, function(item) { return !item.customer || item.customer.toString() != CUSTOMER_ID; });
1514
1624
  doc.operating.acquired.private = _.find(doc.operating.acquired.private, function(item) { return !item.customer || item.customer.toString() == CUSTOMER_ID; });
1515
1625
  doc.operating.closed.private = _.find(doc.operating.closed.private, function(item) { return !item.customer || item.customer.toString() == CUSTOMER_ID; });
1626
+ doc.deals = _.reject(doc.deals, function(item) { return !item.customer || item.customer.toString() != CUSTOMER_ID; });
1516
1627
 
1517
1628
  // people
1518
1629
  _.each(doc.people, function(person) {
package/lib/Person.js CHANGED
@@ -37,7 +37,8 @@ module.exports = function(mongoose, config) {
37
37
  phone: [{
38
38
  type: { type: String, enum: ['home', 'work', 'assistant', 'mobile', 'other'] },
39
39
  number: { type: String, default: '' },
40
- primary: { type: Boolean, default: false }
40
+ primary: { type: Boolean, default: false },
41
+ //customer: { type: Schema.ObjectId, ref: 'Organization' }
41
42
  }],
42
43
 
43
44
  email: [{