@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.
@@ -15,7 +15,7 @@ module.exports = function(mongoose, config) {
15
15
  const moment = require('moment');
16
16
 
17
17
  const FinancialsAnalysis = new Schema({
18
-
18
+
19
19
  organization: { type: Schema.ObjectId, ref: 'Organization', required: true, index: true },
20
20
 
21
21
  customer: { type: Schema.ObjectId, ref: 'Organization', required: true, index: true },
@@ -31,77 +31,88 @@ module.exports = function(mongoose, config) {
31
31
  category: { type: String, enum: ['highlight', 'lowlight', 'goal', 'ask'] },
32
32
  page: { type: Number, default: -1 }
33
33
  }]
34
-
35
- });
36
34
 
37
- FinancialsAnalysis.statics.getByOrg = function getByOrg(customerId, orgId, cb) {
35
+ });
38
36
 
39
- const self = this;
37
+ FinancialsAnalysis.statics.getByOrg = async function getByOrg(customerId, orgId, cb) {
40
38
 
41
- var query = self.find({
42
- 'customer': customerId,
43
- 'organization': orgId
44
- })
39
+ try {
40
+ var query = this.find({
41
+ 'customer': customerId,
42
+ 'organization': orgId
43
+ })
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
- FinancialsAnalysis.statics.getByCustomer = function getByCustomer(customerId, cb) {
51
-
52
- var self = this;
53
+ FinancialsAnalysis.statics.getByCustomer = async function getByCustomer(customerId, cb) {
53
54
 
54
- var query = self.find({
55
- 'customer': customerId
56
- });
55
+ try {
56
+ var query = this.find({
57
+ 'customer': customerId
58
+ });
57
59
 
58
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
60
+ const result = await query;
61
+ return cb(null, result);
62
+ } catch(err) {
63
+ return cb(err);
64
+ }
59
65
 
60
66
  };
61
67
 
62
- FinancialsAnalysis.statics.getByFinancials = function getByFinancials(financials, cb) {
68
+ FinancialsAnalysis.statics.getByFinancials = async function getByFinancials(financials, cb) {
63
69
 
64
- let self = this;
70
+ try {
71
+ let query = this.find({
72
+ 'financials': financials
73
+ });
65
74
 
66
- let query = self.find({
67
- 'financials': financials
68
- });
69
-
70
- query.exec().then(function(result) {
75
+ const result = await query;
71
76
  if (!result) {
72
77
  return cb(null, null);
73
78
  }
74
79
 
75
80
  return cb(null, result);
76
-
77
- }).catch(function(err) { cb(err); });
81
+ } catch(err) {
82
+ return cb(err);
83
+ }
78
84
 
79
85
  };
80
86
 
81
- FinancialsAnalysis.statics.getBySnapshotUUID = function getBySnapshotUUID(uuids, cb) {
82
-
83
- let self = this;
87
+ FinancialsAnalysis.statics.getBySnapshotUUID = async function getBySnapshotUUID(uuids, cb) {
84
88
 
85
- let query = self.find({
86
- 'snapshotUUID': uuids
87
- });
89
+ try {
90
+ let query = this.find({
91
+ 'snapshotUUID': uuids
92
+ });
88
93
 
89
- query.exec().then(function(result) {
94
+ const result = await query;
90
95
  if (!result) {
91
96
  return cb(null, null);
92
97
  }
93
98
 
94
99
  return cb(null, result);
95
-
96
- }).catch(function(err) { cb(err); });
100
+ } catch(err) {
101
+ return cb(err);
102
+ }
97
103
 
98
104
  };
99
105
 
100
- FinancialsAnalysis.statics.upsert = function upsert(doc, cb) {
106
+ FinancialsAnalysis.statics.upsert = async function upsert(doc, cb) {
101
107
 
102
108
  if (!doc) { return cb(new Error('doc is required'), null); }
103
109
 
104
- doc.save().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
110
+ try {
111
+ const result = await doc.save();
112
+ return cb(null, result);
113
+ } catch(err) {
114
+ return cb(err);
115
+ }
105
116
 
106
117
  };
107
118
 
@@ -114,4 +125,4 @@ module.exports = function(mongoose, config) {
114
125
 
115
126
  mongoose.model('FinancialsAnalysis', FinancialsAnalysis);
116
127
 
117
- };
128
+ };
package/lib/Flag.js CHANGED
@@ -21,7 +21,7 @@ module.exports = function(mongoose, config) {
21
21
 
22
22
  });
23
23
 
24
- Flag.statics.createForModel = function(flag, Model, modelId, cb) {
24
+ Flag.statics.createForModel = async function(flag, Model, modelId, cb) {
25
25
 
26
26
  if (!cb) { throw new Error('cb is required'); }
27
27
  if (!flag) { throw new Error('flag is required'); }
@@ -29,27 +29,22 @@ module.exports = function(mongoose, config) {
29
29
  if (!modelId) { throw new Error('modelId is required'); }
30
30
  if (!mongoose.Types.ObjectId.isValid(modelId)) { return cb(new Error('modelId is not a valid ObjectId'), null); }
31
31
 
32
- const self = this;
33
-
34
- var addToModel = function(err, createdFlag) {
35
- if (err) return cb(err, null);
36
- Model.findByIdAndUpdate(modelId, { $push: { flags: createdFlag }}, { new: true, upsert: false }).then(function(addedTo) {
37
- return cb(null, {
38
- flag: createdFlag,
39
- addedTo: addedTo
40
- });
41
- }).catch(function(err) {
42
- return cb(err);
43
- });
44
- };
45
-
46
32
  flag.createdOn = new Date();
47
33
 
48
- self.create(flag).then(function(created) { addToModel(null, created); }).catch(function(err) { addToModel(err); });
34
+ try {
35
+ const createdFlag = await this.create(flag);
36
+ const addedTo = await Model.findByIdAndUpdate(modelId, { $push: { flags: createdFlag }}, { new: true, upsert: false });
37
+ return cb(null, {
38
+ flag: createdFlag,
39
+ addedTo: addedTo
40
+ });
41
+ } catch(err) {
42
+ return cb(err);
43
+ }
49
44
 
50
45
  };
51
46
 
52
- Flag.statics.delete = function(flagId, customerId, cb) {
47
+ Flag.statics.delete = async function(flagId, customerId, cb) {
53
48
 
54
49
  if (!cb) { throw new Error('cb is required'); }
55
50
  if (!flagId) { throw new Error('flagId is required'); }
@@ -57,40 +52,44 @@ module.exports = function(mongoose, config) {
57
52
  if (!customerId) { return cb(new Error('customerId is required'), null); }
58
53
  if (!mongoose.Types.ObjectId.isValid(customerId)) { return cb(new Error('customerId is not a valid ObjectId'), null); }
59
54
 
60
- const self = this;
61
-
62
55
  // Not strictly necessary but provides verification that the flag being deleted belongs to the customer doing the deleting
63
- let query = self.findOne({
56
+ let query = this.findOne({
64
57
  '_id': flagId,
65
58
  'customer': customerId
66
59
  });
67
60
 
68
- query.exec().then(function(flag) {
61
+ try {
62
+ const flag = await query;
69
63
  if (!flag) { return cb(null, null); }
70
64
 
71
- flag.deleteOne().then(function() { cb(null, flag); }).catch(function(err) { cb(err); });
72
-
73
- }).catch(function(err) { cb(err); });
65
+ await flag.deleteOne();
66
+ return cb(null, flag);
67
+ } catch(err) {
68
+ return cb(err);
69
+ }
74
70
 
75
71
  };
76
72
 
77
- Flag.statics.getById = function(id, cb) {
73
+ Flag.statics.getById = async function(id, cb) {
78
74
 
79
75
  if (!cb) { throw new Error('cb is required'); }
80
76
  if (!id) { throw new Error('id is required'); }
81
77
  if (!mongoose.Types.ObjectId.isValid(id)) { return cb(new Error('id is not a valid ObjectId'), null); }
82
78
 
83
- const self = this;
84
-
85
- let query = self.findById({
79
+ let query = this.findById({
86
80
  '_id': id
87
81
  });
88
82
 
89
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
83
+ try {
84
+ const result = await query;
85
+ return cb(null, result);
86
+ } catch(err) {
87
+ return cb(err);
88
+ }
90
89
 
91
90
  };
92
91
 
93
- Flag.statics.modifyById = function(id, update, cb) {
92
+ Flag.statics.modifyById = async function(id, update, cb) {
94
93
 
95
94
  // VERY IMPORTANT NOTE
96
95
  // findByIdAndUpdate and findOneAndUpdate do not trigger pre-save hook so that code will not run here
@@ -99,18 +98,26 @@ module.exports = function(mongoose, config) {
99
98
  if (!id) { return cb(new Error('id is required'), null); }
100
99
  if (!update) { return cb(new Error('update is required'), null); }
101
100
 
102
- var self = this;
103
-
104
101
  // https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
105
102
  // options runValidators defaults false which is ok since we have upsert false
106
103
  // new returns the updated document
107
104
 
108
- self.findByIdAndUpdate(id, update, { upsert: false, new: true }).then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
105
+ try {
106
+ const result = await this.findByIdAndUpdate(id, update, { upsert: false, new: true });
107
+ return cb(null, result);
108
+ } catch(err) {
109
+ return cb(err);
110
+ }
109
111
 
110
112
  };
111
113
 
112
- Flag.statics.upsert = function(flag, cb) {
113
- flag.save().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
114
+ Flag.statics.upsert = async function(flag, cb) {
115
+ try {
116
+ const result = await flag.save();
117
+ return cb(null, result);
118
+ } catch(err) {
119
+ return cb(err);
120
+ }
114
121
  };
115
122
 
116
123
  Flag.post('init', function(doc) {
package/lib/Folder.js CHANGED
@@ -16,7 +16,7 @@ module.exports = function(mongoose, config) {
16
16
  documents: [{ type: Schema.ObjectId, ref: 'Document' }]
17
17
  });
18
18
 
19
- Folder.statics.modifyById = function(id, update, cb) {
19
+ Folder.statics.modifyById = async function(id, update, cb) {
20
20
 
21
21
  // VERY IMPORTANT NOTE
22
22
  // findByIdAndUpdate and findOneAndUpdate do not trigger pre-save hook so that code will not run here
@@ -27,17 +27,20 @@ module.exports = function(mongoose, config) {
27
27
  if (!mongoose.Types.ObjectId.isValid(id)) { return cb(new Error('id is not a valid ObjectId'), null); }
28
28
  if (!update) { return cb(new Error('update is required'), null); }
29
29
 
30
- var self = this;
31
-
32
30
  // https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
33
31
  // options runValidators defaults false which is ok since we have upsert false
34
32
  // new returns the updated document
35
33
 
36
- self.findByIdAndUpdate(id, update, { upsert: false, new: true }).then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
34
+ try {
35
+ const result = await this.findByIdAndUpdate(id, update, { upsert: false, new: true });
36
+ return cb(null, result);
37
+ } catch(err) {
38
+ return cb(err);
39
+ }
37
40
 
38
41
  };
39
42
 
40
- Folder.statics.getByEntity = function getByEntity(entity, options, cb) {
43
+ Folder.statics.getByEntity = async function getByEntity(entity, options, cb) {
41
44
 
42
45
  if (!cb) { throw new Error('cb is required'); }
43
46
  if (!entity) { return cb(new Error('entity is required'), null); }
@@ -48,30 +51,40 @@ module.exports = function(mongoose, config) {
48
51
  if (!options.CUSTOMER_ID) { return cb(new Error('options.CUSTOMER_ID is required'), null); }
49
52
  if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
50
53
 
51
- const self = this;
52
-
53
- self.find({
54
- 'entity.id': entity.id,
55
- 'entity.model': entity.model,
56
- customer: options.CUSTOMER_ID
57
- })
58
- .exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
54
+ try {
55
+ const result = await this.find({
56
+ 'entity.id': entity.id,
57
+ 'entity.model': entity.model,
58
+ customer: options.CUSTOMER_ID
59
+ });
60
+ return cb(null, result);
61
+ } catch(err) {
62
+ return cb(err);
63
+ }
59
64
 
60
65
  };
61
66
 
62
- Folder.statics.upsert = function (folder, cb) {
63
- folder.save().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
67
+ Folder.statics.upsert = async function (folder, cb) {
68
+ try {
69
+ const result = await folder.save();
70
+ return cb(null, result);
71
+ } catch(err) {
72
+ return cb(err);
73
+ }
64
74
  };
65
75
 
66
- Folder.statics.delete = function (id, options, cb) {
67
-
68
- const self = this;
76
+ Folder.statics.delete = async function (id, options, cb) {
69
77
 
70
78
  if (!cb) { throw new Error('cb is required'); }
71
79
  if (!id) { return cb(new Error('id is required'), null); }
72
80
  if (!mongoose.Types.ObjectId.isValid(id)) { return cb(new Error('id is not a valid ObjectId'), null); }
73
81
 
74
- self.findByIdAndDelete(id).then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
82
+ try {
83
+ const result = await this.findByIdAndDelete(id);
84
+ return cb(null, result);
85
+ } catch(err) {
86
+ return cb(err);
87
+ }
75
88
 
76
89
  };
77
90
  Folder.set('autoIndex', false);
@@ -79,4 +92,4 @@ module.exports = function(mongoose, config) {
79
92
 
80
93
  mongoose.model('Folder', Folder);
81
94
 
82
- };
95
+ };
package/lib/Fund.js CHANGED
@@ -66,44 +66,57 @@ module.exports = function(mongoose, config) {
66
66
 
67
67
  ///////////////////////////////////////
68
68
 
69
- Fund.statics.findBySlugs = function findBySlugs(slugs, cb) {
70
-
71
- const self = this;
69
+ Fund.statics.findBySlugs = async function findBySlugs(slugs, cb) {
72
70
 
73
71
  if (!cb) { throw new Error('cb is required'); }
74
72
  if (!slugs) { return cb(new Error('slug must be provided'), null); }
75
73
 
76
- self
77
- .find({ 'slug': { $in : slugs }})
78
- .exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
74
+ try {
75
+ const result = await this.find({ 'slug': { $in : slugs }});
76
+ return cb(null, result);
77
+ } catch(err) {
78
+ return cb(err);
79
+ }
79
80
 
80
81
  };
81
82
 
82
- Fund.statics.getById = function getById(id, cb) {
83
- this
84
- .findById(id)
85
- .exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
83
+ Fund.statics.getById = async function getById(id, cb) {
84
+ try {
85
+ const result = await this.findById(id);
86
+ return cb(null, result);
87
+ } catch(err) {
88
+ return cb(err);
89
+ }
86
90
  };
87
91
 
88
- Fund.statics.getBySlug = function getBySlug(slug, cb) {
89
- this
90
- .find({slug: slug})
91
- .exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
92
+ Fund.statics.getBySlug = async function getBySlug(slug, cb) {
93
+ try {
94
+ const result = await this.find({slug: slug});
95
+ return cb(null, result);
96
+ } catch(err) {
97
+ return cb(err);
98
+ }
92
99
  };
93
100
 
94
- Fund.statics.getCount = function getCount(cb) {
95
- this
96
- .countDocuments({})
97
- .exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
101
+ Fund.statics.getCount = async function getCount(cb) {
102
+ try {
103
+ const result = await this.countDocuments({});
104
+ return cb(null, result);
105
+ } catch(err) {
106
+ return cb(err);
107
+ }
98
108
  };
99
109
 
100
- Fund.statics.list = function list(cb) {
101
- this
102
- .find()
103
- .exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
110
+ Fund.statics.list = async function list(cb) {
111
+ try {
112
+ const result = await this.find();
113
+ return cb(null, result);
114
+ } catch(err) {
115
+ return cb(err);
116
+ }
104
117
  };
105
118
 
106
- Fund.statics.search = function search(query, options, cb) {
119
+ Fund.statics.search = async function search(query, options, cb) {
107
120
 
108
121
  if (!cb) throw new Error('cb is required');
109
122
  if (!query) return cb(null, []);
@@ -112,16 +125,16 @@ module.exports = function(mongoose, config) {
112
125
  'name': new RegExp(query, "i")
113
126
  };
114
127
 
115
- this
116
- .find(conditions)
117
- .sort('name')
118
- .exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
128
+ try {
129
+ const result = await this.find(conditions).sort('name');
130
+ return cb(null, result);
131
+ } catch(err) {
132
+ return cb(err);
133
+ }
119
134
 
120
135
  };
121
136
 
122
- Fund.statics.search2 = function search2(value, options, cb) {
123
-
124
- const self = this;
137
+ Fund.statics.search2 = async function search2(value, options, cb) {
125
138
 
126
139
  if (!cb) { throw new Error('cb is required'); }
127
140
  if (!value) { return cb(new Error('value is required'), null); }
@@ -138,46 +151,51 @@ module.exports = function(mongoose, config) {
138
151
  // combine provided and default options
139
152
  options = _.defaults(options || {}, defaultOptions);
140
153
 
141
- self.aggregate([
142
- {
143
- "$search": {
144
- index: 'default',
145
- compound: {
146
- should: [
147
- {
148
- text: {
149
- query: value,
150
- path: [
151
- 'name',
152
- 'stakeholders'
153
- ],
154
- fuzzy: {
155
- maxEdits: options.maxEdits,
156
- maxExpansions: options.maxExpansions
157
- },
158
- score: { boost: { value: 3 } }
154
+ try {
155
+ const result = await this.aggregate([
156
+ {
157
+ "$search": {
158
+ index: 'default',
159
+ compound: {
160
+ should: [
161
+ {
162
+ text: {
163
+ query: value,
164
+ path: [
165
+ 'name',
166
+ 'stakeholders'
167
+ ],
168
+ fuzzy: {
169
+ maxEdits: options.maxEdits,
170
+ maxExpansions: options.maxExpansions
171
+ },
172
+ score: { boost: { value: 3 } }
173
+ }
159
174
  }
160
- }
161
- ],
162
- minimumShouldMatch: 1
175
+ ],
176
+ minimumShouldMatch: 1
177
+ }
178
+ }
179
+ },
180
+ {
181
+ $limit: options.limit,
182
+ },
183
+ {
184
+ $project: {
185
+ name: 1,
186
+ abbreviation: 1,
187
+ score: { $meta: "searchScore" }
163
188
  }
164
189
  }
165
- },
166
- {
167
- $limit: options.limit,
168
- },
169
- {
170
- $project: {
171
- name: 1,
172
- abbreviation: 1,
173
- score: { $meta: "searchScore" }
174
- }
175
- }
176
- ]).exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
190
+ ]);
191
+ return cb(null, result);
192
+ } catch(err) {
193
+ return cb(err);
194
+ }
177
195
 
178
196
  };
179
197
 
180
- Fund.statics.upsert = function upsert(fund, username, cb) {
198
+ Fund.statics.upsert = async function upsert(fund, username, cb) {
181
199
 
182
200
  if (!fund) { return cb(new Error('Fund is required'), null); }
183
201
  if (!username) { return cb(new Error('Username is required'), null); }
@@ -185,11 +203,16 @@ module.exports = function(mongoose, config) {
185
203
  // Required for mixed types
186
204
  fund.markModified('customFields');
187
205
 
188
- fund.save().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
206
+ try {
207
+ const result = await fund.save();
208
+ return cb(null, result);
209
+ } catch(err) {
210
+ return cb(err);
211
+ }
189
212
 
190
213
  };
191
214
 
192
- Fund.statics.modify = function(filter, update, cb) {
215
+ Fund.statics.modify = async function(filter, update, cb) {
193
216
 
194
217
  // VERY IMPORTANT NOTE
195
218
  // findByIdAndUpdate and findOneAndUpdate do not trigger pre-save hook so that code will not run here
@@ -199,17 +222,20 @@ module.exports = function(mongoose, config) {
199
222
  if (!filter) { return cb(new Error('filter is required'), null); }
200
223
  if (!update) { return cb(new Error('update is required'), null); }
201
224
 
202
- var self = this;
203
-
204
225
  // https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
205
226
  // options runValidators defaults false which is ok since we have upsert false
206
227
  // new returns the updated document
207
228
 
208
- self.findOneAndUpdate(filter, update, { upsert: false, new: true }).then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
229
+ try {
230
+ const result = await this.findOneAndUpdate(filter, update, { upsert: false, new: true });
231
+ return cb(null, result);
232
+ } catch(err) {
233
+ return cb(err);
234
+ }
209
235
 
210
236
  };
211
237
 
212
- Fund.statics.modifyById = function(id, update, cb) {
238
+ Fund.statics.modifyById = async function(id, update, cb) {
213
239
 
214
240
  // VERY IMPORTANT NOTE
215
241
  // findByIdAndUpdate and findOneAndUpdate do not trigger pre-save hook so that code will not run here
@@ -220,13 +246,16 @@ module.exports = function(mongoose, config) {
220
246
  if (!mongoose.Types.ObjectId.isValid(id)) { return cb(new Error('id is not a valid ObjectId'), null); }
221
247
  if (!update) { return cb(new Error('update is required'), null); }
222
248
 
223
- var self = this;
224
-
225
249
  // https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
226
250
  // options runValidators defaults false which is ok since we have upsert false
227
251
  // new returns the updated document
228
252
 
229
- self.findByIdAndUpdate(id, update, { upsert: false, new: true }).then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
253
+ try {
254
+ const result = await this.findByIdAndUpdate(id, update, { upsert: false, new: true });
255
+ return cb(null, result);
256
+ } catch(err) {
257
+ return cb(err);
258
+ }
230
259
 
231
260
  };
232
261
  Fund.set('autoIndex', false);
@@ -234,4 +263,4 @@ module.exports = function(mongoose, config) {
234
263
 
235
264
  mongoose.model('Fund', Fund);
236
265
 
237
- };
266
+ };