@dhyasama/totem-models 7.57.0 → 8.0.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/lib/List.js CHANGED
@@ -2,15 +2,14 @@
2
2
 
3
3
  module.exports = function(mongoose, config) {
4
4
 
5
- var
5
+ let
6
6
 
7
7
  Schema = mongoose.Schema,
8
8
  env = process.env.NODE_ENV || 'development',
9
9
  utils = require('@dhyasama/ffvc-utilities'),
10
- _ = require('underscore'),
11
- postFind = require('mongoose-post-find');
10
+ _ = require('underscore');
12
11
 
13
- var List = new Schema({
12
+ let List = new Schema({
14
13
 
15
14
  customer: { type: Schema.ObjectId, ref: 'Organization', index: true, required: true },
16
15
 
@@ -32,26 +31,6 @@ module.exports = function(mongoose, config) {
32
31
 
33
32
  });
34
33
 
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
-
55
34
  // Items are kept separate to facilitate population
56
35
  // This is a convenience function to get the full list of items
57
36
  List.virtual('items').get(function () {
@@ -83,17 +62,28 @@ module.exports = function(mongoose, config) {
83
62
 
84
63
  });
85
64
 
86
- List.statics.addItem = function(listId, itemId, collection, cb) {
65
+ List.statics.addItem = function(listId, itemId, collection, options, cb) {
87
66
 
88
- var self = this;
67
+ if (!cb) { throw new Error('cb is required'); }
68
+ if (!listId) { return cb(new Error('listId is required'), null); }
69
+ if (!mongoose.Types.ObjectId.isValid(listId)) { return cb(new Error('listId is not a valid ObjectId'), null); }
70
+ if (!itemId) { return cb(new Error('itemId is required'), null); }
71
+ if (!mongoose.Types.ObjectId.isValid(itemId)) { return cb(new Error('itemId is not a valid ObjectId'), null); }
72
+ if (!collection) { return cb(new Error('collection is required'), null); }
73
+ if (!options) { return cb(new Error('options is required'), null); }
74
+ if (!options.CUSTOMER_ID) { return cb(new Error('options.CUSTOMER_ID is required'), null); }
75
+ if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
89
76
 
90
- self.findOne({
77
+ const self = this;
78
+ let query = self.findOne({
91
79
  '_id': listId,
92
- 'customer': config.CUSTOMER_ID
93
- }, function(err, result) {
80
+ 'customer': options.CUSTOMER_ID
81
+ });
82
+
83
+ query.exec(function(err, result) {
94
84
 
95
- if (err) return cb(err, null);
96
- if (!result) return cb(null, null);
85
+ if (err) { return cb(err, null); }
86
+ if (!result) { return cb(null, null); }
97
87
 
98
88
  result[collection].push(itemId);
99
89
 
@@ -103,18 +93,20 @@ module.exports = function(mongoose, config) {
103
93
 
104
94
  };
105
95
 
106
- List.statics.createList = function(name, items, creator, cb) {
96
+ List.statics.createList = function(name, items, creator, options, cb) {
107
97
 
108
- var self = this;
109
- var CUSTOMER_ID = config.CUSTOMER_ID;
98
+ if (!cb) { throw new Error('cb is required'); }
99
+ if (!name) { return cb(new Error('name is required'), null); }
100
+ if (!items) { return cb(new Error('items is required'), null); }
101
+ if (!creator) { return cb(new Error('creator is required'), null); }
102
+ if (!options) { return cb(new Error('options is required'), null); }
103
+ if (!options.CUSTOMER_ID) { return cb(new Error('options.CUSTOMER_ID is required'), null); }
104
+ if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
110
105
 
111
- if (CUSTOMER_ID == 'GLOBAL_PROCESS') throw new Error('Cannot create lists in a global process');
112
- if (!name) throw new Error('Name is required');
113
- if (!items) items = {};
114
- if (!cb) throw new Error('cb is required');
106
+ const self = this;
115
107
 
116
- var list = new self();
117
- list.customer = config.CUSTOMER_ID;
108
+ let list = new self();
109
+ list.customer = options.CUSTOMER_ID;
118
110
  list.name = name;
119
111
  list.createdOn = new Date();
120
112
  list.createdBy = creator;
@@ -129,83 +121,47 @@ module.exports = function(mongoose, config) {
129
121
 
130
122
  };
131
123
 
132
- List.statics.getById = function (id, cb) {
133
- this
134
- .findOne({
135
- '_id': id,
136
- 'customer': config.CUSTOMER_ID
137
- })
138
- .populate('createdBy', 'name avatarUrl')
139
- .populate('limitedPartners', 'name logoUrl fundsInvested')
140
- .populate('organizations', 'name logoUrl contact')
141
- .populate('people', 'name title avatarUrl contact')
142
- .exec(cb);
143
- };
144
-
145
- // Get all lists that contain an lp
146
- List.statics.getByLP = function(lpId, cb) {
147
-
148
- var self = this;
149
-
150
- // DANGER
151
- // Note this is not filtered by customer!
152
- // It is used for merging which spans customers
153
-
154
- self
155
- .find({ limitedPartners: lpId })
156
- .populate('limitedPartners', 'name logoUrl')
157
- .populate('organizations', 'name logoUrl')
158
- .populate('people', 'name title avatarUrl')
159
- .exec(cb);
124
+ List.statics.getById = function (id, options, cb) {
160
125
 
161
- };
126
+ if (!cb) { throw new Error('cb is required'); }
127
+ if (!id) { return cb(new Error('name is required'), null); }
128
+ if (!mongoose.Types.ObjectId.isValid(id)) { return cb(new Error('id is not a valid ObjectId'), null); }
129
+ if (!options) { return cb(new Error('options is required'), null); }
130
+ if (!options.CUSTOMER_ID) { return cb(new Error('options.CUSTOMER_ID is required'), null); }
131
+ if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
162
132
 
163
- // Get all lists that contain an org
164
- List.statics.getByOrg = function(orgId, cb) {
133
+ const self = this;
165
134
 
166
- var self = this;
135
+ let query = self.findOne({
136
+ '_id': id,
137
+ 'customer': options.CUSTOMER_ID
138
+ });
167
139
 
168
- // DANGER
169
- // Note this is not filtered by customer!
170
- // It is used for merging which spans customers
140
+ query.populate('createdBy', 'name avatarUrl');
141
+ query.populate('limitedPartners', 'name logoUrl fundsInvested');
142
+ query.populate('organizations', 'name logoUrl contact');
143
+ query.populate('people', 'name title avatarUrl contact');
171
144
 
172
- self
173
- .find({ organizations: orgId })
174
- .populate('limitedPartners', 'name logoUrl')
175
- .populate('organizations', 'name logoUrl')
176
- .populate('people', 'name title avatarUrl')
177
- .exec(cb);
145
+ query.exec(cb);
178
146
 
179
147
  };
180
148
 
181
- // Get all lists that contain a person
182
- List.statics.getByPerson = function(personId, cb) {
183
-
184
- var self = this;
185
-
186
- // DANGER
187
- // Note this is not filtered by customer!
188
- // It is used for merging which spans customers
149
+ List.statics.getLists = function(options, cb) {
189
150
 
190
- self
191
- .find({ people: personId })
192
- .populate('limitedPartners', 'name logoUrl')
193
- .populate('organizations', 'name logoUrl')
194
- .populate('people', 'name title avatarUrl')
195
- .exec(cb);
151
+ if (!cb) { throw new Error('cb is required'); }
152
+ if (!options) { return cb(new Error('options is required'), null); }
153
+ if (!options.CUSTOMER_ID) { return cb(new Error('options.CUSTOMER_ID is required'), null); }
154
+ if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
196
155
 
197
- };
156
+ const self = this;
198
157
 
199
- List.statics.getLists = function(cb) {
158
+ let query = self.find({ 'customer': options.CUSTOMER_ID });
200
159
 
201
- var self = this;
160
+ query.populate('limitedPartners', 'name logoUrl');
161
+ query.populate('organizations', 'name logoUrl');
162
+ query.populate('people', 'name title avatarUrl');
202
163
 
203
- self
204
- .find({ 'customer': config.CUSTOMER_ID })
205
- .populate('limitedPartners', 'name logoUrl')
206
- .populate('organizations', 'name logoUrl')
207
- .populate('people', 'name title avatarUrl')
208
- .exec(cb);
164
+ query.exec(cb);
209
165
 
210
166
  };
211
167
 
@@ -218,9 +174,8 @@ module.exports = function(mongoose, config) {
218
174
  if (!id) { return cb(new Error('id is required'), null); }
219
175
  if (!update) { return cb(new Error('update is required'), null); }
220
176
 
221
- var self = this;
177
+ let self = this;
222
178
 
223
- // https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
224
179
  // options runValidators defaults false which is ok since we have upsert false
225
180
  // new returns the updated document
226
181
 
@@ -228,23 +183,44 @@ module.exports = function(mongoose, config) {
228
183
 
229
184
  };
230
185
 
231
- List.statics.remove = function(id, cb) {
232
- this.findOneAndRemove({
186
+ List.statics.remove = function(id, options, cb) {
187
+
188
+ if (!cb) { throw new Error('cb is required'); }
189
+ if (!id) { return cb(new Error('name is required'), null); }
190
+ if (!mongoose.Types.ObjectId.isValid(id)) { return cb(new Error('id is not a valid ObjectId'), null); }
191
+ if (!options) { return cb(new Error('options is required'), null); }
192
+ if (!options.CUSTOMER_ID) { return cb(new Error('options.CUSTOMER_ID is required'), null); }
193
+ if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
194
+
195
+ const self = this;
196
+
197
+ self.findOneAndRemove({
233
198
  '_id': id,
234
- 'customer': config.CUSTOMER_ID
199
+ 'customer': options.CUSTOMER_ID
235
200
  }, cb);
201
+
236
202
  };
237
203
 
238
- List.statics.removeItem = function(listId, itemId, collection, cb) {
204
+ List.statics.removeItem = function(listId, itemId, collection, options, cb) {
239
205
 
240
- var self = this;
206
+ if (!cb) { throw new Error('cb is required'); }
207
+ if (!listId) { return cb(new Error('listId is required'), null); }
208
+ if (!mongoose.Types.ObjectId.isValid(listId)) { return cb(new Error('listId is not a valid ObjectId'), null); }
209
+ if (!itemId) { return cb(new Error('itemId is required'), null); }
210
+ if (!mongoose.Types.ObjectId.isValid(itemId)) { return cb(new Error('itemId is not a valid ObjectId'), null); }
211
+ if (!collection) { return cb(new Error('collection is required'), null); }
212
+ if (!options) { return cb(new Error('options is required'), null); }
213
+ if (!options.CUSTOMER_ID) { return cb(new Error('options.CUSTOMER_ID is required'), null); }
214
+ if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
241
215
 
242
- var update = {};
216
+ const self = this;
217
+
218
+ let update = {};
243
219
  update[collection] = itemId;
244
220
 
245
221
  self.findOneAndUpdate({
246
222
  '_id': listId,
247
- 'customer': config.CUSTOMER_ID
223
+ 'customer': options.CUSTOMER_ID
248
224
  }, { $pull: update }, { new: true }, cb);
249
225
 
250
226
  };
@@ -255,49 +231,31 @@ module.exports = function(mongoose, config) {
255
231
 
256
232
  List.pre('save', function(next) {
257
233
 
258
- var self = this;
259
-
260
- cleanDoc(self);
261
-
262
- next();
263
-
264
- });
265
-
266
- List.set('autoIndex', (env == 'development'));
267
- List.set('toJSON', { virtuals: true });
268
-
269
- List.plugin(postFind, {
270
-
271
- find: function(results, done) {
234
+ let doc = this;
272
235
 
273
- var CUSTOMER_ID = config.CUSTOMER_ID;
274
-
275
- if (CUSTOMER_ID == 'GLOBAL_PROCESS') return done(null, results);
276
-
277
- // Reject any item that is for a different customer
278
- results = _.reject(results, function(item) {
279
- return item.customer.toString() != CUSTOMER_ID;
236
+ let dedupeArray = function dedupeArray(arr) {
237
+ return _.uniq(arr, function(item) {
238
+ return (utils.isValidObjectId(item) ? item : item._id).toString();
280
239
  });
240
+ };
281
241
 
282
- return done(null, results);
283
-
284
- },
285
-
286
- findOne: function(result, done) {
287
-
288
- var CUSTOMER_ID = config.CUSTOMER_ID;
242
+ // enforce unique items in arrays
243
+ doc.limitedPartners = dedupeArray(doc.limitedPartners);
244
+ doc.organizations = dedupeArray(doc.organizations);
245
+ doc.people = dedupeArray(doc.people);
289
246
 
290
- if (!result) return done(null, null);
291
- else if (CUSTOMER_ID == 'GLOBAL_PROCESS') return done(null, result);
292
- else if (result.customer.toString() == CUSTOMER_ID) return done(null, result);
293
- else return done(null, null);
247
+ // remove falsy values
248
+ doc.limitedPartners = _.compact(doc.limitedPartners);
249
+ doc.organizations = _.compact(doc.organizations);
250
+ doc.people = _.compact(doc.people);
294
251
 
295
- }
252
+ next();
296
253
 
297
254
  });
298
255
 
256
+ List.set('autoIndex', (env === 'development'));
257
+ List.set('toJSON', { virtuals: true });
299
258
  List.set('autoIndex', false);
300
- List.set('usePushEach', true);
301
259
  List.on('index', function(err) { console.log('error building list indexes: ' + err); });
302
260
 
303
261
  mongoose.model('List', List);