@dhyasama/totem-models 12.3.0 → 12.4.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.
@@ -31,11 +31,11 @@ module.exports = function(mongoose, config) {
31
31
  end: { type: Number }
32
32
  }],
33
33
  notes: [{
34
- title: { type: String },
34
+ title: { type: String },
35
35
  list: [{ type: String }]
36
36
  }],
37
37
  actions: [{
38
- title: { type: String },
38
+ title: { type: String },
39
39
  list: [{ type: String }]
40
40
  }],
41
41
  insights: [{
@@ -51,25 +51,26 @@ module.exports = function(mongoose, config) {
51
51
 
52
52
  ///////////////////////////////////////
53
53
 
54
- Interaction.statics.delete = function(providerEventId, totemCustomerId, cb) {
54
+ Interaction.statics.delete = async function(providerEventId, totemCustomerId, cb) {
55
55
 
56
56
  if (!cb) { throw new Error('cb is required'); }
57
57
  if (!providerEventId) { return cb(new Error('providerEventId is required'), null); }
58
58
  if (!totemCustomerId) { return cb(new Error('totemCustomerId is required'), null); }
59
59
  if (!mongoose.Types.ObjectId.isValid(totemCustomerId)) { return cb(new Error('totemCustomerId is not a valid ObjectId'), null); }
60
60
 
61
- var self = this;
62
-
63
- self.findOneAndDelete({
64
- 'providerEventId': providerEventId,
65
- 'totemCustomerId': totemCustomerId
66
- }).then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
61
+ try {
62
+ const result = await this.findOneAndDelete({
63
+ 'providerEventId': providerEventId,
64
+ 'totemCustomerId': totemCustomerId
65
+ });
66
+ return cb(null, result);
67
+ } catch(err) {
68
+ return cb(err);
69
+ }
67
70
 
68
71
  };
69
72
 
70
- Interaction.statics.getById = function getById(id, options, cb) {
71
-
72
- const self = this;
73
+ Interaction.statics.getById = async function getById(id, options, cb) {
73
74
 
74
75
  if (!cb) { throw new Error('cb is required'); }
75
76
  if (!id) { return cb(new Error('id is required'), null); }
@@ -83,80 +84,92 @@ module.exports = function(mongoose, config) {
83
84
  if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
84
85
  }
85
86
 
86
- let query = self.findById(id);
87
-
88
- query.populate('attendees.internal');
89
- query.populate('attendees.external');
87
+ try {
88
+ let query = this.findById(id);
90
89
 
91
- query.exec().then(function(result) {if (options.isWorkerProcess) { return cb(null, result); }
92
- else { return cb(null, result); }
90
+ query.populate('attendees.internal');
91
+ query.populate('attendees.external');
93
92
 
94
- }).catch(function(err) { cb(err); });
93
+ const result = await query;
94
+ return cb(null, result);
95
+ } catch(err) {
96
+ return cb(err);
97
+ }
95
98
 
96
99
  };
97
100
 
98
101
 
99
- Interaction.statics.getByCalendarEventId = function getByCalendarEventId(calendarEventId, cb) {
102
+ Interaction.statics.getByCalendarEventId = async function getByCalendarEventId(calendarEventId, cb) {
100
103
 
101
104
  if (!cb) { throw new Error('cb is required'); }
102
105
  if (!calendarEventId) { return cb(new Error('calendarEventId is required'), null); }
103
106
 
104
- var self = this;
105
- var query = self.find({ calendarEventId: calendarEventId });
107
+ try {
108
+ let query = this.find({ calendarEventId: calendarEventId });
106
109
 
107
- query.populate('attendees.internal');
108
- query.populate('attendees.external');
110
+ query.populate('attendees.internal');
111
+ query.populate('attendees.external');
109
112
 
110
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
113
+ const result = await query;
114
+ return cb(null, result);
115
+ } catch(err) {
116
+ return cb(err);
117
+ }
111
118
 
112
119
  };
113
120
 
114
- Interaction.statics.getBySummaryAndStartTime = function getBySummaryAndStartTime(summary, startTime, cb) {
115
-
121
+ Interaction.statics.getBySummaryAndStartTime = async function getBySummaryAndStartTime(summary, startTime, cb) {
122
+
116
123
  if (!cb) { throw new Error('cb is required'); }
117
124
  if (!summary) { return cb(new Error('summary is required'), null); }
118
125
  if (!startTime) { return cb(new Error('startTime is required'), null); }
119
126
 
120
- const self = this;
127
+ try {
128
+ // Query to match summary directly
129
+ const query = this.find({
130
+ $and: [
131
+ { startTime: new Date(startTime) },
132
+ { summary: { $regex: summary, $options: 'i' } } // Simple case-insensitive regex match
133
+ ]
134
+ });
121
135
 
122
- // Query to match summary directly
123
- const query = self.find({
124
- $and: [
125
- { startTime: new Date(startTime) },
126
- { summary: { $regex: summary, $options: 'i' } } // Simple case-insensitive regex match
127
- ]
128
- });
136
+ // Populate attendees
137
+ query.populate('attendees.internal');
138
+ query.populate('attendees.external');
129
139
 
130
- // Populate attendees
131
- query.populate('attendees.internal');
132
- query.populate('attendees.external');
140
+ // Execute the query
141
+ const result = await query;
142
+ return cb(null, result);
143
+ } catch(err) {
144
+ return cb(err);
145
+ }
133
146
 
134
- // Execute the query
135
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
136
-
137
147
  };
138
148
 
139
- Interaction.statics.getByProviderEventId = function getByProviderEventId(providerEventId, cb) {
140
-
149
+ Interaction.statics.getByProviderEventId = async function getByProviderEventId(providerEventId, cb) {
150
+
141
151
  if (!cb) { throw new Error('cb is required'); }
142
152
  if (!providerEventId) { return cb(new Error('providerEventId is required'), null); }
143
153
 
144
- const self = this;
154
+ try {
155
+ const query = this.find({
156
+ providerEventId: providerEventId
157
+ });
145
158
 
146
- const query = self.find({
147
- providerEventId: providerEventId
148
- });
159
+ // Populate attendees
160
+ query.populate('attendees.internal');
161
+ query.populate('attendees.external');
149
162
 
150
- // Populate attendees
151
- query.populate('attendees.internal');
152
- query.populate('attendees.external');
163
+ // Execute the query
164
+ const result = await query;
165
+ return cb(null, result);
166
+ } catch(err) {
167
+ return cb(err);
168
+ }
153
169
 
154
- // Execute the query
155
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
156
-
157
170
  };
158
171
 
159
- Interaction.statics.getForCustomer = function getForCustomer(customerId, since, cb) {
172
+ Interaction.statics.getForCustomer = async function getForCustomer(customerId, since, cb) {
160
173
 
161
174
  if (!cb) { throw new Error('cb is required'); }
162
175
  if (!customerId) { return cb(new Error('customerId is required'), null); }
@@ -166,14 +179,18 @@ module.exports = function(mongoose, config) {
166
179
  var sinceObjectId = utilities.convertTimestampToObjectId(since);
167
180
  if (!mongoose.Types.ObjectId.isValid(sinceObjectId)) { return cb(new Error('sinceObjectId is not a valid ObjectId'), null); }
168
181
 
169
- var self = this;
170
- var query = self.find({ totemCustomerId: customerId, _id: { $gt: sinceObjectId } });
171
- query.populate('attendees.external');
172
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
182
+ try {
183
+ let query = this.find({ totemCustomerId: customerId, _id: { $gt: sinceObjectId } });
184
+ query.populate('attendees.external');
185
+ const result = await query;
186
+ return cb(null, result);
187
+ } catch(err) {
188
+ return cb(err);
189
+ }
173
190
 
174
191
  };
175
192
 
176
- Interaction.statics.getForCustomerSkinny = function getForCustomer(customerId, since, cb) {
193
+ Interaction.statics.getForCustomerSkinny = async function getForCustomer(customerId, since, cb) {
177
194
 
178
195
  if (!cb) { throw new Error('cb is required'); }
179
196
  if (!customerId) { return cb(new Error('customerId is required'), null); }
@@ -184,13 +201,17 @@ module.exports = function(mongoose, config) {
184
201
  var sinceObjectId = utilities.convertTimestampToObjectId(since);
185
202
  if (!mongoose.Types.ObjectId.isValid(sinceObjectId)) { return cb(new Error('sinceObjectId is not a valid ObjectId'), null); }
186
203
 
187
- var self = this;
188
- var query = self.find({ totemCustomerId: customerId, _id: { $gt: sinceObjectId } });
189
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
204
+ try {
205
+ let query = this.find({ totemCustomerId: customerId, _id: { $gt: sinceObjectId } });
206
+ const result = await query;
207
+ return cb(null, result);
208
+ } catch(err) {
209
+ return cb(err);
210
+ }
190
211
 
191
212
  };
192
213
 
193
- Interaction.statics.getForPeople = function getForPeople(personIds, options, cb) {
214
+ Interaction.statics.getForPeople = async function getForPeople(personIds, options, cb) {
194
215
 
195
216
  if (!cb) { throw new Error('cb is required'); }
196
217
  if (!personIds) { return cb(new Error('personIds is required'), null); }
@@ -200,8 +221,6 @@ module.exports = function(mongoose, config) {
200
221
 
201
222
  // NOTE: This is not for global process workers. Use getForPeopleLite instead.
202
223
 
203
- let self = this;
204
-
205
224
  options.limit = options.limit || -1;
206
225
  options.skip = options.skip || -1;
207
226
 
@@ -212,31 +231,34 @@ module.exports = function(mongoose, config) {
212
231
  options.before = options.before || moment().toISOString();
213
232
  options.after = options.after || moment().subtract(20, 'year').toISOString();
214
233
 
215
- let query = self.find({ totemCustomerId: options.CUSTOMER_ID });
234
+ try {
235
+ let query = this.find({ totemCustomerId: options.CUSTOMER_ID });
216
236
 
217
- query.where({ $or: [ { 'attendees.external': { $in: personIds } }, { 'attendees.internal': { $in: personIds } } ] });
218
- query.where({ startTime: { $gte: options.after }});
219
- query.where({ startTime: { $lte: options.before }});
237
+ query.where({ $or: [ { 'attendees.external': { $in: personIds } }, { 'attendees.internal': { $in: personIds } } ] });
238
+ query.where({ startTime: { $gte: options.after }});
239
+ query.where({ startTime: { $lte: options.before }});
220
240
 
221
- query.populate('attendees.internal', 'name title avatarUrl doNotDisplay');
222
- query.populate('attendees.external', 'name title avatarUrl doNotDisplay');
241
+ query.populate('attendees.internal', 'name title avatarUrl doNotDisplay');
242
+ query.populate('attendees.external', 'name title avatarUrl doNotDisplay');
223
243
 
224
- query.sort(options.sort);
244
+ query.sort(options.sort);
225
245
 
226
- if (options.attendee) { query.where({'attendees.internal': options.attendee}); }
227
- if (options.limit >= 0) query.limit(options.limit);
228
- if (options.skip >= 0) query.skip(options.skip);
246
+ if (options.attendee) { query.where({'attendees.internal': options.attendee}); }
247
+ if (options.limit >= 0) query.limit(options.limit);
248
+ if (options.skip >= 0) query.skip(options.skip);
229
249
 
230
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
250
+ const result = await query;
251
+ return cb(null, result);
252
+ } catch(err) {
253
+ return cb(err);
254
+ }
231
255
 
232
256
  };
233
257
 
234
- Interaction.statics.getForPeopleLite = function getForPeopleLite(personIds, options, cb) {
258
+ Interaction.statics.getForPeopleLite = async function getForPeopleLite(personIds, options, cb) {
235
259
 
236
260
  // This doesn't filter by customer so is only usable by admin tools
237
261
 
238
- let self = this;
239
-
240
262
  if (!cb) { throw new Error('cb is required'); }
241
263
  if (!personIds) { return cb(new Error('personIds is required'), null); }
242
264
  if (!options) { return cb(new Error('options is required'), null); }
@@ -245,56 +267,64 @@ module.exports = function(mongoose, config) {
245
267
 
246
268
  if (!options.isWorkerProcess) { return cb(null, []); }
247
269
 
248
- let query = self.find({'attendees.external': { $in : personIds }});
249
-
250
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
270
+ try {
271
+ let query = this.find({'attendees.external': { $in : personIds }});
272
+ const result = await query;
273
+ return cb(null, result);
274
+ } catch(err) {
275
+ return cb(err);
276
+ }
251
277
 
252
278
  };
253
279
 
254
- Interaction.statics.getForTimeline = function getForTimeline(customerId, options, cb) {
280
+ Interaction.statics.getForTimeline = async function getForTimeline(customerId, options, cb) {
255
281
 
256
282
  if (!cb) { throw new Error('cb is required'); }
257
283
  if (!customerId) { return cb(new Error('customerId is required'), null); }
258
284
  if (!mongoose.Types.ObjectId.isValid(customerId)) { return cb(new Error('customerId is not a valid ObjectId'), null); }
259
285
 
260
- const self = this;
261
286
  const startDate = options.startDate ? moment(options.startDate).toDate() : moment().subtract(20, 'years').startOf('day').toDate();
262
287
  const endDate = options.endDate ? moment(options.endDate).toDate() : new Date();
263
288
 
264
- self.aggregate([
265
- {
266
- $match: {
267
- totemCustomerId: new mongoose.Types.ObjectId(customerId),
268
- startTime: {
269
- $gte: startDate,
270
- $lte: endDate
289
+ try {
290
+ const result = await this.aggregate([
291
+ {
292
+ $match: {
293
+ totemCustomerId: new mongoose.Types.ObjectId(customerId),
294
+ startTime: {
295
+ $gte: startDate,
296
+ $lte: endDate
297
+ }
298
+ }
299
+ },
300
+ {
301
+ $sort: {
302
+ startTime: -1
303
+ }
304
+ },
305
+ {
306
+ $group: {
307
+ _id: "$providerEventId", // Group by providerEventId to deduplicate
308
+ doc: { $first: "$$ROOT" } // Take the first (latest) document encountered for each providerEventId after sorting
309
+ }
310
+ },
311
+ {
312
+ $replaceRoot: { newRoot: "$doc" } // Replace the root to output the original document structure
313
+ },
314
+ {
315
+ $sort: {
316
+ startTime: -1 // Final sort by startTime in descending order to order the output
271
317
  }
272
318
  }
273
- },
274
- {
275
- $sort: {
276
- startTime: -1
277
- }
278
- },
279
- {
280
- $group: {
281
- _id: "$providerEventId", // Group by providerEventId to deduplicate
282
- doc: { $first: "$$ROOT" } // Take the first (latest) document encountered for each providerEventId after sorting
283
- }
284
- },
285
- {
286
- $replaceRoot: { newRoot: "$doc" } // Replace the root to output the original document structure
287
- },
288
- {
289
- $sort: {
290
- startTime: -1 // Final sort by startTime in descending order to order the output
291
- }
292
- }
293
- ]).exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
319
+ ]);
320
+ return cb(null, result);
321
+ } catch(err) {
322
+ return cb(err);
323
+ }
294
324
 
295
325
  };
296
326
 
297
- Interaction.statics.getMostRecent = function getMostRecent(customerId, personIds, cb) {
327
+ Interaction.statics.getMostRecent = async function getMostRecent(customerId, personIds, cb) {
298
328
 
299
329
  // Gets the most recent interaction with a customer across a list of people
300
330
  // Typically used to get the last interaction the firm had with an org
@@ -307,13 +337,12 @@ module.exports = function(mongoose, config) {
307
337
 
308
338
  // Note - This filters by customer so is NOT usable by admin tools
309
339
 
310
- const self = this;
340
+ try {
341
+ let query = this.find({ totemCustomerId: customerId });
311
342
 
312
- let query = self.find({ totemCustomerId: customerId });
343
+ query.where({ 'attendees.external': { $in : personIds } });
313
344
 
314
- query.where({ 'attendees.external': { $in : personIds } });
315
-
316
- query.exec().then(function(interactions) {
345
+ let interactions = await query;
317
346
  if (!interactions) {
318
347
  return cb(null, null);
319
348
  }
@@ -327,14 +356,15 @@ module.exports = function(mongoose, config) {
327
356
  });
328
357
 
329
358
  var interaction = _.last(interactions);
330
-
331
- return cb(null, interaction);
332
359
 
333
- }).catch(function(err) { cb(err); });
360
+ return cb(null, interaction);
361
+ } catch(err) {
362
+ return cb(err);
363
+ }
334
364
 
335
365
  };
336
366
 
337
- Interaction.statics.getMostRecent2 = function getMostRecent2(customerId, personIds, timestamp, cb) {
367
+ Interaction.statics.getMostRecent2 = async function getMostRecent2(customerId, personIds, timestamp, cb) {
338
368
 
339
369
  // Gets the most recent interaction with a customer across a list of people
340
370
  // Typically used to get the last interaction the firm had with an org
@@ -348,28 +378,31 @@ module.exports = function(mongoose, config) {
348
378
 
349
379
  // Note - This filters by customer so is NOT usable by admin tools
350
380
 
351
- const self = this;
352
-
353
- // only for this customer
354
- let query = self.findOne({ totemCustomerId: customerId });
381
+ try {
382
+ // only for this customer
383
+ let query = this.findOne({ totemCustomerId: customerId });
355
384
 
356
- // with these outside attendees
357
- query.where({ 'attendees.external': { $in : personIds } });
385
+ // with these outside attendees
386
+ query.where({ 'attendees.external': { $in : personIds } });
358
387
 
359
- // only events that have already happened
360
- query.where({'startTime': { $lte: new Date() }});
388
+ // only events that have already happened
389
+ query.where({'startTime': { $lte: new Date() }});
361
390
 
362
- // Optionally limit to events newer than timestamp (useful for seeing if anything since a previous event)
363
- if (timestamp) query.where({'startTime': { $gt: timestamp }});
391
+ // Optionally limit to events newer than timestamp (useful for seeing if anything since a previous event)
392
+ if (timestamp) query.where({'startTime': { $gt: timestamp }});
364
393
 
365
- query.sort({ 'startTime': -1 });
366
- query.limit(1);
394
+ query.sort({ 'startTime': -1 });
395
+ query.limit(1);
367
396
 
368
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
397
+ const result = await query;
398
+ return cb(null, result);
399
+ } catch(err) {
400
+ return cb(err);
401
+ }
369
402
 
370
403
  };
371
404
 
372
- Interaction.statics.modifyById = function(id, update, cb) {
405
+ Interaction.statics.modifyById = async function(id, update, cb) {
373
406
 
374
407
  // VERY IMPORTANT NOTE
375
408
  // findByIdAndUpdate and findOneAndUpdate do not trigger pre-save hook so that code will not run here
@@ -379,21 +412,29 @@ module.exports = function(mongoose, config) {
379
412
  if (!mongoose.Types.ObjectId.isValid(id)) { return cb(new Error('id is not a valid ObjectId'), null); }
380
413
  if (!update) { return cb(new Error('update is required'), null); }
381
414
 
382
- var self = this;
383
-
384
415
  // options runValidators defaults false which is ok since we have upsert false
385
416
  // new returns the updated document
386
417
 
387
- self.findByIdAndUpdate(id, update, { upsert: false, new: true }).then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
418
+ try {
419
+ const result = await this.findByIdAndUpdate(id, update, { upsert: false, new: true });
420
+ return cb(null, result);
421
+ } catch(err) {
422
+ return cb(err);
423
+ }
388
424
 
389
425
  };
390
426
 
391
- Interaction.statics.upsert = function upsert(interaction, cb) {
427
+ Interaction.statics.upsert = async function upsert(interaction, cb) {
392
428
 
393
429
  if (!cb) { throw new Error('cb is required'); }
394
430
  if (!interaction) { return cb(new Error('Interaction is required'), null); }
395
431
 
396
- interaction.save().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
432
+ try {
433
+ const result = await interaction.save();
434
+ return cb(null, result);
435
+ } catch(err) {
436
+ return cb(err);
437
+ }
397
438
 
398
439
  };
399
440
  Interaction.set('autoIndex', false);
package/lib/Investment.js CHANGED
@@ -62,90 +62,99 @@ module.exports = function(mongoose, config) {
62
62
  // Statics operate on the entire collection
63
63
  //////////////////////////////////////////////////////
64
64
 
65
- Investment.statics.getOrgIds = function(options, cb) {
65
+ Investment.statics.getOrgIds = async function(options, cb) {
66
66
 
67
67
  if (!cb) { throw new Error('cb is required'); }
68
68
  if (!options) { return cb(new Error('options is required'), null); }
69
-
69
+
70
70
  if (!options.isWorkerProcess) {
71
71
  if (!options.CUSTOMER_ID) { return cb(new Error('options.CUSTOMER_ID is required'), null); }
72
72
  if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
73
73
  }
74
74
 
75
- var self = this;
76
- var query = self.find({ customer: options.CUSTOMER_ID });
75
+ try {
76
+ let query = this.find({ customer: options.CUSTOMER_ID });
77
77
 
78
- if (options.startDate) {
79
- var startDate = options.startDate || moment().subtract(20, 'years').startOf('day').toISOString();
80
- query.where('date').gte(startDate);
81
- }
78
+ if (options.startDate) {
79
+ var startDate = options.startDate || moment().subtract(20, 'years').startOf('day').toISOString();
80
+ query.where('date').gte(startDate);
81
+ }
82
82
 
83
- if (options.endDate) {
84
- var endDate = options.endDate || moment().toISOString();
85
- query.where('date').lte(endDate);
86
- }
83
+ if (options.endDate) {
84
+ var endDate = options.endDate || moment().toISOString();
85
+ query.where('date').lte(endDate);
86
+ }
87
87
 
88
- query.exec().then(function(investments) {Round.find({
89
- 'vehicles.investments': { $in: _.map(investments, function(inv) { return inv._id; }) }
90
- })
91
- .exec().then(function(rounds) {var organizationIds = _.compact(_.map(investments, function(inv) {
92
- var organization = null;
88
+ const investments = await query;
93
89
 
94
- var round = _.find(rounds, function(r) {
95
- return _.some(r.vehicles, function(v) {
96
- var investmentFound = _.some(v.investments, function(investmentId) {
97
- return investmentId.toString() === inv._id.toString();
98
- });
90
+ const rounds = await Round.find({
91
+ 'vehicles.investments': { $in: _.map(investments, function(inv) { return inv._id; }) }
92
+ });
99
93
 
100
- if (investmentFound) {
101
- organization = r.organization;
102
- }
94
+ var organizationIds = _.compact(_.map(investments, function(inv) {
95
+ var organization = null;
103
96
 
104
- return investmentFound;
97
+ var round = _.find(rounds, function(r) {
98
+ return _.some(r.vehicles, function(v) {
99
+ var investmentFound = _.some(v.investments, function(investmentId) {
100
+ return investmentId.toString() === inv._id.toString();
105
101
  });
102
+
103
+ if (investmentFound) {
104
+ organization = r.organization;
105
+ }
106
+
107
+ return investmentFound;
106
108
  });
109
+ });
110
+
111
+ return organization;
107
112
 
108
- return organization;
109
-
110
- }));
113
+ }));
114
+
115
+ return cb(null, organizationIds);
116
+ } catch(err) {
117
+ return cb(err);
118
+ }
111
119
 
112
- cb(null, organizationIds);
113
-
114
- }).catch(function(err) { cb(err); });
115
-
116
- }).catch(function(err) { cb(err); });
117
-
118
120
  };
119
121
 
120
- Investment.statics.removeCustomerInvestments = function removeCustomerInvestments(customerId, cb) {
122
+ Investment.statics.removeCustomerInvestments = async function removeCustomerInvestments(customerId, cb) {
121
123
 
122
124
  // Note that rounds hold references to investments, so when an investment is deleted we need
123
125
  // to also remove any references to it. Investment has a post-deleteOne hook which does that, but
124
126
  // post-deleteOne hooks only fire on document.deleteOne not query.deleteMany, hence the loop below
125
127
  // to call deleteOne on each matching investment.
126
128
 
127
- let self = this;
129
+ try {
130
+ const investments = await this
131
+ .find({ customer: customerId })
132
+ .select('_id');
128
133
 
129
- self
130
- .find({ customer: customerId })
131
- .select('_id')
132
- .exec().then(function(investments) {async.each(investments, function(investment, callback) {
133
- investment.deleteOne().then(function() { callback(null, investment); }).catch(function(err) { callback(err); });
134
- }, function(err) {
135
- return cb(err, null);
136
- });
134
+ for (const investment of investments) {
135
+ await investment.deleteOne();
136
+ }
137
137
 
138
- }).catch(function(err) { cb(err); });
138
+ return cb(null, null);
139
+ } catch(err) {
140
+ return cb(err);
141
+ }
139
142
 
140
143
  };
141
144
 
142
- Investment.statics.upsert = function upsert(investment, cb) {
145
+ Investment.statics.upsert = async function upsert(investment, cb) {
143
146
 
144
147
  // Required for mixed types
145
148
  investment.markModified('details');
146
149
 
147
150
  if (!investment) { return cb(new Error('investment is required'), null); }
148
- investment.save().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
151
+
152
+ try {
153
+ const result = await investment.save();
154
+ return cb(null, result);
155
+ } catch(err) {
156
+ return cb(err);
157
+ }
149
158
 
150
159
  };
151
160
 
@@ -181,4 +190,4 @@ module.exports = function(mongoose, config) {
181
190
 
182
191
  mongoose.model('Investment', Investment);
183
192
 
184
- };
193
+ };