@dhyasama/totem-models 4.4.1 → 4.5.1
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 +14 -0
- package/lib/Organization.js +116 -0
- package/lib/Person.js +2 -1
- package/package-lock.json +1900 -0
- package/package.json +2 -1
- package/test/Organization.js +255 -2
package/.npmignore
ADDED
package/lib/Organization.js
CHANGED
|
@@ -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: [{
|
|
@@ -188,6 +198,15 @@ module.exports = function(mongoose, config) {
|
|
|
188
198
|
|
|
189
199
|
inbound: {
|
|
190
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
|
+
}]
|
|
191
210
|
}
|
|
192
211
|
|
|
193
212
|
},
|
|
@@ -362,6 +381,29 @@ module.exports = function(mongoose, config) {
|
|
|
362
381
|
|
|
363
382
|
});
|
|
364
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
|
+
|
|
365
407
|
Organization.virtual('employees.all').get(function () {
|
|
366
408
|
|
|
367
409
|
var self = this;
|
|
@@ -530,6 +572,53 @@ module.exports = function(mongoose, config) {
|
|
|
530
572
|
// Note that running a method on a document does not save it
|
|
531
573
|
///////////////////////////////////////////////////////////////////////////////////////
|
|
532
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
|
+
// 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
|
+
};
|
|
594
|
+
|
|
595
|
+
// Check if this deal already exists
|
|
596
|
+
var dealMatch = _.find(self.deals, function(d) {
|
|
597
|
+
var cid = d.customer._id ? d.customer._id.toString() : d.customer.toString();
|
|
598
|
+
return cid == customer._id.toString();
|
|
599
|
+
});
|
|
600
|
+
|
|
601
|
+
// Update the deal
|
|
602
|
+
if (dealMatch) {
|
|
603
|
+
|
|
604
|
+
dealMatch.status = status;
|
|
605
|
+
dealMatch.history.push(historyEntry);
|
|
606
|
+
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
// Add the deal
|
|
610
|
+
else {
|
|
611
|
+
|
|
612
|
+
self.deals.push({
|
|
613
|
+
customer: customer,
|
|
614
|
+
status: status,
|
|
615
|
+
history: [historyEntry]
|
|
616
|
+
});
|
|
617
|
+
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
};
|
|
621
|
+
|
|
533
622
|
Organization.methods.addPerson = function(personToAdd) {
|
|
534
623
|
|
|
535
624
|
var self = this;
|
|
@@ -961,6 +1050,32 @@ module.exports = function(mongoose, config) {
|
|
|
961
1050
|
|
|
962
1051
|
};
|
|
963
1052
|
|
|
1053
|
+
Organization.statics.getDeals = function getDeals(cb) {
|
|
1054
|
+
|
|
1055
|
+
var self = this;
|
|
1056
|
+
|
|
1057
|
+
self
|
|
1058
|
+
.find({ 'deals.customer': config.CUSTOMER_ID, 'deleted': { $ne: true } })
|
|
1059
|
+
.select('name website websiteAliases deals')
|
|
1060
|
+
.exec(function(err, result) {
|
|
1061
|
+
|
|
1062
|
+
if (err) return cb(err, null);
|
|
1063
|
+
|
|
1064
|
+
result = _.map(result, function(item) {
|
|
1065
|
+
return {
|
|
1066
|
+
name: item.name,
|
|
1067
|
+
website: item.website,
|
|
1068
|
+
websiteAliases: item.websiteAliases,
|
|
1069
|
+
deals: item.deals
|
|
1070
|
+
}
|
|
1071
|
+
});
|
|
1072
|
+
|
|
1073
|
+
return cb(null, result);
|
|
1074
|
+
|
|
1075
|
+
});
|
|
1076
|
+
|
|
1077
|
+
};
|
|
1078
|
+
|
|
964
1079
|
Organization.statics.getFlags = function getFlags(orgid, cb) {
|
|
965
1080
|
|
|
966
1081
|
var self = this;
|
|
@@ -1517,6 +1632,7 @@ module.exports = function(mongoose, config) {
|
|
|
1517
1632
|
doc.chairs = _.reject(doc.chairs, function(item) { return !item.customer || item.customer.toString() != CUSTOMER_ID; });
|
|
1518
1633
|
doc.operating.acquired.private = _.find(doc.operating.acquired.private, function(item) { return !item.customer || item.customer.toString() == CUSTOMER_ID; });
|
|
1519
1634
|
doc.operating.closed.private = _.find(doc.operating.closed.private, function(item) { return !item.customer || item.customer.toString() == CUSTOMER_ID; });
|
|
1635
|
+
doc.deals = _.reject(doc.deals, function(item) { return !item.customer || item.customer.toString() != CUSTOMER_ID; });
|
|
1520
1636
|
|
|
1521
1637
|
// people
|
|
1522
1638
|
_.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: [{
|