@dhyasama/totem-models 4.4.1 → 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 +14 -0
- package/lib/Organization.js +107 -0
- package/lib/Person.js +2 -1
- package/package-lock.json +1900 -0
- package/package.json +2 -1
- package/test/Organization.js +252 -1
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,44 @@ 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
|
+
// 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
|
+
|
|
533
613
|
Organization.methods.addPerson = function(personToAdd) {
|
|
534
614
|
|
|
535
615
|
var self = this;
|
|
@@ -961,6 +1041,32 @@ module.exports = function(mongoose, config) {
|
|
|
961
1041
|
|
|
962
1042
|
};
|
|
963
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
|
+
|
|
964
1070
|
Organization.statics.getFlags = function getFlags(orgid, cb) {
|
|
965
1071
|
|
|
966
1072
|
var self = this;
|
|
@@ -1517,6 +1623,7 @@ module.exports = function(mongoose, config) {
|
|
|
1517
1623
|
doc.chairs = _.reject(doc.chairs, function(item) { return !item.customer || item.customer.toString() != CUSTOMER_ID; });
|
|
1518
1624
|
doc.operating.acquired.private = _.find(doc.operating.acquired.private, function(item) { return !item.customer || item.customer.toString() == CUSTOMER_ID; });
|
|
1519
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; });
|
|
1520
1627
|
|
|
1521
1628
|
// people
|
|
1522
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: [{
|