@dhyasama/totem-models 10.9.0 → 10.11.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.
@@ -33,6 +33,12 @@ module.exports = function(mongoose, config) {
33
33
  linkedin: { type: String, trim: true, unique: true, partialFilterExpression : { type :"string" } }
34
34
  },
35
35
 
36
+ social_handle: {
37
+ twitter: { type: String, trim: true },
38
+ facebook: { type: String, trim: true },
39
+ linkedin: { type: String, trim: true }
40
+ },
41
+
36
42
  contact: {
37
43
 
38
44
  phone: [{
@@ -683,8 +689,10 @@ module.exports = function(mongoose, config) {
683
689
 
684
690
  var self = this;
685
691
 
692
+ if (!self.customer) return [];
693
+
686
694
  // Backwards schema compatibility
687
- if (!self.customer.deals) return [];
695
+ else if (!self.customer.deals) return [];
688
696
 
689
697
  // Deals aren't on for customer
690
698
  else if (!self.customer.deals.active) return [];
@@ -809,8 +817,8 @@ module.exports = function(mongoose, config) {
809
817
  Organization.virtual('operating.closed.on').get(function () {
810
818
 
811
819
  const self = this;
812
- const publicData = self.operating.closed.public || {};
813
- const privateData = self.operating.closed.private || [];
820
+ const publicData = self.operating && self.operating.closed && self.operating.closed.public || {};
821
+ const privateData = self.operating && self.operating.closed && self.operating.closed.private || [];
814
822
 
815
823
  if (publicData.closedOn) return publicData.closedOn;
816
824
  else if (privateData.length >= 1) {
@@ -824,8 +832,8 @@ module.exports = function(mongoose, config) {
824
832
  Organization.virtual('operating.closed.hasZeroUnrealizedValue').get(function () {
825
833
 
826
834
  const self = this;
827
- const publicData = self.operating.closed.public || {};
828
- const privateData = self.operating.closed.private || [];
835
+ const publicData = self.operating && self.operating.closed && self.operating.closed.public || {};
836
+ const privateData = self.operating && self.operating.closed && self.operating.closed.private || [];
829
837
 
830
838
  if (publicData.closedOn) return false;
831
839
  else if (privateData.length >= 1) {
@@ -839,8 +847,8 @@ module.exports = function(mongoose, config) {
839
847
  Organization.virtual('operating.acquired.amount').get(function () {
840
848
 
841
849
  const self = this;
842
- const publicData = self.operating.acquired.public || {};
843
- const privateData = self.operating.acquired.private || [];
850
+ const publicData = self.operating && self.operating.acquired && self.operating.acquired.public || {};
851
+ const privateData = self.operating && self.operating.acquired && self.operating.acquired.private || [];
844
852
 
845
853
  if (publicData.amount) return publicData.amount;
846
854
  else if (privateData.length >= 1) {
@@ -854,8 +862,8 @@ module.exports = function(mongoose, config) {
854
862
  Organization.virtual('operating.acquired.by').get(function () {
855
863
 
856
864
  const self = this;
857
- const publicData = self.operating.acquired.public || {};
858
- const privateData = self.operating.acquired.private || [];
865
+ const publicData = self.operating && self.operating.acquired && self.operating.acquired.public || {};
866
+ const privateData = self.operating && self.operating.acquired && self.operating.acquired.private || [];
859
867
 
860
868
  if (publicData.by) return publicData.by;
861
869
  else if (privateData.length >= 1) {
@@ -869,8 +877,8 @@ module.exports = function(mongoose, config) {
869
877
  Organization.virtual('operating.acquired.on').get(function () {
870
878
 
871
879
  const self = this;
872
- const publicData = self.operating.acquired.public || {};
873
- const privateData = self.operating.acquired.private || [];
880
+ const publicData = self.operating && self.operating.acquired && self.operating.acquired.public || {};
881
+ const privateData = self.operating && self.operating.acquired && self.operating.acquired.private || [];
874
882
 
875
883
  if (publicData.acquiredOn) return publicData.acquiredOn;
876
884
  else if (privateData.length >= 1) {
@@ -884,8 +892,8 @@ module.exports = function(mongoose, config) {
884
892
  Organization.virtual('operating.merged.with').get(function () {
885
893
 
886
894
  const self = this;
887
- const publicData = self.operating.merged.public || {};
888
- const privateData = self.operating.merged.private || [];
895
+ const publicData = self.operating && self.operating.merged && self.operating.merged.public || {};
896
+ const privateData = self.operating && self.operating.merged && self.operating.merged.private || [];
889
897
 
890
898
  if (publicData.with) return publicData.with;
891
899
  else if (privateData.length >= 1) {
@@ -899,8 +907,8 @@ module.exports = function(mongoose, config) {
899
907
  Organization.virtual('operating.merged.on').get(function () {
900
908
 
901
909
  const self = this;
902
- const publicData = self.operating.merged.public || {};
903
- const privateData = self.operating.merged.private || [];
910
+ const publicData = self.operating && self.operating.merged && self.operating.merged.public || {};
911
+ const privateData = self.operating && self.operating.merged && self.operating.merged.private || [];
904
912
 
905
913
  if (publicData.mergedOn) return publicData.mergedOn;
906
914
  else if (privateData.length >= 1) {
@@ -1963,7 +1971,7 @@ module.exports = function(mongoose, config) {
1963
1971
 
1964
1972
  Organization.pre('save', function(next) {
1965
1973
 
1966
- let prepForSave = function prepForSave(doc) {
1974
+ const prepForSave = function prepForSave(doc) {
1967
1975
 
1968
1976
  var cleanContactInfo = function cleanContactInfo(obj) {
1969
1977
 
@@ -2088,7 +2096,46 @@ module.exports = function(mongoose, config) {
2088
2096
 
2089
2097
  };
2090
2098
 
2099
+ const setSocialHandles = function setSocialHandles(doc) {
2100
+
2101
+ const getHandle = function getHandle(url) {
2102
+
2103
+ let handle = "";
2104
+
2105
+ if (!url) return handle;
2106
+
2107
+ try {
2108
+
2109
+ const segments = url.split("/");
2110
+
2111
+ // normal case, e.g., http://facebook.com/hello
2112
+ if (segments[segments.length - 1]) handle = segments[segments.length - 1];
2113
+
2114
+ // trailing slash, e.g., http://facebook.com/hello/
2115
+ else if (segments[segments.length - 2]) handle = segments[segments.length - 2];
2116
+
2117
+ else handle = "";
2118
+
2119
+ }
2120
+ catch (err) {
2121
+ console.log('Error for url', url);
2122
+ console.error(err);
2123
+ }
2124
+
2125
+ return handle;
2126
+
2127
+ };
2128
+
2129
+ if (!doc) return;
2130
+ if (!doc.social) return;
2131
+ doc.social_handle.facebook = getHandle(doc.social.facebook);
2132
+ doc.social_handle.twitter = getHandle(doc.social.twitter);
2133
+ doc.social_handle.linkedin = getHandle(doc.social.linkedin);
2134
+
2135
+ }
2136
+
2091
2137
  prepForSave(this);
2138
+ setSocialHandles(this);
2092
2139
 
2093
2140
  return next();
2094
2141
 
@@ -0,0 +1,119 @@
1
+ "use strict";
2
+
3
+ const mongoose = require('mongoose');
4
+ const models = require('../index')(mongoose, { suppressDebugLog: true });
5
+ const Organization = mongoose.model('Organization');
6
+ const db_uri = "mongodb://jason:T576G150HPXLA5q4BJ2o2zj747B5030x@production-shard-00-00-gv0f3.mongodb.net:27017,production-shard-00-01-gv0f3.mongodb.net:27017,production-shard-00-02-gv0f3.mongodb.net:27017/production?replicaSet=Production-shard-0&ssl=true&authSource=admin";
7
+ const _ = require('underscore');
8
+ const async = require('async');
9
+
10
+ let dirtyOrgs = [];
11
+ let savedOrgCount = 0;
12
+
13
+ const getHandle = function getHandle(url) {
14
+
15
+ let handle = "";
16
+
17
+ try {
18
+
19
+ const segments = url.split("/");
20
+
21
+ // normal case, e.g., http://facebook.com/hello
22
+ if (segments[segments.length - 1]) handle = segments[segments.length - 1];
23
+
24
+ // trailing slash, e.g., http://facebook.com/hello/
25
+ else if (segments[segments.length - 2]) handle = segments[segments.length - 2];
26
+
27
+ else handle = "";
28
+
29
+ }
30
+ catch (err) {
31
+ console.log('Error for url', url);
32
+ console.error(err);
33
+ }
34
+
35
+ //console.log(handle, url);
36
+
37
+ return handle;
38
+
39
+ };
40
+
41
+ const saveOrg = function(org, cb) {
42
+
43
+ Organization.upsert(org, 'social-handle-transformation', function(err) {
44
+ if (err) console.error(err);
45
+ else {
46
+ savedOrgCount += 1;
47
+ //console.log(org.name);
48
+ }
49
+ return cb();
50
+ });
51
+
52
+ }
53
+
54
+ mongoose.connect(db_uri, function(err) {
55
+
56
+ if (err) {
57
+ console.error(err);
58
+ process.exit();
59
+ }
60
+
61
+ const page = 9;
62
+ const limit = 10000;
63
+ const skip = (page - 1) * limit;
64
+
65
+ console.log('Getting page', page);
66
+
67
+ Organization
68
+ .find({
69
+ $and: [
70
+ { social: { $exists: true } },
71
+ {
72
+ $or: [
73
+ { "social.facebook": { $exists: true } },
74
+ { "social.twitter": { $exists: true } },
75
+ { "social.linkedin": { $exists: true } }
76
+ ]
77
+ }
78
+ ]
79
+ })
80
+ .skip(skip)
81
+ .limit(limit)
82
+ .sort({"name": "asc"})
83
+ .exec(function(err, orgs) {
84
+
85
+ if (err) {
86
+ console.error(err);
87
+ process.exit();
88
+ }
89
+
90
+ console.log('Found', orgs.length, 'orgs');
91
+
92
+ orgs.forEach(function(org) {
93
+
94
+ let dirty = false;
95
+
96
+ if (org.social.facebook || org.social.twitter || org.social.linkedin) {
97
+ dirty = true;
98
+ }
99
+
100
+ if (dirty) dirtyOrgs.push(org);
101
+
102
+ });
103
+
104
+ console.log(dirtyOrgs.length, 'orgs need to be saved');
105
+
106
+ async.each(dirtyOrgs, saveOrg, function(err) {
107
+
108
+ if (err) console.error(err);
109
+
110
+ console.log('Saved', savedOrgCount, 'orgs');
111
+ console.log('Done page', page);
112
+
113
+ process.exit();
114
+
115
+ });
116
+
117
+ });
118
+
119
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dhyasama/totem-models",
3
- "version": "10.9.0",
3
+ "version": "10.11.0",
4
4
  "author": "Jason Reynolds",
5
5
  "license": "UNLICENSED",
6
6
  "description": "Models for Totem platform",
@@ -19,7 +19,7 @@ var someOtherLp;
19
19
  var account;
20
20
  var customer1, customer2;
21
21
 
22
- describe('Organization', function() {
22
+ describe.only('Organization', function() {
23
23
 
24
24
  var org1, org2, org3, org4, org5;
25
25
  var mergeId = new mongoose.Types.ObjectId();
@@ -448,6 +448,7 @@ describe('Organization', function() {
448
448
  org1 = org;
449
449
  done();
450
450
  });
451
+
451
452
  }); // end adds an org
452
453
 
453
454
  it('finds a fuzzy match by name', function (done) {
@@ -1148,6 +1149,28 @@ describe('Organization', function() {
1148
1149
 
1149
1150
  });
1150
1151
 
1152
+ it('sets social handles', function (done) {
1153
+
1154
+ const o = new Organization();
1155
+ o.name = 'Social Butterfly';
1156
+ o.slug = 'sb';
1157
+ o.social = {
1158
+ facebook: 'https://facebook.com/socialbutterfly',
1159
+ twitter: 'https://twitter.com/socialbutterfly',
1160
+ linkedin: 'http://linkedin.com/in/socialbutterfly',
1161
+ }
1162
+
1163
+ Organization.upsert(o, 'test-user', function (err, org) {
1164
+ should.not.exist(err);
1165
+ should.exist(org);
1166
+ org.social_handle.facebook.should.equal('socialbutterfly');
1167
+ org.social_handle.twitter.should.equal('socialbutterfly');
1168
+ org.social_handle.linkedin.should.equal('socialbutterfly');
1169
+ done();
1170
+ });
1171
+
1172
+ });
1173
+
1151
1174
  });
1152
1175
 
1153
1176
  describe('Deals', function() {