@dhyasama/totem-models 3.14.0 → 4.1.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.
@@ -213,125 +213,6 @@ module.exports = function(mongoose, config) {
213
213
  // HELPERS
214
214
  ///////////////////////////////////////////////////////////////////////////////////////
215
215
 
216
- var cleanDoc = function cleanDoc(doc) {
217
-
218
- var cleanContactInfo = function cleanContactInfo(obj) {
219
-
220
- var thereCanOnlyBeOne = function thereCanOnlyBeOne(items) {
221
-
222
- var primaryItems = _.filter(items, function(item) { return item.primary; });
223
-
224
- if (primaryItems.length > 1) {
225
-
226
- _.each(primaryItems, function(item, index) {
227
- // skip the first one (leave it primary) and mark the rest as not primary
228
- if (index >= 1) item.primary = false;
229
- });
230
-
231
- }
232
-
233
- return items;
234
-
235
- };
236
-
237
- if (!obj || !obj.contact) return;
238
-
239
- if (obj.contact.address) {
240
-
241
- // must have something meaningful
242
- obj.contact.address = _.map(obj.contact.address, function(address) {
243
- if (!address.city && !address.state && !address.country) return null;
244
- else return address;
245
- });
246
-
247
- // remove emptiness and dedupe
248
- obj.contact.address = _.uniq(_.compact(obj.contact.address), function(item) { return item.city + item.state + item.country; });
249
-
250
- // only one primary
251
- obj.contact.address = thereCanOnlyBeOne(obj.contact.address);
252
-
253
- }
254
-
255
- if (obj.contact.email) {
256
-
257
- // must have something meaningful
258
- obj.contact.email = _.map(doc.contact.email, function(item) {
259
- if (!item.email) return null;
260
- else return item;
261
- });
262
-
263
- // remove emptiness and dedupe
264
- obj.contact.email = _.uniq(_.compact(obj.contact.email), function(item) { return item.email; });
265
-
266
- // only one primary
267
- obj.contact.email = thereCanOnlyBeOne(obj.contact.email);
268
-
269
- }
270
-
271
- if (obj.contact.phone) {
272
-
273
- // must have something meaningful
274
- obj.contact.phone = _.map(doc.contact.phone, function(item) {
275
- if (!item.number) return null;
276
- else return item;
277
- });
278
-
279
- // remove emptiness and dedupe
280
- obj.contact.phone = _.uniq(_.compact(obj.contact.phone), function(item) { return item.number; });
281
-
282
- // only one primary
283
- obj.contact.phone = thereCanOnlyBeOne(obj.contact.phone);
284
-
285
- }
286
-
287
- };
288
-
289
- var cleanPeople = function cleanPeople(obj) {
290
-
291
- // remove junk
292
- obj.people = _.reject(obj.people, function(p) { return p.person == null; });
293
-
294
- // dedupe
295
- obj.people = _.uniq(obj.people, function(p) { return p.person._id ? p.person._id.toString() : p.person.toString(); });
296
-
297
- };
298
-
299
- var cleanWebsites = function cleanWebsites(obj) {
300
-
301
- // Format website
302
- if (obj.website) {
303
- obj.website = String(obj.website);
304
- obj.website = obj.website.replace('http://', '').replace('https://', '').replace(/\/+$/, '');
305
- obj.website = utils.getDomain(obj.website);
306
- }
307
-
308
- // Format website aliases
309
- if (obj.websiteAliases && obj.websiteAliases.length >= 1) {
310
- obj.websiteAliases = _.map(obj.websiteAliases, function(alias) {
311
- alias = String(alias);
312
- alias = alias.replace('http://', '').replace('https://', '').replace(/\/+$/, '');
313
- return utils.getDomain(alias);
314
- });
315
- }
316
-
317
- // Add website to website aliases
318
- // There are unique constraints on website and website aliases
319
- // This enables us to enforce uniqueness across both and across documents
320
- if (obj.website && obj.websiteAliases) obj.websiteAliases.push(obj.website);
321
-
322
- if (obj.websiteAliases) {
323
- obj.websiteAliases = _.uniq(obj.websiteAliases);
324
- obj.websiteAliases = _.compact(obj.websiteAliases);
325
- }
326
-
327
- };
328
-
329
- cleanContactInfo(doc);
330
- cleanPeople(doc);
331
- cleanWebsites(doc);
332
-
333
- };
334
-
335
216
  var getSources = function getSources(people) {
336
217
 
337
218
  var result = _.compact(_.pluck(people, 'person'));
@@ -940,7 +821,7 @@ module.exports = function(mongoose, config) {
940
821
 
941
822
  };
942
823
 
943
- Organization.statics.getAcquistions = function getAcquistions(orgid, cb) {
824
+ Organization.statics.getAcquisitions = function getAcquisitions(orgid, cb) {
944
825
  // todo - logic to check public and private
945
826
  var self = this;
946
827
  self
@@ -1407,15 +1288,16 @@ module.exports = function(mongoose, config) {
1407
1288
 
1408
1289
  Organization.statics.modify = function(filter, update, cb) {
1409
1290
 
1291
+ // VERY IMPORTANT NOTE
1292
+ // findOneAndUpdate does not trigger pre-save hook so that code will not run here
1293
+ // at this time, the only known and approved use of this method is for adding and removing roles in totem web
1294
+
1410
1295
  if (!cb) throw new Error('cb is required');
1411
1296
  if (!filter) { return cb(new Error('filter is required'), null); }
1412
1297
  if (!update) { return cb(new Error('update is required'), null); }
1413
1298
 
1414
1299
  var self = this;
1415
1300
 
1416
- // cleaning is done here because findOneAndUpdate does not trigger pre-save hook
1417
- cleanDoc(update['$set']);
1418
-
1419
1301
  self.findOneAndUpdate(filter, update, { upsert: false, new: true }, cb);
1420
1302
 
1421
1303
  };
@@ -1432,6 +1314,125 @@ module.exports = function(mongoose, config) {
1432
1314
 
1433
1315
  var self = this;
1434
1316
 
1317
+ var cleanDoc = function cleanDoc(doc) {
1318
+
1319
+ var cleanContactInfo = function cleanContactInfo(obj) {
1320
+
1321
+ var thereCanOnlyBeOne = function thereCanOnlyBeOne(items) {
1322
+
1323
+ var primaryItems = _.filter(items, function(item) { return item.primary; });
1324
+
1325
+ if (primaryItems.length > 1) {
1326
+
1327
+ _.each(primaryItems, function(item, index) {
1328
+ // skip the first one (leave it primary) and mark the rest as not primary
1329
+ if (index >= 1) item.primary = false;
1330
+ });
1331
+
1332
+ }
1333
+
1334
+ return items;
1335
+
1336
+ };
1337
+
1338
+ if (!obj || !obj.contact) return;
1339
+
1340
+ if (obj.contact.address) {
1341
+
1342
+ // must have something meaningful
1343
+ obj.contact.address = _.map(obj.contact.address, function(address) {
1344
+ if (!address.city && !address.state && !address.country) return null;
1345
+ else return address;
1346
+ });
1347
+
1348
+ // remove emptiness and dedupe
1349
+ obj.contact.address = _.uniq(_.compact(obj.contact.address), function(item) { return item.city + item.state + item.country; });
1350
+
1351
+ // only one primary
1352
+ obj.contact.address = thereCanOnlyBeOne(obj.contact.address);
1353
+
1354
+ }
1355
+
1356
+ if (obj.contact.email) {
1357
+
1358
+ // must have something meaningful
1359
+ obj.contact.email = _.map(doc.contact.email, function(item) {
1360
+ if (!item.email) return null;
1361
+ else return item;
1362
+ });
1363
+
1364
+ // remove emptiness and dedupe
1365
+ obj.contact.email = _.uniq(_.compact(obj.contact.email), function(item) { return item.email; });
1366
+
1367
+ // only one primary
1368
+ obj.contact.email = thereCanOnlyBeOne(obj.contact.email);
1369
+
1370
+ }
1371
+
1372
+ if (obj.contact.phone) {
1373
+
1374
+ // must have something meaningful
1375
+ obj.contact.phone = _.map(doc.contact.phone, function(item) {
1376
+ if (!item.number) return null;
1377
+ else return item;
1378
+ });
1379
+
1380
+ // remove emptiness and dedupe
1381
+ obj.contact.phone = _.uniq(_.compact(obj.contact.phone), function(item) { return item.number; });
1382
+
1383
+ // only one primary
1384
+ obj.contact.phone = thereCanOnlyBeOne(obj.contact.phone);
1385
+
1386
+ }
1387
+
1388
+ };
1389
+
1390
+ var cleanPeople = function cleanPeople(obj) {
1391
+
1392
+ // remove junk
1393
+ obj.people = _.reject(obj.people, function(p) { return p.person == null; });
1394
+
1395
+ // dedupe
1396
+ obj.people = _.uniq(obj.people, function(p) { return p.person._id ? p.person._id.toString() : p.person.toString(); });
1397
+
1398
+ };
1399
+
1400
+ var cleanWebsites = function cleanWebsites(obj) {
1401
+
1402
+ // Format website
1403
+ if (obj.website) {
1404
+ obj.website = String(obj.website);
1405
+ obj.website = obj.website.replace('http://', '').replace('https://', '').replace(/\/+$/, '');
1406
+ obj.website = utils.getDomain(obj.website);
1407
+ }
1408
+
1409
+ // Format website aliases
1410
+ if (obj.websiteAliases && obj.websiteAliases.length >= 1) {
1411
+ obj.websiteAliases = _.map(obj.websiteAliases, function(alias) {
1412
+ alias = String(alias);
1413
+ alias = alias.replace('http://', '').replace('https://', '').replace(/\/+$/, '');
1414
+ return utils.getDomain(alias);
1415
+ });
1416
+ }
1417
+
1418
+ // Add website to website aliases
1419
+ // There are unique constraints on website and website aliases
1420
+ // This enables us to enforce uniqueness across both and across documents
1421
+ if (obj.website && obj.websiteAliases) obj.websiteAliases.push(obj.website);
1422
+
1423
+ if (obj.websiteAliases) {
1424
+ obj.websiteAliases = _.uniq(obj.websiteAliases);
1425
+ obj.websiteAliases = _.compact(obj.websiteAliases);
1426
+ }
1427
+
1428
+ };
1429
+
1430
+ cleanContactInfo(doc);
1431
+ cleanPeople(doc);
1432
+ cleanWebsites(doc);
1433
+
1434
+ };
1435
+
1435
1436
  cleanDoc(self);
1436
1437
 
1437
1438
  return next();
package/lib/Person.js CHANGED
@@ -42,7 +42,7 @@ module.exports = function(mongoose, config) {
42
42
 
43
43
  email: [{
44
44
  type: { type: String, enum: ['personal', 'work', 'assistant', 'other'] },
45
- email: { type: String, default: '' },
45
+ email: { type: String, default: '', trim: true, lowercase: true },
46
46
  primary: { type: Boolean, default: false },
47
47
  inactive: { type: Boolean, default: false }
48
48
  }],
@@ -137,98 +137,6 @@ module.exports = function(mongoose, config) {
137
137
 
138
138
  /////////////
139
139
 
140
- var cleanDoc = function cleanDoc(doc) {
141
-
142
- var cleanContactInfo = function cleanContactInfo(obj) {
143
-
144
- var thereCanOnlyBeOne = function thereCanOnlyBeOne(items) {
145
-
146
- var primaryItems = _.filter(items, function(item) { return item.primary; });
147
-
148
- if (primaryItems.length > 1) {
149
-
150
- _.each(primaryItems, function(item, index) {
151
- // skip the first one (leave it primary) and mark the rest as not primary
152
- if (index >= 1) item.primary = false;
153
- });
154
-
155
- }
156
-
157
- return items;
158
-
159
- };
160
-
161
- if (!obj || !obj.contact) return;
162
-
163
- if (obj.contact.address) {
164
-
165
- // must have something meaningful
166
- obj.contact.address = _.map(obj.contact.address, function(address) {
167
- if (!address.city && !address.state && !address.country) return null;
168
- else return address;
169
- });
170
-
171
- // remove emptiness and dedupe
172
- obj.contact.address = _.uniq(_.compact(obj.contact.address), function(item) { return item.city + item.state + item.country; });
173
-
174
- // only one primary
175
- obj.contact.address = thereCanOnlyBeOne(obj.contact.address);
176
-
177
- }
178
-
179
- if (obj.contact.email) {
180
-
181
- // must have something meaningful
182
- obj.contact.email = _.map(doc.contact.email, function(item) {
183
- if (!item.email) return null;
184
- else return item;
185
- });
186
-
187
- // remove emptiness and dedupe
188
- obj.contact.email = _.uniq(_.compact(obj.contact.email), function(item) { return item.email; });
189
-
190
- // only one primary
191
- obj.contact.email = thereCanOnlyBeOne(obj.contact.email);
192
-
193
- }
194
-
195
- if (obj.contact.phone) {
196
-
197
- // must have something meaningful
198
- obj.contact.phone = _.map(doc.contact.phone, function(item) {
199
- if (!item.number) return null;
200
- else return item;
201
- });
202
-
203
- // remove emptiness and dedupe
204
- obj.contact.phone = _.uniq(_.compact(obj.contact.phone), function(item) { return item.number; });
205
-
206
- // only one primary
207
- obj.contact.phone = thereCanOnlyBeOne(obj.contact.phone);
208
-
209
- }
210
-
211
- };
212
-
213
- cleanContactInfo(doc);
214
-
215
- // set full name for ease of searching
216
- if (doc.fullName) doc.fullName = doc.name.first + ' ' + doc.name.last;
217
-
218
- if (doc.sources) {
219
-
220
- // must have person and customer refs
221
- doc.sources = _.reject(doc.sources, function(source) { return !source.person || !source.customer; });
222
-
223
- // dedupe and compact
224
- doc.sources = _.compact(_.uniq(doc.sources, function(source) { return source.person.toString() + source.customer.toString(); }));
225
-
226
- }
227
-
228
- };
229
-
230
- /////////////
231
-
232
140
  Person.virtual('name.full').get(function () {
233
141
  return this.name.first + ' ' + this.name.last;
234
142
  });
@@ -1073,20 +981,6 @@ module.exports = function(mongoose, config) {
1073
981
 
1074
982
  };
1075
983
 
1076
- Person.statics.modify = function(personId, update, cb) {
1077
-
1078
- if (!cb) throw new Error('cb is required');
1079
- if (!personId) { return cb(new Error('personId is required'), null); }
1080
- if (!update) { return cb(new Error('update is required'), null); }
1081
-
1082
- var self = this;
1083
-
1084
- cleanDoc(update['$set']);
1085
-
1086
- self.findOneAndUpdate({ _id: personId }, update, { upsert: false, new: true }, cb);
1087
-
1088
- };
1089
-
1090
984
  Person.statics.addFlag = function(personId, creatorPersonId, text, cb) {
1091
985
 
1092
986
  Flag.createForModel({
@@ -1180,57 +1074,95 @@ module.exports = function(mongoose, config) {
1180
1074
 
1181
1075
  var self = this;
1182
1076
 
1183
- // var thereCanOnlyBeOne = function thereCanOnlyBeOne(items) {
1184
- //
1185
- // var primaryItems = _.filter(items, function(item) { return item.primary; });
1186
- //
1187
- // if (primaryItems.length > 1) {
1188
- //
1189
- // _.each(primaryItems, function(item, index) {
1190
- // // skip the first one (leave it primary) and mark the rest as not primary
1191
- // if (index >= 1) item.primary = false;
1192
- // });
1193
- //
1194
- // }
1195
- //
1196
- // return items;
1197
- //
1198
- // };
1199
- //
1200
- // // set full name for ease of searching
1201
- // self.fullName = self.name.first + ' ' + self.name.last;
1202
- //
1203
- // // must have something meaningful
1204
- // self.contact.address = _.map(self.contact.address, function(address) {
1205
- // if (!address.city && !address.state && !address.country) return null;
1206
- // else return address;
1207
- // });
1208
- //
1209
- // self.contact.email = _.map(self.contact.email, function(item) {
1210
- // if (!item.email) return null;
1211
- // else return item;
1212
- // });
1213
- //
1214
- // self.contact.phone = _.map(self.contact.phone, function(item) {
1215
- // if (!item.number) return null;
1216
- // else return item;
1217
- // });
1218
- //
1219
- // // remove emptiness and dedupe
1220
- // self.contact.address = _.uniq(_.compact(self.contact.address), function(item) { return item.city + item.state + item.country; });
1221
- // self.contact.email = _.uniq(_.compact(self.contact.email), function(item) { return item.email; });
1222
- // self.contact.phone = _.uniq(_.compact(self.contact.phone), function(item) { return item.number; });
1223
- //
1224
- // // only one primary
1225
- // self.contact.address = thereCanOnlyBeOne(self.contact.address);
1226
- // self.contact.email = thereCanOnlyBeOne(self.contact.email);
1227
- // self.contact.phone = thereCanOnlyBeOne(self.contact.phone);
1228
- //
1229
- // // must have person and customer refs
1230
- // self.sources = _.reject(self.sources, function(source) { return !source.person || !source.customer; });
1231
- //
1232
- // // dedupe
1233
- // self.sources = _.uniq(self.sources, function(source) { return source.person.toString() + source.customer.toString(); });
1077
+ var cleanDoc = function cleanDoc(doc) {
1078
+
1079
+ var cleanContactInfo = function cleanContactInfo(obj) {
1080
+
1081
+ var thereCanOnlyBeOne = function thereCanOnlyBeOne(items) {
1082
+
1083
+ var primaryItems = _.filter(items, function(item) { return item.primary; });
1084
+
1085
+ if (primaryItems.length > 1) {
1086
+
1087
+ _.each(primaryItems, function(item, index) {
1088
+ // skip the first one (leave it primary) and mark the rest as not primary
1089
+ if (index >= 1) item.primary = false;
1090
+ });
1091
+
1092
+ }
1093
+
1094
+ return items;
1095
+
1096
+ };
1097
+
1098
+ if (!obj || !obj.contact) return;
1099
+
1100
+ if (obj.contact.address) {
1101
+
1102
+ // must have something meaningful
1103
+ obj.contact.address = _.map(obj.contact.address, function(address) {
1104
+ if (!address.city && !address.state && !address.country) return null;
1105
+ else return address;
1106
+ });
1107
+
1108
+ // remove emptiness and dedupe
1109
+ obj.contact.address = _.uniq(_.compact(obj.contact.address), function(item) { return item.city + item.state + item.country; });
1110
+
1111
+ // only one primary
1112
+ obj.contact.address = thereCanOnlyBeOne(obj.contact.address);
1113
+
1114
+ }
1115
+
1116
+ if (obj.contact.email) {
1117
+
1118
+ // must have something meaningful
1119
+ obj.contact.email = _.map(doc.contact.email, function(item) {
1120
+ if (!item.email) return null;
1121
+ else return item;
1122
+ });
1123
+
1124
+ // remove emptiness and dedupe
1125
+ obj.contact.email = _.uniq(_.compact(obj.contact.email), function(item) { return item.email; });
1126
+
1127
+ // only one primary
1128
+ obj.contact.email = thereCanOnlyBeOne(obj.contact.email);
1129
+
1130
+ }
1131
+
1132
+ if (obj.contact.phone) {
1133
+
1134
+ // must have something meaningful
1135
+ obj.contact.phone = _.map(doc.contact.phone, function(item) {
1136
+ if (!item.number) return null;
1137
+ else return item;
1138
+ });
1139
+
1140
+ // remove emptiness and dedupe
1141
+ obj.contact.phone = _.uniq(_.compact(obj.contact.phone), function(item) { return item.number; });
1142
+
1143
+ // only one primary
1144
+ obj.contact.phone = thereCanOnlyBeOne(obj.contact.phone);
1145
+
1146
+ }
1147
+
1148
+ };
1149
+
1150
+ cleanContactInfo(doc);
1151
+
1152
+ // set full name for ease of searching
1153
+ if (doc.fullName) doc.fullName = doc.name.first + ' ' + doc.name.last;
1154
+
1155
+ if (doc.sources) {
1156
+
1157
+ // must have person and customer refs
1158
+ doc.sources = _.reject(doc.sources, function(source) { return !source.person || !source.customer; });
1159
+
1160
+ // dedupe and compact
1161
+ doc.sources = _.compact(_.uniq(doc.sources, function(source) { return source.person.toString() + source.customer.toString(); }));
1162
+
1163
+ }
1164
+
1165
+ };
1234
1166
 
1235
1167
  cleanDoc(self);
1236
1168
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dhyasama/totem-models",
3
- "version": "3.14.0",
3
+ "version": "4.1.0",
4
4
  "author": "Jason Reynolds",
5
5
  "license": "UNLICENSED",
6
6
  "description": "Models for Totem platform",
package/test/Person.js CHANGED
@@ -158,7 +158,7 @@ describe('Person', function() {
158
158
  Person.upsert(person3, 'test-user', function(err, person3) {
159
159
  Person.upsert(person4, 'test-user', function(err, person4) {
160
160
  Person.upsert(person5, 'test-user', function(err, person5) {
161
- done();
161
+ done();
162
162
  });
163
163
  });
164
164
  });
@@ -581,25 +581,6 @@ describe('Person', function() {
581
581
 
582
582
  });
583
583
 
584
- it('modifies a person', function(done) {
585
-
586
- var update = {
587
- $set: {
588
- 'title': 'Chief Testing Officer',
589
- 'entered.by': 'Testy McTester',
590
- 'entered.on': new Date()
591
- }
592
- };
593
-
594
- Person.modify(person._id, update, function(err, org) {
595
- should.not.exist(err);
596
- should.exist(org);
597
- org.title.should.equal('Chief Testing Officer');
598
- done();
599
- });
600
-
601
- });
602
-
603
584
  it('lists all people', function(done) {
604
585
 
605
586
  Person.listAllPeople(function(err, people) {