@dhyasama/totem-models 12.4.0 → 12.6.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/helpers.js CHANGED
@@ -134,16 +134,3 @@ const getPeopleSources = module.exports.getPeopleSources = function getPeopleSou
134
134
 
135
135
  };
136
136
 
137
- const sortPeopleSources = module.exports.sortPeopleSources = function sortPeopleSources(items, customerId) {
138
-
139
- items = _.sortBy(items, function(item) {
140
- return item.interactionsCount(customerId);
141
- });
142
-
143
- items.reverse();
144
-
145
- items = _.sortBy(items, 'name.full');
146
-
147
- return items;
148
-
149
- };
package/lib/Account.js CHANGED
@@ -93,66 +93,61 @@ module.exports = function(mongoose, config) {
93
93
  return self.totemLive && self.totemLive.sheetsUser;
94
94
  });
95
95
 
96
- Account.statics.findByEmail = async function(email, options, cb) {
96
+ Account.statics.findByEmail = function(email, options, cb) {
97
97
 
98
- if (!cb) { throw new Error('cb is required'); }
99
- if (!email) { return cb(new Error('email is required'), null); }
98
+ const run = async () => {
99
+ if (!email) { throw new Error('email is required'); }
100
100
 
101
- options = helpers.getDefaultOptions(options);
101
+ options = helpers.getDefaultOptions(options);
102
102
 
103
- let query = { email: email.toLowerCase() };
103
+ let query = { email: email.toLowerCase() };
104
104
 
105
- if (options.current !== undefined || options.customerId !== undefined) {
105
+ if (options.current !== undefined || options.customerId !== undefined) {
106
106
 
107
- if (options.current !== undefined) { query.current = options.current; }
108
- if (options.customerId !== undefined) { query.org = options.customerId; }
107
+ if (options.current !== undefined) { query.current = options.current; }
108
+ if (options.customerId !== undefined) { query.org = options.customerId; }
109
109
 
110
- try {
111
- const account = await this.findOne(query).populate('person');
112
- if (!account) { return cb(null, null); }
110
+ const account = await this.findOne(query).populate('person').lean({ virtuals: true });
111
+ if (!account) { return null; }
113
112
 
114
- if (account.populated('person')) {
113
+ if (account.person && typeof account.person === 'object') {
115
114
 
116
115
  if (account.org) { helpers.cleanPerson(account.person, account.org); }
117
116
  else { account.person.sources = []; }
118
117
 
119
118
  }
120
119
 
121
- return cb(null, account);
122
- } catch(err) {
123
- return cb(err);
124
- }
120
+ return account;
125
121
 
126
- }
122
+ }
127
123
 
128
- else {
124
+ else {
129
125
 
130
- try {
131
126
  const result = await this.find(query).populate({
132
127
  path: 'org',
133
128
  select: 'name'
134
- });
135
- return cb(null, result);
136
- } catch(err) {
137
- return cb(err);
129
+ }).lean({ virtuals: true });
130
+ return result;
131
+
138
132
  }
133
+ };
139
134
 
140
- }
135
+ if (typeof cb === 'function') { run().then(result => cb(null, result)).catch(cb); return; }
136
+ else { return run(); }
141
137
 
142
138
  };
143
139
 
144
- Account.statics.findByEmails = async function(emails, cb) {
140
+ Account.statics.findByEmails = function(emails, cb) {
145
141
 
146
- if (!cb) { throw new Error('cb is required'); }
147
- if (!emails) { return cb(new Error('email is required'), null); }
142
+ const run = async () => {
143
+ if (!emails) { throw new Error('email is required'); }
148
144
 
149
- try {
150
- const accounts = await this.find({ 'email': { $in : emails }}).populate('person');
151
- if (!accounts) { return cb(null, null); }
145
+ const accounts = await this.find({ 'email': { $in : emails }}).populate('person').lean({ virtuals: true });
146
+ if (!accounts) { return null; }
152
147
 
153
148
  _.each(accounts, function(account) {
154
149
 
155
- if (account.populated('person')) {
150
+ if (account.person && typeof account.person === 'object') {
156
151
 
157
152
  if (account.org) { helpers.cleanPerson(account.person, account.org); }
158
153
  else { account.person.sources = []; }
@@ -161,251 +156,247 @@ module.exports = function(mongoose, config) {
161
156
 
162
157
  });
163
158
 
164
- return cb(null, accounts);
165
- } catch(err) {
166
- return cb(err);
167
- }
159
+ return accounts;
160
+ };
161
+
162
+ if (typeof cb === 'function') { run().then(result => cb(null, result)).catch(cb); return; }
163
+ else { return run(); }
168
164
 
169
165
  };
170
166
 
171
- Account.statics.getById = async function (id, cb) {
167
+ Account.statics.getById = function (id, cb) {
172
168
 
173
- if (!cb) { throw new Error('cb is required'); }
174
- if (!id) { throw new Error('id is required'); }
175
- if (!mongoose.Types.ObjectId.isValid(id)) { return cb(new Error('id is not a valid ObjectId'), null); }
169
+ const run = async () => {
170
+ if (!id) { throw new Error('id is required'); }
171
+ if (!mongoose.Types.ObjectId.isValid(id)) { throw new Error('id is not a valid ObjectId'); }
176
172
 
177
- let query = this.findById(id);
173
+ let query = this.findById(id);
178
174
 
179
- query.populate('person');
180
- query.populate('org');
175
+ query.populate('person');
176
+ query.populate('org');
181
177
 
182
- try {
183
- const account = await query;
184
- if (account && account.populated('person') && account.populated('org')) {
178
+ const account = await query.lean({ virtuals: true });
179
+ if (account && account.person && typeof account.person === 'object' && account.org && typeof account.org === 'object') {
185
180
 
186
181
  // Scrub customer info before returning
187
182
 
188
- let customerId = account.populated('org');
183
+ let customerId = account.org._id;
189
184
 
190
- if (account.populated('org')) { helpers.cleanOrg(account.org, customerId); }
185
+ if (account.org && typeof account.org === 'object') { helpers.cleanOrg(account.org, customerId); }
191
186
 
192
- if (account.populated('person')) { helpers.cleanPerson(account.person, customerId); }
187
+ if (account.person && typeof account.person === 'object') { helpers.cleanPerson(account.person, customerId); }
193
188
 
194
- return cb(null, account);
189
+ return account;
195
190
 
196
191
  }
197
192
  else if (!account) {
198
- return cb(new Error('No account with id ' + id.toString()), null);
193
+ throw new Error('No account with id ' + id.toString());
199
194
  }
200
195
  else if (account && !account.person) {
201
- return cb(new Error('Missing person reference on account with id ' + id.toString()), null);
196
+ throw new Error('Missing person reference on account with id ' + id.toString());
202
197
  }
203
198
  else if (account && !account.org) {
204
- return cb(new Error('Missing org reference on account with id ' + id.toString()), null);
199
+ throw new Error('Missing org reference on account with id ' + id.toString());
205
200
  }
206
201
  else {
207
- return cb(new Error('Unknown error'), null);
202
+ throw new Error('Unknown error');
208
203
  }
209
- } catch(err) {
210
- return cb(err);
211
- }
204
+ };
205
+
206
+ if (typeof cb === 'function') { run().then(result => cb(null, result)).catch(cb); return; }
207
+ else { return run(); }
212
208
 
213
209
  };
214
210
 
215
- Account.statics.getForCustomer = async function (customerId, cb) {
211
+ Account.statics.getForCustomer = function (customerId, cb) {
216
212
 
217
213
  // No population so no need to scrub
218
214
 
219
- if (!cb) { throw new Error('cb is required'); }
220
- if (!customerId) { return cb(new Error('customerId is required'), null); }
221
- if (!mongoose.Types.ObjectId.isValid(customerId)) { return cb(new Error('customerId is not a valid ObjectId'), null); }
215
+ const run = async () => {
216
+ if (!customerId) { throw new Error('customerId is required'); }
217
+ if (!mongoose.Types.ObjectId.isValid(customerId)) { throw new Error('customerId is not a valid ObjectId'); }
222
218
 
223
- try {
224
- const result = await this.find({ 'org': customerId });
225
- return cb(null, result);
226
- } catch(err) {
227
- return cb(err);
228
- }
219
+ const result = await this.find({ 'org': customerId }).lean({ virtuals: true });
220
+ return result;
221
+ };
222
+
223
+ if (typeof cb === 'function') { run().then(result => cb(null, result)).catch(cb); return; }
224
+ else { return run(); }
229
225
 
230
226
  };
231
227
 
232
- Account.statics.updateCurrent = async function(email, customerId, cb) {
228
+ Account.statics.updateCurrent = function(email, customerId, cb) {
233
229
 
234
- if (!cb) { throw new Error('cb is required'); }
235
- if (!email) { return cb(new Error('email is required'), null); }
236
- if (!customerId) { return cb(new Error('customerId is required'), null); }
237
- if (!mongoose.Types.ObjectId.isValid(customerId)) { return cb(new Error('customerId is not a valid ObjectId'), null); }
230
+ const run = async () => {
231
+ if (!email) { throw new Error('email is required'); }
232
+ if (!customerId) { throw new Error('customerId is required'); }
233
+ if (!mongoose.Types.ObjectId.isValid(customerId)) { throw new Error('customerId is not a valid ObjectId'); }
238
234
 
239
- try {
240
235
  await this.updateMany({ email: email }, { $set: { current: false } });
241
236
  const res = await this.updateOne({ email: email, org: customerId }, { $set: { current: true } });
242
- return cb(null, res);
243
- } catch(err) {
244
- return cb(err);
245
- }
237
+ return res;
238
+ };
239
+
240
+ if (typeof cb === 'function') { run().then(result => cb(null, result)).catch(cb); return; }
241
+ else { return run(); }
246
242
 
247
243
  };
248
244
 
249
- Account.statics.updateOrg = async function(email, customerId, cb) {
245
+ Account.statics.updateOrg = function(email, customerId, cb) {
250
246
 
251
- if (!cb) { throw new Error('cb is required'); }
252
- if (!email) { return cb(new Error('email is required'), null); }
253
- if (!customerId) { return cb(new Error('customerId is required'), null); }
254
- if (!mongoose.Types.ObjectId.isValid(customerId)) { return cb(new Error('customerId is not a valid ObjectId'), null); }
247
+ const run = async () => {
248
+ if (!email) { throw new Error('email is required'); }
249
+ if (!customerId) { throw new Error('customerId is required'); }
250
+ if (!mongoose.Types.ObjectId.isValid(customerId)) { throw new Error('customerId is not a valid ObjectId'); }
255
251
 
256
- try {
257
252
  const res = await this.updateOne({ email: email }, { $set: { org: customerId } });
258
- return cb(null, res);
259
- } catch(err) {
260
- return cb(err);
261
- }
253
+ return res;
254
+ };
255
+
256
+ if (typeof cb === 'function') { run().then(result => cb(null, result)).catch(cb); return; }
257
+ else { return run(); }
262
258
 
263
259
  };
264
260
 
265
- Account.statics.listAdminAccounts = async function (options, cb) {
261
+ Account.statics.listAdminAccounts = function (options, cb) {
266
262
 
267
- if (!cb) { throw new Error('cb is required'); }
263
+ const run = async () => {
264
+ const result = await this.find({ 'active': true, 'role': 'admin' }).lean({ virtuals: true });
265
+ return result;
266
+ };
268
267
 
269
- try {
270
- const result = await this.find({ 'active': true, 'role': 'admin' });
271
- return cb(null, result);
272
- } catch(err) {
273
- return cb(err);
274
- }
268
+ if (typeof cb === 'function') { run().then(result => cb(null, result)).catch(cb); return; }
269
+ else { return run(); }
275
270
 
276
271
  };
277
272
 
278
- Account.statics.listSummaryAccounts = async function (options, cb) {
273
+ Account.statics.listSummaryAccounts = function (options, cb) {
279
274
 
280
- if (!cb) { throw new Error('cb is required'); }
275
+ const run = async () => {
276
+ let queryObj = { 'active': true, 'summary.active': true };
281
277
 
282
- let queryObj = { 'active': true, 'summary.active': true };
278
+ if (options.CUSTOMER_ID) {
279
+ queryObj['org'] = options.CUSTOMER_ID;
280
+ }
283
281
 
284
- if (options.CUSTOMER_ID) {
285
- queryObj['org'] = options.CUSTOMER_ID;
286
- }
282
+ const result = await this.find(queryObj).lean({ virtuals: true });
283
+ return result;
284
+ };
287
285
 
288
- try {
289
- const result = await this.find(queryObj);
290
- return cb(null, result);
291
- } catch(err) {
292
- return cb(err);
293
- }
286
+ if (typeof cb === 'function') { run().then(result => cb(null, result)).catch(cb); return; }
287
+ else { return run(); }
294
288
 
295
289
  };
296
290
 
297
- Account.statics.listAllAccounts = async function (options, cb) {
291
+ Account.statics.listAllAccounts = function (options, cb) {
298
292
 
299
293
  // This is only for worker
300
294
  // Since it's only worker, no need to scrub results
301
295
 
302
- if (!cb) { throw new Error('cb is required'); }
296
+ const run = async () => {
297
+ options = helpers.getDefaultOptions(options);
303
298
 
304
- options = helpers.getDefaultOptions(options);
299
+ if (!options.isWorkerProcess) { return []; }
305
300
 
306
- if (!options.isWorkerProcess) { return cb(null, []); }
301
+ const result = await this.find({'active': true}).populate('person').sort({'person.name.last': 1}).lean({ virtuals: true });
302
+ return result;
303
+ };
307
304
 
308
- try {
309
- const result = await this.find({'active': true}).populate('person').sort({'person.name.last': 1});
310
- return cb(null, result);
311
- } catch(err) {
312
- return cb(err);
313
- }
305
+ if (typeof cb === 'function') { run().then(result => cb(null, result)).catch(cb); return; }
306
+ else { return run(); }
314
307
 
315
308
  };
316
309
 
317
- Account.statics.listAllAccountsSkinny = async function (options, cb) {
310
+ Account.statics.listAllAccountsSkinny = function (options, cb) {
318
311
 
319
- if (!cb) { throw new Error('cb is required'); }
312
+ const run = async () => {
313
+ options = helpers.getDefaultOptions(options);
320
314
 
321
- options = helpers.getDefaultOptions(options);
315
+ if (!options.isWorkerProcess) { return []; }
322
316
 
323
- if (!options.isWorkerProcess) { return cb(null, []); }
317
+ const result = await this.find({'active': true}).select('active email auth').lean({ virtuals: true });
318
+ return result;
319
+ };
324
320
 
325
- try {
326
- const result = await this.find({'active': true}).select('active email auth');
327
- return cb(null, result);
328
- } catch(err) {
329
- return cb(err);
330
- }
321
+ if (typeof cb === 'function') { run().then(result => cb(null, result)).catch(cb); return; }
322
+ else { return run(); }
331
323
 
332
324
  };
333
325
 
334
- Account.statics.listGoogleAccounts = async function(options, cb) {
326
+ Account.statics.listGoogleAccounts = function(options, cb) {
335
327
 
336
328
  // This is only for worker
337
329
  // Since it's only worker, no need to scrub results
338
330
 
339
- if (!cb) { throw new Error('cb is required'); }
331
+ const run = async () => {
332
+ options = helpers.getDefaultOptions(options);
340
333
 
341
- options = helpers.getDefaultOptions(options);
334
+ if (!options.isWorkerProcess) { return []; }
342
335
 
343
- if (!options.isWorkerProcess) { return cb(null, []); }
344
-
345
- try {
346
336
  const result = await this.find({
347
337
  'active': true,
348
338
  'calendar.active': true,
349
339
  'auth.strategy': 'google',
350
340
  'auth.google.refreshToken': { $ne: null }
351
- });
352
- return cb(null, result);
353
- } catch(err) {
354
- return cb(err);
355
- }
341
+ }).lean({ virtuals: true });
342
+ return result;
343
+ };
344
+
345
+ if (typeof cb === 'function') { run().then(result => cb(null, result)).catch(cb); return; }
346
+ else { return run(); }
356
347
 
357
348
  };
358
349
 
359
- Account.statics.listMicrosoftAccounts = async function(options, cb) {
350
+ Account.statics.listMicrosoftAccounts = function(options, cb) {
360
351
 
361
352
  // This is only for worker
362
353
  // Since it's only worker, no need to scrub results
363
354
 
364
- if (!cb) { throw new Error('cb is required'); }
355
+ const run = async () => {
356
+ options = helpers.getDefaultOptions(options);
365
357
 
366
- options = helpers.getDefaultOptions(options);
358
+ if (!options.isWorkerProcess) { return []; }
367
359
 
368
- if (!options.isWorkerProcess) { return cb(null, []); }
369
-
370
- try {
371
360
  const result = await this.find({
372
361
  'active': true,
373
362
  'calendar.active': true,
374
363
  'auth.strategy': 'microsoft',
375
364
  'auth.microsoft.refreshToken': { $ne: null }
376
- });
377
- return cb(null, result);
378
- } catch(err) {
379
- return cb(err);
380
- }
365
+ }).lean({ virtuals: true });
366
+ return result;
367
+ };
368
+
369
+ if (typeof cb === 'function') { run().then(result => cb(null, result)).catch(cb); return; }
370
+ else { return run(); }
381
371
 
382
372
  };
383
373
 
384
- Account.statics.delete = async function (id, options, cb) {
374
+ Account.statics.delete = function (id, options, cb) {
385
375
 
386
- if (!cb) { throw new Error('cb is required'); }
387
- if (!id) { return cb(new Error('id is required'), null); }
388
- if (!mongoose.Types.ObjectId.isValid(id)) { return cb(new Error('id is not a valid ObjectId'), null); }
376
+ const run = async () => {
377
+ if (!id) { throw new Error('id is required'); }
378
+ if (!mongoose.Types.ObjectId.isValid(id)) { throw new Error('id is not a valid ObjectId'); }
389
379
 
390
- try {
391
380
  const result = await this.findByIdAndDelete(id);
392
- return cb(null, result);
393
- } catch(err) {
394
- return cb(err);
395
- }
381
+ return result;
382
+ };
383
+
384
+ if (typeof cb === 'function') { run().then(result => cb(null, result)).catch(cb); return; }
385
+ else { return run(); }
396
386
 
397
387
  };
398
388
 
399
- Account.statics.upsert = async function(account, cb) {
389
+ Account.statics.upsert = function(account, cb) {
400
390
 
401
391
  // No population so no need to scrub
402
392
 
403
- try {
393
+ const run = async () => {
404
394
  const result = await account.save();
405
- return cb(null, result);
406
- } catch(err) {
407
- return cb(err);
408
- }
395
+ return result;
396
+ };
397
+
398
+ if (typeof cb === 'function') { run().then(result => cb(null, result)).catch(cb); return; }
399
+ else { return run(); }
409
400
 
410
401
  };
411
402
  Account.set('autoIndex', false);
package/lib/Activity.js CHANGED
@@ -40,15 +40,14 @@ module.exports = function(mongoose, config) {
40
40
 
41
41
  });
42
42
 
43
- Activity.statics.add = async function(username, type, remoteAddress, url, filename, callback) {
43
+ Activity.statics.add = function(username, type, remoteAddress, url, filename, callback) {
44
44
 
45
- if (!username) throw new Error('username is required');
46
- if (!type) throw new Error('type is required');
47
- if (!callback) throw new Error('callback is required');
45
+ const run = async () => {
46
+ if (!username) throw new Error('username is required');
47
+ if (!type) throw new Error('type is required');
48
48
 
49
- username = username.toLowerCase();
49
+ username = username.toLowerCase();
50
50
 
51
- try {
52
51
  let activity = await this.findOne({ 'username': username });
53
52
  if (!activity) {
54
53
  const A = mongoose.model('Activity');
@@ -72,38 +71,43 @@ module.exports = function(mongoose, config) {
72
71
  if (activity) {
73
72
  activity.items.unshift(item);
74
73
  await activity.save();
75
- return callback(null, item);
74
+ return item;
76
75
  }
77
76
  else {
78
- return callback(null, null);
77
+ return null;
79
78
  }
80
- } catch(err) {
81
- return callback(err);
82
- }
79
+ };
80
+
81
+ if (typeof callback === 'function') { run().then(result => callback(null, result)).catch(callback); }
82
+ else { return run(); }
83
83
 
84
84
  };
85
85
 
86
- Activity.statics.forMessage = async function(username, messageId, cb) {
87
- try {
88
- const result = await this.find({ 'username': username.toLowerCase(), 'items.document.messageId': messageId });
89
- return cb(null, result);
90
- } catch(err) {
91
- return cb(err);
92
- }
86
+ Activity.statics.forMessage = function(username, messageId, cb) {
87
+
88
+ const run = async () => {
89
+ const result = await this.find({ 'username': username.toLowerCase(), 'items.document.messageId': messageId }).lean({ virtuals: true });
90
+ return result;
91
+ };
92
+
93
+ if (typeof cb === 'function') { run().then(result => cb(null, result)).catch(cb); return; }
94
+ else { return run(); }
95
+
93
96
  };
94
97
 
95
- Activity.statics.list = async function(username, maxItems, cb) {
98
+ Activity.statics.list = function(username, maxItems, cb) {
99
+
100
+ const run = async () => {
101
+ var query = this.findOne({ 'username': username.toLowerCase() });
96
102
 
97
- var query = this.findOne({ 'username': username.toLowerCase() });
103
+ if (maxItems && maxItems >= 1) { query = query.slice('items', maxItems); }
98
104
 
99
- if (maxItems && maxItems >= 1) { query = query.slice('items', maxItems); }
105
+ const result = await query.lean({ virtuals: true });
106
+ return result;
107
+ };
100
108
 
101
- try {
102
- const result = await query;
103
- return cb(null, result);
104
- } catch(err) {
105
- return cb(err);
106
- }
109
+ if (typeof cb === 'function') { run().then(result => cb(null, result)).catch(cb); return; }
110
+ else { return run(); }
107
111
 
108
112
  };
109
113
  Activity.set('autoIndex', false);
package/lib/ApiKey.js CHANGED
@@ -18,49 +18,49 @@ module.exports = function(mongoose, config) {
18
18
  ApiKey.index({ customer: 1, account: 1 });
19
19
  ApiKey.index({ tokenHash: 1 });
20
20
 
21
- ApiKey.statics.getByTokenHash = async function(hash, cb) {
21
+ ApiKey.statics.getByTokenHash = function(hash, cb) {
22
22
 
23
- if (!cb) { throw new Error('cb is required'); }
24
- if (!hash) { return cb(new Error('hash is required'), null); }
23
+ const run = async () => {
24
+ if (!hash) { throw new Error('hash is required'); }
25
25
 
26
- try {
27
- const result = await this.findOne({ tokenHash: hash });
28
- return cb(null, result);
29
- } catch(err) {
30
- return cb(err);
31
- }
26
+ const result = await this.findOne({ tokenHash: hash }).lean({ virtuals: true });
27
+ return result;
28
+ };
29
+
30
+ if (typeof cb === 'function') { run().then(result => cb(null, result)).catch(cb); return; }
31
+ else { return run(); }
32
32
 
33
33
  };
34
34
 
35
- ApiKey.statics.getByAccount = async function(accountId, customerId, cb) {
35
+ ApiKey.statics.getByAccount = function(accountId, customerId, cb) {
36
+
37
+ const run = async () => {
38
+ if (!accountId) { throw new Error('accountId is required'); }
39
+ if (!customerId) { throw new Error('customerId is required'); }
36
40
 
37
- if (!cb) { throw new Error('cb is required'); }
38
- if (!accountId) { return cb(new Error('accountId is required'), null); }
39
- if (!customerId) { return cb(new Error('customerId is required'), null); }
41
+ const result = await this.find({ account: accountId, customer: customerId, active: true }).sort({ createdOn: -1 }).select('scopes createdOn lastUsedOn').lean({ virtuals: true });
42
+ return result;
43
+ };
40
44
 
41
- try {
42
- const result = await this.find({ account: accountId, customer: customerId, active: true }).sort({ createdOn: -1 }).select('scopes createdOn lastUsedOn');
43
- return cb(null, result);
44
- } catch(err) {
45
- return cb(err);
46
- }
45
+ if (typeof cb === 'function') { run().then(result => cb(null, result)).catch(cb); return; }
46
+ else { return run(); }
47
47
 
48
48
  };
49
49
 
50
- ApiKey.statics.revoke = async function(tokenId, accountId, customerId, cb) {
50
+ ApiKey.statics.revoke = function(tokenId, accountId, customerId, cb) {
51
51
 
52
- if (!cb) { throw new Error('cb is required'); }
53
- if (!tokenId) { return cb(new Error('tokenId is required'), null); }
52
+ const run = async () => {
53
+ if (!tokenId) { throw new Error('tokenId is required'); }
54
54
 
55
- try {
56
55
  const result = await this.findOneAndUpdate(
57
56
  { _id: tokenId, account: accountId, customer: customerId },
58
57
  { active: false },
59
58
  { new: true });
60
- return cb(null, result);
61
- } catch(err) {
62
- return cb(err);
63
- }
59
+ return result;
60
+ };
61
+
62
+ if (typeof cb === 'function') { run().then(result => cb(null, result)).catch(cb); return; }
63
+ else { return run(); }
64
64
 
65
65
  };
66
66
  ApiKey.set('autoIndex', false);