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