@dhyasama/totem-models 3.12.1 → 3.12.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/Financials.js CHANGED
@@ -28,7 +28,6 @@ module.exports = function(mongoose, config) {
28
28
  year: { type: Number, default: 0 },
29
29
  quarter: { type: String, enum: ['all', 'q1', 'q2', 'q3', 'q4'] },
30
30
 
31
- // top line numbers
32
31
  cash: {
33
32
  amount: { type: Number, default: 0 },
34
33
  },
@@ -37,11 +36,11 @@ module.exports = function(mongoose, config) {
37
36
  },
38
37
  burn: {
39
38
  amount: { type: Number, default: 0 },
40
- period: { type: String, enum: [null, 'monthly', 'quarter']},
39
+ period: { type: String, enum: [null, 'monthly', 'quarterly']},
41
40
  },
42
41
  runway: {
43
42
  amount: { type: Number, default: 0 },
44
- period: { type: String, enum: [null, 'monthly', 'quarter']},
43
+ period: { type: String, enum: [null, 'monthly', 'quarterly']},
45
44
  },
46
45
 
47
46
  // income statement
package/lib/List.js CHANGED
@@ -32,6 +32,26 @@ module.exports = function(mongoose, config) {
32
32
 
33
33
  });
34
34
 
35
+ var cleanDoc = function cleanDoc(doc) {
36
+
37
+ var dedupeArray = function dedupeArray(arr) {
38
+ return _.uniq(arr, function(item) {
39
+ return (utils.isValidObjectId(item) ? item : item._id).toString();
40
+ });
41
+ };
42
+
43
+ // enforce unique items in arrays
44
+ doc.limitedPartners = dedupeArray(doc.limitedPartners);
45
+ doc.organizations = dedupeArray(doc.organizations);
46
+ doc.people = dedupeArray(doc.people);
47
+
48
+ // remove falsy values
49
+ doc.limitedPartners = _.compact(doc.limitedPartners);
50
+ doc.organizations = _.compact(doc.organizations);
51
+ doc.people = _.compact(doc.people);
52
+
53
+ };
54
+
35
55
  // Items are kept separate to facilitate population
36
56
  // This is a convenience function to get the full list of items
37
57
  List.virtual('items').get(function () {
@@ -218,16 +238,7 @@ module.exports = function(mongoose, config) {
218
238
 
219
239
  var self = this;
220
240
 
221
- var dedupeArray = function dedupeArray(arr) {
222
- return _.uniq(arr, function(item) {
223
- return (utils.isValidObjectId(item) ? item : item._id).toString();
224
- });
225
- };
226
-
227
- // enforce unique items in arrays
228
- self.limitedPartners = dedupeArray(self.limitedPartners);
229
- self.organizations = dedupeArray(self.organizations);
230
- self.people = dedupeArray(self.people);
241
+ cleanDoc(self);
231
242
 
232
243
  next();
233
244
 
@@ -213,6 +213,125 @@ 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
+
216
335
  var getSources = function getSources(people) {
217
336
 
218
337
  var result = _.compact(_.pluck(people, 'person'));
@@ -688,8 +807,6 @@ module.exports = function(mongoose, config) {
688
807
 
689
808
  domains = domains.concat(alternativeDomains);
690
809
 
691
- _.each(domains, function(d) { console.log(d); });
692
-
693
810
  var query = this.find({ $or: [ {'website': { $in : domains }}, {'websiteAliases': { $in : domains }} ], 'deleted': {$ne: true} });
694
811
  query.exec(cb);
695
812
 
@@ -1290,12 +1407,15 @@ module.exports = function(mongoose, config) {
1290
1407
 
1291
1408
  Organization.statics.modify = function(filter, update, cb) {
1292
1409
 
1293
- if (!cb) throw new Error('cb is required');
1410
+ if (!cb) throw new Error('cb is required');
1294
1411
  if (!filter) { return cb(new Error('filter is required'), null); }
1295
1412
  if (!update) { return cb(new Error('update is required'), null); }
1296
1413
 
1297
1414
  var self = this;
1298
1415
 
1416
+ // cleaning is done here because findOneAndUpdate does not trigger pre-save hook
1417
+ cleanDoc(update['$set']);
1418
+
1299
1419
  self.findOneAndUpdate(filter, update, { upsert: false, new: true }, cb);
1300
1420
 
1301
1421
  };
@@ -1312,93 +1432,7 @@ module.exports = function(mongoose, config) {
1312
1432
 
1313
1433
  var self = this;
1314
1434
 
1315
- var cleanContactInfo = function cleanContactInfo() {
1316
-
1317
- var thereCanOnlyBeOne = function thereCanOnlyBeOne(items) {
1318
-
1319
- var primaryItems = _.filter(items, function(item) { return item.primary; });
1320
-
1321
- if (primaryItems.length > 1) {
1322
-
1323
- _.each(primaryItems, function(item, index) {
1324
- // skip the first one (leave it primary) and mark the rest as not primary
1325
- if (index >= 1) item.primary = false;
1326
- });
1327
-
1328
- }
1329
-
1330
- return items;
1331
-
1332
- };
1333
-
1334
- // must have something meaningful
1335
- self.contact.address = _.map(self.contact.address, function(address) {
1336
- if (!address.city && !address.state && !address.country) return null;
1337
- else return address;
1338
- });
1339
-
1340
- self.contact.email = _.map(self.contact.email, function(item) {
1341
- if (!item.email) return null;
1342
- else return item;
1343
- });
1344
-
1345
- self.contact.phone = _.map(self.contact.phone, function(item) {
1346
- if (!item.number) return null;
1347
- else return item;
1348
- });
1349
-
1350
- // remove emptiness and dedupe
1351
- self.contact.address = _.uniq(_.compact(self.contact.address), function(item) { return item.city + item.state + item.country; });
1352
- self.contact.email = _.uniq(_.compact(self.contact.email), function(item) { return item.email; });
1353
- self.contact.phone = _.uniq(_.compact(self.contact.phone), function(item) { return item.number; });
1354
-
1355
- // only one primary
1356
- self.contact.address = thereCanOnlyBeOne(self.contact.address);
1357
- self.contact.email = thereCanOnlyBeOne(self.contact.email);
1358
- self.contact.phone = thereCanOnlyBeOne(self.contact.phone);
1359
-
1360
- };
1361
-
1362
- var cleanPeople = function cleanPeople() {
1363
-
1364
- // remove junk
1365
- self.people = _.reject(self.people, function(p) { return p.person == null; });
1366
-
1367
- // dedupe
1368
- self.people = _.uniq(self.people, function(p) { return p.person._id ? p.person._id.toString() : p.person.toString(); });
1369
-
1370
- };
1371
-
1372
- var cleanWebsites = function cleanWebsites() {
1373
-
1374
- // Format website
1375
- if (self.website) {
1376
- self.website = utils.getDomain(self.website);
1377
- self.website = self.website.replace('http://', '').replace('https://', '');
1378
- }
1379
-
1380
- // Format website aliases
1381
- if (self.websiteAliases && self.websiteAliases.length >= 1) {
1382
- self.websiteAliases = _.map(self.websiteAliases, function(alias) {
1383
- var website = utils.getDomain(alias);
1384
- website = website.replace('http://', '').replace('https://', '');
1385
- return utils.getDomain(website);
1386
- });
1387
- }
1388
-
1389
- // Add website to website aliases
1390
- // There are unique constraints on website and website aliases
1391
- // This enables us to enforce uniqueness across both and across documents
1392
- if (self.website && self.websiteAliases) {
1393
- self.websiteAliases.push(self.website)
1394
- self.websiteAliases = _.uniq(self.websiteAliases);
1395
- }
1396
-
1397
- };
1398
-
1399
- cleanContactInfo();
1400
- cleanPeople();
1401
- cleanWebsites();
1435
+ cleanDoc(self);
1402
1436
 
1403
1437
  return next();
1404
1438
 
package/lib/Person.js CHANGED
@@ -137,6 +137,98 @@ 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
+
140
232
  Person.virtual('name.full').get(function () {
141
233
  return this.name.first + ' ' + this.name.last;
142
234
  });
@@ -989,6 +1081,8 @@ module.exports = function(mongoose, config) {
989
1081
 
990
1082
  var self = this;
991
1083
 
1084
+ cleanDoc(update['$set']);
1085
+
992
1086
  self.findOneAndUpdate({ _id: personId }, update, { upsert: false, new: true }, cb);
993
1087
 
994
1088
  };
@@ -1086,57 +1180,59 @@ module.exports = function(mongoose, config) {
1086
1180
 
1087
1181
  var self = this;
1088
1182
 
1089
- var thereCanOnlyBeOne = function thereCanOnlyBeOne(items) {
1090
-
1091
- var primaryItems = _.filter(items, function(item) { return item.primary; });
1092
-
1093
- if (primaryItems.length > 1) {
1094
-
1095
- _.each(primaryItems, function(item, index) {
1096
- // skip the first one (leave it primary) and mark the rest as not primary
1097
- if (index >= 1) item.primary = false;
1098
- });
1099
-
1100
- }
1101
-
1102
- return items;
1103
-
1104
- };
1105
-
1106
- // set full name for ease of searching
1107
- self.fullName = self.name.first + ' ' + self.name.last;
1108
-
1109
- // must have something meaningful
1110
- self.contact.address = _.map(self.contact.address, function(address) {
1111
- if (!address.city && !address.state && !address.country) return null;
1112
- else return address;
1113
- });
1114
-
1115
- self.contact.email = _.map(self.contact.email, function(item) {
1116
- if (!item.email) return null;
1117
- else return item;
1118
- });
1119
-
1120
- self.contact.phone = _.map(self.contact.phone, function(item) {
1121
- if (!item.number) return null;
1122
- else return item;
1123
- });
1124
-
1125
- // remove emptiness and dedupe
1126
- self.contact.address = _.uniq(_.compact(self.contact.address), function(item) { return item.city + item.state + item.country; });
1127
- self.contact.email = _.uniq(_.compact(self.contact.email), function(item) { return item.email; });
1128
- self.contact.phone = _.uniq(_.compact(self.contact.phone), function(item) { return item.number; });
1129
-
1130
- // only one primary
1131
- self.contact.address = thereCanOnlyBeOne(self.contact.address);
1132
- self.contact.email = thereCanOnlyBeOne(self.contact.email);
1133
- self.contact.phone = thereCanOnlyBeOne(self.contact.phone);
1134
-
1135
- // must have person and customer refs
1136
- self.sources = _.reject(self.sources, function(source) { return !source.person || !source.customer; });
1137
-
1138
- // dedupe
1139
- self.sources = _.uniq(self.sources, function(source) { return source.person.toString() + source.customer.toString(); });
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(); });
1234
+
1235
+ cleanDoc(self);
1140
1236
 
1141
1237
  next();
1142
1238
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dhyasama/totem-models",
3
- "version": "3.12.1",
3
+ "version": "3.12.3",
4
4
  "author": "Jason Reynolds",
5
5
  "license": "UNLICENSED",
6
6
  "description": "Models for Totem platform",