@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.
package/lib/Message.js CHANGED
@@ -50,39 +50,44 @@ module.exports = function(mongoose, config) {
50
50
 
51
51
  };
52
52
 
53
- Message.statics.delete = function(messageId, customerId, cb) {
54
-
55
- const self = this;
56
-
57
- self.findOneAndDelete({
58
- '_id': messageId,
59
- 'customer': customerId
60
- }).then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
53
+ Message.statics.delete = async function(messageId, customerId, cb) {
54
+
55
+ try {
56
+ const result = await this.findOneAndDelete({
57
+ '_id': messageId,
58
+ 'customer': customerId
59
+ });
60
+ return cb(null, result);
61
+ } catch(err) {
62
+ return cb(err);
63
+ }
61
64
 
62
65
  };
63
66
 
64
- Message.statics.deleteDocument = function(docId, customerId, cb) {
67
+ Message.statics.deleteDocument = async function(docId, customerId, cb) {
65
68
 
66
69
  // Delete the doc itself along with any references
67
70
 
68
- const self = this;
71
+ try {
72
+ await new Promise((resolve, reject) => {
73
+ Document.delete({ CUSTOMER_ID: customerId }, docId, function(err, result) {
74
+ if (err) return reject(err);
75
+ resolve(result);
76
+ });
77
+ });
69
78
 
70
- let removeReferences = function removeReferences(callback) {
71
- self.updateMany({}, {
79
+ await this.updateMany({}, {
72
80
  $pull: { 'documents' : [docId] }
73
- }).then(function(result) { callback(null, result); }).catch(function(err) { callback(err); });
74
- };
81
+ });
75
82
 
76
- async.series([
77
- Document.delete.bind(Document, { CUSTOMER_ID: customerId }, docId),
78
- removeReferences
79
- ], function(err, results) {
83
+ return cb(null, null);
84
+ } catch(err) {
80
85
  return cb(err, null);
81
- });
86
+ }
82
87
 
83
88
  };
84
89
 
85
- Message.statics.getDocuments = function getDocuments(personIds, options, cb) {
90
+ Message.statics.getDocuments = async function getDocuments(personIds, options, cb) {
86
91
 
87
92
  if (!cb) { throw new Error('cb is required'); }
88
93
  if (!personIds) { return cb(new Error('personIds is required'), null); }
@@ -90,21 +95,25 @@ module.exports = function(mongoose, config) {
90
95
  if (!options.CUSTOMER_ID) { return cb(new Error('options.CUSTOMER_ID is required'), null); }
91
96
  if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
92
97
 
93
- const self = this;
94
- let query = self.find({ customer: options.CUSTOMER_ID });
98
+ try {
99
+ let query = this.find({ customer: options.CUSTOMER_ID });
95
100
 
96
- query.where({'attendees.external': { $in : personIds }});
97
- query.populate({
98
- path: 'documents',
99
- match: { customer: options.CUSTOMER_ID },
100
- populate: { path: 'createdBy', select: 'name avatarUrl title' }
101
- });
101
+ query.where({'attendees.external': { $in : personIds }});
102
+ query.populate({
103
+ path: 'documents',
104
+ match: { customer: options.CUSTOMER_ID },
105
+ populate: { path: 'createdBy', select: 'name avatarUrl title' }
106
+ });
102
107
 
103
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
108
+ const result = await query;
109
+ return cb(null, result);
110
+ } catch(err) {
111
+ return cb(err);
112
+ }
104
113
 
105
114
  };
106
115
 
107
- Message.statics.getById = function getById(id, options, cb) {
116
+ Message.statics.getById = async function getById(id, options, cb) {
108
117
 
109
118
  if (!cb) { throw new Error('cb is required'); }
110
119
  if (!id) { return cb(new Error('id is required'), null); }
@@ -113,50 +122,60 @@ module.exports = function(mongoose, config) {
113
122
  if (!options.CUSTOMER_ID) { return cb(new Error('options.CUSTOMER_ID is required'), null); }
114
123
  if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
115
124
 
116
- const self = this;
117
- let query = self.findOne({
118
- _id: id,
119
- customer: options.CUSTOMER_ID
120
- });
121
- query.populate('organization', 'name logoUrl');
122
- query.populate('recipients.internal', 'name avatarUrl title doNotDisplay');
123
- query.populate('recipients.external', 'name avatarUrl title doNotDisplay');
124
- query.populate({
125
- path: 'documents',
126
- match: { customer: options.CUSTOMER_ID }
127
- });
128
-
129
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
125
+ try {
126
+ let query = this.findOne({
127
+ _id: id,
128
+ customer: options.CUSTOMER_ID
129
+ });
130
+ query.populate('organization', 'name logoUrl');
131
+ query.populate('recipients.internal', 'name avatarUrl title doNotDisplay');
132
+ query.populate('recipients.external', 'name avatarUrl title doNotDisplay');
133
+ query.populate({
134
+ path: 'documents',
135
+ match: { customer: options.CUSTOMER_ID }
136
+ });
137
+
138
+ const result = await query;
139
+ return cb(null, result);
140
+ } catch(err) {
141
+ return cb(err);
142
+ }
130
143
 
131
144
  };
132
145
 
133
- Message.statics.getForCustomer = function getForCustomer(customerId, cb) {
146
+ Message.statics.getForCustomer = async function getForCustomer(customerId, cb) {
134
147
 
135
148
  if (!cb) { throw new Error('cb is required'); }
136
149
  if (!customerId) { return cb(new Error('customerId is required'), null); }
137
150
  if (!mongoose.Types.ObjectId.isValid(customerId)) { return cb(new Error('customerId is not a valid ObjectId'), null); }
138
151
 
139
- const self = this;
140
- let query = self.find({ customer: customerId });
141
-
142
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
152
+ try {
153
+ let query = this.find({ customer: customerId });
154
+ const result = await query;
155
+ return cb(null, result);
156
+ } catch(err) {
157
+ return cb(err);
158
+ }
143
159
 
144
160
  };
145
161
 
146
- Message.statics.getForDocument = function getForCustomer(docId, cb) {
162
+ Message.statics.getForDocument = async function getForCustomer(docId, cb) {
147
163
 
148
164
  if (!cb) { throw new Error('cb is required'); }
149
165
  if (!docId) { return cb(new Error('docId is required'), null); }
150
166
  if (!mongoose.Types.ObjectId.isValid(docId)) { return cb(new Error('docId is not a valid ObjectId'), null); }
151
167
 
152
- const self = this;
153
- let query = self.findOne({ documents: { $in: [docId] } });
154
-
155
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
168
+ try {
169
+ let query = this.findOne({ documents: { $in: [docId] } });
170
+ const result = await query;
171
+ return cb(null, result);
172
+ } catch(err) {
173
+ return cb(err);
174
+ }
156
175
 
157
176
  };
158
177
 
159
- Message.statics.getForOrg = function getForOrg(orgId, options, cb) {
178
+ Message.statics.getForOrg = async function getForOrg(orgId, options, cb) {
160
179
 
161
180
  if (!cb) { throw new Error('cb is required'); }
162
181
  if (!orgId) { return cb(new Error('orgId is required'), null); }
@@ -167,23 +186,26 @@ module.exports = function(mongoose, config) {
167
186
 
168
187
  // Note - This filters by customer so is NOT usable by admin tools
169
188
 
170
- const self = this;
171
-
172
- let query = self.find({ customer: options.CUSTOMER_ID, organization: orgId });
173
-
174
- query.populate('recipients.internal', 'name title avatarUrl doNotDisplay');
175
- query.populate('recipients.external', 'name title avatarUrl doNotDisplay');
176
- query.populate({
177
- path: 'documents',
178
- match: { customer: options.CUSTOMER_ID },
179
- populate: { path: 'createdBy', select: 'name avatarUrl title' }
180
- });
181
-
182
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
189
+ try {
190
+ let query = this.find({ customer: options.CUSTOMER_ID, organization: orgId });
191
+
192
+ query.populate('recipients.internal', 'name title avatarUrl doNotDisplay');
193
+ query.populate('recipients.external', 'name title avatarUrl doNotDisplay');
194
+ query.populate({
195
+ path: 'documents',
196
+ match: { customer: options.CUSTOMER_ID },
197
+ populate: { path: 'createdBy', select: 'name avatarUrl title' }
198
+ });
199
+
200
+ const result = await query;
201
+ return cb(null, result);
202
+ } catch(err) {
203
+ return cb(err);
204
+ }
183
205
 
184
206
  };
185
207
 
186
- Message.statics.getForOrgs = function(orgIds, options, cb) {
208
+ Message.statics.getForOrgs = async function(orgIds, options, cb) {
187
209
 
188
210
  if (!cb) { throw new Error('cb is required'); }
189
211
  if (!orgIds) { return cb(new Error('orgIds is required'), null); }
@@ -191,27 +213,30 @@ module.exports = function(mongoose, config) {
191
213
  if (!options.CUSTOMER_ID) { return cb(new Error('options.CUSTOMER_ID is required'), null); }
192
214
  if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
193
215
 
194
- const self = this;
195
-
196
- let query = self.find({ customer: options.CUSTOMER_ID });
197
- query.find({ organization: { $in : orgIds }});
198
-
199
- if (options.startDate) {
200
- const startDate = options.startDate || moment().subtract(20, 'years').startOf('day').toISOString();
201
- query.where('messageDate').gte(startDate)
202
- }
203
-
204
- if (options.endDate) {
205
- const endDate = options.endDate || moment().toISOString();
206
- query.where('messageDate').lte(endDate)
216
+ try {
217
+ let query = this.find({ customer: options.CUSTOMER_ID });
218
+ query.find({ organization: { $in : orgIds }});
219
+
220
+ if (options.startDate) {
221
+ const startDate = options.startDate || moment().subtract(20, 'years').startOf('day').toISOString();
222
+ query.where('messageDate').gte(startDate)
223
+ }
224
+
225
+ if (options.endDate) {
226
+ const endDate = options.endDate || moment().toISOString();
227
+ query.where('messageDate').lte(endDate)
228
+ }
229
+
230
+ query.populate('documents');
231
+ const result = await query;
232
+ return cb(null, result);
233
+ } catch(err) {
234
+ return cb(err);
207
235
  }
208
236
 
209
- query.populate('documents');
210
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
211
-
212
237
  };
213
238
 
214
- Message.statics.getMostRecent = function getMostRecent(orgId, personIds, options, cb) {
239
+ Message.statics.getMostRecent = async function getMostRecent(orgId, personIds, options, cb) {
215
240
 
216
241
  if (!cb) { throw new Error('cb is required'); }
217
242
  if (!orgId) { return cb(new Error('orgId is required'), null); }
@@ -222,29 +247,32 @@ module.exports = function(mongoose, config) {
222
247
 
223
248
  // Note - This filters by customer so is NOT usable by admin tools
224
249
 
225
- const self = this;
226
-
227
- // Must match customer
228
- let query = self.find({ customer: options.CUSTOMER_ID });
250
+ try {
251
+ // Must match customer
252
+ let query = this.find({ customer: options.CUSTOMER_ID });
229
253
 
230
- // Match either the org or a list of people
231
- query.where({ $or: [{organization: orgId}, {'recipients.external': { $in : personIds }}] });
254
+ // Match either the org or a list of people
255
+ query.where({ $or: [{organization: orgId}, {'recipients.external': { $in : personIds }}] });
232
256
 
233
- // Optionally limit search space to message newer than some timestamp
234
- if (options.timestamp) query.where({'messageDate': { $gte: options.timestamp }});
257
+ // Optionally limit search space to message newer than some timestamp
258
+ if (options.timestamp) query.where({'messageDate': { $gte: options.timestamp }});
235
259
 
236
- // Don't need the full raw message
237
- query.select({ 'raw': 0 });
260
+ // Don't need the full raw message
261
+ query.select({ 'raw': 0 });
238
262
 
239
- // Only need one
240
- query.sort({ 'messageDate': -1 });
241
- query.limit(1);
263
+ // Only need one
264
+ query.sort({ 'messageDate': -1 });
265
+ query.limit(1);
242
266
 
243
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
267
+ const result = await query;
268
+ return cb(null, result);
269
+ } catch(err) {
270
+ return cb(err);
271
+ }
244
272
 
245
273
  };
246
274
 
247
- Message.statics.getMostRecent2 = function getMostRecent2(orgId, personIds, options, cb) {
275
+ Message.statics.getMostRecent2 = async function getMostRecent2(orgId, personIds, options, cb) {
248
276
 
249
277
  if (!cb) { throw new Error('cb is required'); }
250
278
  if (!orgId) { return cb(new Error('orgId is required'), null); }
@@ -255,33 +283,36 @@ module.exports = function(mongoose, config) {
255
283
 
256
284
  // Note - This filters by customer so is NOT usable by admin tools
257
285
 
258
- const self = this;
259
-
260
- // Must match customer
261
- let query = self.findOne({ customer: options.CUSTOMER_ID });
286
+ try {
287
+ // Must match customer
288
+ let query = this.findOne({ customer: options.CUSTOMER_ID });
262
289
 
263
- personIds = personIds || [];
290
+ personIds = personIds || [];
264
291
 
265
- // Match either the org or a list of people
266
- if (personIds.length >= 1) { query.where({ $or: [{organization: orgId}, {'recipients.external': { $in : personIds }}] }); }
292
+ // Match either the org or a list of people
293
+ if (personIds.length >= 1) { query.where({ $or: [{organization: orgId}, {'recipients.external': { $in : personIds }}] }); }
267
294
 
268
- // No people, just do the org
269
- else { query.where({ organization: orgId }); }
295
+ // No people, just do the org
296
+ else { query.where({ organization: orgId }); }
270
297
 
271
- // Optionally limit search space to message newer than some timestamp
272
- if (options.timestamp) query.where({'messageDate': { $gt: options.timestamp }});
298
+ // Optionally limit search space to message newer than some timestamp
299
+ if (options.timestamp) query.where({'messageDate': { $gt: options.timestamp }});
273
300
 
274
- // Don't need the full raw message
275
- query.select({ 'raw': 0 });
301
+ // Don't need the full raw message
302
+ query.select({ 'raw': 0 });
276
303
 
277
- query.sort({ 'startTime': -1 });
278
- query.limit(1);
304
+ query.sort({ 'startTime': -1 });
305
+ query.limit(1);
279
306
 
280
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
307
+ const result = await query;
308
+ return cb(null, result);
309
+ } catch(err) {
310
+ return cb(err);
311
+ }
281
312
 
282
313
  };
283
314
 
284
- Message.statics.getForPeople = function getForPeople(personIds, options, cb) {
315
+ Message.statics.getForPeople = async function getForPeople(personIds, options, cb) {
285
316
 
286
317
  if (!cb) { throw new Error('cb is required'); }
287
318
  if (!personIds) { return cb(new Error('personIds is required'), null); }
@@ -291,24 +322,27 @@ module.exports = function(mongoose, config) {
291
322
 
292
323
  // Note: This filters by customer so is NOT usable by admin tools
293
324
 
294
- const self = this;
295
-
296
- let query = self.find({ customer: options.CUSTOMER_ID });
297
-
298
- query.where({'recipients.external': { $in : personIds }});
299
- query.populate('recipients.internal', 'name title avatarUrl doNotDisplay');
300
- query.populate('recipients.external', 'name title avatarUrl doNotDisplay');
301
- query.populate({
302
- path: 'documents',
303
- match: { customer: options.CUSTOMER_ID },
304
- populate: { path: 'createdBy', select: 'name avatarUrl title' }
305
- });
306
-
307
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
325
+ try {
326
+ let query = this.find({ customer: options.CUSTOMER_ID });
327
+
328
+ query.where({'recipients.external': { $in : personIds }});
329
+ query.populate('recipients.internal', 'name title avatarUrl doNotDisplay');
330
+ query.populate('recipients.external', 'name title avatarUrl doNotDisplay');
331
+ query.populate({
332
+ path: 'documents',
333
+ match: { customer: options.CUSTOMER_ID },
334
+ populate: { path: 'createdBy', select: 'name avatarUrl title' }
335
+ });
336
+
337
+ const result = await query;
338
+ return cb(null, result);
339
+ } catch(err) {
340
+ return cb(err);
341
+ }
308
342
 
309
343
  };
310
344
 
311
- Message.statics.getForPeopleLite = function getForPeopleLite(personIds, options, cb) {
345
+ Message.statics.getForPeopleLite = async function getForPeopleLite(personIds, options, cb) {
312
346
 
313
347
  // Note - This doesn't filter by customer so is only usable by admin tools
314
348
 
@@ -319,49 +353,59 @@ module.exports = function(mongoose, config) {
319
353
 
320
354
  if (!options.isWorkerProcess) { return cb(null, []); }
321
355
 
322
- const self = this;
323
-
324
- let query = self.find({'recipients.external': { $in : personIds }});
325
-
326
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
356
+ try {
357
+ let query = this.find({'recipients.external': { $in : personIds }});
358
+ const result = await query;
359
+ return cb(null, result);
360
+ } catch(err) {
361
+ return cb(err);
362
+ }
327
363
 
328
364
  };
329
365
 
330
- Message.statics.getUnprocessedDeals = function getUnprocessedDeals(customerId, cb) {
331
-
332
- const self = this;
333
- let query = self.find({
334
- customer: customerId,
335
- subtype: 'deals',
336
- processed: { $ne: true }
337
- });
338
- query.populate({
339
- path: 'documents',
340
- match: { customer: customerId }
341
- });
342
-
343
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
366
+ Message.statics.getUnprocessedDeals = async function getUnprocessedDeals(customerId, cb) {
367
+
368
+ try {
369
+ let query = this.find({
370
+ customer: customerId,
371
+ subtype: 'deals',
372
+ processed: { $ne: true }
373
+ });
374
+ query.populate({
375
+ path: 'documents',
376
+ match: { customer: customerId }
377
+ });
378
+
379
+ const result = await query;
380
+ return cb(null, result);
381
+ } catch(err) {
382
+ return cb(err);
383
+ }
344
384
 
345
385
  };
346
386
 
347
- Message.statics.getUnprocessedPortfolio = function getUnprocessedPortfolio(customerId, cb) {
348
-
349
- const self = this;
350
- let query = self.find({
351
- customer: customerId,
352
- subtype: 'portfolio',
353
- processed: { $ne: true }
354
- });
355
- query.populate({
356
- path: 'documents',
357
- match: { customer: customerId }
358
- });
359
-
360
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
387
+ Message.statics.getUnprocessedPortfolio = async function getUnprocessedPortfolio(customerId, cb) {
388
+
389
+ try {
390
+ let query = this.find({
391
+ customer: customerId,
392
+ subtype: 'portfolio',
393
+ processed: { $ne: true }
394
+ });
395
+ query.populate({
396
+ path: 'documents',
397
+ match: { customer: customerId }
398
+ });
399
+
400
+ const result = await query;
401
+ return cb(null, result);
402
+ } catch(err) {
403
+ return cb(err);
404
+ }
361
405
 
362
406
  };
363
407
 
364
- Message.statics.modifyById = function(id, update, cb) {
408
+ Message.statics.modifyById = async function(id, update, cb) {
365
409
 
366
410
  // VERY IMPORTANT NOTE
367
411
  // findByIdAndUpdate and findOneAndUpdate do not trigger pre-save hook so that code will not run here
@@ -370,16 +414,19 @@ module.exports = function(mongoose, config) {
370
414
  if (!id) { return cb(new Error('id is required'), null); }
371
415
  if (!update) { return cb(new Error('update is required'), null); }
372
416
 
373
- const self = this;
374
-
375
417
  // options runValidators defaults false which is ok since we have upsert false
376
418
  // new returns the updated document
377
419
 
378
- self.findByIdAndUpdate(id, update, { upsert: false, new: true }).then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
420
+ try {
421
+ const result = await this.findByIdAndUpdate(id, update, { upsert: false, new: true });
422
+ return cb(null, result);
423
+ } catch(err) {
424
+ return cb(err);
425
+ }
379
426
 
380
427
  };
381
428
 
382
- Message.statics.upsert = function(message, cb) {
429
+ Message.statics.upsert = async function(message, cb) {
383
430
 
384
431
  const timestamp = new Date();
385
432
 
@@ -392,7 +439,12 @@ module.exports = function(mongoose, config) {
392
439
  // The original message date isn't always contained in the email. In its absence, use created on.
393
440
  message.messageDate = message.messageDate || message.originalMessageDate || message.createdOn;
394
441
 
395
- message.save().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
442
+ try {
443
+ const result = await message.save();
444
+ return cb(null, result);
445
+ } catch(err) {
446
+ return cb(err);
447
+ }
396
448
 
397
449
  };
398
450
 
@@ -402,4 +454,4 @@ module.exports = function(mongoose, config) {
402
454
 
403
455
  mongoose.model('Message', Message);
404
456
 
405
- };
457
+ };
@@ -16,7 +16,7 @@ module.exports = function(mongoose, config) {
16
16
 
17
17
  ///////////////////////////////////////
18
18
 
19
- MessageRecipient.statics.getMostRecent = function getMostRecent(orgId, customerId, personId, options, cb) {
19
+ MessageRecipient.statics.getMostRecent = async function getMostRecent(orgId, customerId, personId, options, cb) {
20
20
 
21
21
  if (!cb) { throw new Error('cb is required'); }
22
22
  if (!orgId) { return cb(new Error('orgId is required'), null); }
@@ -26,28 +26,31 @@ module.exports = function(mongoose, config) {
26
26
  if (!personId) { return cb(new Error('personId is required'), null); }
27
27
  if (!mongoose.Types.ObjectId.isValid(personId)) { return cb(new Error('personId is not a valid ObjectId'), null); }
28
28
 
29
- const self = this;
30
-
31
29
  options = options || {};
32
30
 
33
- let query = self.find({
34
- customer: options.CUSTOMER_ID,
35
- organization: orgId,
36
- recipient: personId
37
- });
31
+ try {
32
+ let query = this.find({
33
+ customer: options.CUSTOMER_ID,
34
+ organization: orgId,
35
+ recipient: personId
36
+ });
38
37
 
39
- // Optionally limit search space to message newer than some timestamp
40
- if (options.timestamp) query.where({'messageDate': { $gte: options.timestamp }});
38
+ // Optionally limit search space to message newer than some timestamp
39
+ if (options.timestamp) query.where({'messageDate': { $gte: options.timestamp }});
41
40
 
42
- // Only need one
43
- query.sort({ 'messageDate': -1 });
44
- query.limit(1);
41
+ // Only need one
42
+ query.sort({ 'messageDate': -1 });
43
+ query.limit(1);
45
44
 
46
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
45
+ const result = await query;
46
+ return cb(null, result);
47
+ } catch(err) {
48
+ return cb(err);
49
+ }
47
50
 
48
51
  };
49
52
 
50
- MessageRecipient.statics.getMostRecentBatch = function getMostRecentBatch(orgId, customerId, personIds, options, cb) {
53
+ MessageRecipient.statics.getMostRecentBatch = async function getMostRecentBatch(orgId, customerId, personIds, options, cb) {
51
54
 
52
55
  if (!cb) { throw new Error('cb is required'); }
53
56
  if (!orgId) { return cb(new Error('orgId is required'), null); }
@@ -57,34 +60,42 @@ module.exports = function(mongoose, config) {
57
60
  if (!personIds) { return cb(new Error('personIds is required'), null); }
58
61
  if (!Array.isArray(personIds)) { return cb(new Error('personIds must be an array'), null); }
59
62
 
60
- const self = this;
61
-
62
63
  options = options || {};
63
64
 
64
- let query = self.find({
65
- customer: options.CUSTOMER_ID,
66
- organization: orgId
67
- });
65
+ try {
66
+ let query = this.find({
67
+ customer: options.CUSTOMER_ID,
68
+ organization: orgId
69
+ });
68
70
 
69
- query.where({ 'recipient': { $in : personIds }});
71
+ query.where({ 'recipient': { $in : personIds }});
70
72
 
71
- // Optionally limit search space to message newer than some timestamp
72
- if (options.timestamp) query.where({'messageDate': { $gte: options.timestamp }});
73
+ // Optionally limit search space to message newer than some timestamp
74
+ if (options.timestamp) query.where({'messageDate': { $gte: options.timestamp }});
73
75
 
74
- // Only need one
75
- query.sort({ 'messageDate': -1 });
76
- query.limit(1);
76
+ // Only need one
77
+ query.sort({ 'messageDate': -1 });
78
+ query.limit(1);
77
79
 
78
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
80
+ const result = await query;
81
+ return cb(null, result);
82
+ } catch(err) {
83
+ return cb(err);
84
+ }
79
85
 
80
86
  };
81
87
 
82
- MessageRecipient.statics.upsert = function(message, cb) {
83
- message.save().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
88
+ MessageRecipient.statics.upsert = async function(message, cb) {
89
+ try {
90
+ const result = await message.save();
91
+ return cb(null, result);
92
+ } catch(err) {
93
+ return cb(err);
94
+ }
84
95
  };
85
96
 
86
97
  ///////////////////////////////////////
87
98
 
88
99
  mongoose.model('MessageRecipient', MessageRecipient);
89
100
 
90
- };
101
+ };