@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/News.js CHANGED
@@ -42,9 +42,7 @@ module.exports = function(mongoose, config) {
42
42
 
43
43
  });
44
44
 
45
- News.statics.getById = function getById(id, options, cb) {
46
-
47
- const self = this;
45
+ News.statics.getById = async function getById(id, options, cb) {
48
46
 
49
47
  if (!cb) { throw new Error('cb is required'); }
50
48
  if (!id) { return cb(new Error('ids is required'), null); }
@@ -52,15 +50,16 @@ module.exports = function(mongoose, config) {
52
50
 
53
51
  options = helpers.getDefaultOptions(options);
54
52
 
55
- self
56
- .find({ org: id })
57
- .populate('org')
58
- .sort('-createdOn')
59
- .exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
53
+ try {
54
+ const result = await this.find({ org: id }).populate('org').sort('-createdOn');
55
+ return cb(null, result);
56
+ } catch(err) {
57
+ return cb(err);
58
+ }
60
59
 
61
60
  };
62
61
 
63
- News.statics.getByIds = function getByIds(ids, options, cb) {
62
+ News.statics.getByIds = async function getByIds(ids, options, cb) {
64
63
 
65
64
  if (!cb) { throw new Error('cb is required'); }
66
65
  if (!ids) { return cb(new Error('ids is required'), null); }
@@ -68,9 +67,7 @@ module.exports = function(mongoose, config) {
68
67
 
69
68
  options = helpers.getDefaultOptions(options);
70
69
 
71
- const self = this;
72
-
73
- let query = self.find({ org: { $in : ids } })
70
+ let query = this.find({ org: { $in : ids } })
74
71
 
75
72
  if (options.startDate) {
76
73
  const startDate = options.startDate || moment().subtract(20, 'years').startOf('day').toISOString();
@@ -83,11 +80,17 @@ module.exports = function(mongoose, config) {
83
80
  }
84
81
 
85
82
  query.sort('-createdOn')
86
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
83
+
84
+ try {
85
+ const result = await query;
86
+ return cb(null, result);
87
+ } catch(err) {
88
+ return cb(err);
89
+ }
87
90
 
88
91
  };
89
92
 
90
- News.statics.modifyById = function(id, update, cb) {
93
+ News.statics.modifyById = async function(id, update, cb) {
91
94
 
92
95
  // VERY IMPORTANT NOTE
93
96
  // findByIdAndUpdate and findOneAndUpdate do not trigger pre-save hook so that code will not run here
@@ -96,18 +99,26 @@ module.exports = function(mongoose, config) {
96
99
  if (!id) { return cb(new Error('id is required'), null); }
97
100
  if (!update) { return cb(new Error('update is required'), null); }
98
101
 
99
- const self = this;
100
-
101
102
  // https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
102
103
  // options runValidators defaults false which is ok since we have upsert false
103
104
  // new returns the updated document
104
105
 
105
- self.findByIdAndUpdate(id, update, { upsert: false, new: true }).then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
106
+ try {
107
+ const result = await this.findByIdAndUpdate(id, update, { upsert: false, new: true });
108
+ return cb(null, result);
109
+ } catch(err) {
110
+ return cb(err);
111
+ }
106
112
 
107
113
  };
108
114
 
109
- News.statics.upsert = function (news, cb) {
110
- news.save().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
115
+ News.statics.upsert = async function (news, cb) {
116
+ try {
117
+ const result = await news.save();
118
+ return cb(null, result);
119
+ } catch(err) {
120
+ return cb(err);
121
+ }
111
122
  };
112
123
  News.set('autoIndex', false);
113
124
  News.on('index', function(err) { console.log('error building news indexes: ' + err); });
package/lib/Note.js CHANGED
@@ -18,7 +18,7 @@ module.exports = function(mongoose, config) {
18
18
  title: { type: String, index: false, required: false, trim: true },
19
19
 
20
20
  text: { type: String, index: false, required: true, trim: true },
21
-
21
+
22
22
  analysis: {
23
23
  insights: [{
24
24
  text: { type: String },
@@ -28,7 +28,7 @@ module.exports = function(mongoose, config) {
28
28
 
29
29
  });
30
30
 
31
- Note.statics.delete = function(noteId, customerId, cb) {
31
+ Note.statics.delete = async function(noteId, customerId, cb) {
32
32
 
33
33
  if (!cb) { throw new Error('cb is required'); }
34
34
  if (!noteId) { throw new Error('noteId is required'); }
@@ -36,25 +36,26 @@ module.exports = function(mongoose, config) {
36
36
  if (!customerId) { return cb(new Error('customerId is required'), null); }
37
37
  if (!mongoose.Types.ObjectId.isValid(customerId)) { return cb(new Error('customerId is not a valid ObjectId'), null); }
38
38
 
39
- const self = this;
40
-
41
39
  // Not strictly necessary but provides verification that the note being deleted belongs to the customer doing the deleting
42
40
 
43
- let query = self.findOne({
41
+ let query = this.findOne({
44
42
  '_id': noteId,
45
43
  'customer': customerId
46
44
  });
47
45
 
48
- query.exec().then(function(note) {
46
+ try {
47
+ const note = await query;
49
48
  if (!note) return cb(null, null);
50
49
 
51
- note.deleteOne().then(function() { cb(null, note); }).catch(function(err) { cb(err); });
52
-
53
- }).catch(function(err) { cb(err); });
50
+ await note.deleteOne();
51
+ return cb(null, note);
52
+ } catch(err) {
53
+ return cb(err);
54
+ }
54
55
 
55
56
  };
56
57
 
57
- Note.statics.createForModel = function(Model, modelId, note, cb) {
58
+ Note.statics.createForModel = async function(Model, modelId, note, cb) {
58
59
 
59
60
  if (!cb) { throw new Error('cb is required'); }
60
61
  if (!Model) { return cb(new Error('Model is required'), null); }
@@ -62,22 +63,17 @@ module.exports = function(mongoose, config) {
62
63
  if (!mongoose.Types.ObjectId.isValid(modelId)) { return cb(new Error('modelId is not a valid ObjectId'), null); }
63
64
  if (!note) { return cb(new Error('note is required'), null); }
64
65
 
65
- const self = this;
66
-
67
- var addToModel = function(err, createdNote) {
68
- if (err) return cb(err, null);
69
- Model.findByIdAndUpdate(modelId, { $push: { notes: createdNote }}, { new: true, upsert: false }).then(function(addedTo) {
70
- return cb(null, createdNote);
71
- }).catch(function(err) {
72
- return cb(err);
73
- });
74
- };
75
-
76
- self.create(note).then(function(created) { addToModel(null, created); }).catch(function(err) { addToModel(err); });
66
+ try {
67
+ const createdNote = await this.create(note);
68
+ await Model.findByIdAndUpdate(modelId, { $push: { notes: createdNote }}, { new: true, upsert: false });
69
+ return cb(null, createdNote);
70
+ } catch(err) {
71
+ return cb(err);
72
+ }
77
73
 
78
74
  };
79
75
 
80
- Note.statics.getById = function(id, options, cb) {
76
+ Note.statics.getById = async function(id, options, cb) {
81
77
 
82
78
  if (!cb) { throw new Error('cb is required'); }
83
79
  if (!id) { throw new Error('id is required'); }
@@ -86,18 +82,21 @@ module.exports = function(mongoose, config) {
86
82
  if (!options.CUSTOMER_ID) { return cb(new Error('options.CUSTOMER_ID is required'), null); }
87
83
  if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
88
84
 
89
- const self = this;
90
-
91
- let query = self.findOne({
85
+ let query = this.findOne({
92
86
  '_id': id,
93
87
  'customer': options.CUSTOMER_ID
94
88
  });
95
89
 
96
- query.exec().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
90
+ try {
91
+ const result = await query;
92
+ return cb(null, result);
93
+ } catch(err) {
94
+ return cb(err);
95
+ }
97
96
 
98
97
  };
99
98
 
100
- Note.statics.modifyById = function(id, update, options, cb) {
99
+ Note.statics.modifyById = async function(id, update, options, cb) {
101
100
 
102
101
  // VERY IMPORTANT NOTE
103
102
  // findByIdAndUpdate and findOneAndUpdate do not trigger pre-save hook so that code will not run here
@@ -110,18 +109,26 @@ module.exports = function(mongoose, config) {
110
109
  if (!options.CUSTOMER_ID) { return cb(new Error('options.CUSTOMER_ID is required'), null); }
111
110
  if (!mongoose.Types.ObjectId.isValid(options.CUSTOMER_ID)) { return cb(new Error('options.CUSTOMER_ID is not a valid ObjectId'), null); }
112
111
 
113
- const self = this;
114
-
115
112
  // https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate
116
113
  // options runValidators defaults false which is ok since we have upsert false
117
114
  // new returns the updated document
118
115
 
119
- self.findOneAndUpdate({ _id: id, customer: options.CUSTOMER_ID }, update, { upsert: false, new: true }).then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
116
+ try {
117
+ const result = await this.findOneAndUpdate({ _id: id, customer: options.CUSTOMER_ID }, update, { upsert: false, new: true });
118
+ return cb(null, result);
119
+ } catch(err) {
120
+ return cb(err);
121
+ }
120
122
 
121
123
  };
122
124
 
123
- Note.statics.upsert = function(note, cb) {
124
- note.save().then(function(result) { cb(null, result); }).catch(function(err) { cb(err); });
125
+ Note.statics.upsert = async function(note, cb) {
126
+ try {
127
+ const result = await note.save();
128
+ return cb(null, result);
129
+ } catch(err) {
130
+ return cb(err);
131
+ }
125
132
  };
126
133
 
127
134
  Note.post('init', function(doc) {