@dhyasama/totem-models 1.22.1 → 1.22.3

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/lib/List.js CHANGED
@@ -127,8 +127,12 @@ module.exports = function(mongoose, config) {
127
127
 
128
128
  var self = this;
129
129
 
130
+ // DANGER
131
+ // Note this is not filtered by customer!
132
+ // It is used for merging which spans customers
133
+
130
134
  self
131
- .find({ 'customer': config.CUSTOMER_ID, limitedPartners: lpId })
135
+ .find({ limitedPartners: lpId })
132
136
  .populate('limitedPartners', 'name logoUrl')
133
137
  .populate('organizations', 'name logoUrl')
134
138
  .populate('people', 'name title avatarUrl')
@@ -141,8 +145,12 @@ module.exports = function(mongoose, config) {
141
145
 
142
146
  var self = this;
143
147
 
148
+ // DANGER
149
+ // Note this is not filtered by customer!
150
+ // It is used for merging which spans customers
151
+
144
152
  self
145
- .find({ 'customer': config.CUSTOMER_ID, organizations: orgId })
153
+ .find({ organizations: orgId })
146
154
  .populate('limitedPartners', 'name logoUrl')
147
155
  .populate('organizations', 'name logoUrl')
148
156
  .populate('people', 'name title avatarUrl')
@@ -155,8 +163,12 @@ module.exports = function(mongoose, config) {
155
163
 
156
164
  var self = this;
157
165
 
166
+ // DANGER
167
+ // Note this is not filtered by customer!
168
+ // It is used for merging which spans customers
169
+
158
170
  self
159
- .find({ 'customer': config.CUSTOMER_ID, people: personId })
171
+ .find({ people: personId })
160
172
  .populate('limitedPartners', 'name logoUrl')
161
173
  .populate('organizations', 'name logoUrl')
162
174
  .populate('people', 'name title avatarUrl')
@@ -941,6 +941,16 @@ module.exports = function(mongoose, config) {
941
941
 
942
942
  };
943
943
 
944
+ Organization.statics.listCustomers = function getById(cb) {
945
+
946
+ var self = this;
947
+
948
+ self
949
+ .find({$where:'this.customer && this.customer.totemUrl && this.customer.totemUrl.length >=1'})
950
+ .exec(cb);
951
+
952
+ };
953
+
944
954
  Organization.statics.listPage = function (skip, cb) {
945
955
 
946
956
  this
@@ -1152,6 +1162,49 @@ module.exports = function(mongoose, config) {
1152
1162
 
1153
1163
  var self = this;
1154
1164
 
1165
+ var thereCanOnlyBeOne = function thereCanOnlyBeOne(items) {
1166
+
1167
+ var primaryItems = _.filter(items, function(item) { return item.primary; });
1168
+
1169
+ if (primaryItems.length > 1) {
1170
+
1171
+ _.each(primaryItems, function(item, index) {
1172
+ // skip the first one (leave it primary) and mark the rest as not primary
1173
+ if (index >= 1) item.primary = false;
1174
+ });
1175
+
1176
+ }
1177
+
1178
+ return items;
1179
+
1180
+ };
1181
+
1182
+ // must have something meaningful
1183
+ self.contact.address = _.map(self.contact.address, function(address) {
1184
+ if (!address.city && !address.state && !address.country) return null;
1185
+ else return address;
1186
+ });
1187
+
1188
+ self.contact.email = _.map(self.contact.email, function(item) {
1189
+ if (!item.email) return null;
1190
+ else return item;
1191
+ });
1192
+
1193
+ self.contact.phone = _.map(self.contact.phone, function(item) {
1194
+ if (!item.number) return null;
1195
+ else return item;
1196
+ });
1197
+
1198
+ // remove emptiness and dedupe
1199
+ self.contact.address = _.uniq(_.compact(self.contact.address), function(item) { return item.city + item.state + item.country; });
1200
+ self.contact.email = _.uniq(_.compact(self.contact.email), function(item) { return item.email; });
1201
+ self.contact.phone = _.uniq(_.compact(self.contact.phone), function(item) { return item.number; });
1202
+
1203
+ // only one primary
1204
+ self.contact.address = thereCanOnlyBeOne(self.contact.address);
1205
+ self.contact.email = thereCanOnlyBeOne(self.contact.email);
1206
+ self.contact.phone = thereCanOnlyBeOne(self.contact.phone);
1207
+
1155
1208
  // Format website
1156
1209
  if (self.website) self.website = utils.getDomain(self.website);
1157
1210
 
package/lib/Person.js CHANGED
@@ -1112,9 +1112,52 @@ module.exports = function(mongoose, config) {
1112
1112
 
1113
1113
  var self = this;
1114
1114
 
1115
+ var thereCanOnlyBeOne = function thereCanOnlyBeOne(items) {
1116
+
1117
+ var primaryItems = _.filter(items, function(item) { return item.primary; });
1118
+
1119
+ if (primaryItems.length > 1) {
1120
+
1121
+ _.each(primaryItems, function(item, index) {
1122
+ // skip the first one (leave it primary) and mark the rest as not primary
1123
+ if (index >= 1) item.primary = false;
1124
+ });
1125
+
1126
+ }
1127
+
1128
+ return items;
1129
+
1130
+ };
1131
+
1115
1132
  // set full name for ease of searching
1116
1133
  self.fullName = self.name.first + ' ' + self.name.last;
1117
1134
 
1135
+ // must have something meaningful
1136
+ self.contact.address = _.map(self.contact.address, function(address) {
1137
+ if (!address.city && !address.state && !address.country) return null;
1138
+ else return address;
1139
+ });
1140
+
1141
+ self.contact.email = _.map(self.contact.email, function(item) {
1142
+ if (!item.email) return null;
1143
+ else return item;
1144
+ });
1145
+
1146
+ self.contact.phone = _.map(self.contact.phone, function(item) {
1147
+ if (!item.number) return null;
1148
+ else return item;
1149
+ });
1150
+
1151
+ // remove emptiness and dedupe
1152
+ self.contact.address = _.uniq(_.compact(self.contact.address), function(item) { return item.city + item.state + item.country; });
1153
+ self.contact.email = _.uniq(_.compact(self.contact.email), function(item) { return item.email; });
1154
+ self.contact.phone = _.uniq(_.compact(self.contact.phone), function(item) { return item.number; });
1155
+
1156
+ // only one primary
1157
+ self.contact.address = thereCanOnlyBeOne(self.contact.address);
1158
+ self.contact.email = thereCanOnlyBeOne(self.contact.email);
1159
+ self.contact.phone = thereCanOnlyBeOne(self.contact.phone);
1160
+
1118
1161
  // Reset counters so we can calc from scratch
1119
1162
  _.each(self.sources, function(source) {
1120
1163
  source.interactions.calendar = 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dhyasama/totem-models",
3
- "version": "1.22.1",
3
+ "version": "1.22.3",
4
4
  "author": "Jason Reynolds",
5
5
  "license": "UNLICENSED",
6
6
  "description": "Models for Totem platform",
@@ -3,6 +3,7 @@
3
3
  var
4
4
 
5
5
  should = require("should"),
6
+ _ = require('underscore'),
6
7
  config = require('./config')['test'],
7
8
  mongoose = require('mongoose'),
8
9
  clearDB = require('mocha-mongoose')(config.db.uri, {noClear: true}),
@@ -800,6 +801,110 @@ describe('Organization', function() {
800
801
 
801
802
  });
802
803
 
804
+ it('cleans contact info', function(done) {
805
+
806
+ var o = new Organization();
807
+ o.name = 'ORG';
808
+ o.slug = 'org';
809
+ o.website = 'org.org';
810
+
811
+ o.contact.address = [];
812
+ o.contact.address.push({
813
+ city: 'Testville',
814
+ state: 'Test',
815
+ country: 'TSA',
816
+ type: 'work',
817
+ primary: true
818
+ });
819
+ o.contact.address.push({
820
+ city: 'Testville',
821
+ state: 'Test',
822
+ country: 'TSA',
823
+ type: 'other',
824
+ primary: true
825
+ });
826
+ o.contact.address.push({
827
+ city: 'Bangor',
828
+ state: 'ME',
829
+ country: 'USA',
830
+ type: 'other',
831
+ primary: true
832
+ });
833
+ o.contact.address.push({
834
+ city: '',
835
+ state: '',
836
+ country: '',
837
+ type: 'other',
838
+ primary: true
839
+ });
840
+
841
+ o.contact.email = [];
842
+ o.contact.email.push({
843
+ email: 'test@org.org',
844
+ type: 'work',
845
+ primary: true
846
+ });
847
+ o.contact.email.push({
848
+ email: 'test@org.org',
849
+ type: 'other',
850
+ primary: true
851
+ });
852
+ o.contact.email.push({
853
+ email: 'admin@org.org',
854
+ type: 'other',
855
+ primary: true
856
+ });
857
+ o.contact.email.push({
858
+ email: '',
859
+ type: 'test',
860
+ primary: false
861
+ });
862
+
863
+ o.contact.phone = [];
864
+ o.contact.phone.push({
865
+ number: '2075551212',
866
+ type: 'work',
867
+ primary: true
868
+ });
869
+ o.contact.phone.push({
870
+ number: '2075551212',
871
+ type: 'other',
872
+ primary: true
873
+ });
874
+ o.contact.phone.push({
875
+ number: null,
876
+ type: null,
877
+ primary: null
878
+ });
879
+ o.contact.phone.push({
880
+ number: '3455551212',
881
+ type: 'other',
882
+ primary: true
883
+ });
884
+
885
+ Organization.upsert(o, 'test-user', function(err, org) {
886
+
887
+ should.not.exist(err);
888
+ should.exist(org);
889
+
890
+ org.contact.address.length.should.equal(2);
891
+ org.contact.email.length.should.equal(2);
892
+ org.contact.phone.length.should.equal(2);
893
+
894
+ var primaryAddresses = _.filter(org.contact.address, function(item) { return item.primary; });
895
+ primaryAddresses.length.should.be.lessThan(2);
896
+
897
+ var primaryEmails = _.filter(org.contact.email, function(item) { return item.primary; });
898
+ primaryEmails.length.should.be.lessThan(2);
899
+
900
+ var primaryPhones = _.filter(org.contact.phone, function(item) { return item.primary; });
901
+ primaryPhones.length.should.be.lessThan(2);
902
+
903
+ done();
904
+
905
+ });
906
+ });
907
+
803
908
  describe('State', function() {
804
909
 
805
910
  it('gets ipo info', function(done) {
package/test/Person.js CHANGED
@@ -854,6 +854,109 @@ describe('Person', function() {
854
854
 
855
855
  });
856
856
 
857
+ it('cleans contact info', function(done) {
858
+
859
+ var p = new Person();
860
+ p.name = { first: 'Testy', last: 'Tester' };
861
+ p.slug = 'testy-tester';
862
+
863
+ p.contact.address = [];
864
+ p.contact.address.push({
865
+ city: 'Testville',
866
+ state: 'Test',
867
+ country: 'TSA',
868
+ type: 'work',
869
+ primary: true
870
+ });
871
+ p.contact.address.push({
872
+ city: 'Testville',
873
+ state: 'Test',
874
+ country: 'TSA',
875
+ type: 'other',
876
+ primary: true
877
+ });
878
+ p.contact.address.push({
879
+ city: 'Bangor',
880
+ state: 'ME',
881
+ country: 'USA',
882
+ type: 'other',
883
+ primary: true
884
+ });
885
+ p.contact.address.push({
886
+ city: '',
887
+ state: '',
888
+ country: '',
889
+ type: 'other',
890
+ primary: true
891
+ });
892
+
893
+ p.contact.email = [];
894
+ p.contact.email.push({
895
+ email: 'test@org.org',
896
+ type: 'work',
897
+ primary: true
898
+ });
899
+ p.contact.email.push({
900
+ email: 'test@org.org',
901
+ type: 'other',
902
+ primary: true
903
+ });
904
+ p.contact.email.push({
905
+ email: 'admin@org.org',
906
+ type: 'other',
907
+ primary: true
908
+ });
909
+ p.contact.email.push({
910
+ email: '',
911
+ type: 'other',
912
+ primary: false
913
+ });
914
+
915
+ p.contact.phone = [];
916
+ p.contact.phone.push({
917
+ number: '2075551212',
918
+ type: 'work',
919
+ primary: true
920
+ });
921
+ p.contact.phone.push({
922
+ number: '2075551212',
923
+ type: 'other',
924
+ primary: true
925
+ });
926
+ p.contact.phone.push({
927
+ number: null,
928
+ type: 'other',
929
+ primary: null
930
+ });
931
+ p.contact.phone.push({
932
+ number: '3455551212',
933
+ type: 'other',
934
+ primary: true
935
+ });
936
+
937
+ Person.upsert(p, 'test-user', function(err, person) {
938
+
939
+ should.not.exist(err);
940
+ should.exist(person);
941
+
942
+ person.contact.address.length.should.equal(2);
943
+ person.contact.email.length.should.equal(2);
944
+ person.contact.phone.length.should.equal(2);
945
+
946
+ var primaryAddresses = _.filter(person.contact.address, function(item) { return item.primary; });
947
+ primaryAddresses.length.should.be.lessThan(2);
948
+
949
+ var primaryEmails = _.filter(person.contact.email, function(item) { return item.primary; });
950
+ primaryEmails.length.should.be.lessThan(2);
951
+
952
+ var primaryPhones = _.filter(person.contact.phone, function(item) { return item.primary; });
953
+ primaryPhones.length.should.be.lessThan(2);
954
+
955
+ done();
956
+
957
+ });
958
+ });
959
+
857
960
  // describe('sources', function() {
858
961
  //
859
962
  // // can only add source if global process